如何签署比特币现金原始交易?

时间:2019-11-06 19:04:33

标签: blockchain bitcoin bitcore bitcoin-testnet

我必须在testnet上签署原始交易,其十六进制如下所示,我尝试对交易生成部分中的所有内容进行硬编码,它可以正确广播。 但我必须分别实现交易和签名部分。  我正在使用bitcore-lib-cash包

const bitcore = require('bitcore-lib-cash') const txhex = 010000000139a7e6578a862a10151bdbe0ed4a833cd615273b0cd0ecda1616ee8407d7d8040000000000ffffffff0241010000000000001976a914f0ac6825bd05b406d5224eab0be73852a487e06c88ac94dc0100000000001976a914185ec62d62510d40795109e6484e0487c28a3caf88ac00000000

const private_key = 'private key here' 
let transaction = new bitcore.Transaction(txbuffer).sign(private_key)
console.log(private_key)

{
"errorMessage": "Invalid state: Not all utxo information is available to sign the transaction.",
"errorType": "bitcore.ErrorInvalidState",
"stackTrace": [
    "Error",
    "new NodeError (/var/task/node_modules/bitcore-lib-cash/lib/errors/index.js:20:41)",
    "Object.checkState (/var/task/node_modules/bitcore-lib-cash/lib/util/preconditions.js:9:13)",
    "Transaction.sign (/var/task/node_modules/bitcore-lib-cash/lib/transaction/transaction.js:1077:5)",
    "/var/task/src/custody/utils/biputils.js:155:12",
    "sign_transaction (/var/task/src/custody/utils/biputils.js:167:6)",
    "Object.generate_signature (/var/task/src/custody/assets/bitcoincash.js:220:23)",
    "<anonymous>",
    "process._tickDomainCallback (internal/process/next_tick.js:228:7)"
]

}

1 个答案:

答案 0 :(得分:0)

更新

我刚刚检查了bitcore-lib-cash的源代码,当调用toStringtoBuffer函数时,它们并未对input中的每个字段进行编码。

这是对input进行编码的过程之一:

Input.prototype.toBufferWriter = function(writer) {
  if (!writer) {
    writer = new BufferWriter();
  }
  writer.writeReverse(this.prevTxId);
  writer.writeUInt32LE(this.outputIndex);
  var script = this._scriptBuffer;
  writer.writeVarintNum(script.length);
  writer.write(script);
  writer.writeUInt32LE(this.sequenceNumber);
  return writer;
};

所以我认为您必须重写此toBufferWriter函数或解析txhex中的每个字段,然后再次重建事务。


您的txhex无效。

解码txhex之后,它在每个UTXO输入对象中都应该有一个output字段,这就是错误的出处。