getTransactionDigest method
returns the transaction input's digest that is to be signed according.
txInIndex The index of the input that we wish to sign
script The scriptPubKey of the UTXO that we want to spend
sighash The type of the signature hash to be created
Implementation
Uint8List getTransactionDigest(
{required int txInIndex,
required Script script,
int sighash = SIGHASH_ALL}) {
final tx = copy(this);
for (final i in tx.inputs) {
i.scriptSig = const Script(script: []);
}
tx.inputs[txInIndex].scriptSig = script;
if ((sighash & 0x1f) == SIGHASH_NONE) {
tx.outputs.clear();
for (int i = 0; i < tx.inputs.length; i++) {
if (i != txInIndex) {
tx.inputs[i].sequence = Uint8List.fromList(EMPTY_TX_SEQUENCE);
}
}
} else if ((sighash & 0x1f) == SIGHASH_SINGLE) {
if (txInIndex >= tx.outputs.length) {
throw ArgumentError(
"Transaction index is greater than theavailable outputs");
}
final txout = tx.outputs[txInIndex];
tx.outputs.clear();
for (int i = 0; i < txInIndex; i++) {
tx.outputs.add(TxOutput(
amount: BigInt.from(NEGATIVE_SATOSHI),
scriptPubKey: const Script(script: [])));
}
tx.outputs.add(txout);
for (int i = 0; i < tx.inputs.length; i++) {
if (i != txInIndex) {
tx.inputs[i].sequence = Uint8List.fromList(EMPTY_TX_SEQUENCE);
}
}
}
if ((sighash & SIGHASH_ANYONECANPAY) != 0) {
final inp = tx.inputs[txInIndex];
tx.inputs.clear();
tx.inputs.add(inp);
}
Uint8List txForSign = tx.toBytes(segwit: false);
Uint8List packedData = packInt32LE(sighash);
txForSign = Uint8List.fromList([...txForSign, ...packedData]);
return doubleHash(txForSign);
}