首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用bitcoinjs对BlockCypher事务进行签名

如何使用bitcoinjs对BlockCypher事务进行签名
EN

Stack Overflow用户
提问于 2019-05-21 17:21:42
回答 1查看 361关注 0票数 3

我正在尝试使用here中描述的比特币引擎在比特币测试网上签署一项BlockCypher交易,但我一直收到错误:

{"error": "Couldn't deserialize request: invalid character 'x' in literal true (expecting 'r')"}

我已经到处搜索了,但找不到关于问题所在的文档。下面是我用来尝试和签署事务的代码。

代码语言:javascript
复制
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");这一行是问题所在,但我不确定哪里出了问题。

EN

回答 1

Stack Overflow用户

发布于 2020-09-11 17:50:31

讨论了这个问题,并回答了hereThis postthis one将特别受到关注。

据我所知,根据这个问题,respective one是在BlockCypher repo上打开的。尽管到目前为止它的状态仍然是opened,但是当前的BlockCypher JS docs respective API description包含该行的更改版本

代码语言:javascript
复制
return keys.sign(new buffer.Buffer(tosign, "hex")).toString("hex"); 

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

代码语言:javascript
复制
return keys.sign(new buffer.Buffer(tosign, "hex")).toDER().toString("hex"); 
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56235135

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档