createScriptSignature function

Uint8List createScriptSignature({
  1. required RawTransaction tx,
  2. required int i,
  3. required ElectrumOutput output,
  4. required HDWalletPurpose walletPurpose,
  5. required UTXONetworkType networkType,
  6. required BIP32 node,
})

Implementation

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

  final sigHash = switch (networkType) {
    BITCOINCASH_NETWORK() ||
    ZENIQ_NETWORK() when tx is BTCRawTransaction =>
      tx.bip143sigHash(
        index: i,
        prevScriptPubKey: prevScriptPubKey,
        output: output,
        hashType: hashType,
      ),
    LITECOIN_NETWORK() ||
    BITCOIN_NETWORK() ||
    EUROCOIN_NETWORK() =>
      tx.legacySigHash(
        index: i,
        prevScriptPubKey: prevScriptPubKey,
        hashType: hashType,
      ),
    _ =>
      throw SendFailure("Could not find sigHash for networkType $networkType"),
  };

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

  final scriptSig = encodeSignature(sig, hashType);

  final unlockingScript = constructScriptSig(
    walletPurpose: walletPurpose,
    signature: scriptSig,
    publicKey: node.publicKey,
  );

  return unlockingScript;
}