I am working on a function in node.js that uses bitcoinjs-lib to sweep the private key, but always gets an "Invalid Signature" from blockchain.info:
function sweep(privateKeyBase58, destAddr, cb) {
var sourceAddr = BitcoinTools.addressFromPrivateKey(privateKeyBase58)
, tx = new BitcoinLib.Transaction()
debug('gathering unspent outputs from blockchain.info...')
blockchaininfo.unspent(sourceAddr, function(err, unspent) {
debug('found %s unspent outputs', unspent.length)
var value = unspent.reduce(function(p, c) {
return p + c.value
}, 0)
debug('will sweep %s BTC from the private key', value / 1e8)
tx.addOutput(destAddr, value)
unspent.forEach(function(unspentOutput, i) {
var hashReversed = reverseHash(unspentOutput.tx_hash)
tx.addInput({ hash: hashReversed }, unspentOutput.tx_output_n)
var unspentOutScript = new BitcoinLib.Script(BitcoinLib.convert.hexToBytes(unspentOutput.script))
, hash = tx.hashTransactionForSignature(unspentOutScript, i, BitcoinLib.Transaction.SIGHASH_ALL)
, key = new BitcoinLib.Key(privateKeyBase58)
, signature = key.sign(hash)
signature.push(parseInt(BitcoinLib.Transaction.SIGHASH_ALL, 10))
tx.ins[i].script = BitcoinLib.Script.createInputScript(signature, key.getPub())
})
var hex = BitcoinLib.convert.bytesToHex(tx.serialize())
console.log(hex)
debug('pushing transaction...')
blockchaininfo.pushtx(hex, cb)
})
}
Here is an example transaction (hex):
0100000001310494ff7ab6a29beca3d7b1d4cd854ebea1632d1b46c7b9dcfd08dd261d4956000000008a4730440220756830acbf3ee0c55638c456d8986bc9f598d500927e2e60ebcbb35b
4909902702207b4fb9b106c35ff05a1b5c465c1194d2c91117781b621fef642256499fdd173a004104efdaca19c866fc4c57e906126687ff5a58cd5c517bb3af4d83428f2d3c5702e2fa01
ed8d4ea196124f16bba17c516e70ab1e3073e789d6bdd696de5656dc36f6ffffffff01204e0000000000001976a914065ad31915faa8a6145c4b770bc244e59bdb127288ac00000000
abrkn source
share