signSchnorr method

void signSchnorr({
  1. required int vin,
  2. required ECPair keyPair,
  3. int? hashType,
  4. bool hd = false,
})

Implementation

void signSchnorr(
    {required int vin,
    required ECPair keyPair,
    int? hashType,
    bool hd = false}) {
  if (vin >= ins.length) throw ArgumentError('No input at index: $vin');

  hashType = hashType ?? SIGHASH_DEFAULT;
  final input = ins[vin];

  final prevouts =
      ins.where((e) => e.prevOutScript != null && e.value != null);
  final scripts = prevouts.map((e) {
    if (e.witnessUtxo != null) return e.witnessUtxo!;
    return e.prevOutScript!;
  }).toList();
  final values = prevouts.map((e) => e.value!).toList();
  final leafHash = tapLeafHash((input.tapLeafScript?.isNotEmpty ?? false)
      ? input.tapLeafScript!.first
      : null);

  final hashesForSig =
      hashForWitnessV1(vin, scripts, values, hashType, leafHash, null);
  final sig = keyPair.signSchnorr(message: hashesForSig);

  final witness = [_serializeTaprootSignature(sig, hashType)];
  if (input.tapLeafScript?.isNotEmpty ?? false) {
    final item = input.tapLeafScript!.first;
    witness.addAll([item.script, item.controlBlock]);
  }

  input.update(witness: witness);
}