gpt4 book ai didi

node.js - 你如何使用 bitcoinjs-lib 构建交易?

转载 作者:搜寻专家 更新时间:2023-10-31 23:28:11 25 4
gpt4 key购买 nike

我正在 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)
})
}

这是一个交易示例(十六进制):

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>

关于node.js - 你如何使用 bitcoinjs-lib 构建交易?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20933304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com