@secondlayer/verify 0.5.2 → 0.5.3

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/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  type Bytes = Uint8Array;
2
+ /** Encodes bytes as a lowercase hex string. */
2
3
  declare const hex: (b: Bytes) => string;
3
4
  /** Hex to bytes; tolerates a 0x prefix. */
4
5
  declare const unhex: (s: string) => Bytes;
@@ -26,8 +27,10 @@ interface NakamotoHeader {
26
27
  */
27
28
  declare function parseNakamotoHeader(raw: Bytes): NakamotoHeader;
28
29
  /**
29
- * NakamotoBlockHeader::signer_signature_hash: sha512/256 over every header field
30
- * except the signer signatures. Also the header's block_hash.
30
+ * Hashes a Nakamoto header the way its signers sign it.
31
+ *
32
+ * It is sha512/256 over every header field except the signer signatures, which
33
+ * is also the header's `block_hash`.
31
34
  */
32
35
  declare const signerSignatureHash: (h: NakamotoHeader) => Bytes;
33
36
  /**
@@ -55,28 +58,32 @@ interface Epoch2Header {
55
58
  }
56
59
  type StacksHeader = NakamotoHeader | Epoch2Header;
57
60
  /**
58
- * Parse the epoch 2.x StacksBlockHeader at the start of `raw` (a header from
59
- * `/v2/headers`, or block bytes from `/v2/blocks/<id>`), with the 20-byte
60
- * consensus hash of the sortition that elected it.
61
+ * Parses the epoch 2.x block header at the start of `raw`.
62
+ *
63
+ * `raw` is a header from `/v2/headers`, or block bytes from `/v2/blocks/<id>`.
64
+ * `consensusHash` is the 20-byte hash of the sortition that elected the block.
61
65
  */
62
66
  declare function parseEpoch2Header(raw: Bytes, consensusHash: Bytes): Epoch2Header;
67
+ /** Narrows a Stacks header to an epoch 2.x header. */
63
68
  declare const isEpoch2Header: (h: StacksHeader) => h is Epoch2Header;
64
69
  /**
65
- * StacksBlockId (index block hash) = sha512/256(block_hash || consensus_hash).
66
- * A Nakamoto block hash is its signer signature hash; a 2.x one hashes the
67
- * whole header.
70
+ * Computes a header's block id, also called the index block hash.
71
+ *
72
+ * The id is sha512/256(block_hash || consensus_hash). A Nakamoto block hash is
73
+ * its signer signature hash; a 2.x one hashes the whole header.
68
74
  */
69
75
  declare const blockId2: (h: StacksHeader) => Bytes;
70
- /** TrieHash::from_key: sha512/256(utf8(key)). */
76
+ /** Hashes a key to its MARF path: sha512/256 of the UTF-8 key. */
71
77
  declare const marfPath: (key: string) => Bytes;
72
- /** MARFValue::from_value: sha512/256(utf8(value)) padded to 40 bytes. */
78
+ /** Hashes a value to its MARF value: sha512/256, padded to 40 bytes. */
73
79
  declare function marfValue(value: string): Bytes;
74
80
  /**
75
- * Block ids of the ancestor tries a proof crosses: each one is the backptr a
76
- * descendant trie follows toward the leaf. `verifyMarfProof` needs every one's
77
- * header. Read from untrusted proof bytes, so they are only fetch hints; the
78
- * proof still fails unless each header hashes to the id the trie committed.
79
- * Malformed proofs yield no ids.
81
+ * Lists the ancestor tries a MARF proof crosses.
82
+ *
83
+ * Each id is the backptr a descendant trie follows toward the leaf, and
84
+ * `verifyMarfProof` needs every one's header. They are read from untrusted
85
+ * proof bytes, so they are only fetch hints: the proof still fails unless each
86
+ * header hashes to the id the trie committed. Malformed proofs yield no ids.
80
87
  */
81
88
  declare function marfProofAncestors(proof: Bytes): string[];
82
89
  interface MarfProofInput {
@@ -97,9 +104,11 @@ interface MarfProofInput {
97
104
  headers: StacksHeader[];
98
105
  }
99
106
  /**
100
- * TrieMerkleProof::verify. True iff `value` is stored at `path` in the MARF
101
- * whose tip root is `root`. Inclusion only: MARF proofs cannot prove absence.
102
- * Malformed proof bytes verify false.
107
+ * Verifies a MARF inclusion proof.
108
+ *
109
+ * It is true only when `value` is stored at `path` in the MARF whose tip root
110
+ * is `root`. Inclusion only: MARF proofs cannot prove absence. Malformed proof
111
+ * bytes verify false.
103
112
  */
104
113
  declare function verifyMarfProof(input: MarfProofInput): boolean;
105
114
  interface WitnessLeaf {
@@ -159,8 +168,10 @@ declare const SIGNERS_CONTRACT = "SP000000000000000000002Q6VF78.signers";
159
168
  /** Signer principal -> weight. */
160
169
  type SignerSet = Map<string, bigint>;
161
170
  /**
162
- * Decode the `.signers` `cycle-signer-set` map value (hex or bytes, as stored
163
- * in the MARF): `(some (list (tuple (signer principal) (weight uint) ...)))`.
171
+ * Decodes a `cycle-signer-set` value from the `.signers` contract.
172
+ *
173
+ * The value is hex or bytes, as stored in the MARF:
174
+ * `(some (list (tuple (signer principal) (weight uint) ...)))`.
164
175
  */
165
176
  declare function decodeSignerSet(value: string | Bytes): SignerSet;
166
177
  interface SignerCheck {
@@ -180,9 +191,11 @@ interface SignerCheck {
180
191
  invalid: number;
181
192
  }
182
193
  /**
183
- * Recover each header signature's key over signer_signature_hash, map it to
184
- * its mainnet p2pkh principal, and sum weights from `set`. The set must be the
185
- * one for the reward cycle of the tenure-electing burn block.
194
+ * Sums the weight of the signers who signed a block header.
195
+ *
196
+ * It recovers each header signature's key over `signer_signature_hash`, maps it
197
+ * to its mainnet p2pkh principal, and sums weights from `set`. The set must be
198
+ * the one for the reward cycle of the tenure-electing burn block.
186
199
  */
187
200
  declare function verifySignerSignatures(header: NakamotoHeader, set: SignerSet): SignerCheck;
188
201
  /**
@@ -193,6 +206,7 @@ declare function verifySignerSignatures(header: NakamotoHeader, set: SignerSet):
193
206
  declare function verifyConsensusPreimage(consensusHash: Bytes, preimage: Bytes): string | null;
194
207
  /** Mainnet PoX parameters (first_burnchain_block_height, reward_cycle_length). */
195
208
  declare const MAINNET_FIRST_BURN_HEIGHT = 666050;
209
+ /** Burn blocks in a mainnet reward cycle. */
196
210
  declare const MAINNET_REWARD_CYCLE_LENGTH = 2100;
197
211
  /** Mainnet prepare phase: the last 100 burn blocks of a cycle pick the next cycle's signers. */
198
212
  declare const MAINNET_PREPARE_LENGTH = 100;
@@ -201,13 +215,14 @@ declare const cycleStart: (cycle: number, firstBurnHeight?: number, cycleLength?
201
215
  /** Reward cycle containing `burnHeight`. */
202
216
  declare function rewardCycle(burnHeight: number, firstBurnHeight?: number, cycleLength?: number): number;
203
217
  import { ClarityValue } from "@secondlayer/stacks/clarity";
204
- /** `vm::<contract>::0::<map>::<serialized key hex>` (make_key_for_data_map_entry). */
218
+ /** The MARF key of a map entry: `vm::<contract>::0::<map>::<serialized key hex>`. */
205
219
  declare function mapEntryKey(contractId: string, mapName: string, key: ClarityValue): string;
206
- /** `vm::<contract>::1::<var>` (make_key_for_trip, StoreType::Variable). */
220
+ /** The MARF key of a data variable: `vm::<contract>::1::<var>`. */
207
221
  declare function dataVarKey(contractId: string, varName: string): string;
208
222
  /**
209
- * `vm::<contract>::2::<token>::<principal json>` (get_ft_balance). The holder
210
- * is serde-JSON PrincipalData, e.g. `{"Standard":[22,[...20 bytes]]}`.
223
+ * The MARF key of a fungible token balance: `vm::<contract>::2::<token>::<principal json>`.
224
+ *
225
+ * The holder is serde-JSON PrincipalData, e.g. `{"Standard":[22,[...20 bytes]]}`.
211
226
  */
212
227
  declare function ftBalanceKey(contractId: string, tokenName: string, holder: string): string;
213
228
  interface BlockHeader {
@@ -224,6 +239,11 @@ interface BlockHeader {
224
239
  nonce: number;
225
240
  }
226
241
  type HeaderRule = "checkpoint" | "format" | "prev-link" | "bits" | "retarget" | "median-time-past" | "version" | "proof-of-work";
242
+ /**
243
+ * Thrown when a Bitcoin header fails a validation rule.
244
+ *
245
+ * `rule` names the rule and `height` the header that failed it.
246
+ */
227
247
  declare class HeaderValidationError extends Error {
228
248
  readonly height: number;
229
249
  readonly rule: HeaderRule;
@@ -248,6 +268,7 @@ interface ChainEntry {
248
268
  header: BlockHeader;
249
269
  raw: Uint8Array;
250
270
  }
271
+ /** A validated chain of Bitcoin headers, grown forward from a checkpoint. */
251
272
  declare class HeaderChain {
252
273
  private readonly base;
253
274
  private readonly periodStartTime;
@@ -255,6 +276,7 @@ declare class HeaderChain {
255
276
  private readonly heights;
256
277
  private work;
257
278
  private constructor();
279
+ /** Starts a header chain at a checkpoint. */
258
280
  static fromCheckpoint(checkpoint: Checkpoint): HeaderChain;
259
281
  get tip(): ChainEntry;
260
282
  /** Work summed from the checkpoint (inclusive) to the tip; not chainwork since genesis. */
@@ -262,6 +284,7 @@ declare class HeaderChain {
262
284
  get checkpointHeight(): number;
263
285
  /** Height of a header in this chain by display-order hash, or undefined. */
264
286
  heightOf(hashHex: string): number | undefined;
287
+ /** The header entry at a height, if the chain has one. */
265
288
  headerAt(height: number): ChainEntry | undefined;
266
289
  /**
267
290
  * Validates and appends headers in order. All-or-nothing: if any header
@@ -366,6 +389,12 @@ interface ProofSource {
366
389
  /** Indexed vm_events write rows of the block, checked against its proven diff. */
367
390
  getVmEvents?(height: number): Promise<VmEventRow[]>;
368
391
  }
392
+ /**
393
+ * Thrown when a proof source request fails.
394
+ *
395
+ * It carries the request `url`, the HTTP `status` (null when there was none)
396
+ * and the detail.
397
+ */
369
398
  declare class SourceError extends Error {
370
399
  readonly url: string;
371
400
  readonly status: number | null;
@@ -386,21 +415,39 @@ interface SecondlayerSourceOptions {
386
415
  fetch?: Fetch;
387
416
  }
388
417
  /**
389
- * Every proof input from the Secondlayer API: `/v1/proofs/*` (free, rate
390
- * limited) for blocks, epoch 2.x headers, MARF proofs, witnesses, burn
391
- * preimages, Bitcoin headers and each block's state writes; the Index API
392
- * (metered rows) for vm_events, read only when verifyBlock gets `rows: true`.
393
- * Methods are own properties, so a partial source spreads over it:
418
+ * Reads every proof input from the Secondlayer API.
419
+ *
420
+ * Blocks, epoch 2.x headers, MARF proofs, witnesses, burn preimages, Bitcoin
421
+ * headers and each block's state writes come from `/v1/proofs/*`, which is free
422
+ * and rate limited. Indexed `vm_events` rows come from the Index API as metered
423
+ * rows, and are read only when `verifyBlock` gets `rows: true`. Methods are own
424
+ * properties, so a partial source spreads over it:
394
425
  * `{ ...new SecondlayerProofSource(o), ...new NodeRpcProofSource(n) }`.
395
426
  */
396
427
  declare class SecondlayerProofSource implements ProofSource {
428
+ /** Raw block bytes, header first, by block id or height. */
397
429
  getBlock: (ref: string | number) => Promise<Bytes>;
430
+ /** An epoch 2.x header and its consensus hash, by index block hash. */
398
431
  getEpoch2Header: (blockId: string) => Promise<Epoch2HeaderResponse>;
432
+ /**
433
+ * A MARF inclusion proof for a path at a tip.
434
+ *
435
+ * It is null when the key is absent.
436
+ */
399
437
  getMarfProof: (pathHex: string, tipId: string) => Promise<MarfProofResponse | null>;
438
+ /** The state witness for a block. */
400
439
  getWitness: (blockId: string) => Promise<Bytes>;
440
+ /** The burn preimage behind a consensus hash. */
401
441
  getBurnPreimage: (consensusHash: string) => Promise<BurnPreimage>;
442
+ /** Raw 80-byte Bitcoin headers, `count` of them from height `from`. */
402
443
  getBitcoinHeaders: (from: number, count: number) => Promise<string[]>;
444
+ /**
445
+ * Every MARF write of a block, in ordinal order.
446
+ *
447
+ * It is null when there are none.
448
+ */
403
449
  getStateWrites: (height: number) => Promise<StateWrite[] | null>;
450
+ /** The indexed `vm_events` write rows of a block, from the Index API. */
404
451
  getVmEvents: (height: number) => Promise<VmEventRow[]>;
405
452
  constructor(opts?: SecondlayerSourceOptions);
406
453
  }
@@ -410,13 +457,21 @@ interface NodeRpcSourceOptions {
410
457
  fetch?: Fetch;
411
458
  }
412
459
  /**
413
- * Blocks, epoch 2.x headers and MARF proofs straight from a Stacks node's RPC.
414
- * Witnesses, burn preimages and Bitcoin headers need another source: spread
415
- * this over one.
460
+ * Reads blocks, epoch 2.x headers and MARF proofs from a Stacks node.
461
+ *
462
+ * It uses the node's RPC. Witnesses, burn preimages and Bitcoin headers need
463
+ * another source: spread this over one.
416
464
  */
417
465
  declare class NodeRpcProofSource implements Pick<ProofSource, "getBlock" | "getEpoch2Header" | "getMarfProof"> {
466
+ /** Raw block bytes, header first, by block id or height. */
418
467
  getBlock: (ref: string | number) => Promise<Bytes>;
468
+ /** An epoch 2.x header and its consensus hash, by index block hash. */
419
469
  getEpoch2Header: (blockId: string) => Promise<Epoch2HeaderResponse>;
470
+ /**
471
+ * A MARF inclusion proof for a path at a tip.
472
+ *
473
+ * It is null when the key is absent.
474
+ */
420
475
  getMarfProof: (pathHex: string, tipId: string) => Promise<MarfProofResponse | null>;
421
476
  constructor(opts: NodeRpcSourceOptions);
422
477
  }
@@ -489,11 +544,12 @@ interface BlockStateResult {
489
544
  failures: StateFailure[];
490
545
  }
491
546
  /**
492
- * Verify block state against an authenticated header: the witness must
493
- * recompute `stateRoot`, then every leaf is classified. With state_writes,
494
- * any leaf that is not a named write, a carried parent value or MARF
495
- * bookkeeping is a hidden write. Without them the diff is still proven
496
- * (every leaf is in the block): carried leaves are told apart only through
547
+ * Verifies a block's state against an authenticated header.
548
+ *
549
+ * The witness must recompute `stateRoot`, then every leaf is classified. With
550
+ * state writes, any leaf that is not a named write, a carried parent value or
551
+ * MARF bookkeeping is a hidden write. Without them the diff is still proven,
552
+ * since every leaf is in the block: carried leaves are told apart only through
497
553
  * `parentLeaves`, and the remaining writes come back unnamed.
498
554
  */
499
555
  declare function verifyBlockState(input: BlockStateInput): Promise<BlockStateResult>;
@@ -504,20 +560,25 @@ interface BlockTransaction {
504
560
  raw: Bytes;
505
561
  }
506
562
  /**
507
- * tx_merkle_root over txids: leaf = H(0x00 ‖ txid), node = H(0x01 ‖ l ‖ r),
508
- * an odd level duplicates its last node, and a lone leaf is still paired with
509
- * itself (one-tx blocks hash H(0x01 ‖ leaf ‖ leaf)).
563
+ * Computes the `tx_merkle_root` over transaction ids.
564
+ *
565
+ * A leaf is H(0x00 ‖ txid) and a node is H(0x01 ‖ l ‖ r). An odd level
566
+ * duplicates its last node, and a lone leaf is still paired with itself, so a
567
+ * one-transaction block hashes H(0x01 ‖ leaf ‖ leaf).
510
568
  */
511
569
  declare function txMerkleRoot2(txids: Bytes[]): Bytes;
570
+ /** Thrown for a malformed block body or a transaction root mismatch. */
512
571
  declare class TransactionsError extends Error {
513
572
  readonly code: "malformed" | "tx-root-mismatch";
514
573
  constructor(code: "malformed" | "tx-root-mismatch", message: string);
515
574
  }
516
575
  /**
517
- * The block's transactions, each hashed to its txid, checked against the
518
- * header's tx_merkle_root. The body after `header.byteLength` is a u32 count
519
- * then the transactions back to back. Null when the bytes are the header
520
- * alone (a source that serves headers only): nothing to check. Throws
576
+ * Hashes a block's transactions and checks them against the header.
577
+ *
578
+ * Each transaction is hashed to its txid and checked against the header's
579
+ * `tx_merkle_root`. The body after `header.byteLength` is a u32 count, then the
580
+ * transactions back to back. It returns null when the bytes are the header
581
+ * alone, as a source that serves headers only does. It throws
521
582
  * {@link TransactionsError} on a malformed body, trailing bytes or a root
522
583
  * mismatch.
523
584
  */
@@ -606,8 +667,10 @@ interface VerifyOptions {
606
667
  onStep?: (step: VerifyStep) => void;
607
668
  }
608
669
  /**
609
- * Verifies blocks from one checkpoint against one source, caching the Bitcoin
610
- * header chain, proven signer sets and fetched headers across calls.
670
+ * Verifies blocks from one checkpoint against one source.
671
+ *
672
+ * It caches the Bitcoin header chain, proven signer sets and fetched headers
673
+ * across calls.
611
674
  */
612
675
  declare class BlockVerifier {
613
676
  private readonly source;
@@ -749,9 +812,11 @@ declare const LINKS: readonly {
749
812
  label: string
750
813
  }[];
751
814
  /**
752
- * One line per link, then the verdict: the human view of a verification,
753
- * shared by the CLI and the browser checker. Ancestry appears only below the
754
- * checkpoint. `rows` says whether the caller asked for the rows link.
815
+ * Describes a verification as one line per link and a verdict.
816
+ *
817
+ * It is the human view, shared by the CLI and the browser checker. Ancestry
818
+ * appears only below the checkpoint. `rows` says whether the caller asked for
819
+ * the rows link.
755
820
  */
756
821
  declare function describeLinks(r: BlockVerification, cp: VerifyCheckpoint, opts?: {
757
822
  rows?: boolean