@neuraiproject/neurai-scripts 0.2.0 → 0.4.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.
@@ -121,6 +121,7 @@ export declare const OP_XNA_ASSET = 192;
121
121
  export declare const OP_REFINPUTFIELD = 210;
122
122
  export declare const OP_REFINPUTASSETFIELD = 211;
123
123
  export declare const OP_REFINPUTCOUNT = 212;
124
+ export declare const OP_OUTPUTAUTHCOMMITMENT = 213;
124
125
  export declare const OP_CAT = 126;
125
126
  export declare const OP_SPLIT = 183;
126
127
  export declare const OP_REVERSEBYTES = 188;
@@ -33,3 +33,16 @@ export declare function decodeScriptNum(data: Uint8Array, label: string): bigint
33
33
  * only for values that are strictly positive (prices, selectors, indices).
34
34
  */
35
35
  export declare function readPushPositiveInt(c: Cursor, label: string): bigint;
36
+ /**
37
+ * Read a 1-byte selector as an UNSIGNED 8-bit integer (0..255). Accepts
38
+ * two on-wire encodings, because old vs new covenant builders differ:
39
+ * - `OP_1..OP_16` shorthand (single opcode) → values 1..16.
40
+ * - `0x01 <byte>` raw 1-byte push → any value 1..255.
41
+ *
42
+ * Values 0x80..0xff MUST use the raw-push form; the CScriptNum encoding
43
+ * would need a 0x00 padding byte and become 2 bytes on-stack, which
44
+ * consensus `OP_TXHASH` rejects. The builder in `script-pq.ts` emits the
45
+ * raw-push form unconditionally; the parser stays lenient so covenants
46
+ * built by older tools (using OP_N for small values) still round-trip.
47
+ */
48
+ export declare function readPushUint8(c: Cursor, label: string): number;
@@ -13,12 +13,16 @@
13
13
  *
14
14
  * output[0] = XNA payment to seller (value >= N * unitPriceSats)
15
15
  * output[1] = asset to buyer (tokenId, amount == N)
16
- * output[2] = covenant remainder (same scriptPubKey, amount == in - N)
16
+ * output[2] = covenant remainder (same AuthScript commitment, amount == in - N)
17
17
  * output[3+] = optional buyer change (not constrained by the covenant)
18
18
  *
19
- * The remainder UTXO reuses the spent scriptPubKey via
20
- * `OP_OUTPUTSCRIPT == OP_TXFIELD 0x03`, so the covenant is self-replicating
21
- * without hardcoding its own hash.
19
+ * The remainder UTXO reuses the spent covenant's AuthScript v1 commitment
20
+ * via `OP_OUTPUTAUTHCOMMITMENT(2) == OP_TXFIELD(0x02)` (NIP-023). Comparing
21
+ * commitments rather than full scriptPubKeys is mandatory here: the
22
+ * remainder output's asset wrapper (`OP_XNA_ASSET ... OP_DROP`) encodes a
23
+ * smaller `amountRaw` than the spent UTXO's wrapper, so the full-spk
24
+ * equality used by earlier drafts was vacuously unsatisfiable on any
25
+ * asset-wrapped covenant UTXO.
22
26
  *
23
27
  * The cancel branch uses classical ECDSA (`OP_HASH160 + OP_CHECKSIG`) and
24
28
  * commits to a 20-byte PKH. As a consequence this variant only accepts
package/dist/index.cjs CHANGED
@@ -189,6 +189,11 @@ const OP_XNA_ASSET = 0xc0;
189
189
  const OP_REFINPUTFIELD = 0xd2;
190
190
  const OP_REFINPUTASSETFIELD = 0xd3;
191
191
  const OP_REFINPUTCOUNT = 0xd4;
192
+ // ---------- Output auth commitment introspection (NIP-023) ----------
193
+ // Pushes the 32-byte AuthScript v1 commitment of a selected output's
194
+ // scriptPubKey. Symmetric to TXFIELD_AUTHSCRIPT_COMMITMENT for inputs;
195
+ // ignores any trailing OP_XNA_ASSET asset wrapper bytes.
196
+ const OP_OUTPUTAUTHCOMMITMENT = 0xd5;
192
197
  // ---------- Byte manipulation (DePIN-Test) ----------
193
198
  const OP_CAT = 0x7e;
194
199
  const OP_SPLIT = 0xb7;
@@ -311,6 +316,7 @@ var opcodes = /*#__PURE__*/Object.freeze({
311
316
  OP_NUMEQUALVERIFY: OP_NUMEQUALVERIFY,
312
317
  OP_NUMNOTEQUAL: OP_NUMNOTEQUAL,
313
318
  OP_OUTPUTASSETFIELD: OP_OUTPUTASSETFIELD,
319
+ OP_OUTPUTAUTHCOMMITMENT: OP_OUTPUTAUTHCOMMITMENT,
314
320
  OP_OUTPUTCOUNT: OP_OUTPUTCOUNT,
315
321
  OP_OUTPUTSCRIPT: OP_OUTPUTSCRIPT,
316
322
  OP_OUTPUTVALUE: OP_OUTPUTVALUE,
@@ -1754,12 +1760,16 @@ function splitAssetWrappedScriptPubKey(spkHex) {
1754
1760
  *
1755
1761
  * output[0] = XNA payment to seller (value >= N * unitPriceSats)
1756
1762
  * output[1] = asset to buyer (tokenId, amount == N)
1757
- * output[2] = covenant remainder (same scriptPubKey, amount == in - N)
1763
+ * output[2] = covenant remainder (same AuthScript commitment, amount == in - N)
1758
1764
  * output[3+] = optional buyer change (not constrained by the covenant)
1759
1765
  *
1760
- * The remainder UTXO reuses the spent scriptPubKey via
1761
- * `OP_OUTPUTSCRIPT == OP_TXFIELD 0x03`, so the covenant is self-replicating
1762
- * without hardcoding its own hash.
1766
+ * The remainder UTXO reuses the spent covenant's AuthScript v1 commitment
1767
+ * via `OP_OUTPUTAUTHCOMMITMENT(2) == OP_TXFIELD(0x02)` (NIP-023). Comparing
1768
+ * commitments rather than full scriptPubKeys is mandatory here: the
1769
+ * remainder output's asset wrapper (`OP_XNA_ASSET ... OP_DROP`) encodes a
1770
+ * smaller `amountRaw` than the spent UTXO's wrapper, so the full-spk
1771
+ * equality used by earlier drafts was vacuously unsatisfiable on any
1772
+ * asset-wrapped covenant UTXO.
1763
1773
  *
1764
1774
  * The cancel branch uses classical ECDSA (`OP_HASH160 + OP_CHECKSIG`) and
1765
1775
  * commits to a 20-byte PKH. As a consequence this variant only accepts
@@ -1856,11 +1866,14 @@ function buildPartialFillScript(params) {
1856
1866
  .op(OP_OUTPUTASSETFIELD) // [ N, name_out1 ]
1857
1867
  .pushBytes(tokenIdBytes)
1858
1868
  .op(OP_EQUALVERIFY); // [ N ]
1859
- // 5. Remainder covenant continuity (output 2): same scriptPubKey as spent
1869
+ // 5. Remainder covenant continuity (output 2): same AuthScript commitment
1870
+ // as spent. NIP-023: comparing 32-byte commitments rather than full
1871
+ // scriptPubKeys, because the remainder's asset wrapper carries a
1872
+ // different `amountRaw` than the spent UTXO's wrapper.
1860
1873
  b.pushInt(2)
1861
- .op(OP_OUTPUTSCRIPT) // [ N, spk_out2 ]
1862
- .pushInt(TXFIELD_SCRIPTPUBKEY)
1863
- .op(OP_TXFIELD) // [ N, spk_out2, spent_spk ]
1874
+ .op(OP_OUTPUTAUTHCOMMITMENT) // [ N, auth_out2 ]
1875
+ .pushInt(TXFIELD_AUTHSCRIPT_COMMITMENT)
1876
+ .op(OP_TXFIELD) // [ N, auth_out2, spent_auth ]
1864
1877
  .op(OP_EQUALVERIFY); // [ N ]
1865
1878
  // 6. Remainder output: same tokenId
1866
1879
  b.pushInt(2)
@@ -1965,12 +1978,19 @@ function buildPartialFillScriptPQ(params) {
1965
1978
  // ───────── Cancel branch (PQ via OP_CHECKSIGFROMSTACK) ─────────
1966
1979
  // scriptSig expected: <sigPQ> <pubKeyPQ> OP_1
1967
1980
  // After OP_IF consumes the flag: [ sig, pubKey ]
1981
+ //
1982
+ // The selector MUST be pushed as a raw 1-byte element — consensus
1983
+ // (`interpreter.cpp` OP_TXHASH case) rejects any stack item of
1984
+ // size ≠ 1. Using `pushInt(selector)` would work for 1..127 but emit a
1985
+ // 2-byte CScriptNum for 0x80..0xff (the sign-disambiguation pad), which
1986
+ // makes OP_TXHASH fail with SCRIPT_ERR_TXHASH. See
1987
+ // `memory/project_covenant_v3_findings.md` bug B for details.
1968
1988
  b.op(OP_IF)
1969
1989
  .op(OP_DUP) // [ sig, pubKey, pubKey ]
1970
1990
  .op(OP_SHA256) // [ sig, pubKey, H(pubKey) ]
1971
1991
  .pushBytes(pubKeyCommitment) // [ sig, pubKey, H(pubKey), commitment ]
1972
1992
  .op(OP_EQUALVERIFY) // [ sig, pubKey ]
1973
- .pushInt(txHashSelector) // [ sig, pubKey, selector ]
1993
+ .pushBytes(Uint8Array.of(txHashSelector)) // [ sig, pubKey, selector ] — always 1-byte stack item
1974
1994
  .op(OP_TXHASH) // [ sig, pubKey, txHash ]
1975
1995
  .op(OP_SWAP) // [ sig, txHash, pubKey ]
1976
1996
  .op(OP_CHECKSIGFROMSTACK) // [ 1 | 0 ]
@@ -2004,10 +2024,12 @@ function buildPartialFillScriptPQ(params) {
2004
2024
  .op(OP_OUTPUTASSETFIELD)
2005
2025
  .pushBytes(tokenIdBytes)
2006
2026
  .op(OP_EQUALVERIFY);
2007
- // 5. Remainder continuity (output 2) — same scriptPubKey as spent
2027
+ // 5. Remainder continuity (output 2) — same AuthScript commitment as spent
2028
+ // (NIP-023; see script.ts for why full-spk equality is unsatisfiable on
2029
+ // asset-wrapped covenant UTXOs).
2008
2030
  b.pushInt(2)
2009
- .op(OP_OUTPUTSCRIPT)
2010
- .pushInt(TXFIELD_SCRIPTPUBKEY)
2031
+ .op(OP_OUTPUTAUTHCOMMITMENT)
2032
+ .pushInt(TXFIELD_AUTHSCRIPT_COMMITMENT)
2011
2033
  .op(OP_TXFIELD)
2012
2034
  .op(OP_EQUALVERIFY);
2013
2035
  // 6. Remainder tokenId
@@ -2265,6 +2287,33 @@ function readPushPositiveInt(c, label) {
2265
2287
  const data = readPush(c, label);
2266
2288
  return decodeScriptNum(data, label);
2267
2289
  }
2290
+ /**
2291
+ * Read a 1-byte selector as an UNSIGNED 8-bit integer (0..255). Accepts
2292
+ * two on-wire encodings, because old vs new covenant builders differ:
2293
+ * - `OP_1..OP_16` shorthand (single opcode) → values 1..16.
2294
+ * - `0x01 <byte>` raw 1-byte push → any value 1..255.
2295
+ *
2296
+ * Values 0x80..0xff MUST use the raw-push form; the CScriptNum encoding
2297
+ * would need a 0x00 padding byte and become 2 bytes on-stack, which
2298
+ * consensus `OP_TXHASH` rejects. The builder in `script-pq.ts` emits the
2299
+ * raw-push form unconditionally; the parser stays lenient so covenants
2300
+ * built by older tools (using OP_N for small values) still round-trip.
2301
+ */
2302
+ function readPushUint8(c, label) {
2303
+ if (c.pos >= c.bytes.length) {
2304
+ throw new Error(`parse: end of script at ${label}`);
2305
+ }
2306
+ const opcode = c.bytes[c.pos];
2307
+ if (opcode >= OP_1 && opcode <= 0x60) {
2308
+ c.pos += 1;
2309
+ return opcode - OP_1 + 1;
2310
+ }
2311
+ const data = readPush(c, label);
2312
+ if (data.length !== 1) {
2313
+ throw new Error(`parse: ${label} must be a single-byte push, got ${data.length} bytes`);
2314
+ }
2315
+ return data[0];
2316
+ }
2268
2317
 
2269
2318
  /**
2270
2319
  * Parser for the Partial-Fill Sell Order covenant.
@@ -2326,12 +2375,12 @@ function parsePartialFillScript(script, network = 'xna-test') {
2326
2375
  expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer name)');
2327
2376
  const tokenIdBytes1 = readPush(c, 'tokenId #1');
2328
2377
  expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer name)');
2329
- // ───── Remainder continuity: same scriptPubKey ─────
2378
+ // ───── Remainder continuity: same AuthScript commitment (NIP-023) ─────
2330
2379
  expectByte(c, OP_2, 'OP_2 (remainder idx)');
2331
- expectByte(c, OP_OUTPUTSCRIPT, 'OP_OUTPUTSCRIPT (remainder)');
2332
- expectByte(c, OP_3, 'OP_3 (TXFIELD selector)');
2380
+ expectByte(c, OP_OUTPUTAUTHCOMMITMENT, 'OP_OUTPUTAUTHCOMMITMENT (remainder)');
2381
+ expectByte(c, OP_2, 'OP_2 (TXFIELD selector: AUTHSCRIPT_COMMITMENT)');
2333
2382
  expectByte(c, OP_TXFIELD, 'OP_TXFIELD');
2334
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder spk)');
2383
+ expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder auth)');
2335
2384
  // ───── Remainder tokenId check ─────
2336
2385
  expectByte(c, OP_2, 'OP_2 (remainder idx)');
2337
2386
  expectByte(c, OP_1, 'OP_1 (NAME selector)');
@@ -2398,11 +2447,13 @@ function parsePartialFillScriptPQ(script, network = 'xna-test') {
2398
2447
  throw new Error(`parse-pq: pubKeyCommitment must be 32 bytes, got ${pubKeyCommitment.length}`);
2399
2448
  }
2400
2449
  expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (cancel)');
2401
- const txHashSelectorBig = readPushPositiveInt(c, 'txHashSelector');
2402
- if (txHashSelectorBig < 1n || txHashSelectorBig > 0xffn) {
2403
- throw new Error(`parse-pq: txHashSelector out of range (${txHashSelectorBig})`);
2450
+ // Selector is read as an unsigned byte — consensus OP_TXHASH treats the
2451
+ // on-stack element as uint8, and the builder emits a raw 1-byte push so
2452
+ // selectors 0x80..0xff round-trip correctly (plan v3 bug B).
2453
+ const txHashSelector = readPushUint8(c, 'txHashSelector');
2454
+ if (txHashSelector < 1) {
2455
+ throw new Error(`parse-pq: txHashSelector 0x00 is rejected by OP_TXHASH`);
2404
2456
  }
2405
- const txHashSelector = Number(txHashSelectorBig);
2406
2457
  expectByte(c, OP_TXHASH, 'OP_TXHASH');
2407
2458
  expectByte(c, OP_SWAP, 'OP_SWAP');
2408
2459
  expectByte(c, OP_CHECKSIGFROMSTACK, 'OP_CHECKSIGFROMSTACK');
@@ -2430,11 +2481,12 @@ function parsePartialFillScriptPQ(script, network = 'xna-test') {
2430
2481
  expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer name)');
2431
2482
  const tokenIdBytes1 = readPush(c, 'tokenId #1');
2432
2483
  expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer name)');
2484
+ // Remainder continuity: same AuthScript commitment (NIP-023)
2433
2485
  expectByte(c, OP_2, 'OP_2 (remainder idx)');
2434
- expectByte(c, OP_OUTPUTSCRIPT, 'OP_OUTPUTSCRIPT (remainder)');
2435
- expectByte(c, OP_3, 'OP_3 (TXFIELD selector)');
2486
+ expectByte(c, OP_OUTPUTAUTHCOMMITMENT, 'OP_OUTPUTAUTHCOMMITMENT (remainder)');
2487
+ expectByte(c, OP_2, 'OP_2 (TXFIELD selector: AUTHSCRIPT_COMMITMENT)');
2436
2488
  expectByte(c, OP_TXFIELD, 'OP_TXFIELD');
2437
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder spk)');
2489
+ expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder auth)');
2438
2490
  expectByte(c, OP_2, 'OP_2 (remainder idx)');
2439
2491
  expectByte(c, OP_1, 'OP_1 (NAME selector)');
2440
2492
  expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (remainder name)');