@neuraiproject/neurai-scripts 0.9.1 → 0.9.2

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
@@ -241,20 +241,27 @@ const CHAINCONTEXT_CHAIN_ID = 0x03;
241
241
  // Native Merkle inclusion verifier. Consumes (leaf, scheme_id, proof,
242
242
  // root) and pushes a boolean. Flag off → bad-opcode.
243
243
  const OP_CHECKMERKLEINCLUSION = 0xc1;
244
+ // NIP-041: (outputIndex -- witnessVersion || 32-byte program).
245
+ const OP_OUTPUTAUTHDEST = 0xc2;
246
+ // NIP-018: verifies an AuthScript proof and returns a boolean.
247
+ const OP_ZKVERIFY = 0xc3;
248
+ // NIP-043: spent-input field introspection, distinct from reference inputs.
249
+ const OP_INPUTFIELD = 0xc4;
244
250
  // ---------- Byte manipulation (DePIN-Test) ----------
245
251
  const OP_CAT = 0x7e;
246
252
  const OP_SPLIT = 0xb7;
247
253
  const OP_REVERSEBYTES = 0xbc;
248
254
  // ---------- Selectors for OP_TXFIELD / OP_REFINPUTFIELD ----------
249
- // Both opcodes share the same selector table (OP_TXFIELD on the spent UTXO,
250
- // OP_REFINPUTFIELD on an output referenced via vrefin). Valid: 0x01..0x03.
255
+ // OP_TXFIELD, OP_INPUTFIELD and OP_REFINPUTFIELD share selectors 0x01..0x03.
256
+ // NIP-041 adds 0x04 for a strict 33-byte AuthScript destination.
251
257
  const TXFIELD_VALUE = 0x01;
252
258
  const TXFIELD_AUTHSCRIPT_COMMITMENT = 0x02;
253
259
  const TXFIELD_SCRIPTPUBKEY = 0x03;
260
+ const TXFIELD_AUTHDEST = 0x04;
254
261
  // ---------- Bitmask selectors for OP_TXHASH ----------
255
- // The selector is a single byte where each bit selects which transaction
256
- // field to include in the double-SHA256. Selector 0x00 is invalid; any
257
- // non-zero combination is valid. 0xff = all eight fields.
262
+ // NIP-042 requires a two-byte little-endian selector, even for low masks.
263
+ // Bits 0..8 select fields for the tagged NeuraiTxHash digest. Valid masks
264
+ // are 0x001..0x1ff; zero and all higher bits are rejected by consensus.
258
265
  const TXHASH_VERSION = 0x01;
259
266
  const TXHASH_LOCKTIME = 0x02;
260
267
  const TXHASH_INPUT_PREVOUTS = 0x04;
@@ -263,12 +270,11 @@ const TXHASH_OUTPUTS = 0x10;
263
270
  const TXHASH_CURRENT_PREVOUT = 0x20;
264
271
  const TXHASH_CURRENT_SEQUENCE = 0x40;
265
272
  const TXHASH_CURRENT_INDEX = 0x80;
266
- const TXHASH_ALL = 0xff;
273
+ const TXHASH_REFINPUTS = 0x100;
274
+ const TXHASH_ALL = 0x1ff;
267
275
  // ---------- Selectors for OP_OUTPUTASSETFIELD / OP_INPUTASSETFIELD / OP_REFINPUTASSETFIELD ----------
268
- // All three opcodes share the same selector table. Valid range: 0x01..0x07.
269
- // Selector 0x05 is the boolean "has IPFS" flag; 0x06 is the IPFS hash
270
- // payload; 0x07 is the asset operation type. (This matches the asset-op
271
- // encoding in `src/assets/assets.cpp` and the NIP spec §3.1.)
276
+ // All three opcodes share this selector table. 0x08 is gated by NIP-043
277
+ // and extracts the optional transfer message from a strict asset wrapper.
272
278
  const ASSETFIELD_NAME = 0x01;
273
279
  const ASSETFIELD_AMOUNT = 0x02;
274
280
  const ASSETFIELD_UNITS = 0x03;
@@ -276,12 +282,20 @@ const ASSETFIELD_REISSUABLE = 0x04;
276
282
  const ASSETFIELD_HAS_IPFS = 0x05;
277
283
  const ASSETFIELD_IPFS_HASH = 0x06;
278
284
  const ASSETFIELD_TYPE = 0x07;
285
+ const ASSETFIELD_MESSAGE = 0x08;
286
+ // NIP-031/043 Merkle inclusion scheme identifiers.
287
+ const MERKLE_SCHEME_BITCOIN_NEURAI = 0x01;
288
+ const MERKLE_SCHEME_SHA256_PLAIN = 0x02;
289
+ const MERKLE_SCHEME_KECCAK256_PLAIN = 0x03;
290
+ const MERKLE_SCHEME_BLAKE2B_PLAIN = 0x04;
291
+ const MERKLE_SCHEME_POSEIDON_BN254 = 0x05;
279
292
 
280
293
  var opcodes = /*#__PURE__*/Object.freeze({
281
294
  __proto__: null,
282
295
  ASSETFIELD_AMOUNT: ASSETFIELD_AMOUNT,
283
296
  ASSETFIELD_HAS_IPFS: ASSETFIELD_HAS_IPFS,
284
297
  ASSETFIELD_IPFS_HASH: ASSETFIELD_IPFS_HASH,
298
+ ASSETFIELD_MESSAGE: ASSETFIELD_MESSAGE,
285
299
  ASSETFIELD_NAME: ASSETFIELD_NAME,
286
300
  ASSETFIELD_REISSUABLE: ASSETFIELD_REISSUABLE,
287
301
  ASSETFIELD_TYPE: ASSETFIELD_TYPE,
@@ -289,6 +303,11 @@ var opcodes = /*#__PURE__*/Object.freeze({
289
303
  CHAINCONTEXT_CHAIN_ID: CHAINCONTEXT_CHAIN_ID,
290
304
  CHAINCONTEXT_HEIGHT: CHAINCONTEXT_HEIGHT,
291
305
  CHAINCONTEXT_MTP: CHAINCONTEXT_MTP,
306
+ MERKLE_SCHEME_BITCOIN_NEURAI: MERKLE_SCHEME_BITCOIN_NEURAI,
307
+ MERKLE_SCHEME_BLAKE2B_PLAIN: MERKLE_SCHEME_BLAKE2B_PLAIN,
308
+ MERKLE_SCHEME_KECCAK256_PLAIN: MERKLE_SCHEME_KECCAK256_PLAIN,
309
+ MERKLE_SCHEME_POSEIDON_BN254: MERKLE_SCHEME_POSEIDON_BN254,
310
+ MERKLE_SCHEME_SHA256_PLAIN: MERKLE_SCHEME_SHA256_PLAIN,
292
311
  OP_0: OP_0,
293
312
  OP_0NOTEQUAL: OP_0NOTEQUAL,
294
313
  OP_1: OP_1,
@@ -354,6 +373,7 @@ var opcodes = /*#__PURE__*/Object.freeze({
354
373
  OP_IFDUP: OP_IFDUP,
355
374
  OP_INPUTASSETFIELD: OP_INPUTASSETFIELD,
356
375
  OP_INPUTCOUNT: OP_INPUTCOUNT,
376
+ OP_INPUTFIELD: OP_INPUTFIELD,
357
377
  OP_INPUTVALUE: OP_INPUTVALUE,
358
378
  OP_KECCAK256: OP_KECCAK256,
359
379
  OP_LESSTHAN: OP_LESSTHAN,
@@ -375,6 +395,7 @@ var opcodes = /*#__PURE__*/Object.freeze({
375
395
  OP_NUMNOTEQUAL: OP_NUMNOTEQUAL,
376
396
  OP_OUTPUTASSETFIELD: OP_OUTPUTASSETFIELD,
377
397
  OP_OUTPUTAUTHCOMMITMENT: OP_OUTPUTAUTHCOMMITMENT,
398
+ OP_OUTPUTAUTHDEST: OP_OUTPUTAUTHDEST,
378
399
  OP_OUTPUTCOUNT: OP_OUTPUTCOUNT,
379
400
  OP_OUTPUTSCRIPT: OP_OUTPUTSCRIPT,
380
401
  OP_OUTPUTVALUE: OP_OUTPUTVALUE,
@@ -410,6 +431,8 @@ var opcodes = /*#__PURE__*/Object.freeze({
410
431
  OP_VERIFY: OP_VERIFY,
411
432
  OP_WITHIN: OP_WITHIN,
412
433
  OP_XNA_ASSET: OP_XNA_ASSET,
434
+ OP_ZKVERIFY: OP_ZKVERIFY,
435
+ TXFIELD_AUTHDEST: TXFIELD_AUTHDEST,
413
436
  TXFIELD_AUTHSCRIPT_COMMITMENT: TXFIELD_AUTHSCRIPT_COMMITMENT,
414
437
  TXFIELD_SCRIPTPUBKEY: TXFIELD_SCRIPTPUBKEY,
415
438
  TXFIELD_VALUE: TXFIELD_VALUE,
@@ -421,6 +444,7 @@ var opcodes = /*#__PURE__*/Object.freeze({
421
444
  TXHASH_INPUT_SEQUENCES: TXHASH_INPUT_SEQUENCES,
422
445
  TXHASH_LOCKTIME: TXHASH_LOCKTIME,
423
446
  TXHASH_OUTPUTS: TXHASH_OUTPUTS,
447
+ TXHASH_REFINPUTS: TXHASH_REFINPUTS,
424
448
  TXHASH_VERSION: TXHASH_VERSION
425
449
  });
426
450
 
@@ -2096,16 +2120,9 @@ function readPushPositiveInt(c, label) {
2096
2120
  return decodeScriptNum(data, label);
2097
2121
  }
2098
2122
  /**
2099
- * Read a 1-byte selector as an UNSIGNED 8-bit integer (0..255). Accepts
2100
- * two on-wire encodings, because old vs new covenant builders differ:
2101
- * - `OP_1..OP_16` shorthand (single opcode) → values 1..16.
2102
- * - `0x01 <byte>` raw 1-byte push → any value 1..255.
2103
- *
2104
- * Values 0x80..0xff MUST use the raw-push form; the CScriptNum encoding
2105
- * would need a 0x00 padding byte and become 2 bytes on-stack, which
2106
- * consensus `OP_TXHASH` rejects. The builder in `script-pq.ts` emits the
2107
- * raw-push form unconditionally; the parser stays lenient so covenants
2108
- * built by older tools (using OP_N for small values) still round-trip.
2123
+ * Read a one-byte unsigned selector. This accepts OP_1..OP_16 shorthand
2124
+ * and a raw one-byte push for scripts that permit either form. NIP-042
2125
+ * TXHASH masks use a separate strict two-byte parser.
2109
2126
  */
2110
2127
  function readPushUint8(c, label) {
2111
2128
  if (c.pos >= c.bytes.length) {
@@ -2424,7 +2441,7 @@ function buildPartialFillScriptHex(params) {
2424
2441
  * `./script.ts` for the three-branch layout description.
2425
2442
  */
2426
2443
  const ASSET_NAME_MAX = 32;
2427
- const DEFAULT_PQ_TXHASH_SELECTOR = 0xff;
2444
+ const DEFAULT_PQ_TXHASH_SELECTOR = 0x1ff;
2428
2445
  function assertCommitment(commitment) {
2429
2446
  if (!(commitment instanceof Uint8Array) || commitment.length !== 32) {
2430
2447
  throw new Error('pubKeyCommitment must be a 32-byte Uint8Array (SHA256 of pubKey)');
@@ -2453,11 +2470,11 @@ function assertPrice(priceSats) {
2453
2470
  }
2454
2471
  }
2455
2472
  function assertSelector(selector) {
2456
- if (!Number.isInteger(selector) || selector < 0 || selector > 0xff) {
2457
- throw new Error('txHashSelector must be a single byte (0x00..0xff)');
2473
+ if (!Number.isInteger(selector) || selector < 0 || selector > 0x1ff) {
2474
+ throw new Error('txHashSelector must be in the NIP-042 range (0x001..0x1ff)');
2458
2475
  }
2459
2476
  if (selector === 0) {
2460
- throw new Error('txHashSelector 0x00 is rejected by OP_TXHASH');
2477
+ throw new Error('txHashSelector 0x000 is rejected by OP_TXHASH');
2461
2478
  }
2462
2479
  }
2463
2480
  /**
@@ -2477,16 +2494,14 @@ function buildPartialFillScriptPQ(params) {
2477
2494
  // scriptSig: <sigPQ> <pubKeyPQ> <1>
2478
2495
  // After OP_IF consumes the flag: [ sig, pubKey ]
2479
2496
  //
2480
- // The selector MUST be pushed as a raw 1-byte element — consensus rejects
2481
- // any stack item of size ≠ 1. Using `pushInt(selector)` would work for
2482
- // 1..127 but emit a 2-byte CScriptNum for 0x80..0xff (sign-disambiguation
2483
- // pad), which makes OP_TXHASH fail with SCRIPT_ERR_TXHASH.
2497
+ // NIP-042 consumes exactly two raw little-endian bytes. CScriptNum and
2498
+ // OP_N encodings are invalid even when they represent the same mask.
2484
2499
  b.op(OP_IF)
2485
2500
  .op(OP_DUP) // [ sig, pubKey, pubKey ]
2486
2501
  .op(OP_SHA256) // [ sig, pubKey, H(pubKey) ]
2487
2502
  .pushBytes(pubKeyCommitment) // [ sig, pubKey, H(pubKey), commitment ]
2488
2503
  .op(OP_EQUALVERIFY) // [ sig, pubKey ]
2489
- .pushBytes(Uint8Array.of(txHashSelector)) // [ sig, pubKey, selector ]
2504
+ .pushBytes(Uint8Array.of(txHashSelector & 0xff, txHashSelector >> 8)) // [ sig, pubKey, selector ]
2490
2505
  .op(OP_TXHASH) // [ sig, pubKey, txHash ]
2491
2506
  .op(OP_SWAP) // [ sig, txHash, pubKey ]
2492
2507
  .op(OP_CHECKSIGFROMSTACK) // [ 1 | 0 ]
@@ -2998,9 +3013,13 @@ function parsePartialFillScriptPQ(script, network = 'xna-test') {
2998
3013
  throw new Error(`parse-pq: pubKeyCommitment must be 32 bytes, got ${pubKeyCommitment.length}`);
2999
3014
  }
3000
3015
  expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (cancel)');
3001
- const txHashSelector = readPushUint8(c, 'txHashSelector');
3002
- if (txHashSelector < 1) {
3003
- throw new Error(`parse-pq: txHashSelector 0x00 is rejected by OP_TXHASH`);
3016
+ const selectorBytes = readPush(c, 'txHashSelector');
3017
+ if (selectorBytes.length !== 2) {
3018
+ throw new Error(`parse-pq: txHashSelector must be a two-byte LE push, got ${selectorBytes.length} bytes`);
3019
+ }
3020
+ const txHashSelector = selectorBytes[0] | (selectorBytes[1] << 8);
3021
+ if (txHashSelector === 0 || txHashSelector > 0x1ff) {
3022
+ throw new Error(`parse-pq: txHashSelector 0x${txHashSelector.toString(16)} is rejected by OP_TXHASH`);
3004
3023
  }
3005
3024
  expectByte(c, OP_TXHASH, 'OP_TXHASH');
3006
3025
  expectByte(c, OP_SWAP, 'OP_SWAP');