createScriptWitness function

Uint8List createScriptWitness({
  1. required BTCRawTransaction tx,
  2. required int i,
  3. required ElectrumOutput output,
  4. required UTXONetworkType networkType,
  5. required BIP32 node,
})

Implementation

Uint8List createScriptWitness({
  required BTCRawTransaction tx,
  required int i,
  required ElectrumOutput output,
  required UTXONetworkType networkType,
  required BIP32 node,
}) {
  final hashType = networkType.sighash.all;
  final prevScriptPubKey = output.scriptPubKey.lockingScript;

  assert(output.scriptPubKey.isSegwit);

  final sigHash = tx.bip143sigHash(
    index: i,
    prevScriptPubKey: prevScriptPubKey,
    output: output,
    hashType: hashType,
  );

  final sig = signInput(bip32: node, sigHash: sigHash);

  final scriptSig = encodeSignature(sig, hashType);

  final pubkey = node.publicKey;

  return [
    0x02,
    scriptSig.length,
    ...scriptSig,
    pubkey.length,
    ...pubkey,
  ].toUint8List;
}