computeBchSighash function

Uint8List computeBchSighash({
  1. required DecodedBchTx tx,
  2. required int inputIndex,
  3. required Uint8List scriptCode,
  4. required BigInt value,
  5. int? hashType,
})

BIP-143 sighash preimage with FORKID, exactly as consensus defines it: version ‖ hashPrevouts ‖ hashSequence ‖ outpoint ‖ scriptCode ‖ value ‖ sequence ‖ hashOutputs ‖ locktime ‖ hashType(LE), double-SHA256d.

Implementation

Uint8List computeBchSighash({
  required DecodedBchTx tx,
  required int inputIndex,
  required Uint8List scriptCode,
  required BigInt value,
  int? hashType,
}) {
  final type = hashType ?? _sighashForkidAll;
  if (inputIndex < 0 || inputIndex >= tx.inputs.length) {
    throw EraSdkError('invalid-props', 'sighash input index out of range');
  }
  final input = tx.inputs[inputIndex];
  final hashPrevouts = sha256d(concatBytes([
    for (final i in tx.inputs) concatBytes([i.txidLE, _le32(i.index)]),
  ]));
  final hashSequence = sha256d(concatBytes([
    for (final i in tx.inputs) _le32(i.sequence),
  ]));
  final hashOutputs = sha256d(concatBytes([
    for (final o in tx.outputs)
      concatBytes([_le64(o.value), _varintBytes(o.script.length), o.script]),
  ]));
  final preimage = concatBytes([
    _le32(tx.version),
    hashPrevouts,
    hashSequence,
    input.txidLE,
    _le32(input.index),
    _varintBytes(scriptCode.length),
    scriptCode,
    _le64(value),
    _le32(input.sequence),
    hashOutputs,
    _le32(tx.locktime),
    _le32(type),
  ]);
  return sha256d(preimage);
}