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