简体   繁体   English

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

[英]How to sign bitcoin cash raw transaction?

I have to sign a raw transaction on testnet whose hex is given below, I tried hard coding everything in transaction generate part, it is broadcasting properly.我必须在下面给出十六进制的测试网上签署原始交易,我尝试在交易生成部分对所有内容进行硬编码,它正在正确广播。 but I have to implement the transaction and signature part separately.但我必须分别实现交易和签名部分。 I am using bitcore-lib-cash package我正在使用 bitcore-lib-cash package

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)"
]

} }

UPDATE更新

I just checked the source code of bitcore-lib-cash, when calling a toString or toBuffer function, they don't encode every field in input .我刚刚检查了 bitcore-lib-cash 的源代码,当调用toStringtoBuffer function 时,它们不会对input中的每个字段进行编码。

Here is one of the part of process to encode 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;
};

So I think you have to rewrite this toBufferWriter function or parse every field in the txhex and rebuild the transaction again.所以我认为你必须重写这个toBufferWriter function 或解析 txhex 中的每个字段并再次重建事务。


Your txhex is invalid.您的 txhex 无效。

After decode txhex, it should have a output field in each UTXO input object, that's where the error came from.解码 txhex 后,在每个 UTXO 输入 object 中应该有一个output字段,这就是错误的来源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM