@smartledger/bsv 9.6.0 → 9.8.0

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/bsv.d.ts CHANGED
@@ -12,7 +12,96 @@
12
12
  declare module '@smartledger/bsv' {
13
13
 
14
14
  export namespace crypto {
15
- class BN { }
15
+ /**
16
+ * bn.js, extended with Bitcoin's script-number and sign-magnitude codecs.
17
+ *
18
+ * This was `class BN { }` — an empty declaration — so `new BN(0)` was a
19
+ * compile error and every BN-typed value in the public API widened to `{}`.
20
+ * That made the whole Interpreter surface unusable from TypeScript, since a
21
+ * satoshi amount is a BN.
22
+ *
23
+ * The arithmetic below is bn.js's own; only the codecs are Bitcoin's. The
24
+ * bn.js internals (`red`, `mont`, `iushrn` and friends) are deliberately not
25
+ * enumerated — they are inherited, not part of this library's contract.
26
+ */
27
+ class BN {
28
+ constructor(
29
+ number?: number | string | number[] | Buffer | BN,
30
+ base?: number | 'hex',
31
+ endian?: 'le' | 'be'
32
+ );
33
+
34
+ static Zero: BN;
35
+ static One: BN;
36
+ static Minus1: BN;
37
+ static isBN(b: any): boolean;
38
+ static max(a: BN, b: BN): BN;
39
+ static min(a: BN, b: BN): BN;
40
+
41
+ static fromNumber(n: number): BN;
42
+ static fromString(str: string, base?: number): BN;
43
+ static fromHex(hex: string, opts?: { endian?: 'little' | 'big' }): BN;
44
+ static fromBuffer(buf: Buffer, opts?: { endian?: 'little' | 'big' | 'le' | 'be'; size?: number }): BN;
45
+ /** Sign-magnitude, as Bitcoin serialises signed values. */
46
+ static fromSM(buf: Buffer, opts?: { endian?: 'little' | 'big' }): BN;
47
+ /**
48
+ * Decode a script number. `size` is the era's width — 4 before Genesis,
49
+ * 750,000 after it, 32,000,000 after Chronicle. Ask the interpreter for it
50
+ * with `maxScriptNumLength()` rather than passing a constant.
51
+ */
52
+ static fromScriptNumBuffer(buf: Buffer, fRequireMinimal?: boolean, size?: number): BN;
53
+
54
+ toNumber(): number;
55
+ toString(base?: number | 'hex', length?: number): string;
56
+ toHex(opts?: { endian?: 'little' | 'big'; size?: number }): string;
57
+ toBuffer(opts?: { endian?: 'little' | 'big' | 'le' | 'be'; size?: number }): Buffer;
58
+ toSM(opts?: { endian?: 'little' | 'big' }): Buffer;
59
+ toSMBigEndian(): Buffer;
60
+ /** Minimal little-endian script number, sign in the high bit of the last byte. */
61
+ toScriptNumBuffer(): Buffer;
62
+ toJSON(): string;
63
+ toArray(endian?: 'le' | 'be', length?: number): number[];
64
+
65
+ clone(): BN;
66
+ copy(dest: BN): void;
67
+ neg(): BN;
68
+ abs(): BN;
69
+ add(b: BN): BN;
70
+ sub(b: BN): BN;
71
+ mul(b: BN): BN;
72
+ div(b: BN): BN;
73
+ mod(b: BN): BN;
74
+ umod(b: BN): BN;
75
+ sqr(): BN;
76
+ pow(b: BN): BN;
77
+ invm(b: BN): BN;
78
+ gcd(b: BN): BN;
79
+ and(b: BN): BN;
80
+ or(b: BN): BN;
81
+ xor(b: BN): BN;
82
+ shln(bits: number): BN;
83
+ shrn(bits: number): BN;
84
+ addn(n: number): BN;
85
+ subn(n: number): BN;
86
+ muln(n: number): BN;
87
+ divn(n: number): BN;
88
+ modn(n: number): number;
89
+
90
+ cmp(b: BN): -1 | 0 | 1;
91
+ ucmp(b: BN): -1 | 0 | 1;
92
+ eq(b: BN): boolean;
93
+ lt(b: BN): boolean;
94
+ lte(b: BN): boolean;
95
+ gt(b: BN): boolean;
96
+ gte(b: BN): boolean;
97
+ isZero(): boolean;
98
+ isNeg(): boolean;
99
+ isEven(): boolean;
100
+ isOdd(): boolean;
101
+ bitLength(): number;
102
+ byteLength(): number;
103
+ testn(bit: number): boolean;
104
+ }
16
105
 
17
106
  class ECDSA {
18
107
  hashbuf?: Buffer;
@@ -363,69 +452,227 @@ declare module '@smartledger/bsv' {
363
452
  function fromAddress(address: string | Address): Script;
364
453
 
365
454
  function empty(): Script;
366
- namespace Interpreter {
367
- const SCRIPT_ENABLE_SIGHASH_FORKID: any;
455
+ /** The four caps `getLimits()`/`setLimits()` move, in the spelling THEY use. */
456
+ interface InterpreterLimits {
457
+ maxScriptElementSize: number;
458
+ maximumElementSize: number;
459
+ maxOpsPerScript: number;
460
+ maxScriptSize: number;
461
+ }
462
+
463
+ /**
464
+ * A step handed to `stepListener`, before the stacks are mutated.
465
+ *
466
+ * `opcode` is typed structurally rather than as an Opcode: this file does not
467
+ * declare that class yet, and inventing a name here would be worse than
468
+ * describing what the object actually carries.
469
+ */
470
+ interface InterpreterStep {
471
+ pc: number;
472
+ opcode: { num: number; toString(): string };
473
+ }
474
+
475
+ /** A partial snapshot of evaluation state, applied over the defaults. */
476
+ interface InterpreterState {
477
+ stack?: Buffer[];
478
+ altstack?: Buffer[];
479
+ pc?: number;
480
+ pbegincodehash?: number;
481
+ nOpCount?: number;
482
+ vfExec?: boolean[];
483
+ errstr?: string;
484
+ flags?: number;
485
+ script?: Script;
486
+ tx?: Transaction;
487
+ nin?: number;
488
+ satoshisBN?: crypto.BN;
489
+ stepListener?: (step: InterpreterStep, stack: Buffer[], altstack: Buffer[]) => void;
490
+ }
491
+
492
+ interface Interpreter {
493
+ stack: Buffer[];
494
+ altstack: Buffer[];
495
+ pc: number;
496
+ pbegincodehash: number;
497
+ nOpCount: number;
498
+ vfExec: boolean[];
499
+ /** The node's error code for the last failure, or '' if none. */
500
+ errstr: string;
501
+ flags: number;
502
+ script?: Script;
503
+ tx?: Transaction;
504
+ nin?: number;
505
+ satoshisBN?: crypto.BN;
506
+ /** Debugging hook, invoked after each step with clones of the stacks. */
507
+ stepListener?: (step: InterpreterStep, stack: Buffer[], altstack: Buffer[]) => void;
508
+
509
+ initialize(obj?: InterpreterState): void;
510
+ set(obj: InterpreterState): void;
368
511
  /**
369
- * Chronicle: restores OP_2MUL/OP_2DIV, gives OP_VER/OP_VERIF/OP_VERNOTIF
370
- * meaning, and lets SIGHASH_CHRONICLE select the original digest.
371
- * Off by default — enabling it changes script evaluation.
512
+ * Verify an unlocking script against a locking script.
513
+ *
514
+ * Omitting `flags` resolves to `currentConsensusFlags()`. Passing a
515
+ * hand-assembled set is how a validator silently ends up pre-Genesis:
516
+ * without an era flag the interpreter applies the 2019 caps whatever
517
+ * feature opcodes are enabled. Prefer `mainnetFlags()` or no argument.
372
518
  */
373
- const SCRIPT_ENABLE_CHRONICLE: number;
374
- /** Block height at which Chronicle activated on BSV mainnet (2026-04-07). */
375
- const CHRONICLE_ACTIVATION_HEIGHT: number;
519
+ verify(
520
+ scriptSig: Script,
521
+ scriptPubkey: Script,
522
+ tx?: Transaction,
523
+ nin?: number,
524
+ flags?: number,
525
+ satoshisBN?: crypto.BN
526
+ ): boolean;
527
+ evaluate(): boolean;
528
+ step(): boolean;
529
+ checkSignatureEncoding(buf: Buffer): boolean;
530
+ checkPubkeyEncoding(buf: Buffer): boolean;
531
+ checkLockTime(nLockTime: crypto.BN): boolean;
532
+ checkSequence(nSequence: crypto.BN): boolean;
533
+
376
534
  /**
377
- * Script-verification flags matching BSV mainnet consensus.
378
- *
379
- * `verify()` itself defaults to no flags — a validator should state the
380
- * consensus context it is validating against. This assembles that context
381
- * correctly so callers do not have to.
535
+ * Which consensus era this evaluation belongs to, and the limits that
536
+ * follow from it. These read the SCRIPT_UTXO_AFTER_* flags on the
537
+ * instance, because the node decides almost everything by the era of the
538
+ * OUTPUT BEING SPENT — an output made before an upgrade is spent under
539
+ * the old rules forever.
540
+ */
541
+ isAfterGenesis(): boolean;
542
+ isAfterChronicle(): boolean;
543
+ /** 520 before Genesis, UNLIMITED after. */
544
+ maxScriptElementSize(): number;
545
+ /** 10,000 before Genesis, UNLIMITED after. */
546
+ maxScriptSize(): number;
547
+ /** 500 before Genesis (not Core's 201), UNLIMITED after. */
548
+ maxOpsPerScript(): number;
549
+ /** 4 bytes before Genesis, 750,000 after it, 32,000,000 after Chronicle. */
550
+ maxScriptNumLength(): number;
551
+ /** 20 before Genesis, UINT32_MAX after. */
552
+ maxPubKeysPerMultisig(): number;
553
+ /** 1000 elements before Genesis, UNLIMITED after. */
554
+ maxStackSize(): number;
555
+ /** UNLIMITED unless a caller sets MAX_STACK_MEMORY_USAGE_AFTER_GENESIS. */
556
+ maxStackMemoryUsage(): number;
557
+ /** Bytes across both stacks, each element charged STACK_ELEMENT_OVERHEAD. */
558
+ stackMemoryUsage(): number;
559
+ /** The stack limits, applied where the node applies them: after every opcode. */
560
+ checkStackLimits(): string | null;
561
+ }
562
+
563
+ interface InterpreterConstructor {
564
+ (obj?: InterpreterState): Interpreter;
565
+ new(obj?: InterpreterState): Interpreter;
566
+
567
+ /**
568
+ * Script-verification flags matching BSV mainnet consensus, including the
569
+ * ERA flags — without those the interpreter falls back to the pre-Genesis
570
+ * statics, so a validator named after mainnet applies 2019 limits.
382
571
  *
383
- * `afterChronicle` defaults to true; pass false to validate the spend of a
384
- * pre-activation UTXO, which is the distinction the node makes per input.
385
- * Note that script-number and element-size limits are statics raised by
386
- * `useGenesisLimits()`, not flags.
572
+ * `afterChronicle` defaults to true; pass false for a pre-activation UTXO,
573
+ * which is the distinction the node makes per input. CLTV and CSV are
574
+ * deliberately absent: Genesis reverted both to NOPs, and including them
575
+ * yields a validator stricter than consensus.
387
576
  */
388
- function mainnetFlags(opts?: { afterChronicle?: boolean }): number;
389
-
390
- // Pre-Genesis consensus caps (defaults: 520 / 4 / 201 / 10,000).
391
- // Mutable: see useGenesisLimits() for a one-call opt-in.
392
- let MAX_SCRIPT_ELEMENT_SIZE: number;
393
- let MAXIMUM_ELEMENT_SIZE: number;
394
- let MAX_OPS_PER_SCRIPT: number;
395
- /** Total serialized script size. Anything larger fails SCRIPT_ERR_SCRIPT_SIZE. */
396
- let MAX_SCRIPT_SIZE: number;
397
-
398
- interface Limits {
399
- maxScriptElementSize: number;
400
- maximumElementSize: number;
401
- maxOpsPerScript: number;
402
- maxScriptSize: number;
403
- }
577
+ mainnetFlags(opts?: { afterChronicle?: boolean }): number;
578
+ /**
579
+ * What `verify()` uses when given no flags. Every bit here is also in
580
+ * `mainnetFlags()`; a test asserts that, because the two drifting apart
581
+ * once made the default silently resolve to the weaker of two answers.
582
+ */
583
+ currentConsensusFlags(): number;
584
+ /** Applies Genesis limits AND returns mainnet flags. Mutates process state. */
585
+ useMainnetConsensus(opts?: { afterChronicle?: boolean; max?: number }): number;
586
+ /**
587
+ * Opt into post-Genesis limits process-wide. Prefer asking for the era via
588
+ * flags; this exists for callers managing the caps by hand. Pass an
589
+ * explicit `max` when verifying scripts from untrusted sources.
590
+ */
591
+ useGenesisLimits(max?: number): InterpreterConstructor;
592
+ getLimits(): InterpreterLimits;
593
+ setLimits(limits?: Partial<InterpreterLimits>): InterpreterConstructor;
594
+ castToBool(buf: Buffer): boolean;
404
595
 
596
+ /** Block height at which Chronicle activated on BSV mainnet (2026-04-07). */
597
+ CHRONICLE_ACTIVATION_HEIGHT: number;
598
+ /** What a limit reads as once the era removed it. */
599
+ UNLIMITED: number;
600
+
601
+ // PRE-Genesis caps. Since the limits became era-derived these are the
602
+ // fallback for a caller that sets no era flag, not the answer for every
603
+ // script. Mutable, so useGenesisLimits()/setLimits() still work.
604
+ MAX_SCRIPT_ELEMENT_SIZE: number;
605
+ MAXIMUM_ELEMENT_SIZE: number;
606
+ MAX_OPS_PER_SCRIPT: number;
607
+ MAX_SCRIPT_SIZE: number;
608
+ MAX_STACK_SIZE: number;
609
+
610
+ MAX_SCRIPT_NUM_LENGTH_AFTER_GENESIS: number;
611
+ MAX_SCRIPT_NUM_LENGTH_AFTER_CHRONICLE: number;
612
+ MAX_PUBKEYS_PER_MULTISIG_AFTER_GENESIS: number;
405
613
  /**
406
- * Opt into post-Genesis BSV consensus limits (no caps on stack
407
- * element size, script-number width, opcode count, or total script
408
- * size). Mutates Interpreter-wide static state — call once at app
409
- * startup. Pass an explicit `max` (e.g. 64 KB) when verifying
410
- * scripts from untrusted sources.
614
+ * UNLIMITED, because post-Genesis consensus does not bound stack memory.
615
+ * Assign STACK_MEMORY_USAGE_POLICY to validate against relay rules, or any
616
+ * ceiling to harden against untrusted input.
411
617
  */
412
- function useGenesisLimits(max?: number): typeof Interpreter;
413
- /** Capture the four caps, for restoring with setLimits(). */
414
- function getLimits(): Limits;
415
- /** Restore caps captured by getLimits(). */
416
- function setLimits(limits: Partial<Limits>): typeof Interpreter;
618
+ MAX_STACK_MEMORY_USAGE_AFTER_GENESIS: number;
619
+ /** The node's -maxstackmemoryusagepolicy default. Relay policy, not consensus. */
620
+ STACK_MEMORY_USAGE_POLICY: number;
621
+ /** Charged per element on top of its bytes; the container is not free either. */
622
+ STACK_ELEMENT_OVERHEAD: number;
623
+
624
+ LOCKTIME_THRESHOLD: number;
625
+ LOCKTIME_THRESHOLD_BN: crypto.BN;
626
+ SEQUENCE_LOCKTIME_DISABLE_FLAG: number;
627
+ SEQUENCE_LOCKTIME_MASK: number;
628
+ SEQUENCE_LOCKTIME_TYPE_FLAG: number;
629
+
630
+ SCRIPT_VERIFY_NONE: number;
631
+ SCRIPT_VERIFY_P2SH: number;
632
+ SCRIPT_VERIFY_STRICTENC: number;
633
+ SCRIPT_VERIFY_DERSIG: number;
634
+ SCRIPT_VERIFY_LOW_S: number;
635
+ SCRIPT_VERIFY_NULLDUMMY: number;
636
+ SCRIPT_VERIFY_SIGPUSHONLY: number;
637
+ SCRIPT_VERIFY_MINIMALDATA: number;
638
+ SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS: number;
639
+ SCRIPT_VERIFY_CLEANSTACK: number;
640
+ SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY: number;
641
+ SCRIPT_VERIFY_CHECKSEQUENCEVERIFY: number;
642
+ SCRIPT_VERIFY_MINIMALIF: number;
643
+ SCRIPT_VERIFY_NULLFAIL: number;
644
+ SCRIPT_VERIFY_COMPRESSED_PUBKEYTYPE: number;
645
+ SCRIPT_ENABLE_SIGHASH_FORKID: number;
646
+ SCRIPT_ENABLE_REPLAY_PROTECTION: number;
647
+ SCRIPT_ENABLE_MONOLITH_OPCODES: number;
648
+ SCRIPT_ENABLE_MAGNETIC_OPCODES: number;
649
+
650
+ // The era flags. SCRIPT_UTXO_AFTER_* describes the OUTPUT BEING SPENT and
651
+ // governs almost every rule; SCRIPT_GENESIS and SCRIPT_CHRONICLE describe
652
+ // the block the spending transaction is in.
653
+ SCRIPT_GENESIS: number;
654
+ SCRIPT_UTXO_AFTER_GENESIS: number;
655
+ /**
656
+ * Chronicle: restores OP_2MUL/OP_2DIV, gives OP_VER/OP_VERIF/OP_VERNOTIF
657
+ * meaning, and lets SIGHASH_CHRONICLE select the original digest.
658
+ */
659
+ SCRIPT_ENABLE_CHRONICLE: number;
660
+ /** The node's name for the SCRIPT_ENABLE_CHRONICLE bit. */
661
+ SCRIPT_CHRONICLE: number;
662
+ SCRIPT_UTXO_AFTER_CHRONICLE: number;
663
+ /** Every era bit, for masking one off a flag word. */
664
+ ERA_FLAGS: number;
665
+
666
+ /** Errors whose meaning depends on the era, keyed by node error code. */
667
+ ERA_SENSITIVE_ERRORS: Record<string, { what: string; lifts: string }>;
668
+ /** Set false (or BSV_NO_ERA_HINT=1) to silence the era diagnostic. */
669
+ eraDiagnostics: boolean;
670
+
671
+ true: Buffer;
672
+ false: Buffer;
417
673
  }
418
674
 
419
- function Interpreter(): {
420
- verify: (
421
- inputScript: Script,
422
- outputScript: Script,
423
- txn: Transaction,
424
- nin: Number,
425
- flags: any,
426
- satoshisBN: crypto.BN
427
- ) => boolean
428
- }
675
+ const Interpreter: InterpreterConstructor;
429
676
  }
430
677
 
431
678
  export class Script {