4

Im trying to sign a bitcoin testnet transaction using blockcypher but when I go to send the transaction I get the error:

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

Below is the complete code im using to create and sign the transaction

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);
});

This is what it returns in terms of pubkeys, signatures, tosign

pubkeys: Array [ "0280eed82a88edb3c9e303c5bae330c95db41d9f92cafd6081efb6029c6bf38bc6" ]
​
signatures: Array [ "3044022009823c6cffc38b406322f507c36a3875b52a6151eaea80583821c7a5d1bf776d02203690252f20a4fc9a18350d77adc9c3ef0e6b3c5037dceb623aac38904e7062f701" ]
​
tosign: Array  [ "2ebdbde14f8bc2e6d949832e8dfd026147120ce60ff575c70c06f708be1e8556" ]

As it states and error with the signature I can only think it could be this line

  var SIGHASH_ALL = 0x01;
  return bitcoin.script.signature.encode(keys.sign(new buffer.Buffer(tosign, "hex")),SIGHASH_ALL,).toString("hex");

Would anyone know what this means? and how to solve it?

4

2 回答 2

0

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

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;
于 2020-04-26T07:44:41.680 回答
0

它失败的原因是因为 BlockCypher 自动添加了 SIGHASH_ALL。这是修复它的快速方法。

return bitcoin.script.signature.encode(keys.sign(new buffer.Buffer(tosign, "hex")), SIGHASH_ALL).toString("hex").slice(0, -2);
于 2021-06-24T06:41:26.837 回答