Skip to content

Instantly share code, notes, and snippets.

@fanatid
Last active March 28, 2019 21:49
Show Gist options
  • Save fanatid/a116b9b9009bef4c1bac to your computer and use it in GitHub Desktop.
Save fanatid/a116b9b9009bef4c1bac to your computer and use it in GitHub Desktop.
bitcoin multisig transaction demo
var bitcoin = require('bitcoinjs-lib') // 1.4.x ?
var privKeys = [
'L2ALJznyt2croGHs8duBK2TZMB3Bc5ZpFZQSmR5QQUMPDDcUWeTw', // n2t8F1D41xy6f3d2B6DtjXRRsn8dgUzQ6C
'L3gM5giJqPAJcWyD8eKv94d4QgqyzqrYeAA9DnuJe23FszsnqD1w', // mjgF67B4pyEHuGTLU5jS333EasUrZBaxMB
'KwdYxYAbxntKvrMQtDHAamPT1pyuYkDRfAwESBqCGRdz9abQo3dW', // mwzPpNMFwLoJPXw2ez8mz6RrdYGaqZDind
'L5LTWMRBt27dHde1mnvV1RzW1mxQuQBxpm2TvNHsMaNvAQKqKov2', // mt7K2ChnJSp96k93HYdC8B9oqUmqLyESpL
'L4qQgxEbKJFJKans2UxFRzXisS7o3oBkyJGirAUgTxS9CfTo9Cd4', // mmvbfVqYrXdy1i4x9UzWc4PtgN7VjMNjeY
]
privKeys = privKeys.map(function(privKey) {
return bitcoin.ECKey.fromWIF(privKey)
})
var pubKeys = privKeys.map(function(privKey) {
return privKey.pub
})
// create multisig
var script = bitcoin.scripts.multisigOutput(2, [pubKeys[1], pubKeys[2], pubKeys[3]])
var txb1 = new bitcoin.TransactionBuilder()
txb1.addInput('9774024749b0104a98648641285cac4d518c9f824d9432d4ad67c1931028711b', 0)
txb1.addOutput(script, 990000)
txb1.sign(0, privKeys[0])
var tx1 = txb1.build()
console.log(tx1)
console.log(tx1.toHex()) // push tx to network
// spend multisig
var txb2 = new bitcoin.TransactionBuilder()
txb2.addInput(tx1.getId(), 0, bitcoin.Transaction.DEFAULT_SEQUENCE, script)
txb2.addOutput(pubKeys[4].getAddress(bitcoin.networks.testnet), 980000)
txb2.sign(0, privKeys[1])
var tx2 = txb2.buildIncomplete()
var txb3 = bitcoin.TransactionBuilder.fromTransaction(tx2)
txb3.prevOutScripts[0] = script
txb3.prevOutTypes[0] = 'multisig'
txb3.sign(0, privKeys[2])
var tx3 = txb3.build()
console.log(tx3)
console.log(tx3.toHex()) // push tx to network
var _ = require('lodash')
var bitcore = require('bitcore') // 0.12.5
var network = bitcore.Networks.testnet
var totalKeys = 3
var requiredSignatures = 2
var rawPrivateKeys = [
'cUTyK6kpESjzWRtvzb5KfenFB6PaGofwojkJMx68AQA6t2mQSKmV',
'cVQS3PFi9t1XWXAH2fKs5xczmeXKb5v4ZbwJ8u7vPgK9Xjx9CDts',
'cUoMsNvEeJGJpGwZNP3RRu9kYWHW4B2jTA3WPdnk9ZCMdtynJQDc'
]
// rawPrivateKeys = _.range(totalKeys).map(function () { return bitcore.PrivateKey(network).toWIF() })
var privateKeys = rawPrivateKeys.map(function (pk) { return bitcore.PrivateKey(pk) })
var publicKeys = privateKeys.map(function (pk) { return bitcore.PublicKey(pk) })
// multisig address
var address = bitcore.Address(publicKeys, requiredSignatures, network)
// get utxo from external source (chromanode, insight, blockr...)
var utxo = {
txId: '9500bed37ae3deaec7357d67864a57e193b2f0c87906cd17f28a2b0218495879',
outputIndex: 0,
address: address.toString(),
script: bitcore.Script(address).toHex(),
satoshis: 129191578
}
// create tx
var txObj = bitcore.Transaction()
.from(utxo, publicKeys, requiredSignatures)
.to('n3E3sYxTwz4FCU3LdUnKiiG1PTcPC654Za', utxo.satoshis - 100000)
.toObject()
// Alice sign tx
var sigAliceObj = bitcore.Transaction(txObj).getSignatures(privateKeys[0])[0]
// Bob sign tx
var sigBobObj = bitcore.Transaction(txObj).getSignatures(privateKeys[2])[0]
// apply Alice and Bob signatures
var tx = bitcore.Transaction(txObj)
.applySignature(sigAliceObj)
.applySignature(sigBobObj)
@itoonx
Copy link

itoonx commented Sep 15, 2016

very great !

@itoonx
Copy link

itoonx commented Sep 18, 2016

From this line : 23 (bitcoinjs-lib.js)

txb1.addInput('9774024749b0104a98648641285cac4d518c9f824d9432d4ad67c1931028711b', 0)

what is this : 9774024749b0104a98648641285cac4d518c9f824d9432d4ad67c1931028711b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment