Skip to content

Instantly share code, notes, and snippets.

@killerstorm
Created August 6, 2015 10:32
Show Gist options
  • Save killerstorm/1575ace1fa7c7595188b to your computer and use it in GitHub Desktop.
Save killerstorm/1575ace1fa7c7595188b to your computer and use it in GitHub Desktop.
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]
console.log('Alice signature:', sigAliceObj.toObject())
// Bob sign tx
var sigBobObj = bitcore.Transaction(txObj).getSignatures(privateKeys[2])[0]
console.log('Bob signature:', sigBobObj.toObject())
// apply Alice and Bob signatures
var tx = bitcore.Transaction(txObj)
.applySignature(sigAliceObj)
.applySignature(sigBobObj)
console.log('tx is fully signed', tx.isFullySigned())
console.log('tx is valid', tx.verify()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment