Make fund_ examples use the transaction signer :P
This commit is contained in:
parent
b27b72d775
commit
3734cdefd5
@ -3,24 +3,28 @@ var bscript = bitcoin.script
|
||||
var crypto = bitcoin.crypto
|
||||
var networks = bitcoin.networks
|
||||
var TransactionBuilder = bitcoin.TransactionBuilder
|
||||
var TxSigner = bitcoin.TxSigner
|
||||
|
||||
var network = networks.testnet
|
||||
var entropy = new Buffer('14bdfeac14bdfeac14bdfeac14bdfeac14bdfeac14bdfeac14bdfeac14bdfeac')
|
||||
var root = bitcoin.HDNode.fromSeedBuffer(entropy, network)
|
||||
var address = root.keyPair.getAddress()
|
||||
var wif = root.keyPair.toWIF()
|
||||
console.log(address)
|
||||
console.log(wif)
|
||||
|
||||
var txid = '06fc7b675a31bfe3f05dab40d0cd8c044a9b2e890c696a53449d970a4adc6d52'
|
||||
var vout = 0
|
||||
var p2shScript = bscript.witnessPubKeyHash.output.encode(crypto.hash160(root.keyPair.getPublicKeyBuffer()))
|
||||
var scriptPubKey = bscript.scriptHash.output.encode(crypto.hash160(p2shScript))
|
||||
var amount = 22000
|
||||
var receivePK = bscript.pubKeyHash.output.encode(crypto.hash160(root.keyPair.getPublicKeyBuffer()))
|
||||
var receiveAmount = 22000
|
||||
|
||||
var fundP2shScript = bscript.witnessPubKeyHash.output.encode(crypto.hash160(root.keyPair.getPublicKeyBuffer()))
|
||||
var fundScriptPubKey = bscript.scriptHash.output.encode(crypto.hash160(fundP2shScript))
|
||||
|
||||
var txb = new TransactionBuilder(network)
|
||||
txb.addInput(txid, vout, 0xffffffff)
|
||||
txb.addOutput(scriptPubKey, amount - 5000)
|
||||
txb.sign(0, root.keyPair)
|
||||
var tx = txb.build()
|
||||
txb.addOutput(fundScriptPubKey, receiveAmount - 5000)
|
||||
|
||||
var unsigned = txb.buildIncomplete()
|
||||
var signer = new TxSigner(unsigned)
|
||||
signer.sign(0, root.keyPair, {
|
||||
scriptPubKey: receivePK
|
||||
})
|
||||
var tx = signer.done()
|
||||
console.log(tx.toBuffer().toString('hex'))
|
||||
|
||||
@ -3,6 +3,7 @@ var bscript = bitcoin.script
|
||||
var crypto = bitcoin.crypto
|
||||
var networks = bitcoin.networks
|
||||
var TransactionBuilder = bitcoin.TransactionBuilder
|
||||
var TxSigner = bitcoin.TxSigner
|
||||
|
||||
var network = networks.testnet
|
||||
var entropy = new Buffer('14bdfeac14bdfeac14bdfeac14bdfeac14bdfeac14bdfeac14bdfeac14bdfeac')
|
||||
@ -12,18 +13,20 @@ var pubkeyhash = crypto.hash160(root.keyPair.getPublicKeyBuffer())
|
||||
|
||||
var txid = 'beb647db98bda750f8202e6bc3441781ea5cfc6e3630c9d0ae47b0bfb111c249'
|
||||
var vout = 1
|
||||
var txOut = {
|
||||
script: bscript.pubKeyHash.output.encode(pubkeyhash),
|
||||
value: 100000
|
||||
}
|
||||
var receiveAmount = 10000
|
||||
var receivePK = bscript.pubKeyHash.output.encode(pubkeyhash)
|
||||
|
||||
var witnessScript = txOut.script
|
||||
var witnessScript = bscript.pubKeyHash.output.encode(pubkeyhash)
|
||||
var p2shScript = bscript.witnessScriptHash.output.encode(crypto.sha256(witnessScript))
|
||||
var scriptPubKey = bscript.scriptHash.output.encode(crypto.hash160(p2shScript))
|
||||
|
||||
var txb = new TransactionBuilder(network)
|
||||
txb.addInput(txid, vout, 0xffffffff, txOut.script)
|
||||
txb.addOutput(scriptPubKey, txOut.value - 10000)
|
||||
txb.sign(0, root.keyPair)
|
||||
var tx = txb.build()
|
||||
txb.addInput(txid, vout, 0xffffffff)
|
||||
txb.addOutput(scriptPubKey, receiveAmount - 10000)
|
||||
|
||||
var txs = new TxSigner(txb.buildIncomplete())
|
||||
txs.sign(0, root.keyPair, {
|
||||
scriptPubKey: receivePK
|
||||
})
|
||||
var tx = txs.done()
|
||||
console.log(tx.toBuffer().toString('hex'))
|
||||
|
||||
@ -3,6 +3,7 @@ var bscript = bitcoin.script
|
||||
var crypto = bitcoin.crypto
|
||||
var networks = bitcoin.networks
|
||||
var TransactionBuilder = bitcoin.TransactionBuilder
|
||||
var TxSigner = bitcoin.TxSigner
|
||||
|
||||
var network = networks.testnet
|
||||
var entropy = new Buffer('14bdfeac14bdfeac14bdfeac14bdfeac14bdfeac14bdfeac14bdfeac14bdfeac')
|
||||
@ -13,20 +14,20 @@ var pubkeyhash = crypto.hash160(root.keyPair.getPublicKeyBuffer())
|
||||
|
||||
var txid = 'aed14f8e918c6e7cc9347391b790f765030b07e6985fbb146bf3f6b25ddc0043'
|
||||
var vout = 0
|
||||
var txOut = {
|
||||
script: bscript.pubKeyHash.output.encode(pubkeyhash),
|
||||
value: 22000
|
||||
}
|
||||
var receivePK = bscript.pubKeyHash.output.encode(pubkeyhash)
|
||||
var receiveAmount = 22000
|
||||
|
||||
var multisig = bscript.multisig.output.encode(2, [root.getPublicKeyBuffer(), root2.getPublicKeyBuffer()])
|
||||
var p2shScript = bscript.witnessScriptHash.output.encode(crypto.sha256(multisig))
|
||||
var scriptPubKey = bscript.scriptHash.output.encode(crypto.hash160(p2shScript))
|
||||
|
||||
var txb = new TransactionBuilder(network)
|
||||
txb.addInput(txid, vout, 0xffffffff, txOut.script)
|
||||
txb.addOutput(scriptPubKey, txOut.value - 5000)
|
||||
txb.sign(0, root.keyPair)
|
||||
var tx = txb.build()
|
||||
console.log(tx.toBuffer().toString('hex'))
|
||||
txb.addInput(txid, vout, 0xffffffff)
|
||||
txb.addOutput(scriptPubKey, receiveAmount - 5000)
|
||||
|
||||
// b5e7c1d911b078c754770fc372bc92096ce2605ac6b785f91ec7df0a5034ec69
|
||||
var txs = new TxSigner(txb.buildIncomplete())
|
||||
txs.sign(0, root.keyPair, {
|
||||
scriptPubKey: receivePK
|
||||
})
|
||||
var tx = txs.done()
|
||||
console.log(tx.toBuffer().toString('hex'))
|
||||
|
||||
@ -3,27 +3,29 @@ var bscript = bitcoin.script
|
||||
var crypto = bitcoin.crypto
|
||||
var networks = bitcoin.networks
|
||||
var TransactionBuilder = bitcoin.TransactionBuilder
|
||||
var TxSigner = bitcoin.TxSigner
|
||||
|
||||
var network = networks.testnet
|
||||
var entropy = new Buffer('14bdfeac14bdfeac14bdfeac14bdfeac14bdfeac14bdfeac14bdfeac14bdfeac')
|
||||
var root = bitcoin.HDNode.fromSeedBuffer(entropy, network)
|
||||
var address = root.keyPair.getAddress()
|
||||
var wif = root.keyPair.toWIF()
|
||||
console.log(address)
|
||||
console.log(wif)
|
||||
|
||||
var pubkeyhash = crypto.hash160(root.keyPair.getPublicKeyBuffer())
|
||||
|
||||
var txid = '9aa8d1a1c5df0afccf76e84df1029062b65a98dad68e13cc765aef88ab378dd0'
|
||||
var vout = 0
|
||||
var scriptPubKey = bscript.pubKeyHash.output.encode(pubkeyhash)
|
||||
var amount = 22000
|
||||
var receivePK = bscript.pubKeyHash.output.encode(pubkeyhash)
|
||||
var receiveAmount = 22000
|
||||
|
||||
var toSegwitPubkey = bscript.witnessPubKeyHash.output.encode(pubkeyhash)
|
||||
|
||||
var txb = new TransactionBuilder(network)
|
||||
txb.addInput(txid, vout, 0xffffffff, scriptPubKey)
|
||||
txb.addOutput(toSegwitPubkey, amount - 5000)
|
||||
txb.sign(0, root.keyPair)
|
||||
var tx = txb.build()
|
||||
txb.addInput(txid, vout, 0xffffffff)
|
||||
txb.addOutput(toSegwitPubkey, receiveAmount - 5000)
|
||||
|
||||
var txs = new TxSigner(txb.buildIncomplete())
|
||||
txs.sign(0, root.keyPair, {
|
||||
scriptPubKey: receivePK
|
||||
})
|
||||
|
||||
var tx = txs.done()
|
||||
console.log(tx.toBuffer().toString('hex'))
|
||||
|
||||
@ -3,24 +3,28 @@ var bscript = bitcoin.script
|
||||
var crypto = bitcoin.crypto
|
||||
var networks = bitcoin.networks
|
||||
var TransactionBuilder = bitcoin.TransactionBuilder
|
||||
var TxSigner = bitcoin.TxSigner
|
||||
|
||||
var network = networks.testnet
|
||||
var entropy = new Buffer('14bdfeac14bdfeac14bdfeac14bdfeac14bdfeac14bdfeac14bdfeac14bdfeac')
|
||||
var root = bitcoin.HDNode.fromSeedBuffer(entropy, network)
|
||||
console.log(root.getAddress())
|
||||
var pubkeyhash = crypto.hash160(root.keyPair.getPublicKeyBuffer())
|
||||
|
||||
var txid = '79f560d078eacf4cf9381544b15c400773fddd6bbfb1064956e0c345d39be260'
|
||||
var vout = 0
|
||||
var scriptPubKey = bscript.pubKeyHash.output.encode(pubkeyhash)
|
||||
var amount = 70000
|
||||
var receivePK = bscript.pubKeyHash.output.encode(pubkeyhash)
|
||||
var receiveAmount = 70000
|
||||
|
||||
var witnessScriptHash = crypto.sha256(scriptPubKey)
|
||||
var toP2WSH = bscript.witnessScriptHash.output.encode(witnessScriptHash)
|
||||
var witnessScript = bscript.pubKeyHash.output.encode(pubkeyhash)
|
||||
var toP2WSH = bscript.witnessScriptHash.output.encode(crypto.sha256(witnessScript))
|
||||
|
||||
var txb = new TransactionBuilder(network)
|
||||
txb.addInput(txid, vout, 0xffffffff, scriptPubKey)
|
||||
txb.addOutput(toP2WSH, amount - 5000)
|
||||
txb.sign(0, root.keyPair)
|
||||
var tx = txb.build()
|
||||
txb.addInput(txid, vout, 0xffffffff)
|
||||
txb.addOutput(toP2WSH, receiveAmount - 5000)
|
||||
|
||||
var txs = new TxSigner(txb.buildIncomplete())
|
||||
txs.sign(0, root.keyPair, {
|
||||
scriptPubKey: receivePK
|
||||
})
|
||||
var tx = txs.done()
|
||||
console.log(tx.toBuffer().toString('hex'))
|
||||
|
||||
Loading…
Reference in New Issue
Block a user