gpt4 book ai didi

javascript - 如何使用 bitcoinjs 签署 BlockCypher 交易

转载 作者:行者123 更新时间:2023-12-01 15:46:56 24 4
gpt4 key购买 nike

我正在尝试使用 bitcoinjs 在比特币测试网上签署 BlockCypher 交易 here但我不断收到错误消息:

{"error": "无法反序列化请求:文字 true 中的无效字符 'x'(预期 'r')"}

我四处搜索,找不到关于问题所在的文档。下面是我用来尝试签署交易的代码。

var bitcoin = require("bitcoinjs-lib");
var buffer = require('buffer');
var keys = new bitcoin.ECPair.fromWIF('cMvPQZiG5mLARSjxbBwMxKwzhTHaxgpTsXB6ymx7SGAeYUqF8HAT', bitcoin.networks.testnet);
const publicKey = keys.publicKey;

console.log(keys.publicKey.toString("hex"));

var newtx = {
inputs: [{addresses: ['ms9ySK54aEC2ykDviet9jo4GZE6GxEZMzf']}],
outputs: [{addresses: ['msWccFYm5PPCn6TNPbNEnprA4hydPGadBN'], value: 1000}]
};
// calling the new endpoint, same as above
$.post('https://api.blockcypher.com/v1/btc/test3/txs/new', JSON.stringify(newtx))
.then(function(tmptx) {
// signing each of the hex-encoded string required to finalize the transaction
tmptx.pubkeys = [];
tmptx.signatures = tmptx.tosign.map(function(tosign, n) {
tmptx.pubkeys.push(keys.publicKey.toString("hex"));
return keys.sign(new buffer.Buffer(tosign, "hex")).toString("hex");
});
// sending back the transaction with all the signatures to broadcast
$.post('https://api.blockcypher.com/v1/btc/test3/txs/send', tmptx).then(function(finaltx) {
console.log(finaltx);
}).catch(function (response) {
console.log(response.responseText);
});
}).catch(function (response) {
console.log(response.responseText);
});

这行 return keys.sign(new buffer.Buffer(tosign, "hex")).toString("hex"); 似乎是问题所在,但我不确定哪里出了问题.

最佳答案

这个问题已被讨论和回答here . This postthis one需要特别研究。

据我了解,根据issue respective one在 BlockCypher 仓库中打开。尽管直到现在它的状态仍然打开,当前 BlockCypher JS docs respective API description包含该行的更改版本

return keys.sign(new buffer.Buffer(tosign, "hex")).toString("hex"); 

toString() 之前使用 toDER() 转换,因此现在看起来像这样

return keys.sign(new buffer.Buffer(tosign, "hex")).toDER().toString("hex"); 

关于javascript - 如何使用 bitcoinjs 签署 BlockCypher 交易,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56235135/

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