简体   繁体   中英

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

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 .

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


Your txhex is invalid.

After decode txhex, it should have a output field in each UTXO input object, that's where the error came from.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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