Created
June 14, 2019 23:55
-
-
Save coinables/a48aab89665a14774ec2e443fbc40729 to your computer and use it in GitHub Desktop.
Create a testnet key pair
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let bitcoin = require("bitcoinjs-lib"); | |
function getNewAddress(){ | |
let NETWORK = bitcoin.networks.testnet; //main net bitcoin.networks.bitcoin | |
let wif = bitcoin.ECPair.makeRandom({network: NETWORK}).toWIF(); | |
let keyPair = bitcoin.ECPair.fromWIF(wif, NETWORK); | |
//p2pkh | |
let p2pkhAddr = keyPair.getAddress(); | |
//native witness | |
let pubKey = keyPair.getPublicKeyBuffer(); | |
let scriptPubKey = bitcoin.script.witnessPubKeyHash.output.encode(bitcoin.crypto.hash160(pubKey)); | |
let p2wpkhAddr = bitcoin.address.fromOutputScript(scriptPubKey, NETWORK); | |
//p2sh witness | |
let pubKeyHash = bitcoin.crypto.hash160(pubKey); | |
let redeemScript = bitcoin.script.witnessPubKeyHash.output.encode(pubKeyHash); | |
let redeemScriptHex = redeemScript.toString('hex'); | |
let redeemScriptHash = bitcoin.crypto.hash160(redeemScript); | |
let scriptPubKey2 = bitcoin.script.scriptHash.output.encode(redeemScriptHash); | |
let p2shp2wpkhAddr = bitcoin.address.fromOutputScript(scriptPubKey2, NETWORK); | |
return { | |
pk: wif, | |
p2pkh: p2pkhAddr, | |
p2wpkh: p2wpkhAddr, | |
p2shp2wpkh: p2shp2wpkhAddr, | |
redeemScript: redeemScriptHex | |
}; | |
} | |
let keypair = getNewAddress(); | |
let testnetaddress = keypair.p2pkh; | |
let testnetprivatekey = keypair.pk; | |
console.log(testnetaddress,testnetprivatekey); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment