ecash-lib 4.14.6 → 4.14.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/tx.ts CHANGED
@@ -13,7 +13,7 @@ import { Script } from './script.js';
13
13
  import { sha256d } from './hash.js';
14
14
  import { flagSignature } from './signatories.js';
15
15
  import { ALL_BIP143, SigHashType } from './sigHashType.js';
16
- import { UnsignedTx } from './unsignedTx.js';
16
+ import { UnsignedTx, UnsignedTxInput } from './unsignedTx.js';
17
17
 
18
18
  /**
19
19
  * Default value for nSequence of inputs if left undefined; this opts out of
@@ -327,8 +327,19 @@ export class Tx {
327
327
  const sortedIndices = [...indices].sort((a, b) => a - b);
328
328
  for (let i = 0; i < parsed.signatures.length; i++) {
329
329
  const sig = parsed.signatures[i];
330
- if (sig !== undefined && i < sortedIndices.length) {
331
- sigsByPubkey[sortedIndices[i]!] = sig;
330
+ const pkIdx = sortedIndices[i];
331
+ if (
332
+ sig !== undefined &&
333
+ pkIdx !== undefined &&
334
+ isValidMultisigSig(
335
+ ecc,
336
+ inputAt,
337
+ sig,
338
+ parsed.pubkeys[pkIdx]!,
339
+ true,
340
+ )
341
+ ) {
342
+ sigsByPubkey[pkIdx] = sig;
332
343
  }
333
344
  }
334
345
  } else {
@@ -463,7 +474,7 @@ export class Tx {
463
474
 
464
475
  /**
465
476
  * Whether every **multisig** input (identified from `signData`) has enough
466
- * signatures in its scriptSig. Non-multisig inputs are ignored.
477
+ * valid signatures in its scriptSig. Non-multisig inputs are ignored.
467
478
  *
468
479
  * If the transaction has **no** multisig inputs, this returns `true` (there
469
480
  * is nothing multisig-specific left to satisfy). That can look surprising on
@@ -474,6 +485,8 @@ export class Tx {
474
485
  * inputs are finalized elsewhere).
475
486
  */
476
487
  public isFullySignedMultisig(): boolean {
488
+ const ecc = new Ecc();
489
+ const unsignedTx = UnsignedTx.fromTx(this);
477
490
  for (let i = 0; i < this.inputs.length; i++) {
478
491
  const input = this.inputs[i];
479
492
  const multisigScript =
@@ -493,12 +506,60 @@ export class Tx {
493
506
  input.signData?.redeemScript === undefined
494
507
  ? input.script.parseBareMultisigSpend(multisigScript)
495
508
  : input.script.parseP2shMultisigSpend();
496
- const sigCount = parsed.signatures.filter(
497
- s => s !== undefined,
498
- ).length;
499
- if (sigCount < parsed.numSignatures) {
509
+ // Use the keys from signData, not from the scriptSig, which
510
+ // comes from the cosigner
511
+ const { numSignatures, numPubkeys, pubkeys } =
512
+ multisigScript.parseMultisigRedeemScript();
513
+ if (parsed.signatures.length !== numSignatures) {
500
514
  return false;
501
515
  }
516
+ // Rebuild the scriptSig from the parsed parts and require the
517
+ // same bytes. multisigSpend only accepts checkbits naming
518
+ // exactly m keys of the script, so this rejects extra, missing
519
+ // or out-of-range checkbits (the parser drops bits past the
520
+ // last key). It also rejects a P2SH redeem script that is not
521
+ // the one in signData.
522
+ const rebuilt = Script.multisigSpend({
523
+ signatures: parsed.signatures,
524
+ redeemScript: input.signData?.redeemScript,
525
+ pubkeyIndices: parsed.pubkeyIndices,
526
+ numPubkeys,
527
+ });
528
+ if (rebuilt.toHex() !== input.script.toHex()) {
529
+ return false;
530
+ }
531
+ const signerPks = pubkeys.filter(
532
+ (_, pkIdx) => parsed.pubkeyIndices?.has(pkIdx) ?? true,
533
+ );
534
+ // Signatures are in pubkey order. A Schnorr signature must be
535
+ // for the next key in the checkbits. An ECDSA signature may
536
+ // skip keys, as in OP_CHECKMULTISIG.
537
+ let nextPk = 0;
538
+ for (const sig of parsed.signatures) {
539
+ if (sig === undefined) {
540
+ return false;
541
+ }
542
+ for (;;) {
543
+ const pk = signerPks[nextPk++];
544
+ if (pk === undefined) {
545
+ return false;
546
+ }
547
+ if (
548
+ isValidMultisigSig(
549
+ ecc,
550
+ unsignedTx.inputAt(i),
551
+ sig,
552
+ pk,
553
+ parsed.isSchnorr,
554
+ )
555
+ ) {
556
+ break;
557
+ }
558
+ if (parsed.isSchnorr) {
559
+ return false;
560
+ }
561
+ }
562
+ }
502
563
  } catch {
503
564
  return false;
504
565
  }
@@ -507,6 +568,35 @@ export class Tx {
507
568
  }
508
569
  }
509
570
 
571
+ /**
572
+ * Whether `sig` (including its sighash byte) is a valid signature by `pk` for
573
+ * the input, using the sighash type the signature itself specifies.
574
+ */
575
+ function isValidMultisigSig(
576
+ ecc: Ecc,
577
+ input: UnsignedTxInput,
578
+ sig: Uint8Array,
579
+ pk: Uint8Array,
580
+ isSchnorr: boolean,
581
+ ): boolean {
582
+ const sigHashType = SigHashType.fromInt(sig[sig.length - 1]!);
583
+ if (sigHashType === undefined) {
584
+ return false;
585
+ }
586
+ try {
587
+ const sighash = sha256d(input.sigHashPreimage(sigHashType).bytes);
588
+ const rawSig = sig.slice(0, -1);
589
+ if (isSchnorr) {
590
+ ecc.schnorrVerify(rawSig, sighash, pk);
591
+ } else {
592
+ ecc.ecdsaVerify(rawSig, sighash, pk);
593
+ }
594
+ return true;
595
+ } catch {
596
+ return false;
597
+ }
598
+ }
599
+
510
600
  export function readTxOutput(bytes: Bytes): TxOutput {
511
601
  const sats = bytes.readU64();
512
602
  const script = Script.readWithSize(bytes);