attachSignatures method

Btcb2SignedTransaction attachSignatures(
  1. List<List<int>> derSignatures
)

Attaches strict-DER, low-S signatures from an external signer.

Each signature must exclude the final BTCB2 sighash byte. This method validates the signature against the locally rebuilt digest and appends the required 0x21 byte before serializing the transaction.

Implementation

Btcb2SignedTransaction attachSignatures(List<List<int>> derSignatures) {
  if (derSignatures.length != state.inputs.length) {
    throw ArgumentError('Expected one signature for each BTCB2 input.');
  }
  final verifier = Secp256k1Verifier.fromKeyBytes(publicKeyBytes);
  final scriptSigs = <List<int>>[];
  for (var index = 0; index < derSignatures.length; index++) {
    final der = List<int>.from(derSignatures[index]);
    final compact = _strictDerToCompactLowS(der);
    if (!verifier.verify(signingHashBytes(index), compact)) {
      throw FormatException(
        'Signature $index does not match the public key.',
      );
    }
    scriptSigs.add([
      ..._pushData([...der, Btcb2TransactionAssemblyData.unifiedSighashType]),
      ..._pushData(publicKeyBytes),
    ]);
  }
  return Btcb2SignedTransaction._(_serializeTransaction(state, scriptSigs));
}