replaceInput method

Transaction replaceInput(
  1. Input input,
  2. int n
)

Replaces the input at n with the new input and invalidates other input signatures that have standard sighash types accordingly. This is useful for signing or otherwise updating inputs that cannot be signed with the sign method.

Implementation

Transaction replaceInput(Input input, int n) {

  final oldInput = inputs[n];

  if (input == oldInput) return this;

  final newPrevOut = input.prevOut != oldInput.prevOut;
  final newSequence = input.sequence != oldInput.sequence;

  final filtered = inputs.map(
    (input) => input.filterSignatures(
      (insig)
        // Allow ANYONECANPAY
        => insig.hashType.anyOneCanPay
        // Allow signature if previous output hasn't changed and the sequence
        // has not changed for taproot inputs or when using SIGHASH_ALL.
        || !(
          newPrevOut || (
            newSequence
            && (insig.hashType.all || insig is SchnorrInputSignature)
          )
        ),
    ),
  ).toList();

  return Transaction(
    version: version,
    inputs: [...filtered.take(n), input, ...filtered.sublist(n+1)],
    outputs: outputs,
    locktime: locktime,
  );

}