@neuraiproject/neurai-scripts 0.9.0 → 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/README.md CHANGED
@@ -124,15 +124,17 @@ stack, arithmetic, bitwise, crypto) plus every DePIN-Test addition
124
124
  `OP_INPUTCOUNT`/`OP_OUTPUTCOUNT`, `OP_OUTPUTASSETFIELD`,
125
125
  `OP_INPUTASSETFIELD`, `OP_REFINPUTCOUNT`/`OP_REFINPUTFIELD`/
126
126
  `OP_REFINPUTASSETFIELD`, `OP_CHAINCONTEXT`, `OP_CAT`, `OP_SPLIT`,
127
- `OP_REVERSEBYTES`, 64-bit `OP_MUL`/`OP_DIV`/`OP_MOD`).
127
+ `OP_REVERSEBYTES`, `OP_OUTPUTAUTHDEST`, `OP_ZKVERIFY`, `OP_INPUTFIELD`,
128
+ 64-bit `OP_MUL`/`OP_DIV`/`OP_MOD`).
128
129
 
129
130
  Also exports selector tables:
130
131
 
131
132
  ```ts
132
- // For OP_TXFIELD and OP_REFINPUTFIELD
133
+ // For OP_TXFIELD, OP_INPUTFIELD and OP_REFINPUTFIELD
133
134
  opcodes.TXFIELD_VALUE // 0x01
134
135
  opcodes.TXFIELD_AUTHSCRIPT_COMMITMENT // 0x02
135
136
  opcodes.TXFIELD_SCRIPTPUBKEY // 0x03
137
+ opcodes.TXFIELD_AUTHDEST // 0x04 (NIP-041)
136
138
 
137
139
  // For OP_OUTPUTASSETFIELD / OP_INPUTASSETFIELD / OP_REFINPUTASSETFIELD
138
140
  opcodes.ASSETFIELD_NAME // 0x01
@@ -142,13 +144,14 @@ opcodes.ASSETFIELD_REISSUABLE // 0x04
142
144
  opcodes.ASSETFIELD_HAS_IPFS // 0x05
143
145
  opcodes.ASSETFIELD_IPFS_HASH // 0x06
144
146
  opcodes.ASSETFIELD_TYPE // 0x07
147
+ opcodes.ASSETFIELD_MESSAGE // 0x08 (NIP-043)
145
148
 
146
149
  // For OP_CHAINCONTEXT
147
150
  opcodes.CHAINCONTEXT_HEIGHT // 0x01
148
151
  opcodes.CHAINCONTEXT_MTP // 0x02
149
152
  opcodes.CHAINCONTEXT_CHAIN_ID // 0x03
150
153
 
151
- // Bitmask selectors for OP_TXHASH (any non-zero combination is valid)
154
+ // NIP-042: push every OP_TXHASH mask as two raw LE bytes (0x001..0x1ff).
152
155
  opcodes.TXHASH_VERSION // 0x01
153
156
  opcodes.TXHASH_LOCKTIME // 0x02
154
157
  opcodes.TXHASH_INPUT_PREVOUTS // 0x04
@@ -157,7 +160,8 @@ opcodes.TXHASH_OUTPUTS // 0x10
157
160
  opcodes.TXHASH_CURRENT_PREVOUT // 0x20
158
161
  opcodes.TXHASH_CURRENT_SEQUENCE // 0x40
159
162
  opcodes.TXHASH_CURRENT_INDEX // 0x80
160
- opcodes.TXHASH_ALL // 0xff
163
+ opcodes.TXHASH_REFINPUTS // 0x100
164
+ opcodes.TXHASH_ALL // 0x1ff
161
165
  ```
162
166
 
163
167
  ### `ScriptBuilder` (`./core/script-builder.ts`)
@@ -227,8 +231,8 @@ version of the address family:
227
231
  `buildStrictWitnessPQ({ signature, pubKey })` and
228
232
  `buildStrictWitnessECDSA({ signature, pubKey })` build the fixed 4-item stacks
229
233
  of the strict families (the PQ key must be the 1313-byte `0x05`-prefixed key,
230
- the ECDSA key must be compressed). Strict v2 / v3 are active on regtest only
231
- today; generic v1 on testnet and regtest.
234
+ the ECDSA key must be compressed). All three families activate on the reset testnet at block 10 and on
235
+ regtest at block 1.
232
236
 
233
237
  Generic AuthScript v1 spend modes, selected by a 1-byte `auth_type` in the witness stack:
234
238
 
@@ -537,7 +541,7 @@ const scriptPubKeyHex = buildPartialFillScriptPQHex({
537
541
  pubKeyCommitment: sha256(alicePQPubKey), // 32 bytes
538
542
  tokenId: 'CAT',
539
543
  unitPriceSats: 100_000_000n,
540
- txHashSelector: DEFAULT_PQ_TXHASH_SELECTOR // 0xff = all eight tx fields
544
+ txHashSelector: DEFAULT_PQ_TXHASH_SELECTOR // 0x1ff = all nine fields
541
545
  });
542
546
 
543
547
  const cancelScriptSigHex = buildCancelScriptSigPQHex(mlDsa44SigWithSighash, alicePQPubKey);
@@ -555,8 +559,8 @@ OP_IF // scriptSig pushed <sig> <pubKey> OP_1
555
559
  OP_DUP OP_SHA256
556
560
  <pubKeyCommitment 32B>
557
561
  OP_EQUALVERIFY
558
- <txHashSelector 1B>
559
- OP_TXHASH // msg = dSHA256(selected_tx_fields)
562
+ <txHashSelector uint16LE, exactly 2 bytes>
563
+ OP_TXHASH // msg = tagged NeuraiTxHash(mask || fields)
560
564
  OP_SWAP
561
565
  OP_CHECKSIGFROMSTACK // verifies sig over SHA256(msg) for pubKey
562
566
  OP_ELSE
@@ -634,6 +638,30 @@ consumable; callers import directly from the package root.
634
638
 
635
639
  ## Version notes
636
640
 
641
+ ### 0.9.2
642
+
643
+ Requires `@neuraiproject/neurai-create-transaction` `^0.9.2` so new
644
+ installations use the reset-testnet activation height and network metadata.
645
+ The asset-wrapper fixtures from the previous testnet remain historical codec
646
+ examples; live transactions must use the asset marker returned by the node for
647
+ the candidate block. NIP-041/043 and NIP-018 opcode constants and selectors
648
+ match the reset node. PQ cancel covenants now push a two-byte NIP-042 mask;
649
+ the default `0x1ff` also commits reference inputs. Historical one-byte masks
650
+ are rejected by the PQ covenant parser because they cannot execute under the
651
+ current `OP_TXHASH` rule.
652
+
653
+ ### 0.9.1
654
+
655
+ TypeScript declarations for CommonJS. `require()` loads `dist/index.cjs`, but
656
+ the package only published ESM declarations, so a CommonJS file compiled with
657
+ `moduleResolution: "node16"` got `TS1471` and no types. Each condition of
658
+ `exports` now has its own declarations (`import` → `dist/index.d.ts`,
659
+ `require` → `dist/index.d.cts`). No runtime or API change. `npm run
660
+ test:types` compiles ESM, CommonJS and browser consumers against the built
661
+ declarations (NodeNext, Node16, Bundler; `skipLibCheck: false`) and `npm run
662
+ test:package` checks the packed tarball in a clean project, with TypeScript
663
+ 4.7 too.
664
+
637
665
  ### 0.9.0
638
666
 
639
667
  Address types of neurai-key 5 / neurai-create-transaction 0.9.0.
@@ -244,20 +244,27 @@ var NeuraiScriptsBundle = (function (exports) {
244
244
  // Native Merkle inclusion verifier. Consumes (leaf, scheme_id, proof,
245
245
  // root) and pushes a boolean. Flag off → bad-opcode.
246
246
  const OP_CHECKMERKLEINCLUSION = 0xc1;
247
+ // NIP-041: (outputIndex -- witnessVersion || 32-byte program).
248
+ const OP_OUTPUTAUTHDEST = 0xc2;
249
+ // NIP-018: verifies an AuthScript proof and returns a boolean.
250
+ const OP_ZKVERIFY = 0xc3;
251
+ // NIP-043: spent-input field introspection, distinct from reference inputs.
252
+ const OP_INPUTFIELD = 0xc4;
247
253
  // ---------- Byte manipulation (DePIN-Test) ----------
248
254
  const OP_CAT = 0x7e;
249
255
  const OP_SPLIT = 0xb7;
250
256
  const OP_REVERSEBYTES = 0xbc;
251
257
  // ---------- Selectors for OP_TXFIELD / OP_REFINPUTFIELD ----------
252
- // Both opcodes share the same selector table (OP_TXFIELD on the spent UTXO,
253
- // OP_REFINPUTFIELD on an output referenced via vrefin). Valid: 0x01..0x03.
258
+ // OP_TXFIELD, OP_INPUTFIELD and OP_REFINPUTFIELD share selectors 0x01..0x03.
259
+ // NIP-041 adds 0x04 for a strict 33-byte AuthScript destination.
254
260
  const TXFIELD_VALUE = 0x01;
255
261
  const TXFIELD_AUTHSCRIPT_COMMITMENT = 0x02;
256
262
  const TXFIELD_SCRIPTPUBKEY = 0x03;
263
+ const TXFIELD_AUTHDEST = 0x04;
257
264
  // ---------- Bitmask selectors for OP_TXHASH ----------
258
- // The selector is a single byte where each bit selects which transaction
259
- // field to include in the double-SHA256. Selector 0x00 is invalid; any
260
- // non-zero combination is valid. 0xff = all eight fields.
265
+ // NIP-042 requires a two-byte little-endian selector, even for low masks.
266
+ // Bits 0..8 select fields for the tagged NeuraiTxHash digest. Valid masks
267
+ // are 0x001..0x1ff; zero and all higher bits are rejected by consensus.
261
268
  const TXHASH_VERSION = 0x01;
262
269
  const TXHASH_LOCKTIME = 0x02;
263
270
  const TXHASH_INPUT_PREVOUTS = 0x04;
@@ -266,12 +273,11 @@ var NeuraiScriptsBundle = (function (exports) {
266
273
  const TXHASH_CURRENT_PREVOUT = 0x20;
267
274
  const TXHASH_CURRENT_SEQUENCE = 0x40;
268
275
  const TXHASH_CURRENT_INDEX = 0x80;
269
- const TXHASH_ALL = 0xff;
276
+ const TXHASH_REFINPUTS = 0x100;
277
+ const TXHASH_ALL = 0x1ff;
270
278
  // ---------- Selectors for OP_OUTPUTASSETFIELD / OP_INPUTASSETFIELD / OP_REFINPUTASSETFIELD ----------
271
- // All three opcodes share the same selector table. Valid range: 0x01..0x07.
272
- // Selector 0x05 is the boolean "has IPFS" flag; 0x06 is the IPFS hash
273
- // payload; 0x07 is the asset operation type. (This matches the asset-op
274
- // encoding in `src/assets/assets.cpp` and the NIP spec §3.1.)
279
+ // All three opcodes share this selector table. 0x08 is gated by NIP-043
280
+ // and extracts the optional transfer message from a strict asset wrapper.
275
281
  const ASSETFIELD_NAME = 0x01;
276
282
  const ASSETFIELD_AMOUNT = 0x02;
277
283
  const ASSETFIELD_UNITS = 0x03;
@@ -279,12 +285,20 @@ var NeuraiScriptsBundle = (function (exports) {
279
285
  const ASSETFIELD_HAS_IPFS = 0x05;
280
286
  const ASSETFIELD_IPFS_HASH = 0x06;
281
287
  const ASSETFIELD_TYPE = 0x07;
288
+ const ASSETFIELD_MESSAGE = 0x08;
289
+ // NIP-031/043 Merkle inclusion scheme identifiers.
290
+ const MERKLE_SCHEME_BITCOIN_NEURAI = 0x01;
291
+ const MERKLE_SCHEME_SHA256_PLAIN = 0x02;
292
+ const MERKLE_SCHEME_KECCAK256_PLAIN = 0x03;
293
+ const MERKLE_SCHEME_BLAKE2B_PLAIN = 0x04;
294
+ const MERKLE_SCHEME_POSEIDON_BN254 = 0x05;
282
295
 
283
296
  var opcodes = /*#__PURE__*/Object.freeze({
284
297
  __proto__: null,
285
298
  ASSETFIELD_AMOUNT: ASSETFIELD_AMOUNT,
286
299
  ASSETFIELD_HAS_IPFS: ASSETFIELD_HAS_IPFS,
287
300
  ASSETFIELD_IPFS_HASH: ASSETFIELD_IPFS_HASH,
301
+ ASSETFIELD_MESSAGE: ASSETFIELD_MESSAGE,
288
302
  ASSETFIELD_NAME: ASSETFIELD_NAME,
289
303
  ASSETFIELD_REISSUABLE: ASSETFIELD_REISSUABLE,
290
304
  ASSETFIELD_TYPE: ASSETFIELD_TYPE,
@@ -292,6 +306,11 @@ var NeuraiScriptsBundle = (function (exports) {
292
306
  CHAINCONTEXT_CHAIN_ID: CHAINCONTEXT_CHAIN_ID,
293
307
  CHAINCONTEXT_HEIGHT: CHAINCONTEXT_HEIGHT,
294
308
  CHAINCONTEXT_MTP: CHAINCONTEXT_MTP,
309
+ MERKLE_SCHEME_BITCOIN_NEURAI: MERKLE_SCHEME_BITCOIN_NEURAI,
310
+ MERKLE_SCHEME_BLAKE2B_PLAIN: MERKLE_SCHEME_BLAKE2B_PLAIN,
311
+ MERKLE_SCHEME_KECCAK256_PLAIN: MERKLE_SCHEME_KECCAK256_PLAIN,
312
+ MERKLE_SCHEME_POSEIDON_BN254: MERKLE_SCHEME_POSEIDON_BN254,
313
+ MERKLE_SCHEME_SHA256_PLAIN: MERKLE_SCHEME_SHA256_PLAIN,
295
314
  OP_0: OP_0,
296
315
  OP_0NOTEQUAL: OP_0NOTEQUAL,
297
316
  OP_1: OP_1,
@@ -357,6 +376,7 @@ var NeuraiScriptsBundle = (function (exports) {
357
376
  OP_IFDUP: OP_IFDUP,
358
377
  OP_INPUTASSETFIELD: OP_INPUTASSETFIELD,
359
378
  OP_INPUTCOUNT: OP_INPUTCOUNT,
379
+ OP_INPUTFIELD: OP_INPUTFIELD,
360
380
  OP_INPUTVALUE: OP_INPUTVALUE,
361
381
  OP_KECCAK256: OP_KECCAK256,
362
382
  OP_LESSTHAN: OP_LESSTHAN,
@@ -378,6 +398,7 @@ var NeuraiScriptsBundle = (function (exports) {
378
398
  OP_NUMNOTEQUAL: OP_NUMNOTEQUAL,
379
399
  OP_OUTPUTASSETFIELD: OP_OUTPUTASSETFIELD,
380
400
  OP_OUTPUTAUTHCOMMITMENT: OP_OUTPUTAUTHCOMMITMENT,
401
+ OP_OUTPUTAUTHDEST: OP_OUTPUTAUTHDEST,
381
402
  OP_OUTPUTCOUNT: OP_OUTPUTCOUNT,
382
403
  OP_OUTPUTSCRIPT: OP_OUTPUTSCRIPT,
383
404
  OP_OUTPUTVALUE: OP_OUTPUTVALUE,
@@ -413,6 +434,8 @@ var NeuraiScriptsBundle = (function (exports) {
413
434
  OP_VERIFY: OP_VERIFY,
414
435
  OP_WITHIN: OP_WITHIN,
415
436
  OP_XNA_ASSET: OP_XNA_ASSET,
437
+ OP_ZKVERIFY: OP_ZKVERIFY,
438
+ TXFIELD_AUTHDEST: TXFIELD_AUTHDEST,
416
439
  TXFIELD_AUTHSCRIPT_COMMITMENT: TXFIELD_AUTHSCRIPT_COMMITMENT,
417
440
  TXFIELD_SCRIPTPUBKEY: TXFIELD_SCRIPTPUBKEY,
418
441
  TXFIELD_VALUE: TXFIELD_VALUE,
@@ -424,6 +447,7 @@ var NeuraiScriptsBundle = (function (exports) {
424
447
  TXHASH_INPUT_SEQUENCES: TXHASH_INPUT_SEQUENCES,
425
448
  TXHASH_LOCKTIME: TXHASH_LOCKTIME,
426
449
  TXHASH_OUTPUTS: TXHASH_OUTPUTS,
450
+ TXHASH_REFINPUTS: TXHASH_REFINPUTS,
427
451
  TXHASH_VERSION: TXHASH_VERSION
428
452
  });
429
453
 
@@ -2099,16 +2123,9 @@ var NeuraiScriptsBundle = (function (exports) {
2099
2123
  return decodeScriptNum(data, label);
2100
2124
  }
2101
2125
  /**
2102
- * Read a 1-byte selector as an UNSIGNED 8-bit integer (0..255). Accepts
2103
- * two on-wire encodings, because old vs new covenant builders differ:
2104
- * - `OP_1..OP_16` shorthand (single opcode) → values 1..16.
2105
- * - `0x01 <byte>` raw 1-byte push → any value 1..255.
2106
- *
2107
- * Values 0x80..0xff MUST use the raw-push form; the CScriptNum encoding
2108
- * would need a 0x00 padding byte and become 2 bytes on-stack, which
2109
- * consensus `OP_TXHASH` rejects. The builder in `script-pq.ts` emits the
2110
- * raw-push form unconditionally; the parser stays lenient so covenants
2111
- * built by older tools (using OP_N for small values) still round-trip.
2126
+ * Read a one-byte unsigned selector. This accepts OP_1..OP_16 shorthand
2127
+ * and a raw one-byte push for scripts that permit either form. NIP-042
2128
+ * TXHASH masks use a separate strict two-byte parser.
2112
2129
  */
2113
2130
  function readPushUint8(c, label) {
2114
2131
  if (c.pos >= c.bytes.length) {
@@ -2427,7 +2444,7 @@ var NeuraiScriptsBundle = (function (exports) {
2427
2444
  * `./script.ts` for the three-branch layout description.
2428
2445
  */
2429
2446
  const ASSET_NAME_MAX = 32;
2430
- const DEFAULT_PQ_TXHASH_SELECTOR = 0xff;
2447
+ const DEFAULT_PQ_TXHASH_SELECTOR = 0x1ff;
2431
2448
  function assertCommitment(commitment) {
2432
2449
  if (!(commitment instanceof Uint8Array) || commitment.length !== 32) {
2433
2450
  throw new Error('pubKeyCommitment must be a 32-byte Uint8Array (SHA256 of pubKey)');
@@ -2456,11 +2473,11 @@ var NeuraiScriptsBundle = (function (exports) {
2456
2473
  }
2457
2474
  }
2458
2475
  function assertSelector(selector) {
2459
- if (!Number.isInteger(selector) || selector < 0 || selector > 0xff) {
2460
- throw new Error('txHashSelector must be a single byte (0x00..0xff)');
2476
+ if (!Number.isInteger(selector) || selector < 0 || selector > 0x1ff) {
2477
+ throw new Error('txHashSelector must be in the NIP-042 range (0x001..0x1ff)');
2461
2478
  }
2462
2479
  if (selector === 0) {
2463
- throw new Error('txHashSelector 0x00 is rejected by OP_TXHASH');
2480
+ throw new Error('txHashSelector 0x000 is rejected by OP_TXHASH');
2464
2481
  }
2465
2482
  }
2466
2483
  /**
@@ -2480,16 +2497,14 @@ var NeuraiScriptsBundle = (function (exports) {
2480
2497
  // scriptSig: <sigPQ> <pubKeyPQ> <1>
2481
2498
  // After OP_IF consumes the flag: [ sig, pubKey ]
2482
2499
  //
2483
- // The selector MUST be pushed as a raw 1-byte element — consensus rejects
2484
- // any stack item of size ≠ 1. Using `pushInt(selector)` would work for
2485
- // 1..127 but emit a 2-byte CScriptNum for 0x80..0xff (sign-disambiguation
2486
- // pad), which makes OP_TXHASH fail with SCRIPT_ERR_TXHASH.
2500
+ // NIP-042 consumes exactly two raw little-endian bytes. CScriptNum and
2501
+ // OP_N encodings are invalid even when they represent the same mask.
2487
2502
  b.op(OP_IF)
2488
2503
  .op(OP_DUP) // [ sig, pubKey, pubKey ]
2489
2504
  .op(OP_SHA256) // [ sig, pubKey, H(pubKey) ]
2490
2505
  .pushBytes(pubKeyCommitment) // [ sig, pubKey, H(pubKey), commitment ]
2491
2506
  .op(OP_EQUALVERIFY) // [ sig, pubKey ]
2492
- .pushBytes(Uint8Array.of(txHashSelector)) // [ sig, pubKey, selector ]
2507
+ .pushBytes(Uint8Array.of(txHashSelector & 0xff, txHashSelector >> 8)) // [ sig, pubKey, selector ]
2493
2508
  .op(OP_TXHASH) // [ sig, pubKey, txHash ]
2494
2509
  .op(OP_SWAP) // [ sig, txHash, pubKey ]
2495
2510
  .op(OP_CHECKSIGFROMSTACK) // [ 1 | 0 ]
@@ -3001,9 +3016,13 @@ var NeuraiScriptsBundle = (function (exports) {
3001
3016
  throw new Error(`parse-pq: pubKeyCommitment must be 32 bytes, got ${pubKeyCommitment.length}`);
3002
3017
  }
3003
3018
  expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (cancel)');
3004
- const txHashSelector = readPushUint8(c, 'txHashSelector');
3005
- if (txHashSelector < 1) {
3006
- throw new Error(`parse-pq: txHashSelector 0x00 is rejected by OP_TXHASH`);
3019
+ const selectorBytes = readPush(c, 'txHashSelector');
3020
+ if (selectorBytes.length !== 2) {
3021
+ throw new Error(`parse-pq: txHashSelector must be a two-byte LE push, got ${selectorBytes.length} bytes`);
3022
+ }
3023
+ const txHashSelector = selectorBytes[0] | (selectorBytes[1] << 8);
3024
+ if (txHashSelector === 0 || txHashSelector > 0x1ff) {
3025
+ throw new Error(`parse-pq: txHashSelector 0x${txHashSelector.toString(16)} is rejected by OP_TXHASH`);
3007
3026
  }
3008
3027
  expectByte(c, OP_TXHASH, 'OP_TXHASH');
3009
3028
  expectByte(c, OP_SWAP, 'OP_SWAP');