@neuraiproject/neurai-scripts 0.2.0 → 0.3.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.
@@ -1966,12 +1966,19 @@ var NeuraiScriptsBundle = (function (exports) {
1966
1966
  // ───────── Cancel branch (PQ via OP_CHECKSIGFROMSTACK) ─────────
1967
1967
  // scriptSig expected: <sigPQ> <pubKeyPQ> OP_1
1968
1968
  // After OP_IF consumes the flag: [ sig, pubKey ]
1969
+ //
1970
+ // The selector MUST be pushed as a raw 1-byte element — consensus
1971
+ // (`interpreter.cpp` OP_TXHASH case) rejects any stack item of
1972
+ // size ≠ 1. Using `pushInt(selector)` would work for 1..127 but emit a
1973
+ // 2-byte CScriptNum for 0x80..0xff (the sign-disambiguation pad), which
1974
+ // makes OP_TXHASH fail with SCRIPT_ERR_TXHASH. See
1975
+ // `memory/project_covenant_v3_findings.md` bug B for details.
1969
1976
  b.op(OP_IF)
1970
1977
  .op(OP_DUP) // [ sig, pubKey, pubKey ]
1971
1978
  .op(OP_SHA256) // [ sig, pubKey, H(pubKey) ]
1972
1979
  .pushBytes(pubKeyCommitment) // [ sig, pubKey, H(pubKey), commitment ]
1973
1980
  .op(OP_EQUALVERIFY) // [ sig, pubKey ]
1974
- .pushInt(txHashSelector) // [ sig, pubKey, selector ]
1981
+ .pushBytes(Uint8Array.of(txHashSelector)) // [ sig, pubKey, selector ] — always 1-byte stack item
1975
1982
  .op(OP_TXHASH) // [ sig, pubKey, txHash ]
1976
1983
  .op(OP_SWAP) // [ sig, txHash, pubKey ]
1977
1984
  .op(OP_CHECKSIGFROMSTACK) // [ 1 | 0 ]
@@ -2266,6 +2273,33 @@ var NeuraiScriptsBundle = (function (exports) {
2266
2273
  const data = readPush(c, label);
2267
2274
  return decodeScriptNum(data, label);
2268
2275
  }
2276
+ /**
2277
+ * Read a 1-byte selector as an UNSIGNED 8-bit integer (0..255). Accepts
2278
+ * two on-wire encodings, because old vs new covenant builders differ:
2279
+ * - `OP_1..OP_16` shorthand (single opcode) → values 1..16.
2280
+ * - `0x01 <byte>` raw 1-byte push → any value 1..255.
2281
+ *
2282
+ * Values 0x80..0xff MUST use the raw-push form; the CScriptNum encoding
2283
+ * would need a 0x00 padding byte and become 2 bytes on-stack, which
2284
+ * consensus `OP_TXHASH` rejects. The builder in `script-pq.ts` emits the
2285
+ * raw-push form unconditionally; the parser stays lenient so covenants
2286
+ * built by older tools (using OP_N for small values) still round-trip.
2287
+ */
2288
+ function readPushUint8(c, label) {
2289
+ if (c.pos >= c.bytes.length) {
2290
+ throw new Error(`parse: end of script at ${label}`);
2291
+ }
2292
+ const opcode = c.bytes[c.pos];
2293
+ if (opcode >= OP_1 && opcode <= 0x60) {
2294
+ c.pos += 1;
2295
+ return opcode - OP_1 + 1;
2296
+ }
2297
+ const data = readPush(c, label);
2298
+ if (data.length !== 1) {
2299
+ throw new Error(`parse: ${label} must be a single-byte push, got ${data.length} bytes`);
2300
+ }
2301
+ return data[0];
2302
+ }
2269
2303
 
2270
2304
  /**
2271
2305
  * Parser for the Partial-Fill Sell Order covenant.
@@ -2399,11 +2433,13 @@ var NeuraiScriptsBundle = (function (exports) {
2399
2433
  throw new Error(`parse-pq: pubKeyCommitment must be 32 bytes, got ${pubKeyCommitment.length}`);
2400
2434
  }
2401
2435
  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})`);
2436
+ // Selector is read as an unsigned byte — consensus OP_TXHASH treats the
2437
+ // on-stack element as uint8, and the builder emits a raw 1-byte push so
2438
+ // selectors 0x80..0xff round-trip correctly (plan v3 bug B).
2439
+ const txHashSelector = readPushUint8(c, 'txHashSelector');
2440
+ if (txHashSelector < 1) {
2441
+ throw new Error(`parse-pq: txHashSelector 0x00 is rejected by OP_TXHASH`);
2405
2442
  }
2406
- const txHashSelector = Number(txHashSelectorBig);
2407
2443
  expectByte(c, OP_TXHASH, 'OP_TXHASH');
2408
2444
  expectByte(c, OP_SWAP, 'OP_SWAP');
2409
2445
  expectByte(c, OP_CHECKSIGFROMSTACK, 'OP_CHECKSIGFROMSTACK');