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