Blockcypher 错误 非规范签名:长度标记错误

bra*_*i37 5 javascript bitcoin blockchain bitcoin-testnet blockcypher

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

\n\n
Error building input: Error generating scriptsig when building transaction: Invalid signature: Non-canonical signature: wrong length marker\n
Run Code Online (Sandbox Code Playgroud)\n\n

下面是我用来创建和签署交易的完整代码

\n\n
var bitcoin = require("bitcoinjs-lib");\nvar buffer = require(\'buffer\');\nvar keys = new bitcoin.ECPair.fromWIF(\'cMvPQZiG5mLARSjxbBwMxKwzhTHaxgpTsXB6ymx7SGAeYUqF8HAT\', bitcoin.networks.testnet);\n\nvar newtx = {\n    inputs: [{ addresses: [\'ms9ySK54aEC2ykDviet9jo4GZE6GxEZMzf\'] }],\n    outputs: [{ addresses: [\'msWccFYm5PPCn6TNPbNEnprA4hydPGadBN\'], value: 1000 }]\n};\n\n// calling the new endpoint, same as above\n$.post(\'https://api.blockcypher.com/v1/btc/test3/txs/new\', JSON.stringify(newtx)).then(function (tmptx) {\n    console.log(tmptx);\n    // signing each of the hex-encoded string required to finalize the transaction\n    tmptx.pubkeys = [];\n    tmptx.signatures = tmptx.tosign.map(function (tosign, n) {\n        tmptx.pubkeys.push(keys.publicKey.toString("hex"));\n\n        var SIGHASH_ALL = 0x01;\n        return bitcoin.script.signature.encode(keys.sign(new buffer.Buffer(tosign, "hex")), SIGHASH_ALL).toString("hex");\n\n    });\n\n    // sending back the transaction with all the signatures to broadcast\n    $.post(\'https://api.blockcypher.com/v1/btc/test3/txs/send\', tmptx).then(function (finaltx) {\n        console.log(finaltx);\n    }).catch(function (response) {\n        console.log(response.responseText);\n    });\n\n}).catch(function (response) {\n    console.log(response.responseText);\n});\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是它以 pubkeys、signatures、tosign 形式返回的内容

\n\n
pubkeys: Array [ "0280eed82a88edb3c9e303c5bae330c95db41d9f92cafd6081efb6029c6bf38bc6" ]\n\xe2\x80\x8b\nsignatures: Array [ "3044022009823c6cffc38b406322f507c36a3875b52a6151eaea80583821c7a5d1bf776d02203690252f20a4fc9a18350d77adc9c3ef0e6b3c5037dceb623aac38904e7062f701" ]\n\xe2\x80\x8b\ntosign: Array  [ "2ebdbde14f8bc2e6d949832e8dfd026147120ce60ff575c70c06f708be1e8556" ]\n
Run Code Online (Sandbox Code Playgroud)\n\n

由于它指出签名错误,我只能认为可能是这一行

\n\n
  var SIGHASH_ALL = 0x01;\n  return bitcoin.script.signature.encode(keys.sign(new buffer.Buffer(tosign, "hex")),SIGHASH_ALL,).toString("hex");\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n

小智 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;
Run Code Online (Sandbox Code Playgroud)