@neuraiproject/neurai-scripts 0.5.1 → 0.6.1

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.
@@ -101,6 +101,14 @@ export declare const OP_CHECKSIG = 172;
101
101
  export declare const OP_CHECKSIGVERIFY = 173;
102
102
  export declare const OP_CHECKMULTISIG = 174;
103
103
  export declare const OP_CHECKMULTISIGVERIFY = 175;
104
+ export declare const OP_KECCAK256 = 186;
105
+ export declare const OP_BLAKE2B = 187;
106
+ export declare const OP_BLAKE3 = 200;
107
+ export declare const OP_POSEIDON = 201;
108
+ export declare const OP_SHA3_256 = 202;
109
+ export declare const OP_SHA512 = 203;
110
+ export declare const OP_CHECKSIG_ED25519 = 221;
111
+ export declare const OP_CHECKSIGADD = 222;
104
112
  export declare const OP_NOP1 = 176;
105
113
  export declare const OP_NOP9 = 184;
106
114
  export declare const OP_NOP10 = 185;
@@ -122,6 +130,12 @@ export declare const OP_REFINPUTFIELD = 210;
122
130
  export declare const OP_REFINPUTASSETFIELD = 211;
123
131
  export declare const OP_REFINPUTCOUNT = 212;
124
132
  export declare const OP_OUTPUTAUTHCOMMITMENT = 213;
133
+ export declare const OP_INPUTVALUE = 214;
134
+ export declare const OP_CHAINCONTEXT = 215;
135
+ export declare const CHAINCONTEXT_HEIGHT = 1;
136
+ export declare const CHAINCONTEXT_MTP = 2;
137
+ export declare const CHAINCONTEXT_CHAIN_ID = 3;
138
+ export declare const OP_CHECKMERKLEINCLUSION = 193;
125
139
  export declare const OP_CAT = 126;
126
140
  export declare const OP_SPLIT = 183;
127
141
  export declare const OP_REVERSEBYTES = 188;
@@ -0,0 +1,7 @@
1
+ import type { Cursor } from '../../core/script-parser.js';
2
+ import type { ScriptBuilder } from '../../core/script-builder.js';
3
+ import type { PartialFillExpiration } from '../../types.js';
4
+ export declare function assertExpiration(expiration: PartialFillExpiration | undefined): PartialFillExpiration | undefined;
5
+ export declare function appendExpirationGate(b: ScriptBuilder, expiration: PartialFillExpiration | undefined): void;
6
+ export declare function readOptionalExpirationGate(c: Cursor, nextOpcodeWithoutGate: number, label: string): PartialFillExpiration | undefined;
7
+ export declare function assertSameExpiration(a: PartialFillExpiration | undefined, b: PartialFillExpiration | undefined, label: string): void;
package/dist/index.cjs CHANGED
@@ -155,6 +155,26 @@ const OP_CHECKSIG = 0xac;
155
155
  const OP_CHECKSIGVERIFY = 0xad;
156
156
  const OP_CHECKMULTISIG = 0xae;
157
157
  const OP_CHECKMULTISIGVERIFY = 0xaf;
158
+ // ---------- Extended hash opcodes ----------
159
+ // NIP-030: KECCAK256 / BLAKE2B in previously unassigned slots.
160
+ const OP_KECCAK256 = 0xba;
161
+ const OP_BLAKE2B = 0xbb;
162
+ // NIP-034a: modern hash opcodes (BLAKE3, SHA3-256, SHA-512).
163
+ // NIP-036: OP_POSEIDON — SNARK-friendly Poseidon hash over the BN254
164
+ // scalar field. Occupies slot 0xc9.
165
+ const OP_BLAKE3 = 0xc8;
166
+ const OP_POSEIDON = 0xc9;
167
+ const OP_SHA3_256 = 0xca;
168
+ const OP_SHA512 = 0xcb;
169
+ // ---------- Extended signature opcodes ----------
170
+ // NIP-035: strict-profile RFC 8032 PureEd25519 verifier in slot 0xdd.
171
+ // Stack contract: (sig64, msg, pubkey32 -- 0|1). Slots 0xd8..0xdc are
172
+ // reserved by NIP-033 (BLS12-381 family), unassigned in current code.
173
+ const OP_CHECKSIG_ED25519 = 0xdd;
174
+ // NIP-039: generic OP_CHECKSIG-compatible signature accumulator in slot
175
+ // 0xde. Stack contract: (sig, count, pubkey -- count + 0|1). Works with
176
+ // both legacy secp256k1 and PQ ML-DSA-44 keys via CPubKey::Verify().
177
+ const OP_CHECKSIGADD = 0xde;
158
178
  // ---------- Expansion / NOPs ----------
159
179
  // Only the NOPs not aliased by DePIN-Test opcodes are exported as NOPs.
160
180
  // OP_NOP2 == OP_CHECKLOCKTIMEVERIFY (0xb1)
@@ -194,6 +214,23 @@ const OP_REFINPUTCOUNT = 0xd4;
194
214
  // scriptPubKey. Symmetric to TXFIELD_AUTHSCRIPT_COMMITMENT for inputs;
195
215
  // ignores any trailing OP_XNA_ASSET asset wrapper bytes.
196
216
  const OP_OUTPUTAUTHCOMMITMENT = 0xd5;
217
+ // ---------- Input value introspection (NIP-024) ----------
218
+ // Pushes the satoshi value of a selected input's spent prevout.
219
+ const OP_INPUTVALUE = 0xd6;
220
+ // ---------- Chain context introspection (NIP-026) ----------
221
+ // Pushes chain-context fields (block height, median time, etc.) selected
222
+ // by a single-byte selector consumed from the stack.
223
+ const OP_CHAINCONTEXT = 0xd7;
224
+ // ---------- Selectors for OP_CHAINCONTEXT ----------
225
+ // Valid selector bytes consumed by OP_CHAINCONTEXT. HEIGHT is the candidate
226
+ // confirmation block height; MTP is the previous block's median time past.
227
+ const CHAINCONTEXT_HEIGHT = 0x01;
228
+ const CHAINCONTEXT_MTP = 0x02;
229
+ const CHAINCONTEXT_CHAIN_ID = 0x03;
230
+ // ---------- Merkle inclusion (NIP-031) ----------
231
+ // Native Merkle inclusion verifier. Consumes (leaf, scheme_id, proof,
232
+ // root) and pushes a boolean. Flag off → bad-opcode.
233
+ const OP_CHECKMERKLEINCLUSION = 0xc1;
197
234
  // ---------- Byte manipulation (DePIN-Test) ----------
198
235
  const OP_CAT = 0x7e;
199
236
  const OP_SPLIT = 0xb7;
@@ -239,6 +276,9 @@ var opcodes = /*#__PURE__*/Object.freeze({
239
276
  ASSETFIELD_REISSUABLE: ASSETFIELD_REISSUABLE,
240
277
  ASSETFIELD_TYPE: ASSETFIELD_TYPE,
241
278
  ASSETFIELD_UNITS: ASSETFIELD_UNITS,
279
+ CHAINCONTEXT_CHAIN_ID: CHAINCONTEXT_CHAIN_ID,
280
+ CHAINCONTEXT_HEIGHT: CHAINCONTEXT_HEIGHT,
281
+ CHAINCONTEXT_MTP: CHAINCONTEXT_MTP,
242
282
  OP_0: OP_0,
243
283
  OP_0NOTEQUAL: OP_0NOTEQUAL,
244
284
  OP_1: OP_1,
@@ -268,16 +308,22 @@ var opcodes = /*#__PURE__*/Object.freeze({
268
308
  OP_9: OP_9,
269
309
  OP_ABS: OP_ABS,
270
310
  OP_ADD: OP_ADD,
311
+ OP_BLAKE2B: OP_BLAKE2B,
312
+ OP_BLAKE3: OP_BLAKE3,
271
313
  OP_BOOLAND: OP_BOOLAND,
272
314
  OP_BOOLOR: OP_BOOLOR,
273
315
  OP_CAT: OP_CAT,
316
+ OP_CHAINCONTEXT: OP_CHAINCONTEXT,
274
317
  OP_CHECKLOCKTIMEVERIFY: OP_CHECKLOCKTIMEVERIFY,
318
+ OP_CHECKMERKLEINCLUSION: OP_CHECKMERKLEINCLUSION,
275
319
  OP_CHECKMULTISIG: OP_CHECKMULTISIG,
276
320
  OP_CHECKMULTISIGVERIFY: OP_CHECKMULTISIGVERIFY,
277
321
  OP_CHECKSEQUENCEVERIFY: OP_CHECKSEQUENCEVERIFY,
278
322
  OP_CHECKSIG: OP_CHECKSIG,
323
+ OP_CHECKSIGADD: OP_CHECKSIGADD,
279
324
  OP_CHECKSIGFROMSTACK: OP_CHECKSIGFROMSTACK,
280
325
  OP_CHECKSIGVERIFY: OP_CHECKSIGVERIFY,
326
+ OP_CHECKSIG_ED25519: OP_CHECKSIG_ED25519,
281
327
  OP_CHECKTEMPLATEVERIFY: OP_CHECKTEMPLATEVERIFY,
282
328
  OP_CODESEPARATOR: OP_CODESEPARATOR,
283
329
  OP_DEPTH: OP_DEPTH,
@@ -298,6 +344,8 @@ var opcodes = /*#__PURE__*/Object.freeze({
298
344
  OP_IFDUP: OP_IFDUP,
299
345
  OP_INPUTASSETFIELD: OP_INPUTASSETFIELD,
300
346
  OP_INPUTCOUNT: OP_INPUTCOUNT,
347
+ OP_INPUTVALUE: OP_INPUTVALUE,
348
+ OP_KECCAK256: OP_KECCAK256,
301
349
  OP_LESSTHAN: OP_LESSTHAN,
302
350
  OP_LESSTHANOREQUAL: OP_LESSTHANOREQUAL,
303
351
  OP_MAX: OP_MAX,
@@ -322,6 +370,7 @@ var opcodes = /*#__PURE__*/Object.freeze({
322
370
  OP_OUTPUTVALUE: OP_OUTPUTVALUE,
323
371
  OP_OVER: OP_OVER,
324
372
  OP_PICK: OP_PICK,
373
+ OP_POSEIDON: OP_POSEIDON,
325
374
  OP_PUSHDATA1: OP_PUSHDATA1,
326
375
  OP_PUSHDATA2: OP_PUSHDATA2,
327
376
  OP_PUSHDATA4: OP_PUSHDATA4,
@@ -336,6 +385,8 @@ var opcodes = /*#__PURE__*/Object.freeze({
336
385
  OP_ROT: OP_ROT,
337
386
  OP_SHA1: OP_SHA1,
338
387
  OP_SHA256: OP_SHA256,
388
+ OP_SHA3_256: OP_SHA3_256,
389
+ OP_SHA512: OP_SHA512,
339
390
  OP_SIZE: OP_SIZE,
340
391
  OP_SPLIT: OP_SPLIT,
341
392
  OP_SUB: OP_SUB,
@@ -1745,6 +1796,215 @@ function splitAssetWrappedScriptPubKey(spkHex) {
1745
1796
  };
1746
1797
  }
1747
1798
 
1799
+ /**
1800
+ * Shared parsing primitives for strict covenant parsers. Each covenant
1801
+ * parser walks the exact byte layout emitted by its builder and fails on
1802
+ * any deviation; the primitives here centralize the cursor arithmetic,
1803
+ * pushdata decoding, and CScriptNum decoding so the legacy and PQ parsers
1804
+ * (and future covenant parsers) cannot drift in rigor.
1805
+ */
1806
+ function makeCursor(bytes) {
1807
+ return { bytes, pos: 0 };
1808
+ }
1809
+ /** Consume one byte and verify it equals `expected`. */
1810
+ function expectByte(c, expected, label) {
1811
+ if (c.pos >= c.bytes.length) {
1812
+ throw new Error(`parse: unexpected end of script while reading ${label}`);
1813
+ }
1814
+ const got = c.bytes[c.pos];
1815
+ if (got !== expected) {
1816
+ throw new Error(`parse: expected ${label} = 0x${expected.toString(16)} at offset ${c.pos}, got 0x${got.toString(16)}`);
1817
+ }
1818
+ c.pos += 1;
1819
+ }
1820
+ /** Fail if the cursor has not consumed every byte of the script. */
1821
+ function assertTrailing(c) {
1822
+ if (c.pos !== c.bytes.length) {
1823
+ throw new Error(`parse: ${c.bytes.length - c.pos} trailing bytes after end of script`);
1824
+ }
1825
+ }
1826
+ /**
1827
+ * Read one pushdata element from the cursor. Supports direct pushes
1828
+ * (1..75 bytes), `OP_PUSHDATA1` and `OP_PUSHDATA2`. `OP_PUSHDATA4` is not
1829
+ * supported by any current covenant script and would overflow the
1830
+ * per-element cap anyway. Truncation is checked for all length fields
1831
+ * and payload ranges.
1832
+ */
1833
+ function readPush(c, label) {
1834
+ if (c.pos >= c.bytes.length) {
1835
+ throw new Error(`parse: unexpected end of script while reading push for ${label}`);
1836
+ }
1837
+ const opcode = c.bytes[c.pos];
1838
+ c.pos += 1;
1839
+ // Short direct push: 1..75 bytes
1840
+ if (opcode >= 0x01 && opcode <= 0x4b) {
1841
+ const len = opcode;
1842
+ if (c.pos + len > c.bytes.length) {
1843
+ throw new Error(`parse: short push of ${len} bytes exceeds script length at ${label}`);
1844
+ }
1845
+ const data = c.bytes.slice(c.pos, c.pos + len);
1846
+ c.pos += len;
1847
+ return data;
1848
+ }
1849
+ // OP_PUSHDATA1
1850
+ if (opcode === 0x4c) {
1851
+ if (c.pos >= c.bytes.length) {
1852
+ throw new Error(`parse: truncated PUSHDATA1 length at ${label}`);
1853
+ }
1854
+ const len = c.bytes[c.pos];
1855
+ c.pos += 1;
1856
+ if (c.pos + len > c.bytes.length) {
1857
+ throw new Error(`parse: PUSHDATA1 of ${len} bytes exceeds script length at ${label}`);
1858
+ }
1859
+ const data = c.bytes.slice(c.pos, c.pos + len);
1860
+ c.pos += len;
1861
+ return data;
1862
+ }
1863
+ // OP_PUSHDATA2
1864
+ if (opcode === 0x4d) {
1865
+ if (c.pos + 2 > c.bytes.length) {
1866
+ throw new Error(`parse: truncated PUSHDATA2 length at ${label}`);
1867
+ }
1868
+ const len = c.bytes[c.pos] | (c.bytes[c.pos + 1] << 8);
1869
+ c.pos += 2;
1870
+ if (c.pos + len > c.bytes.length) {
1871
+ throw new Error(`parse: PUSHDATA2 of ${len} bytes exceeds script length at ${label}`);
1872
+ }
1873
+ const data = c.bytes.slice(c.pos, c.pos + len);
1874
+ c.pos += len;
1875
+ return data;
1876
+ }
1877
+ throw new Error(`parse: expected a pushdata opcode at ${label}, got 0x${opcode.toString(16)} at offset ${c.pos - 1}`);
1878
+ }
1879
+ /**
1880
+ * Decode a `CScriptNum` byte vector (little-endian sign-magnitude, up to
1881
+ * 8 bytes) into a BigInt. Empty vector encodes 0.
1882
+ */
1883
+ function decodeScriptNum(data, label) {
1884
+ if (data.length === 0)
1885
+ return 0n;
1886
+ if (data.length > 8) {
1887
+ throw new Error(`parse: CScriptNum at ${label} exceeds 8 bytes`);
1888
+ }
1889
+ let n = 0n;
1890
+ for (let i = 0; i < data.length - 1; i += 1) {
1891
+ n |= BigInt(data[i]) << BigInt(8 * i);
1892
+ }
1893
+ const last = data[data.length - 1];
1894
+ n |= BigInt(last & 0x7f) << BigInt(8 * (data.length - 1));
1895
+ if (last & 0x80) {
1896
+ n = -n;
1897
+ }
1898
+ return n;
1899
+ }
1900
+ /**
1901
+ * Read a push as a non-negative CScriptNum. Recognises OP_1..OP_16
1902
+ * shorthand. `OP_0` is not accepted because the covenant callers use this
1903
+ * only for values that are strictly positive (prices, selectors, indices).
1904
+ */
1905
+ function readPushPositiveInt(c, label) {
1906
+ if (c.pos >= c.bytes.length) {
1907
+ throw new Error(`parse: end of script at ${label}`);
1908
+ }
1909
+ const opcode = c.bytes[c.pos];
1910
+ if (opcode >= OP_1 && opcode <= 0x60) {
1911
+ c.pos += 1;
1912
+ return BigInt(opcode - OP_1 + 1);
1913
+ }
1914
+ const data = readPush(c, label);
1915
+ return decodeScriptNum(data, label);
1916
+ }
1917
+ /**
1918
+ * Read a 1-byte selector as an UNSIGNED 8-bit integer (0..255). Accepts
1919
+ * two on-wire encodings, because old vs new covenant builders differ:
1920
+ * - `OP_1..OP_16` shorthand (single opcode) → values 1..16.
1921
+ * - `0x01 <byte>` raw 1-byte push → any value 1..255.
1922
+ *
1923
+ * Values 0x80..0xff MUST use the raw-push form; the CScriptNum encoding
1924
+ * would need a 0x00 padding byte and become 2 bytes on-stack, which
1925
+ * consensus `OP_TXHASH` rejects. The builder in `script-pq.ts` emits the
1926
+ * raw-push form unconditionally; the parser stays lenient so covenants
1927
+ * built by older tools (using OP_N for small values) still round-trip.
1928
+ */
1929
+ function readPushUint8(c, label) {
1930
+ if (c.pos >= c.bytes.length) {
1931
+ throw new Error(`parse: end of script at ${label}`);
1932
+ }
1933
+ const opcode = c.bytes[c.pos];
1934
+ if (opcode >= OP_1 && opcode <= 0x60) {
1935
+ c.pos += 1;
1936
+ return opcode - OP_1 + 1;
1937
+ }
1938
+ const data = readPush(c, label);
1939
+ if (data.length !== 1) {
1940
+ throw new Error(`parse: ${label} must be a single-byte push, got ${data.length} bytes`);
1941
+ }
1942
+ return data[0];
1943
+ }
1944
+
1945
+ const MAX_INT64 = 0x7fffffffffffffffn;
1946
+ function selectorForMode(mode) {
1947
+ if (mode === 'height')
1948
+ return CHAINCONTEXT_HEIGHT;
1949
+ if (mode === 'mtp')
1950
+ return CHAINCONTEXT_MTP;
1951
+ throw new Error('expiration.mode must be "height" or "mtp"');
1952
+ }
1953
+ function modeForSelector(selector) {
1954
+ if (selector === CHAINCONTEXT_HEIGHT)
1955
+ return 'height';
1956
+ if (selector === CHAINCONTEXT_MTP)
1957
+ return 'mtp';
1958
+ throw new Error('expiration OP_CHAINCONTEXT selector must be HEIGHT (0x01) or MTP (0x02)');
1959
+ }
1960
+ function assertExpiration(expiration) {
1961
+ if (expiration === undefined)
1962
+ return undefined;
1963
+ if (expiration === null || typeof expiration !== 'object') {
1964
+ throw new Error('expiration must be an object');
1965
+ }
1966
+ selectorForMode(expiration.mode);
1967
+ if (typeof expiration.value !== 'bigint') {
1968
+ throw new Error('expiration.value must be a bigint');
1969
+ }
1970
+ if (expiration.value <= 0n) {
1971
+ throw new Error('expiration.value must be > 0');
1972
+ }
1973
+ if (expiration.value > MAX_INT64) {
1974
+ throw new Error('expiration.value exceeds int64 range');
1975
+ }
1976
+ return expiration;
1977
+ }
1978
+ function appendExpirationGate(b, expiration) {
1979
+ if (!expiration)
1980
+ return;
1981
+ b.pushInt(expiration.value)
1982
+ .pushBytes(Uint8Array.of(selectorForMode(expiration.mode)))
1983
+ .op(OP_CHAINCONTEXT, OP_GREATERTHAN, OP_VERIFY);
1984
+ }
1985
+ function readOptionalExpirationGate(c, nextOpcodeWithoutGate, label) {
1986
+ if (c.pos >= c.bytes.length || c.bytes[c.pos] === nextOpcodeWithoutGate) {
1987
+ return undefined;
1988
+ }
1989
+ const value = readPushPositiveInt(c, `expiration value (${label})`);
1990
+ if (value <= 0n) {
1991
+ throw new Error(`parse: expiration value (${label}) must be > 0`);
1992
+ }
1993
+ const selector = readPushUint8(c, `expiration selector (${label})`);
1994
+ const mode = modeForSelector(selector);
1995
+ expectByte(c, OP_CHAINCONTEXT, `OP_CHAINCONTEXT (${label})`);
1996
+ expectByte(c, OP_GREATERTHAN, `OP_GREATERTHAN (${label})`);
1997
+ expectByte(c, OP_VERIFY, `OP_VERIFY (${label})`);
1998
+ return { mode, value };
1999
+ }
2000
+ function assertSameExpiration(a, b, label) {
2001
+ if (a === undefined && b === undefined)
2002
+ return;
2003
+ if (a === undefined || b === undefined || a.mode !== b.mode || a.value !== b.value) {
2004
+ throw new Error(`parse: expiration differs between ${label}`);
2005
+ }
2006
+ }
2007
+
1748
2008
  /**
1749
2009
  * Partial-Fill Sell Order covenant script (three-branch).
1750
2010
  *
@@ -1830,6 +2090,7 @@ function buildPartialFillScript(params) {
1830
2090
  const sellerPubKeyHash = decodeSellerAddress(sellerAddress);
1831
2091
  assertTokenId$1(tokenId);
1832
2092
  assertPrice$1(unitPriceSats);
2093
+ const expiration = assertExpiration(params.expiration);
1833
2094
  const sellerScriptPubKey = encodeP2PKHScriptPubKey(sellerPubKeyHash);
1834
2095
  const tokenIdBytes = new TextEncoder().encode(tokenId);
1835
2096
  const b = new ScriptBuilder();
@@ -1850,6 +2111,7 @@ function buildPartialFillScript(params) {
1850
2111
  // push N. The entire covenant is drained to vout[1]; no vout[2] is
1851
2112
  // constrained and none is required (consensus forbids zero-amount asset
1852
2113
  // transfers, so the partial-fill continuity check is simply skipped).
2114
+ appendExpirationGate(b, expiration);
1853
2115
  // 1. N = inputAmount
1854
2116
  b.pushInt(0)
1855
2117
  .pushInt(ASSETFIELD_AMOUNT)
@@ -1886,6 +2148,7 @@ function buildPartialFillScript(params) {
1886
2148
  // scriptSig: <N> <0> <0> ( N, full-flag=0, cancel-flag=0 )
1887
2149
  // Stack entering: [ N ]
1888
2150
  b.op(OP_ELSE);
2151
+ appendExpirationGate(b, expiration);
1889
2152
  // 1. Payment value (output 0) >= N * unitPriceSats
1890
2153
  b.op(OP_DUP) // [ N, N ]
1891
2154
  .pushInt(unitPriceSats) // [ N, N, price ]
@@ -2020,6 +2283,7 @@ function buildPartialFillScriptPQ(params) {
2020
2283
  assertTokenId(tokenId);
2021
2284
  assertPrice(unitPriceSats);
2022
2285
  assertSelector(txHashSelector);
2286
+ const expiration = assertExpiration(params.expiration);
2023
2287
  const payment = encodeSellerScriptPubKey(paymentAddress);
2024
2288
  const tokenIdBytes = new TextEncoder().encode(tokenId);
2025
2289
  const b = new ScriptBuilder();
@@ -2047,6 +2311,7 @@ function buildPartialFillScriptPQ(params) {
2047
2311
  // scriptSig: <1> <0>
2048
2312
  // Stack entering: [ ]
2049
2313
  // 1. N = inputAmount
2314
+ appendExpirationGate(b, expiration);
2050
2315
  b.pushInt(0)
2051
2316
  .pushInt(ASSETFIELD_AMOUNT)
2052
2317
  .op(OP_INPUTASSETFIELD);
@@ -2081,6 +2346,7 @@ function buildPartialFillScriptPQ(params) {
2081
2346
  // scriptSig: <N> <0> <0>
2082
2347
  // Stack entering: [ N ]
2083
2348
  b.op(OP_ELSE);
2349
+ appendExpirationGate(b, expiration);
2084
2350
  // 1. Payment value (output 0) >= N * unitPriceSats
2085
2351
  b.op(OP_DUP)
2086
2352
  .pushInt(unitPriceSats)
@@ -2270,152 +2536,6 @@ function buildCancelScriptSigPQHex(sigPQ, pubKey) {
2270
2536
  return bytesToHex(buildCancelScriptSigPQ(sigPQ, pubKey));
2271
2537
  }
2272
2538
 
2273
- /**
2274
- * Shared parsing primitives for strict covenant parsers. Each covenant
2275
- * parser walks the exact byte layout emitted by its builder and fails on
2276
- * any deviation; the primitives here centralize the cursor arithmetic,
2277
- * pushdata decoding, and CScriptNum decoding so the legacy and PQ parsers
2278
- * (and future covenant parsers) cannot drift in rigor.
2279
- */
2280
- function makeCursor(bytes) {
2281
- return { bytes, pos: 0 };
2282
- }
2283
- /** Consume one byte and verify it equals `expected`. */
2284
- function expectByte(c, expected, label) {
2285
- if (c.pos >= c.bytes.length) {
2286
- throw new Error(`parse: unexpected end of script while reading ${label}`);
2287
- }
2288
- const got = c.bytes[c.pos];
2289
- if (got !== expected) {
2290
- throw new Error(`parse: expected ${label} = 0x${expected.toString(16)} at offset ${c.pos}, got 0x${got.toString(16)}`);
2291
- }
2292
- c.pos += 1;
2293
- }
2294
- /** Fail if the cursor has not consumed every byte of the script. */
2295
- function assertTrailing(c) {
2296
- if (c.pos !== c.bytes.length) {
2297
- throw new Error(`parse: ${c.bytes.length - c.pos} trailing bytes after end of script`);
2298
- }
2299
- }
2300
- /**
2301
- * Read one pushdata element from the cursor. Supports direct pushes
2302
- * (1..75 bytes), `OP_PUSHDATA1` and `OP_PUSHDATA2`. `OP_PUSHDATA4` is not
2303
- * supported by any current covenant script and would overflow the
2304
- * per-element cap anyway. Truncation is checked for all length fields
2305
- * and payload ranges.
2306
- */
2307
- function readPush(c, label) {
2308
- if (c.pos >= c.bytes.length) {
2309
- throw new Error(`parse: unexpected end of script while reading push for ${label}`);
2310
- }
2311
- const opcode = c.bytes[c.pos];
2312
- c.pos += 1;
2313
- // Short direct push: 1..75 bytes
2314
- if (opcode >= 0x01 && opcode <= 0x4b) {
2315
- const len = opcode;
2316
- if (c.pos + len > c.bytes.length) {
2317
- throw new Error(`parse: short push of ${len} bytes exceeds script length at ${label}`);
2318
- }
2319
- const data = c.bytes.slice(c.pos, c.pos + len);
2320
- c.pos += len;
2321
- return data;
2322
- }
2323
- // OP_PUSHDATA1
2324
- if (opcode === 0x4c) {
2325
- if (c.pos >= c.bytes.length) {
2326
- throw new Error(`parse: truncated PUSHDATA1 length at ${label}`);
2327
- }
2328
- const len = c.bytes[c.pos];
2329
- c.pos += 1;
2330
- if (c.pos + len > c.bytes.length) {
2331
- throw new Error(`parse: PUSHDATA1 of ${len} bytes exceeds script length at ${label}`);
2332
- }
2333
- const data = c.bytes.slice(c.pos, c.pos + len);
2334
- c.pos += len;
2335
- return data;
2336
- }
2337
- // OP_PUSHDATA2
2338
- if (opcode === 0x4d) {
2339
- if (c.pos + 2 > c.bytes.length) {
2340
- throw new Error(`parse: truncated PUSHDATA2 length at ${label}`);
2341
- }
2342
- const len = c.bytes[c.pos] | (c.bytes[c.pos + 1] << 8);
2343
- c.pos += 2;
2344
- if (c.pos + len > c.bytes.length) {
2345
- throw new Error(`parse: PUSHDATA2 of ${len} bytes exceeds script length at ${label}`);
2346
- }
2347
- const data = c.bytes.slice(c.pos, c.pos + len);
2348
- c.pos += len;
2349
- return data;
2350
- }
2351
- throw new Error(`parse: expected a pushdata opcode at ${label}, got 0x${opcode.toString(16)} at offset ${c.pos - 1}`);
2352
- }
2353
- /**
2354
- * Decode a `CScriptNum` byte vector (little-endian sign-magnitude, up to
2355
- * 8 bytes) into a BigInt. Empty vector encodes 0.
2356
- */
2357
- function decodeScriptNum(data, label) {
2358
- if (data.length === 0)
2359
- return 0n;
2360
- if (data.length > 8) {
2361
- throw new Error(`parse: CScriptNum at ${label} exceeds 8 bytes`);
2362
- }
2363
- let n = 0n;
2364
- for (let i = 0; i < data.length - 1; i += 1) {
2365
- n |= BigInt(data[i]) << BigInt(8 * i);
2366
- }
2367
- const last = data[data.length - 1];
2368
- n |= BigInt(last & 0x7f) << BigInt(8 * (data.length - 1));
2369
- if (last & 0x80) {
2370
- n = -n;
2371
- }
2372
- return n;
2373
- }
2374
- /**
2375
- * Read a push as a non-negative CScriptNum. Recognises OP_1..OP_16
2376
- * shorthand. `OP_0` is not accepted because the covenant callers use this
2377
- * only for values that are strictly positive (prices, selectors, indices).
2378
- */
2379
- function readPushPositiveInt(c, label) {
2380
- if (c.pos >= c.bytes.length) {
2381
- throw new Error(`parse: end of script at ${label}`);
2382
- }
2383
- const opcode = c.bytes[c.pos];
2384
- if (opcode >= OP_1 && opcode <= 0x60) {
2385
- c.pos += 1;
2386
- return BigInt(opcode - OP_1 + 1);
2387
- }
2388
- const data = readPush(c, label);
2389
- return decodeScriptNum(data, label);
2390
- }
2391
- /**
2392
- * Read a 1-byte selector as an UNSIGNED 8-bit integer (0..255). Accepts
2393
- * two on-wire encodings, because old vs new covenant builders differ:
2394
- * - `OP_1..OP_16` shorthand (single opcode) → values 1..16.
2395
- * - `0x01 <byte>` raw 1-byte push → any value 1..255.
2396
- *
2397
- * Values 0x80..0xff MUST use the raw-push form; the CScriptNum encoding
2398
- * would need a 0x00 padding byte and become 2 bytes on-stack, which
2399
- * consensus `OP_TXHASH` rejects. The builder in `script-pq.ts` emits the
2400
- * raw-push form unconditionally; the parser stays lenient so covenants
2401
- * built by older tools (using OP_N for small values) still round-trip.
2402
- */
2403
- function readPushUint8(c, label) {
2404
- if (c.pos >= c.bytes.length) {
2405
- throw new Error(`parse: end of script at ${label}`);
2406
- }
2407
- const opcode = c.bytes[c.pos];
2408
- if (opcode >= OP_1 && opcode <= 0x60) {
2409
- c.pos += 1;
2410
- return opcode - OP_1 + 1;
2411
- }
2412
- const data = readPush(c, label);
2413
- if (data.length !== 1) {
2414
- throw new Error(`parse: ${label} must be a single-byte push, got ${data.length} bytes`);
2415
- }
2416
- return data[0];
2417
- }
2418
-
2419
2539
  /**
2420
2540
  * Parser for the Partial-Fill Sell Order covenant (three-branch).
2421
2541
  *
@@ -2449,6 +2569,7 @@ function parsePartialFillScript(script, network = 'xna-test') {
2449
2569
  expectByte(c, OP_ELSE, 'OP_ELSE (outer → fill)');
2450
2570
  // ═════ Inner IF — Full-fill branch ═════
2451
2571
  expectByte(c, OP_IF, 'OP_IF (inner full-fill)');
2572
+ const expirationFull = readOptionalExpirationGate(c, OP_0, 'full');
2452
2573
  // N = inputAmount
2453
2574
  expectByte(c, OP_0, 'OP_0 (input idx, full)');
2454
2575
  expectByte(c, OP_2, 'OP_2 (AMOUNT sel, full)');
@@ -2490,6 +2611,7 @@ function parsePartialFillScript(script, network = 'xna-test') {
2490
2611
  expectByte(c, OP_1, 'OP_1 (true, full)');
2491
2612
  expectByte(c, OP_ELSE, 'OP_ELSE (inner → partial fill)');
2492
2613
  // ═════ Inner ELSE — Partial-fill branch ═════
2614
+ const expirationPartial = readOptionalExpirationGate(c, OP_DUP, 'partial');
2493
2615
  // Payment value
2494
2616
  expectByte(c, OP_DUP, 'OP_DUP (price, partial)');
2495
2617
  const unitPriceSatsPartial = readPushPositiveInt(c, 'unitPriceSats (partial)');
@@ -2560,12 +2682,14 @@ function parsePartialFillScript(script, network = 'xna-test') {
2560
2682
  if (unitPriceSatsFull !== unitPriceSatsPartial) {
2561
2683
  throw new Error('parse: unitPriceSats differs between full-fill and partial-fill branches');
2562
2684
  }
2685
+ assertSameExpiration(expirationFull, expirationPartial, 'full-fill and partial-fill branches');
2563
2686
  const tokenId = new TextDecoder('utf-8', { fatal: true }).decode(tokenIdFull);
2564
2687
  return {
2565
2688
  network,
2566
2689
  sellerPubKeyHash,
2567
2690
  unitPriceSats: unitPriceSatsFull,
2568
2691
  tokenId,
2692
+ expiration: expirationFull,
2569
2693
  scriptHex: bytesToHex(bytes)
2570
2694
  };
2571
2695
  }
@@ -2615,6 +2739,7 @@ function parsePartialFillScriptPQ(script, network = 'xna-test') {
2615
2739
  expectByte(c, OP_ELSE, 'OP_ELSE (outer → fill)');
2616
2740
  // ═════ Inner IF — Full-fill branch ═════
2617
2741
  expectByte(c, OP_IF, 'OP_IF (inner full-fill)');
2742
+ const expirationFull = readOptionalExpirationGate(c, OP_0, 'full');
2618
2743
  expectByte(c, OP_0, 'OP_0 (input idx, full)');
2619
2744
  expectByte(c, OP_2, 'OP_2 (AMOUNT sel, full)');
2620
2745
  expectByte(c, OP_INPUTASSETFIELD, 'OP_INPUTASSETFIELD (full)');
@@ -2644,6 +2769,7 @@ function parsePartialFillScriptPQ(script, network = 'xna-test') {
2644
2769
  expectByte(c, OP_1, 'OP_1 (true, full)');
2645
2770
  expectByte(c, OP_ELSE, 'OP_ELSE (inner → partial fill)');
2646
2771
  // ═════ Inner ELSE — Partial-fill branch ═════
2772
+ const expirationPartial = readOptionalExpirationGate(c, OP_DUP, 'partial');
2647
2773
  expectByte(c, OP_DUP, 'OP_DUP (price, partial)');
2648
2774
  const unitPriceSatsPartial = readPushPositiveInt(c, 'unitPriceSats (partial)');
2649
2775
  expectByte(c, OP_MUL, 'OP_MUL (partial)');
@@ -2705,6 +2831,7 @@ function parsePartialFillScriptPQ(script, network = 'xna-test') {
2705
2831
  if (unitPriceSatsFull !== unitPriceSatsPartial) {
2706
2832
  throw new Error('parse-pq: unitPriceSats differs between full-fill and partial-fill branches');
2707
2833
  }
2834
+ assertSameExpiration(expirationFull, expirationPartial, 'full-fill and partial-fill branches');
2708
2835
  const tokenId = new TextDecoder('utf-8', { fatal: true }).decode(tokenIdFull);
2709
2836
  return {
2710
2837
  network,
@@ -2712,6 +2839,7 @@ function parsePartialFillScriptPQ(script, network = 'xna-test') {
2712
2839
  tokenId,
2713
2840
  unitPriceSats: unitPriceSatsFull,
2714
2841
  txHashSelector,
2842
+ expiration: expirationFull,
2715
2843
  paymentScriptPubKey: paymentScriptPubKeyFull,
2716
2844
  scriptHex: bytesToHex(bytes)
2717
2845
  };