Node.js 如何使用bitcoinjs库构造事务?

Node.js 如何使用bitcoinjs库构造事务?,node.js,bitcoin,Node.js,Bitcoin,我正在node.js中开发一个函数,它使用bitcoinjs lib扫描私钥,但总是从blockchain.info获取“无效签名”: // See https://github.com/kyledrake/coinpunk/blob/master/public/js/coinpunk/models/wallet.js function sweep(privateKeyBase58, destAddr, cb) { var sourceAddr = BitcoinTools.addres

我正在node.js中开发一个函数,它使用bitcoinjs lib扫描私钥,但总是从blockchain.info获取“无效签名”:

// See https://github.com/kyledrake/coinpunk/blob/master/public/js/coinpunk/models/wallet.js
function sweep(privateKeyBase58, destAddr, cb) {
    var sourceAddr = BitcoinTools.addressFromPrivateKey(privateKeyBase58)
    , tx = new BitcoinLib.Transaction()

    debug('gathering unspent outputs from blockchain.info...')

    // Retrieve the unspent outputs for the source address
    blockchaininfo.unspent(sourceAddr, function(err, unspent) {
        debug('found %s unspent outputs', unspent.length)

        // Sum the unspent values
        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) {
            // For some reason blockchain.info returns the unspent transaction
            // hashes reversed. ABCDEF is returned EFCDAB
            var hashReversed = reverseHash(unspentOutput.tx_hash)

            // Add the input
            tx.addInput({ hash: hashReversed }, unspentOutput.tx_output_n)

            // Sign the input
            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)

            // Not sure what this does. Taken from coinpunk
            signature.push(parseInt(BitcoinLib.Transaction.SIGHASH_ALL, 10))

            tx.ins[i].script = BitcoinLib.Script.createInputScript(signature, key.getPub())
        })

        // Serialize the transaction, convert to hex and push
        var hex = BitcoinLib.convert.bytesToHex(tx.serialize())

        console.log(hex)

        debug('pushing transaction...')

        blockchaininfo.pushtx(hex, cb)
    })
}
下面是一个示例事务(十六进制):



我认为这没有任何帮助。请提供一个描述,不要只是粘贴一些代码。你解决了这个问题吗?时间太长了,我不知道我当时在做什么。现在我更喜欢bitcore或bcoin而不是bitcoinjs库。
0100000001310494ff7ab6a29beca3d7b1d4cd854ebea1632d1b46c7b9dcfd08dd261d4956000000008a4730440220756830acbf3ee0c55638c456d8986bc9f598d500927e2e60ebcbb35b
4909902702207b4fb9b106c35ff05a1b5c465c1194d2c91117781b621fef642256499fdd173a004104efdaca19c866fc4c57e906126687ff5a58cd5c517bb3af4d83428f2d3c5702e2fa01
ed8d4ea196124f16bba17c516e70ab1e3073e789d6bdd696de5656dc36f6ffffffff01204e0000000000001976a914065ad31915faa8a6145c4b770bc244e59bdb127288ac00000000
<form action="/create_payment" method="POST">
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key=pk_test_6pRNASCoBOKtIshFeQd4XMUh
data-image="/square-image.png"
data-name="Demo Site"
data-description="2 widgets ($20.00)"
data-amount="2000"
data-currency="usd"
data-bitcoin="true">
</script>
</form>