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