javascript - Blockcypher 错误非规范签名 : wrong length marker

标签 javascript blockchain bitcoin bitcoin-testnet blockcypher

我尝试使用 blockcypher 签署比特币测试网交易,但是当我发送交易时出现错误:

Error building input: Error generating scriptsig when building transaction: Invalid signature: Non-canonical signature: wrong length marker

下面是我用来创建和签署交易的完整代码
var bitcoin = require("bitcoinjs-lib");
var buffer = require('buffer');
var keys = new bitcoin.ECPair.fromWIF('cMvPQZiG5mLARSjxbBwMxKwzhTHaxgpTsXB6ymx7SGAeYUqF8HAT', bitcoin.networks.testnet);

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) {
    console.log(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"));

        var SIGHASH_ALL = 0x01;
        return bitcoin.script.signature.encode(keys.sign(new buffer.Buffer(tosign, "hex")), SIGHASH_ALL).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);
});

这是它在公钥、签名、tosign 方面返回的内容
pubkeys: Array [ "0280eed82a88edb3c9e303c5bae330c95db41d9f92cafd6081efb6029c6bf38bc6" ]
​
signatures: Array [ "3044022009823c6cffc38b406322f507c36a3875b52a6151eaea80583821c7a5d1bf776d02203690252f20a4fc9a18350d77adc9c3ef0e6b3c5037dceb623aac38904e7062f701" ]
​
tosign: Array  [ "2ebdbde14f8bc2e6d949832e8dfd026147120ce60ff575c70c06f708be1e8556" ]

正如它所说和签名错误,我只能认为它可能是这一行
  var SIGHASH_ALL = 0x01;
  return bitcoin.script.signature.encode(keys.sign(new buffer.Buffer(tosign, "hex")),SIGHASH_ALL,).toString("hex");

有谁知道这是什么意思?以及如何解决?

最佳答案

请试试这个。根据新的变化,我已经适应了以下事情。请尝试以下答案并进行测试。

tmptx.pubkeys.push(keys.publicKey.toString('hex'));
let signature = keys.sign(Buffer.from(tosign, "hex"));
let encodedSignature = bitcoin.script.signature.encode(signature,bitcoin.Transaction.SIGHASH_ALL);
let hexStr = encodedSignature.toString("hex").slice(0, -2); return hexStr;

关于javascript - Blockcypher 错误非规范签名 : wrong length marker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56406198/

相关文章:

JavaScript 正则表达式异常(无效组)

javascript - 以下示例中上下文的奇怪变化

javascript - 将区 block 链区 block 存储在数组中不是效率很低吗? ( Node .js)

blockchain - 在 Hyperledger 中,链码部署和调用、查询到底发生了什么?

c# - 判断比特币钱包地址是否为 "valid"

python - poloniex 中的 returnTradeHistory 总是返回空列表

javascript - 请求大小太大

web - 在 IPFS 上持久存储数据

javascript - Dogecoin 地址验证,在 nodejs 上

javascript - 上传前获取文件列表