@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.
package/README.md CHANGED
@@ -292,9 +292,13 @@ custodial DEX, no off-chain matching, no second signature from the
292
292
  seller.
293
293
 
294
294
  The covenant is **stateful via UTXO continuity**. The output at `vout[2]`
295
- of every fill must carry the same `scriptPubKey` as the input being
296
- spent (enforced on the fly by `OP_OUTPUTSCRIPT == OP_TXFIELD 0x03`), so
297
- the covenant self-replicates without hardcoding its own hash. The
295
+ of every fill must carry the same AuthScript v1 commitment as the input
296
+ being spent (enforced on the fly by
297
+ `OP_OUTPUTAUTHCOMMITMENT == OP_TXFIELD 0x02`, NIP-023), so the covenant
298
+ self-replicates without hardcoding its own hash. Earlier drafts compared
299
+ full scriptPubKeys via `OP_OUTPUTSCRIPT`, but that is unsatisfiable on
300
+ asset-wrapped covenant UTXOs because the remainder's asset wrapper
301
+ encodes a different `amountRaw` than the spent UTXO's. The
298
302
  remaining asset quantity is read directly from the input via
299
303
  `OP_INPUTASSETFIELD` and the subtraction `inputAmount − N` is verified,
300
304
  so the same script works for any remainder as the lot drains. Asset
@@ -402,7 +406,7 @@ OP_ELSE
402
406
  <0> OP_OUTPUTSCRIPT <sellerP2PKH> OP_EQUALVERIFY // payment dest
403
407
  OP_DUP <1> <0x02> OP_OUTPUTASSETFIELD OP_EQUALVERIFY // buyer amount == N
404
408
  <1> <0x01> OP_OUTPUTASSETFIELD <tokenId> OP_EQUALVERIFY // buyer name == tokenId
405
- <2> OP_OUTPUTSCRIPT <0x03> OP_TXFIELD OP_EQUALVERIFY // continuity
409
+ <2> OP_OUTPUTAUTHCOMMITMENT <0x02> OP_TXFIELD OP_EQUALVERIFY // continuity (NIP-023)
406
410
  <2> <0x01> OP_OUTPUTASSETFIELD <tokenId> OP_EQUALVERIFY // remainder name
407
411
  <2> <0x02> OP_OUTPUTASSETFIELD OP_OVER
408
412
  <0> <0x02> OP_INPUTASSETFIELD OP_SWAP OP_SUB OP_EQUALVERIFY // remainder = in - N
@@ -190,6 +190,11 @@ var NeuraiScriptsBundle = (function (exports) {
190
190
  const OP_REFINPUTFIELD = 0xd2;
191
191
  const OP_REFINPUTASSETFIELD = 0xd3;
192
192
  const OP_REFINPUTCOUNT = 0xd4;
193
+ // ---------- Output auth commitment introspection (NIP-023) ----------
194
+ // Pushes the 32-byte AuthScript v1 commitment of a selected output's
195
+ // scriptPubKey. Symmetric to TXFIELD_AUTHSCRIPT_COMMITMENT for inputs;
196
+ // ignores any trailing OP_XNA_ASSET asset wrapper bytes.
197
+ const OP_OUTPUTAUTHCOMMITMENT = 0xd5;
193
198
  // ---------- Byte manipulation (DePIN-Test) ----------
194
199
  const OP_CAT = 0x7e;
195
200
  const OP_SPLIT = 0xb7;
@@ -312,6 +317,7 @@ var NeuraiScriptsBundle = (function (exports) {
312
317
  OP_NUMEQUALVERIFY: OP_NUMEQUALVERIFY,
313
318
  OP_NUMNOTEQUAL: OP_NUMNOTEQUAL,
314
319
  OP_OUTPUTASSETFIELD: OP_OUTPUTASSETFIELD,
320
+ OP_OUTPUTAUTHCOMMITMENT: OP_OUTPUTAUTHCOMMITMENT,
315
321
  OP_OUTPUTCOUNT: OP_OUTPUTCOUNT,
316
322
  OP_OUTPUTSCRIPT: OP_OUTPUTSCRIPT,
317
323
  OP_OUTPUTVALUE: OP_OUTPUTVALUE,
@@ -1755,12 +1761,16 @@ var NeuraiScriptsBundle = (function (exports) {
1755
1761
  *
1756
1762
  * output[0] = XNA payment to seller (value >= N * unitPriceSats)
1757
1763
  * output[1] = asset to buyer (tokenId, amount == N)
1758
- * output[2] = covenant remainder (same scriptPubKey, amount == in - N)
1764
+ * output[2] = covenant remainder (same AuthScript commitment, amount == in - N)
1759
1765
  * output[3+] = optional buyer change (not constrained by the covenant)
1760
1766
  *
1761
- * The remainder UTXO reuses the spent scriptPubKey via
1762
- * `OP_OUTPUTSCRIPT == OP_TXFIELD 0x03`, so the covenant is self-replicating
1763
- * without hardcoding its own hash.
1767
+ * The remainder UTXO reuses the spent covenant's AuthScript v1 commitment
1768
+ * via `OP_OUTPUTAUTHCOMMITMENT(2) == OP_TXFIELD(0x02)` (NIP-023). Comparing
1769
+ * commitments rather than full scriptPubKeys is mandatory here: the
1770
+ * remainder output's asset wrapper (`OP_XNA_ASSET ... OP_DROP`) encodes a
1771
+ * smaller `amountRaw` than the spent UTXO's wrapper, so the full-spk
1772
+ * equality used by earlier drafts was vacuously unsatisfiable on any
1773
+ * asset-wrapped covenant UTXO.
1764
1774
  *
1765
1775
  * The cancel branch uses classical ECDSA (`OP_HASH160 + OP_CHECKSIG`) and
1766
1776
  * commits to a 20-byte PKH. As a consequence this variant only accepts
@@ -1857,11 +1867,14 @@ var NeuraiScriptsBundle = (function (exports) {
1857
1867
  .op(OP_OUTPUTASSETFIELD) // [ N, name_out1 ]
1858
1868
  .pushBytes(tokenIdBytes)
1859
1869
  .op(OP_EQUALVERIFY); // [ N ]
1860
- // 5. Remainder covenant continuity (output 2): same scriptPubKey as spent
1870
+ // 5. Remainder covenant continuity (output 2): same AuthScript commitment
1871
+ // as spent. NIP-023: comparing 32-byte commitments rather than full
1872
+ // scriptPubKeys, because the remainder's asset wrapper carries a
1873
+ // different `amountRaw` than the spent UTXO's wrapper.
1861
1874
  b.pushInt(2)
1862
- .op(OP_OUTPUTSCRIPT) // [ N, spk_out2 ]
1863
- .pushInt(TXFIELD_SCRIPTPUBKEY)
1864
- .op(OP_TXFIELD) // [ N, spk_out2, spent_spk ]
1875
+ .op(OP_OUTPUTAUTHCOMMITMENT) // [ N, auth_out2 ]
1876
+ .pushInt(TXFIELD_AUTHSCRIPT_COMMITMENT)
1877
+ .op(OP_TXFIELD) // [ N, auth_out2, spent_auth ]
1865
1878
  .op(OP_EQUALVERIFY); // [ N ]
1866
1879
  // 6. Remainder output: same tokenId
1867
1880
  b.pushInt(2)
@@ -1966,12 +1979,19 @@ var NeuraiScriptsBundle = (function (exports) {
1966
1979
  // ───────── Cancel branch (PQ via OP_CHECKSIGFROMSTACK) ─────────
1967
1980
  // scriptSig expected: <sigPQ> <pubKeyPQ> OP_1
1968
1981
  // After OP_IF consumes the flag: [ sig, pubKey ]
1982
+ //
1983
+ // The selector MUST be pushed as a raw 1-byte element — consensus
1984
+ // (`interpreter.cpp` OP_TXHASH case) rejects any stack item of
1985
+ // size ≠ 1. Using `pushInt(selector)` would work for 1..127 but emit a
1986
+ // 2-byte CScriptNum for 0x80..0xff (the sign-disambiguation pad), which
1987
+ // makes OP_TXHASH fail with SCRIPT_ERR_TXHASH. See
1988
+ // `memory/project_covenant_v3_findings.md` bug B for details.
1969
1989
  b.op(OP_IF)
1970
1990
  .op(OP_DUP) // [ sig, pubKey, pubKey ]
1971
1991
  .op(OP_SHA256) // [ sig, pubKey, H(pubKey) ]
1972
1992
  .pushBytes(pubKeyCommitment) // [ sig, pubKey, H(pubKey), commitment ]
1973
1993
  .op(OP_EQUALVERIFY) // [ sig, pubKey ]
1974
- .pushInt(txHashSelector) // [ sig, pubKey, selector ]
1994
+ .pushBytes(Uint8Array.of(txHashSelector)) // [ sig, pubKey, selector ] — always 1-byte stack item
1975
1995
  .op(OP_TXHASH) // [ sig, pubKey, txHash ]
1976
1996
  .op(OP_SWAP) // [ sig, txHash, pubKey ]
1977
1997
  .op(OP_CHECKSIGFROMSTACK) // [ 1 | 0 ]
@@ -2005,10 +2025,12 @@ var NeuraiScriptsBundle = (function (exports) {
2005
2025
  .op(OP_OUTPUTASSETFIELD)
2006
2026
  .pushBytes(tokenIdBytes)
2007
2027
  .op(OP_EQUALVERIFY);
2008
- // 5. Remainder continuity (output 2) — same scriptPubKey as spent
2028
+ // 5. Remainder continuity (output 2) — same AuthScript commitment as spent
2029
+ // (NIP-023; see script.ts for why full-spk equality is unsatisfiable on
2030
+ // asset-wrapped covenant UTXOs).
2009
2031
  b.pushInt(2)
2010
- .op(OP_OUTPUTSCRIPT)
2011
- .pushInt(TXFIELD_SCRIPTPUBKEY)
2032
+ .op(OP_OUTPUTAUTHCOMMITMENT)
2033
+ .pushInt(TXFIELD_AUTHSCRIPT_COMMITMENT)
2012
2034
  .op(OP_TXFIELD)
2013
2035
  .op(OP_EQUALVERIFY);
2014
2036
  // 6. Remainder tokenId
@@ -2266,6 +2288,33 @@ var NeuraiScriptsBundle = (function (exports) {
2266
2288
  const data = readPush(c, label);
2267
2289
  return decodeScriptNum(data, label);
2268
2290
  }
2291
+ /**
2292
+ * Read a 1-byte selector as an UNSIGNED 8-bit integer (0..255). Accepts
2293
+ * two on-wire encodings, because old vs new covenant builders differ:
2294
+ * - `OP_1..OP_16` shorthand (single opcode) → values 1..16.
2295
+ * - `0x01 <byte>` raw 1-byte push → any value 1..255.
2296
+ *
2297
+ * Values 0x80..0xff MUST use the raw-push form; the CScriptNum encoding
2298
+ * would need a 0x00 padding byte and become 2 bytes on-stack, which
2299
+ * consensus `OP_TXHASH` rejects. The builder in `script-pq.ts` emits the
2300
+ * raw-push form unconditionally; the parser stays lenient so covenants
2301
+ * built by older tools (using OP_N for small values) still round-trip.
2302
+ */
2303
+ function readPushUint8(c, label) {
2304
+ if (c.pos >= c.bytes.length) {
2305
+ throw new Error(`parse: end of script at ${label}`);
2306
+ }
2307
+ const opcode = c.bytes[c.pos];
2308
+ if (opcode >= OP_1 && opcode <= 0x60) {
2309
+ c.pos += 1;
2310
+ return opcode - OP_1 + 1;
2311
+ }
2312
+ const data = readPush(c, label);
2313
+ if (data.length !== 1) {
2314
+ throw new Error(`parse: ${label} must be a single-byte push, got ${data.length} bytes`);
2315
+ }
2316
+ return data[0];
2317
+ }
2269
2318
 
2270
2319
  /**
2271
2320
  * Parser for the Partial-Fill Sell Order covenant.
@@ -2327,12 +2376,12 @@ var NeuraiScriptsBundle = (function (exports) {
2327
2376
  expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer name)');
2328
2377
  const tokenIdBytes1 = readPush(c, 'tokenId #1');
2329
2378
  expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer name)');
2330
- // ───── Remainder continuity: same scriptPubKey ─────
2379
+ // ───── Remainder continuity: same AuthScript commitment (NIP-023) ─────
2331
2380
  expectByte(c, OP_2, 'OP_2 (remainder idx)');
2332
- expectByte(c, OP_OUTPUTSCRIPT, 'OP_OUTPUTSCRIPT (remainder)');
2333
- expectByte(c, OP_3, 'OP_3 (TXFIELD selector)');
2381
+ expectByte(c, OP_OUTPUTAUTHCOMMITMENT, 'OP_OUTPUTAUTHCOMMITMENT (remainder)');
2382
+ expectByte(c, OP_2, 'OP_2 (TXFIELD selector: AUTHSCRIPT_COMMITMENT)');
2334
2383
  expectByte(c, OP_TXFIELD, 'OP_TXFIELD');
2335
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder spk)');
2384
+ expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder auth)');
2336
2385
  // ───── Remainder tokenId check ─────
2337
2386
  expectByte(c, OP_2, 'OP_2 (remainder idx)');
2338
2387
  expectByte(c, OP_1, 'OP_1 (NAME selector)');
@@ -2399,11 +2448,13 @@ var NeuraiScriptsBundle = (function (exports) {
2399
2448
  throw new Error(`parse-pq: pubKeyCommitment must be 32 bytes, got ${pubKeyCommitment.length}`);
2400
2449
  }
2401
2450
  expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (cancel)');
2402
- const txHashSelectorBig = readPushPositiveInt(c, 'txHashSelector');
2403
- if (txHashSelectorBig < 1n || txHashSelectorBig > 0xffn) {
2404
- throw new Error(`parse-pq: txHashSelector out of range (${txHashSelectorBig})`);
2451
+ // Selector is read as an unsigned byte — consensus OP_TXHASH treats the
2452
+ // on-stack element as uint8, and the builder emits a raw 1-byte push so
2453
+ // selectors 0x80..0xff round-trip correctly (plan v3 bug B).
2454
+ const txHashSelector = readPushUint8(c, 'txHashSelector');
2455
+ if (txHashSelector < 1) {
2456
+ throw new Error(`parse-pq: txHashSelector 0x00 is rejected by OP_TXHASH`);
2405
2457
  }
2406
- const txHashSelector = Number(txHashSelectorBig);
2407
2458
  expectByte(c, OP_TXHASH, 'OP_TXHASH');
2408
2459
  expectByte(c, OP_SWAP, 'OP_SWAP');
2409
2460
  expectByte(c, OP_CHECKSIGFROMSTACK, 'OP_CHECKSIGFROMSTACK');
@@ -2431,11 +2482,12 @@ var NeuraiScriptsBundle = (function (exports) {
2431
2482
  expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer name)');
2432
2483
  const tokenIdBytes1 = readPush(c, 'tokenId #1');
2433
2484
  expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer name)');
2485
+ // Remainder continuity: same AuthScript commitment (NIP-023)
2434
2486
  expectByte(c, OP_2, 'OP_2 (remainder idx)');
2435
- expectByte(c, OP_OUTPUTSCRIPT, 'OP_OUTPUTSCRIPT (remainder)');
2436
- expectByte(c, OP_3, 'OP_3 (TXFIELD selector)');
2487
+ expectByte(c, OP_OUTPUTAUTHCOMMITMENT, 'OP_OUTPUTAUTHCOMMITMENT (remainder)');
2488
+ expectByte(c, OP_2, 'OP_2 (TXFIELD selector: AUTHSCRIPT_COMMITMENT)');
2437
2489
  expectByte(c, OP_TXFIELD, 'OP_TXFIELD');
2438
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder spk)');
2490
+ expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder auth)');
2439
2491
  expectByte(c, OP_2, 'OP_2 (remainder idx)');
2440
2492
  expectByte(c, OP_1, 'OP_1 (NAME selector)');
2441
2493
  expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (remainder name)');