@neuraiproject/neurai-scripts 0.9.1 → 0.10.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.
@@ -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) {
@@ -2195,7 +2212,224 @@ var NeuraiScriptsBundle = (function (exports) {
2195
2212
  }
2196
2213
 
2197
2214
  /**
2198
- * Partial-Fill Sell Order covenant script (three-branch).
2215
+ * Buyer fill branches shared by both Partial-Fill Sell Order variants
2216
+ * (ECDSA cancel in `./script.ts`, ML-DSA-44 cancel in `./script-pq.ts`).
2217
+ * The variants only differ in their cancel branch; everything after the
2218
+ * outer `OP_ELSE` is emitted and parsed here.
2219
+ *
2220
+ * Input-0 binding. The fill branches read the order's asset amount with
2221
+ * `<0> OP_INPUTASSETFIELD` and constrain outputs 0..2 by fixed index. Input
2222
+ * indices are absolute and Script cannot ask for the index of the input
2223
+ * being evaluated, so without a binding a buyer could:
2224
+ * - put a foreign asset UTXO at input 0 and the order at input 1: the
2225
+ * order then prices the fill by the foreign amount and the rest of the
2226
+ * order leaves through an unconstrained output;
2227
+ * - spend several identical orders together, every one of them satisfied
2228
+ * by the same payment output.
2229
+ * The fill block therefore opens with
2230
+ *
2231
+ * <0x80 0x00> OP_TXHASH <INPUT_ZERO_TXHASH> OP_EQUALVERIFY
2232
+ *
2233
+ * NIP-042 mask 0x0080 commits only to the index of the input being
2234
+ * evaluated, so the digest equals the constant exactly when this covenant
2235
+ * is spent as input 0. At most one order per transaction can take a fill
2236
+ * branch; the seller's cancel branch is not bound.
2237
+ *
2238
+ * Layout (after the variant's `OP_IF <cancel> OP_ELSE`):
2239
+ *
2240
+ * <0x80 0x00> OP_TXHASH <INPUT_ZERO_TXHASH> OP_EQUALVERIFY
2241
+ * OP_IF ← full fill, unlock <1> <0>
2242
+ * [expiration gate]
2243
+ * <0> <2> OP_INPUTASSETFIELD (N = order amount)
2244
+ * DUP <price> MUL <0> OUTPUTVALUE SWAP GE VERIFY (payment value)
2245
+ * <0> OUTPUTSCRIPT <paymentSpk> EQUALVERIFY (payment destination)
2246
+ * DUP <1> <2> OUTPUTASSETFIELD EQUALVERIFY (buyer amount == N)
2247
+ * <1> <1> OUTPUTASSETFIELD <tokenId> EQUALVERIFY (buyer asset name)
2248
+ * DROP <1>
2249
+ * OP_ELSE ← partial fill, unlock <N> <0> <0>
2250
+ * [expiration gate]
2251
+ * payment value, payment destination, buyer amount, buyer name (as above)
2252
+ * <2> OUTPUTAUTHCOMMITMENT <2> TXFIELD EQUALVERIFY (continuation, NIP-023)
2253
+ * <2> <1> OUTPUTASSETFIELD <tokenId> EQUALVERIFY (continuation name)
2254
+ * <2> <2> OUTPUTASSETFIELD OVER <0> <2> INPUTASSETFIELD SWAP SUB EQUALVERIFY
2255
+ * DROP <1>
2256
+ * OP_ENDIF
2257
+ */
2258
+ /** NIP-042 mask (two bytes, little-endian) selecting only the current input index. */
2259
+ const INPUT_INDEX_TXHASH_SELECTOR = Uint8Array.of(TXHASH_CURRENT_INDEX, 0x00);
2260
+ /**
2261
+ * `OP_TXHASH(0x0080)` evaluated at input 0:
2262
+ * `SHA256(tag || tag || 80 00 || 00 00 00 00)` with `tag = SHA256("NeuraiTxHash")`.
2263
+ */
2264
+ const INPUT_ZERO_TXHASH = hexToBytes('419632cabaf8bb70b96a8c92c4601a9c2ba11e96841daebc9d98f4835eeb0e36');
2265
+ function appendPaymentAndBuyerChecks(b, p) {
2266
+ // output[0].value >= N * unitPriceSats [ N ]
2267
+ b.op(OP_DUP)
2268
+ .pushInt(p.unitPriceSats)
2269
+ .op(OP_MUL)
2270
+ .pushInt(0)
2271
+ .op(OP_OUTPUTVALUE)
2272
+ .op(OP_SWAP)
2273
+ .op(OP_GREATERTHANOREQUAL)
2274
+ .op(OP_VERIFY);
2275
+ // output[0].scriptPubKey == paymentScriptPubKey [ N ]
2276
+ b.pushInt(0)
2277
+ .op(OP_OUTPUTSCRIPT)
2278
+ .pushBytes(p.paymentScriptPubKey)
2279
+ .op(OP_EQUALVERIFY);
2280
+ // output[1].asset.amount == N [ N ]
2281
+ b.op(OP_DUP)
2282
+ .pushInt(1)
2283
+ .pushInt(ASSETFIELD_AMOUNT)
2284
+ .op(OP_OUTPUTASSETFIELD)
2285
+ .op(OP_EQUALVERIFY);
2286
+ // output[1].asset.name == tokenId [ N ]
2287
+ b.pushInt(1)
2288
+ .pushInt(ASSETFIELD_NAME)
2289
+ .op(OP_OUTPUTASSETFIELD)
2290
+ .pushBytes(p.tokenIdBytes)
2291
+ .op(OP_EQUALVERIFY);
2292
+ }
2293
+ /** Emit the input-0 binding and both fill branches, ending with the inner `OP_ENDIF`. */
2294
+ function appendFillBranches(b, p) {
2295
+ b.pushBytes(INPUT_INDEX_TXHASH_SELECTOR)
2296
+ .op(OP_TXHASH)
2297
+ .pushBytes(INPUT_ZERO_TXHASH)
2298
+ .op(OP_EQUALVERIFY);
2299
+ // ──────── Full fill: N is the whole order, no continuation ────────
2300
+ b.op(OP_IF);
2301
+ appendExpirationGate(b, p.expiration);
2302
+ b.pushInt(0)
2303
+ .pushInt(ASSETFIELD_AMOUNT)
2304
+ .op(OP_INPUTASSETFIELD); // [ N ]
2305
+ appendPaymentAndBuyerChecks(b, p);
2306
+ b.op(OP_DROP).pushInt(1);
2307
+ // ──────── Partial fill: buyer pushes N, remainder continues at output 2 ────────
2308
+ b.op(OP_ELSE);
2309
+ appendExpirationGate(b, p.expiration);
2310
+ appendPaymentAndBuyerChecks(b, p);
2311
+ // Continuation keeps the spent order's AuthScript commitment (NIP-023).
2312
+ b.pushInt(2)
2313
+ .op(OP_OUTPUTAUTHCOMMITMENT)
2314
+ .pushInt(TXFIELD_AUTHSCRIPT_COMMITMENT)
2315
+ .op(OP_TXFIELD)
2316
+ .op(OP_EQUALVERIFY);
2317
+ b.pushInt(2)
2318
+ .pushInt(ASSETFIELD_NAME)
2319
+ .op(OP_OUTPUTASSETFIELD)
2320
+ .pushBytes(p.tokenIdBytes)
2321
+ .op(OP_EQUALVERIFY);
2322
+ // output[2].asset.amount == orderAmount - N
2323
+ b.pushInt(2)
2324
+ .pushInt(ASSETFIELD_AMOUNT)
2325
+ .op(OP_OUTPUTASSETFIELD) // [ N, rem ]
2326
+ .op(OP_OVER) // [ N, rem, N ]
2327
+ .pushInt(0)
2328
+ .pushInt(ASSETFIELD_AMOUNT)
2329
+ .op(OP_INPUTASSETFIELD) // [ N, rem, N, in ]
2330
+ .op(OP_SWAP) // [ N, rem, in, N ]
2331
+ .op(OP_SUB) // [ N, rem, in - N ]
2332
+ .op(OP_EQUALVERIFY);
2333
+ b.op(OP_DROP).pushInt(1);
2334
+ b.op(OP_ENDIF);
2335
+ }
2336
+ function readPaymentAndBuyerChecks(c, label) {
2337
+ expectByte(c, OP_DUP, `OP_DUP (price, ${label})`);
2338
+ const unitPriceSats = readPushPositiveInt(c, `unitPriceSats (${label})`);
2339
+ expectByte(c, OP_MUL, `OP_MUL (${label})`);
2340
+ expectByte(c, OP_0, `OP_0 (payment idx, ${label})`);
2341
+ expectByte(c, OP_OUTPUTVALUE, `OP_OUTPUTVALUE (${label})`);
2342
+ expectByte(c, OP_SWAP, `OP_SWAP (${label})`);
2343
+ expectByte(c, OP_GREATERTHANOREQUAL, `OP_GREATERTHANOREQUAL (${label})`);
2344
+ expectByte(c, OP_VERIFY, `OP_VERIFY (payment, ${label})`);
2345
+ expectByte(c, OP_0, `OP_0 (spk idx, ${label})`);
2346
+ expectByte(c, OP_OUTPUTSCRIPT, `OP_OUTPUTSCRIPT (${label})`);
2347
+ const paymentScriptPubKey = readPush(c, `paymentScriptPubKey (${label})`);
2348
+ expectByte(c, OP_EQUALVERIFY, `OP_EQUALVERIFY (payment spk, ${label})`);
2349
+ expectByte(c, OP_DUP, `OP_DUP (buyer amount, ${label})`);
2350
+ expectByte(c, OP_1, `OP_1 (buyer idx, ${label})`);
2351
+ expectByte(c, OP_2, `OP_2 (AMOUNT sel, ${label})`);
2352
+ expectByte(c, OP_OUTPUTASSETFIELD, `OP_OUTPUTASSETFIELD (buyer amount, ${label})`);
2353
+ expectByte(c, OP_EQUALVERIFY, `OP_EQUALVERIFY (buyer amount, ${label})`);
2354
+ expectByte(c, OP_1, `OP_1 (buyer idx, ${label})`);
2355
+ expectByte(c, OP_1, `OP_1 (NAME sel, ${label})`);
2356
+ expectByte(c, OP_OUTPUTASSETFIELD, `OP_OUTPUTASSETFIELD (buyer name, ${label})`);
2357
+ const tokenId = readPush(c, `tokenId (${label})`);
2358
+ expectByte(c, OP_EQUALVERIFY, `OP_EQUALVERIFY (buyer name, ${label})`);
2359
+ return { unitPriceSats, paymentScriptPubKey, tokenId };
2360
+ }
2361
+ /**
2362
+ * Parse what `appendFillBranches` emits. Both branches must agree on price,
2363
+ * payment destination, tokenId and expiration; any deviation throws.
2364
+ */
2365
+ function readFillBranches(c) {
2366
+ const selector = readPush(c, 'input-0 binding selector');
2367
+ if (!bytesEqual(selector, INPUT_INDEX_TXHASH_SELECTOR)) {
2368
+ throw new Error('parse: fill branches must open with the input-0 binding <0x8000> OP_TXHASH (covenants without it ' +
2369
+ 'can be drained through a foreign input 0 and are not supported)');
2370
+ }
2371
+ expectByte(c, OP_TXHASH, 'OP_TXHASH (input-0 binding)');
2372
+ const digest = readPush(c, 'input-0 binding digest');
2373
+ if (!bytesEqual(digest, INPUT_ZERO_TXHASH)) {
2374
+ throw new Error('parse: input-0 binding digest is not OP_TXHASH(0x0080) at input 0');
2375
+ }
2376
+ expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (input-0 binding)');
2377
+ // ═════ Full fill ═════
2378
+ expectByte(c, OP_IF, 'OP_IF (full fill)');
2379
+ const expirationFull = readOptionalExpirationGate(c, OP_0, 'full');
2380
+ expectByte(c, OP_0, 'OP_0 (input idx, full)');
2381
+ expectByte(c, OP_2, 'OP_2 (AMOUNT sel, full)');
2382
+ expectByte(c, OP_INPUTASSETFIELD, 'OP_INPUTASSETFIELD (full)');
2383
+ const full = readPaymentAndBuyerChecks(c, 'full');
2384
+ expectByte(c, OP_DROP, 'OP_DROP (full)');
2385
+ expectByte(c, OP_1, 'OP_1 (true, full)');
2386
+ // ═════ Partial fill ═════
2387
+ expectByte(c, OP_ELSE, 'OP_ELSE (partial fill)');
2388
+ const expirationPartial = readOptionalExpirationGate(c, OP_DUP, 'partial');
2389
+ const partial = readPaymentAndBuyerChecks(c, 'partial');
2390
+ expectByte(c, OP_2, 'OP_2 (remainder idx)');
2391
+ expectByte(c, OP_OUTPUTAUTHCOMMITMENT, 'OP_OUTPUTAUTHCOMMITMENT (remainder)');
2392
+ expectByte(c, OP_2, 'OP_2 (TXFIELD sel: AUTHSCRIPT_COMMITMENT)');
2393
+ expectByte(c, OP_TXFIELD, 'OP_TXFIELD');
2394
+ expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder auth)');
2395
+ expectByte(c, OP_2, 'OP_2 (remainder idx)');
2396
+ expectByte(c, OP_1, 'OP_1 (NAME sel)');
2397
+ expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (remainder name)');
2398
+ const remainderTokenId = readPush(c, 'tokenId (remainder)');
2399
+ expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder name)');
2400
+ expectByte(c, OP_2, 'OP_2 (remainder idx)');
2401
+ expectByte(c, OP_2, 'OP_2 (AMOUNT sel)');
2402
+ expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (remainder amount)');
2403
+ expectByte(c, OP_OVER, 'OP_OVER');
2404
+ expectByte(c, OP_0, 'OP_0 (input idx)');
2405
+ expectByte(c, OP_2, 'OP_2 (AMOUNT sel)');
2406
+ expectByte(c, OP_INPUTASSETFIELD, 'OP_INPUTASSETFIELD');
2407
+ expectByte(c, OP_SWAP, 'OP_SWAP');
2408
+ expectByte(c, OP_SUB, 'OP_SUB');
2409
+ expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder amount)');
2410
+ expectByte(c, OP_DROP, 'OP_DROP (partial)');
2411
+ expectByte(c, OP_1, 'OP_1 (true, partial)');
2412
+ expectByte(c, OP_ENDIF, 'OP_ENDIF (fill branches)');
2413
+ if (!bytesEqual(full.tokenId, partial.tokenId) || !bytesEqual(partial.tokenId, remainderTokenId)) {
2414
+ throw new Error('parse: tokenId differs between the fill checks');
2415
+ }
2416
+ if (full.unitPriceSats !== partial.unitPriceSats) {
2417
+ throw new Error('parse: unitPriceSats differs between full-fill and partial-fill branches');
2418
+ }
2419
+ if (!bytesEqual(full.paymentScriptPubKey, partial.paymentScriptPubKey)) {
2420
+ throw new Error('parse: payment scriptPubKey differs between full-fill and partial-fill branches');
2421
+ }
2422
+ assertSameExpiration(expirationFull, expirationPartial, 'full-fill and partial-fill branches');
2423
+ return {
2424
+ paymentScriptPubKey: full.paymentScriptPubKey,
2425
+ tokenId: new TextDecoder('utf-8', { fatal: true }).decode(full.tokenId),
2426
+ unitPriceSats: full.unitPriceSats,
2427
+ expiration: expirationFull
2428
+ };
2429
+ }
2430
+
2431
+ /**
2432
+ * Partial-Fill Sell Order covenant script (three-branch), ECDSA cancel.
2199
2433
  *
2200
2434
  * The covenant has three branches selected by the top of the unlock stack:
2201
2435
  *
@@ -2215,11 +2449,8 @@ var NeuraiScriptsBundle = (function (exports) {
2215
2449
  * amount == inputAmount - N)
2216
2450
  * output[3+] = optional buyer change (not constrained by the covenant)
2217
2451
  *
2218
- * Continuity — the remainder UTXO reuses the spent covenant's AuthScript v1
2219
- * commitment via `OP_OUTPUTAUTHCOMMITMENT(2) == OP_TXFIELD(0x02)` (NIP-023).
2220
- * Comparing commitments rather than full scriptPubKeys is mandatory because
2221
- * the remainder's asset wrapper carries a different `amountRaw` than the
2222
- * spent UTXO's wrapper.
2452
+ * The order must be input 0 for either fill branch to succeed: see the
2453
+ * input-0 binding in `./fill-branches.ts`.
2223
2454
  *
2224
2455
  * Unlock stack shapes (scriptSig pushes, top → bottom):
2225
2456
  *
@@ -2227,10 +2458,12 @@ var NeuraiScriptsBundle = (function (exports) {
2227
2458
  * Full fill: <1> <0> (full-flag=1, cancel-flag=0)
2228
2459
  * Partial fill: <N> <0> <0> (N, full-flag=0, cancel-flag=0)
2229
2460
  *
2230
- * The cancel branch uses classical ECDSA (`OP_HASH160 + OP_CHECKSIG`) and
2231
- * commits to a 20-byte PKH — this variant only accepts legacy P2PKH
2232
- * addresses as the seller destination. For AuthScript bech32m destinations
2233
- * and post-quantum signing, use `buildPartialFillScriptPQ`.
2461
+ * The cancel branch uses classical ECDSA (`OP_HASH160 + OP_CHECKSIG`)
2462
+ * against a 20-byte key hash. The payment destination is independent of
2463
+ * that key: a legacy P2PKH seller passes `sellerAddress` (both at once),
2464
+ * any other seller with a secp256k1 key — typically a strict ECDSA v3
2465
+ * `nq1r…` wallet — passes `paymentAddress` and `sellerPubKeyHash`. For
2466
+ * post-quantum cancellation use `buildPartialFillScriptPQ`.
2234
2467
  */
2235
2468
  const ASSET_NAME_MAX$1 = 32;
2236
2469
  function decodeSellerAddress(sellerAddress) {
@@ -2239,8 +2472,8 @@ var NeuraiScriptsBundle = (function (exports) {
2239
2472
  }
2240
2473
  const decoded = decodeAddress(sellerAddress);
2241
2474
  if (decoded.type !== 'p2pkh') {
2242
- throw new Error(`sellerAddress must be a legacy P2PKH address (the legacy partial-fill covenant uses OP_HASH160 + OP_CHECKSIG); ` +
2243
- `got ${decoded.type}. Use buildPartialFillScriptPQ for AuthScript destinations.`);
2475
+ throw new Error(`sellerAddress must be a legacy P2PKH address; got ${decoded.type}. A seller with another address ` +
2476
+ `type passes paymentAddress and sellerPubKeyHash, or uses buildPartialFillScriptPQ for a PQ key.`);
2244
2477
  }
2245
2478
  const pkh = Uint8Array.from(decoded.hash);
2246
2479
  if (pkh.length !== 20) {
@@ -2248,6 +2481,27 @@ var NeuraiScriptsBundle = (function (exports) {
2248
2481
  }
2249
2482
  return pkh;
2250
2483
  }
2484
+ /** Cancel key hash and payment scriptPubKey from either form of seller params. */
2485
+ function resolveSeller(params) {
2486
+ const { sellerAddress, paymentAddress, sellerPubKeyHash } = params;
2487
+ if (sellerAddress !== undefined) {
2488
+ if (paymentAddress !== undefined || sellerPubKeyHash !== undefined) {
2489
+ throw new Error('pass either sellerAddress, or paymentAddress with sellerPubKeyHash, not both');
2490
+ }
2491
+ const pkh = decodeSellerAddress(sellerAddress);
2492
+ return { sellerPubKeyHash: pkh, paymentScriptPubKey: encodeP2PKHScriptPubKey(pkh) };
2493
+ }
2494
+ if (paymentAddress === undefined || sellerPubKeyHash === undefined) {
2495
+ throw new Error('sellerAddress, or paymentAddress with sellerPubKeyHash, is required');
2496
+ }
2497
+ if (!(sellerPubKeyHash instanceof Uint8Array) || sellerPubKeyHash.length !== 20) {
2498
+ throw new Error('sellerPubKeyHash must be a 20-byte Uint8Array (HASH160 of the compressed public key)');
2499
+ }
2500
+ return {
2501
+ sellerPubKeyHash: Uint8Array.from(sellerPubKeyHash),
2502
+ paymentScriptPubKey: encodeSellerScriptPubKey(paymentAddress).bytes
2503
+ };
2504
+ }
2251
2505
  function assertTokenId$1(tokenId) {
2252
2506
  if (typeof tokenId !== 'string' || tokenId.length === 0) {
2253
2507
  throw new Error('tokenId is required');
@@ -2275,13 +2529,11 @@ var NeuraiScriptsBundle = (function (exports) {
2275
2529
  * Returns raw bytes; wrap with `bytesToHex` for wire format.
2276
2530
  */
2277
2531
  function buildPartialFillScript(params) {
2278
- const { sellerAddress, tokenId, unitPriceSats } = params;
2279
- const sellerPubKeyHash = decodeSellerAddress(sellerAddress);
2532
+ const { tokenId, unitPriceSats } = params;
2533
+ const { sellerPubKeyHash, paymentScriptPubKey } = resolveSeller(params);
2280
2534
  assertTokenId$1(tokenId);
2281
2535
  assertPrice$1(unitPriceSats);
2282
2536
  const expiration = assertExpiration(params.expiration);
2283
- const sellerScriptPubKey = encodeP2PKHScriptPubKey(sellerPubKeyHash);
2284
- const tokenIdBytes = new TextEncoder().encode(tokenId);
2285
2537
  const b = new ScriptBuilder();
2286
2538
  // ════════ Cancel branch (outer IF) ════════
2287
2539
  // scriptSig: <sig> <pubkey> <1>
@@ -2290,107 +2542,13 @@ var NeuraiScriptsBundle = (function (exports) {
2290
2542
  .pushBytes(sellerPubKeyHash)
2291
2543
  .op(OP_EQUALVERIFY, OP_CHECKSIG)
2292
2544
  .op(OP_ELSE);
2293
- // ════════ Fill branches (outer ELSE: inner IF/ELSE) ════════
2294
- b.op(OP_IF);
2295
- // ──────── Full-fill branch (inner IF) ────────
2296
- // scriptSig: <1> <0> ( full-flag=1, cancel-flag=0 )
2297
- // Stack entering: [ ]
2298
- //
2299
- // N is derived from the spent UTXO's asset amount, so the buyer does not
2300
- // push N. The entire covenant is drained to vout[1]; no vout[2] is
2301
- // constrained and none is required (consensus forbids zero-amount asset
2302
- // transfers, so the partial-fill continuity check is simply skipped).
2303
- appendExpirationGate(b, expiration);
2304
- // 1. N = inputAmount
2305
- b.pushInt(0)
2306
- .pushInt(ASSETFIELD_AMOUNT)
2307
- .op(OP_INPUTASSETFIELD); // [ N ]
2308
- // 2. output[0].value >= N * unitPriceSats
2309
- b.op(OP_DUP) // [ N, N ]
2310
- .pushInt(unitPriceSats) // [ N, N, price ]
2311
- .op(OP_MUL) // [ N, N*price ]
2312
- .pushInt(0) // [ N, N*price, 0 ]
2313
- .op(OP_OUTPUTVALUE) // [ N, N*price, value ]
2314
- .op(OP_SWAP) // [ N, value, N*price ]
2315
- .op(OP_GREATERTHANOREQUAL)
2316
- .op(OP_VERIFY); // [ N ]
2317
- // 3. output[0].scriptPubKey == sellerScriptPubKey
2318
- b.pushInt(0)
2319
- .op(OP_OUTPUTSCRIPT) // [ N, spk_out0 ]
2320
- .pushBytes(sellerScriptPubKey)
2321
- .op(OP_EQUALVERIFY); // [ N ]
2322
- // 4. output[1].asset.amount == N
2323
- b.op(OP_DUP) // [ N, N ]
2324
- .pushInt(1)
2325
- .pushInt(ASSETFIELD_AMOUNT)
2326
- .op(OP_OUTPUTASSETFIELD) // [ N, N, amount_out1 ]
2327
- .op(OP_EQUALVERIFY); // [ N ]
2328
- // 5. output[1].asset.name == tokenId
2329
- b.pushInt(1)
2330
- .pushInt(ASSETFIELD_NAME)
2331
- .op(OP_OUTPUTASSETFIELD) // [ N, name_out1 ]
2332
- .pushBytes(tokenIdBytes)
2333
- .op(OP_EQUALVERIFY); // [ N ]
2334
- // 6. Drop N, leave TRUE on the stack.
2335
- b.op(OP_DROP).pushInt(1);
2336
- // ──────── Partial-fill branch (inner ELSE) ────────
2337
- // scriptSig: <N> <0> <0> ( N, full-flag=0, cancel-flag=0 )
2338
- // Stack entering: [ N ]
2339
- b.op(OP_ELSE);
2340
- appendExpirationGate(b, expiration);
2341
- // 1. Payment value (output 0) >= N * unitPriceSats
2342
- b.op(OP_DUP) // [ N, N ]
2343
- .pushInt(unitPriceSats) // [ N, N, price ]
2344
- .op(OP_MUL) // [ N, N*price ]
2345
- .pushInt(0) // [ N, N*price, 0 ]
2346
- .op(OP_OUTPUTVALUE) // [ N, N*price, value ]
2347
- .op(OP_SWAP) // [ N, value, N*price ]
2348
- .op(OP_GREATERTHANOREQUAL)
2349
- .op(OP_VERIFY); // [ N ]
2350
- // 2. Payment scriptPubKey (output 0)
2351
- b.pushInt(0)
2352
- .op(OP_OUTPUTSCRIPT) // [ N, spk_out0 ]
2353
- .pushBytes(sellerScriptPubKey)
2354
- .op(OP_EQUALVERIFY); // [ N ]
2355
- // 3. Asset to buyer (output 1) amount == N
2356
- b.op(OP_DUP) // [ N, N ]
2357
- .pushInt(1)
2358
- .pushInt(ASSETFIELD_AMOUNT)
2359
- .op(OP_OUTPUTASSETFIELD)
2360
- .op(OP_EQUALVERIFY); // [ N ]
2361
- // 4. Asset to buyer (output 1) name == tokenId
2362
- b.pushInt(1)
2363
- .pushInt(ASSETFIELD_NAME)
2364
- .op(OP_OUTPUTASSETFIELD)
2365
- .pushBytes(tokenIdBytes)
2366
- .op(OP_EQUALVERIFY); // [ N ]
2367
- // 5. Continuation (output 2): same AuthScript commitment as spent UTXO
2368
- // (NIP-023 — see file-level comment).
2369
- b.pushInt(2)
2370
- .op(OP_OUTPUTAUTHCOMMITMENT)
2371
- .pushInt(TXFIELD_AUTHSCRIPT_COMMITMENT)
2372
- .op(OP_TXFIELD)
2373
- .op(OP_EQUALVERIFY); // [ N ]
2374
- // 6. Continuation name == tokenId
2375
- b.pushInt(2)
2376
- .pushInt(ASSETFIELD_NAME)
2377
- .op(OP_OUTPUTASSETFIELD)
2378
- .pushBytes(tokenIdBytes)
2379
- .op(OP_EQUALVERIFY); // [ N ]
2380
- // 7. Continuation amount == inputAmount - N
2381
- b.pushInt(2)
2382
- .pushInt(ASSETFIELD_AMOUNT)
2383
- .op(OP_OUTPUTASSETFIELD) // [ N, rem_amount ]
2384
- .op(OP_OVER) // [ N, rem_amount, N ]
2385
- .pushInt(0) // [ N, rem_amount, N, 0 ]
2386
- .pushInt(ASSETFIELD_AMOUNT)
2387
- .op(OP_INPUTASSETFIELD) // [ N, rem_amount, N, in_amount ]
2388
- .op(OP_SWAP) // [ N, rem_amount, in_amount, N ]
2389
- .op(OP_SUB) // [ N, rem_amount, in_amount - N ]
2390
- .op(OP_EQUALVERIFY); // [ N ]
2391
- // 8. Drop N, leave TRUE on the stack.
2392
- b.op(OP_DROP).pushInt(1);
2393
- b.op(OP_ENDIF); // closes full/partial inner IF
2545
+ // ════════ Fill branches (outer ELSE) ════════
2546
+ appendFillBranches(b, {
2547
+ paymentScriptPubKey,
2548
+ tokenIdBytes: new TextEncoder().encode(tokenId),
2549
+ unitPriceSats,
2550
+ expiration
2551
+ });
2394
2552
  b.op(OP_ENDIF); // closes outer cancel/fill IF
2395
2553
  return b.build();
2396
2554
  }
@@ -2420,14 +2578,14 @@ var NeuraiScriptsBundle = (function (exports) {
2420
2578
  *
2421
2579
  * The message that gets signed is **SHA256(OP_TXHASH(selector))** — CSFS
2422
2580
  * single-SHA256s its message argument before verification, and OP_TXHASH
2423
- * produces its own 32-byte hash, so the seller computes:
2424
- * `sign(pqSeckey, SHA256(doubleSHA256(selected_tx_fields)))`
2581
+ * produces its own 32-byte NIP-042 tagged hash, so the seller computes:
2582
+ * `sign(pqSeckey, SHA256(TaggedHash("NeuraiTxHash", mask || selected_tx_fields)))`
2425
2583
  *
2426
- * Fill branches are identical in structure to the legacy variant — see
2427
- * `./script.ts` for the three-branch layout description.
2584
+ * The fill branches, including the input-0 binding, are shared with the
2585
+ * ECDSA variant — see `./fill-branches.ts`.
2428
2586
  */
2429
2587
  const ASSET_NAME_MAX = 32;
2430
- const DEFAULT_PQ_TXHASH_SELECTOR = 0xff;
2588
+ const DEFAULT_PQ_TXHASH_SELECTOR = 0x1ff;
2431
2589
  function assertCommitment(commitment) {
2432
2590
  if (!(commitment instanceof Uint8Array) || commitment.length !== 32) {
2433
2591
  throw new Error('pubKeyCommitment must be a 32-byte Uint8Array (SHA256 of pubKey)');
@@ -2456,11 +2614,11 @@ var NeuraiScriptsBundle = (function (exports) {
2456
2614
  }
2457
2615
  }
2458
2616
  function assertSelector(selector) {
2459
- if (!Number.isInteger(selector) || selector < 0 || selector > 0xff) {
2460
- throw new Error('txHashSelector must be a single byte (0x00..0xff)');
2617
+ if (!Number.isInteger(selector) || selector < 0 || selector > 0x1ff) {
2618
+ throw new Error('txHashSelector must be in the NIP-042 range (0x001..0x1ff)');
2461
2619
  }
2462
2620
  if (selector === 0) {
2463
- throw new Error('txHashSelector 0x00 is rejected by OP_TXHASH');
2621
+ throw new Error('txHashSelector 0x000 is rejected by OP_TXHASH');
2464
2622
  }
2465
2623
  }
2466
2624
  /**
@@ -2480,113 +2638,25 @@ var NeuraiScriptsBundle = (function (exports) {
2480
2638
  // scriptSig: <sigPQ> <pubKeyPQ> <1>
2481
2639
  // After OP_IF consumes the flag: [ sig, pubKey ]
2482
2640
  //
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.
2641
+ // NIP-042 consumes exactly two raw little-endian bytes. CScriptNum and
2642
+ // OP_N encodings are invalid even when they represent the same mask.
2487
2643
  b.op(OP_IF)
2488
2644
  .op(OP_DUP) // [ sig, pubKey, pubKey ]
2489
2645
  .op(OP_SHA256) // [ sig, pubKey, H(pubKey) ]
2490
2646
  .pushBytes(pubKeyCommitment) // [ sig, pubKey, H(pubKey), commitment ]
2491
2647
  .op(OP_EQUALVERIFY) // [ sig, pubKey ]
2492
- .pushBytes(Uint8Array.of(txHashSelector)) // [ sig, pubKey, selector ]
2648
+ .pushBytes(Uint8Array.of(txHashSelector & 0xff, txHashSelector >> 8)) // [ sig, pubKey, selector ]
2493
2649
  .op(OP_TXHASH) // [ sig, pubKey, txHash ]
2494
2650
  .op(OP_SWAP) // [ sig, txHash, pubKey ]
2495
2651
  .op(OP_CHECKSIGFROMSTACK) // [ 1 | 0 ]
2496
2652
  .op(OP_ELSE);
2497
- // ════════ Fill branches (inner IF: full / ELSE: partial) ════════
2498
- b.op(OP_IF);
2499
- // ──────── Full-fill branch ────────
2500
- // scriptSig: <1> <0>
2501
- // Stack entering: [ ]
2502
- // 1. N = inputAmount
2503
- appendExpirationGate(b, expiration);
2504
- b.pushInt(0)
2505
- .pushInt(ASSETFIELD_AMOUNT)
2506
- .op(OP_INPUTASSETFIELD);
2507
- // 2. Payment value (output 0) >= N * unitPriceSats
2508
- b.op(OP_DUP)
2509
- .pushInt(unitPriceSats)
2510
- .op(OP_MUL)
2511
- .pushInt(0)
2512
- .op(OP_OUTPUTVALUE)
2513
- .op(OP_SWAP)
2514
- .op(OP_GREATERTHANOREQUAL)
2515
- .op(OP_VERIFY);
2516
- // 3. Payment scriptPubKey (output 0)
2517
- b.pushInt(0)
2518
- .op(OP_OUTPUTSCRIPT)
2519
- .pushBytes(payment.bytes)
2520
- .op(OP_EQUALVERIFY);
2521
- // 4. Asset to buyer (output 1) amount == N
2522
- b.op(OP_DUP)
2523
- .pushInt(1)
2524
- .pushInt(ASSETFIELD_AMOUNT)
2525
- .op(OP_OUTPUTASSETFIELD)
2526
- .op(OP_EQUALVERIFY);
2527
- // 5. Asset to buyer (output 1) name == tokenId
2528
- b.pushInt(1)
2529
- .pushInt(ASSETFIELD_NAME)
2530
- .op(OP_OUTPUTASSETFIELD)
2531
- .pushBytes(tokenIdBytes)
2532
- .op(OP_EQUALVERIFY);
2533
- b.op(OP_DROP).pushInt(1);
2534
- // ──────── Partial-fill branch ────────
2535
- // scriptSig: <N> <0> <0>
2536
- // Stack entering: [ N ]
2537
- b.op(OP_ELSE);
2538
- appendExpirationGate(b, expiration);
2539
- // 1. Payment value (output 0) >= N * unitPriceSats
2540
- b.op(OP_DUP)
2541
- .pushInt(unitPriceSats)
2542
- .op(OP_MUL)
2543
- .pushInt(0)
2544
- .op(OP_OUTPUTVALUE)
2545
- .op(OP_SWAP)
2546
- .op(OP_GREATERTHANOREQUAL)
2547
- .op(OP_VERIFY);
2548
- // 2. Payment scriptPubKey (output 0)
2549
- b.pushInt(0)
2550
- .op(OP_OUTPUTSCRIPT)
2551
- .pushBytes(payment.bytes)
2552
- .op(OP_EQUALVERIFY);
2553
- // 3. Asset to buyer (output 1) amount == N
2554
- b.op(OP_DUP)
2555
- .pushInt(1)
2556
- .pushInt(ASSETFIELD_AMOUNT)
2557
- .op(OP_OUTPUTASSETFIELD)
2558
- .op(OP_EQUALVERIFY);
2559
- // 4. Asset to buyer (output 1) name == tokenId
2560
- b.pushInt(1)
2561
- .pushInt(ASSETFIELD_NAME)
2562
- .op(OP_OUTPUTASSETFIELD)
2563
- .pushBytes(tokenIdBytes)
2564
- .op(OP_EQUALVERIFY);
2565
- // 5. Continuation (output 2): same AuthScript commitment as spent (NIP-023)
2566
- b.pushInt(2)
2567
- .op(OP_OUTPUTAUTHCOMMITMENT)
2568
- .pushInt(TXFIELD_AUTHSCRIPT_COMMITMENT)
2569
- .op(OP_TXFIELD)
2570
- .op(OP_EQUALVERIFY);
2571
- // 6. Continuation name == tokenId
2572
- b.pushInt(2)
2573
- .pushInt(ASSETFIELD_NAME)
2574
- .op(OP_OUTPUTASSETFIELD)
2575
- .pushBytes(tokenIdBytes)
2576
- .op(OP_EQUALVERIFY);
2577
- // 7. Continuation amount == inputAmount - N
2578
- b.pushInt(2)
2579
- .pushInt(ASSETFIELD_AMOUNT)
2580
- .op(OP_OUTPUTASSETFIELD)
2581
- .op(OP_OVER)
2582
- .pushInt(0)
2583
- .pushInt(ASSETFIELD_AMOUNT)
2584
- .op(OP_INPUTASSETFIELD)
2585
- .op(OP_SWAP)
2586
- .op(OP_SUB)
2587
- .op(OP_EQUALVERIFY);
2588
- b.op(OP_DROP).pushInt(1);
2589
- b.op(OP_ENDIF); // full / partial
2653
+ // ════════ Fill branches (outer ELSE) ════════
2654
+ appendFillBranches(b, {
2655
+ paymentScriptPubKey: payment.bytes,
2656
+ tokenIdBytes,
2657
+ unitPriceSats,
2658
+ expiration
2659
+ });
2590
2660
  b.op(OP_ENDIF); // cancel / fill
2591
2661
  return b.build();
2592
2662
  }
@@ -2809,17 +2879,15 @@ var NeuraiScriptsBundle = (function (exports) {
2809
2879
  }
2810
2880
 
2811
2881
  /**
2812
- * Parser for the Partial-Fill Sell Order covenant (three-branch).
2813
- *
2814
- * Extracts `(sellerPubKeyHash, unitPriceSats, tokenId)` from a scriptPubKey
2815
- * that was produced by `buildPartialFillScript`. Walks the exact byte
2816
- * layout emitted by the builder and fails on any deviation — this is
2817
- * deliberate, so a downstream indexer can unambiguously classify a UTXO as
2818
- * "partial-fill order" or "unknown script" with no false positives.
2819
- *
2820
- * The full-fill and partial-fill branches share `(sellerScriptPubKey,
2821
- * unitPriceSats, tokenId)`. The parser reads both branches and verifies
2822
- * consistency; inconsistency throws.
2882
+ * Parser for the Partial-Fill Sell Order covenant (three-branch, ECDSA cancel).
2883
+ *
2884
+ * Extracts `(sellerPubKeyHash, paymentScriptPubKey, unitPriceSats, tokenId)`
2885
+ * from a scriptPubKey that was produced by `buildPartialFillScript`. Walks
2886
+ * the exact byte layout emitted by the builder and fails on any deviation —
2887
+ * this is deliberate, so a downstream indexer can unambiguously classify a
2888
+ * UTXO as "partial-fill order" or "unknown script" with no false positives.
2889
+ * Covenants built before the input-0 binding (neurai-scripts < 0.10.0) are
2890
+ * rejected: their fill branches can be drained through a foreign input 0.
2823
2891
  */
2824
2892
  /**
2825
2893
  * Parse a covenant scriptPubKey and extract its parameters. Throws with a
@@ -2839,129 +2907,17 @@ var NeuraiScriptsBundle = (function (exports) {
2839
2907
  expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (cancel)');
2840
2908
  expectByte(c, OP_CHECKSIG, 'OP_CHECKSIG (cancel)');
2841
2909
  expectByte(c, OP_ELSE, 'OP_ELSE (outer → fill)');
2842
- // ═════ Inner IF — Full-fill branch ═════
2843
- expectByte(c, OP_IF, 'OP_IF (inner full-fill)');
2844
- const expirationFull = readOptionalExpirationGate(c, OP_0, 'full');
2845
- // N = inputAmount
2846
- expectByte(c, OP_0, 'OP_0 (input idx, full)');
2847
- expectByte(c, OP_2, 'OP_2 (AMOUNT sel, full)');
2848
- expectByte(c, OP_INPUTASSETFIELD, 'OP_INPUTASSETFIELD (full)');
2849
- // payment value check
2850
- expectByte(c, OP_DUP, 'OP_DUP (price, full)');
2851
- const unitPriceSatsFull = readPushPositiveInt(c, 'unitPriceSats (full)');
2852
- expectByte(c, OP_MUL, 'OP_MUL (full)');
2853
- expectByte(c, OP_0, 'OP_0 (payment idx, full)');
2854
- expectByte(c, OP_OUTPUTVALUE, 'OP_OUTPUTVALUE (full)');
2855
- expectByte(c, OP_SWAP, 'OP_SWAP (full)');
2856
- expectByte(c, OP_GREATERTHANOREQUAL, 'OP_GREATERTHANOREQUAL (full)');
2857
- expectByte(c, OP_VERIFY, 'OP_VERIFY (payment full)');
2858
- // payment spk
2859
- expectByte(c, OP_0, 'OP_0 (spk idx, full)');
2860
- expectByte(c, OP_OUTPUTSCRIPT, 'OP_OUTPUTSCRIPT (payment full)');
2861
- const sellerSpkFull = readPush(c, 'sellerScriptPubKey (full)');
2862
- const expectedSpk = new Uint8Array([
2863
- OP_DUP, OP_HASH160, 0x14, ...sellerPubKeyHash, OP_EQUALVERIFY, OP_CHECKSIG
2864
- ]);
2865
- if (!bytesEqual(sellerSpkFull, expectedSpk)) {
2866
- throw new Error('parse: full-fill seller scriptPubKey does not match the cancel PKH');
2867
- }
2868
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (payment spk full)');
2869
- // buyer amount == N (full)
2870
- expectByte(c, OP_DUP, 'OP_DUP (buyer amount, full)');
2871
- expectByte(c, OP_1, 'OP_1 (buyer idx, full)');
2872
- expectByte(c, OP_2, 'OP_2 (AMOUNT sel, full)');
2873
- expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer amount, full)');
2874
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer amount, full)');
2875
- // buyer name == tokenId (full)
2876
- expectByte(c, OP_1, 'OP_1 (buyer idx, full)');
2877
- expectByte(c, OP_1, 'OP_1 (NAME sel, full)');
2878
- expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer name, full)');
2879
- const tokenIdFull = readPush(c, 'tokenId (full)');
2880
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer name, full)');
2881
- // tail (full)
2882
- expectByte(c, OP_DROP, 'OP_DROP (full)');
2883
- expectByte(c, OP_1, 'OP_1 (true, full)');
2884
- expectByte(c, OP_ELSE, 'OP_ELSE (inner → partial fill)');
2885
- // ═════ Inner ELSE — Partial-fill branch ═════
2886
- const expirationPartial = readOptionalExpirationGate(c, OP_DUP, 'partial');
2887
- // Payment value
2888
- expectByte(c, OP_DUP, 'OP_DUP (price, partial)');
2889
- const unitPriceSatsPartial = readPushPositiveInt(c, 'unitPriceSats (partial)');
2890
- expectByte(c, OP_MUL, 'OP_MUL (partial)');
2891
- expectByte(c, OP_0, 'OP_0 (pay idx, partial)');
2892
- expectByte(c, OP_OUTPUTVALUE, 'OP_OUTPUTVALUE (partial)');
2893
- expectByte(c, OP_SWAP, 'OP_SWAP (partial)');
2894
- expectByte(c, OP_GREATERTHANOREQUAL, 'OP_GREATERTHANOREQUAL (partial)');
2895
- expectByte(c, OP_VERIFY, 'OP_VERIFY (payment partial)');
2896
- // Payment spk
2897
- expectByte(c, OP_0, 'OP_0 (spk idx, partial)');
2898
- expectByte(c, OP_OUTPUTSCRIPT, 'OP_OUTPUTSCRIPT (payment partial)');
2899
- const sellerSpkPartial = readPush(c, 'sellerScriptPubKey (partial)');
2900
- if (!bytesEqual(sellerSpkPartial, expectedSpk)) {
2901
- throw new Error('parse: partial-fill seller scriptPubKey does not match the cancel PKH');
2902
- }
2903
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (payment spk partial)');
2904
- // Buyer amount
2905
- expectByte(c, OP_DUP, 'OP_DUP (buyer amount, partial)');
2906
- expectByte(c, OP_1, 'OP_1 (buyer idx, partial)');
2907
- expectByte(c, OP_2, 'OP_2 (AMOUNT sel, partial)');
2908
- expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer amount, partial)');
2909
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer amount, partial)');
2910
- // Buyer name
2911
- expectByte(c, OP_1, 'OP_1 (buyer idx, partial)');
2912
- expectByte(c, OP_1, 'OP_1 (NAME sel, partial)');
2913
- expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer name, partial)');
2914
- const tokenIdPartial1 = readPush(c, 'tokenId partial #1');
2915
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer name, partial)');
2916
- // Continuation commitment
2917
- expectByte(c, OP_2, 'OP_2 (remainder idx)');
2918
- expectByte(c, OP_OUTPUTAUTHCOMMITMENT, 'OP_OUTPUTAUTHCOMMITMENT (remainder)');
2919
- expectByte(c, OP_2, 'OP_2 (TXFIELD sel: AUTHSCRIPT_COMMITMENT)');
2920
- expectByte(c, OP_TXFIELD, 'OP_TXFIELD');
2921
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder auth)');
2922
- // Continuation name
2923
- expectByte(c, OP_2, 'OP_2 (remainder idx)');
2924
- expectByte(c, OP_1, 'OP_1 (NAME sel)');
2925
- expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (remainder name)');
2926
- const tokenIdPartial2 = readPush(c, 'tokenId partial #2');
2927
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder name)');
2928
- // Continuation amount
2929
- expectByte(c, OP_2, 'OP_2 (remainder idx)');
2930
- expectByte(c, OP_2, 'OP_2 (AMOUNT sel)');
2931
- expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (remainder amount)');
2932
- expectByte(c, OP_OVER, 'OP_OVER');
2933
- expectByte(c, OP_0, 'OP_0 (input idx)');
2934
- expectByte(c, OP_2, 'OP_2 (AMOUNT sel)');
2935
- expectByte(c, OP_INPUTASSETFIELD, 'OP_INPUTASSETFIELD');
2936
- expectByte(c, OP_SWAP, 'OP_SWAP');
2937
- expectByte(c, OP_SUB, 'OP_SUB');
2938
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder amount)');
2939
- // Tail (partial)
2940
- expectByte(c, OP_DROP, 'OP_DROP (partial)');
2941
- expectByte(c, OP_1, 'OP_1 (true, partial)');
2942
- // Close the nested structure
2943
- expectByte(c, OP_ENDIF, 'OP_ENDIF (inner)');
2910
+ // ═════ Outer ELSE — input-0 binding + fill branches ═════
2911
+ const fill = readFillBranches(c);
2944
2912
  expectByte(c, OP_ENDIF, 'OP_ENDIF (outer)');
2945
2913
  assertTrailing(c);
2946
- // Cross-branch consistency: all three (full token, partial name #1,
2947
- // partial name #2) must agree, and the two unit prices must match.
2948
- if (!bytesEqual(tokenIdFull, tokenIdPartial1)) {
2949
- throw new Error('parse: tokenId differs between full-fill and partial-fill branches');
2950
- }
2951
- if (!bytesEqual(tokenIdPartial1, tokenIdPartial2)) {
2952
- throw new Error('parse: tokenId bytes differ between buyer and remainder partial checks');
2953
- }
2954
- if (unitPriceSatsFull !== unitPriceSatsPartial) {
2955
- throw new Error('parse: unitPriceSats differs between full-fill and partial-fill branches');
2956
- }
2957
- assertSameExpiration(expirationFull, expirationPartial, 'full-fill and partial-fill branches');
2958
- const tokenId = new TextDecoder('utf-8', { fatal: true }).decode(tokenIdFull);
2959
2914
  return {
2960
2915
  network,
2961
2916
  sellerPubKeyHash,
2962
- unitPriceSats: unitPriceSatsFull,
2963
- tokenId,
2964
- expiration: expirationFull,
2917
+ paymentScriptPubKey: fill.paymentScriptPubKey,
2918
+ unitPriceSats: fill.unitPriceSats,
2919
+ tokenId: fill.tokenId,
2920
+ expiration: fill.expiration,
2965
2921
  scriptHex: bytesToHex(bytes)
2966
2922
  };
2967
2923
  }
@@ -2972,9 +2928,8 @@ var NeuraiScriptsBundle = (function (exports) {
2972
2928
  * payment scriptPubKey bytes (which may be P2PKH or AuthScript) and the
2973
2929
  * configured TXHASH selector.
2974
2930
  *
2975
- * The full-fill and partial-fill branches share `(paymentScriptPubKey,
2976
- * unitPriceSats, tokenId)`. The parser reads both branches and verifies
2977
- * consistency; inconsistency throws.
2931
+ * The fill branches (and their input-0 binding) are read by the shared
2932
+ * `readFillBranches`; covenants built before the binding are rejected.
2978
2933
  */
2979
2934
  /** Quick discriminator without throwing — useful for indexers. */
2980
2935
  function isPartialFillScriptPQ(script) {
@@ -3001,118 +2956,30 @@ var NeuraiScriptsBundle = (function (exports) {
3001
2956
  throw new Error(`parse-pq: pubKeyCommitment must be 32 bytes, got ${pubKeyCommitment.length}`);
3002
2957
  }
3003
2958
  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`);
2959
+ const selectorBytes = readPush(c, 'txHashSelector');
2960
+ if (selectorBytes.length !== 2) {
2961
+ throw new Error(`parse-pq: txHashSelector must be a two-byte LE push, got ${selectorBytes.length} bytes`);
2962
+ }
2963
+ const txHashSelector = selectorBytes[0] | (selectorBytes[1] << 8);
2964
+ if (txHashSelector === 0 || txHashSelector > 0x1ff) {
2965
+ throw new Error(`parse-pq: txHashSelector 0x${txHashSelector.toString(16)} is rejected by OP_TXHASH`);
3007
2966
  }
3008
2967
  expectByte(c, OP_TXHASH, 'OP_TXHASH');
3009
2968
  expectByte(c, OP_SWAP, 'OP_SWAP');
3010
2969
  expectByte(c, OP_CHECKSIGFROMSTACK, 'OP_CHECKSIGFROMSTACK');
3011
2970
  expectByte(c, OP_ELSE, 'OP_ELSE (outer → fill)');
3012
- // ═════ Inner IF — Full-fill branch ═════
3013
- expectByte(c, OP_IF, 'OP_IF (inner full-fill)');
3014
- const expirationFull = readOptionalExpirationGate(c, OP_0, 'full');
3015
- expectByte(c, OP_0, 'OP_0 (input idx, full)');
3016
- expectByte(c, OP_2, 'OP_2 (AMOUNT sel, full)');
3017
- expectByte(c, OP_INPUTASSETFIELD, 'OP_INPUTASSETFIELD (full)');
3018
- expectByte(c, OP_DUP, 'OP_DUP (price, full)');
3019
- const unitPriceSatsFull = readPushPositiveInt(c, 'unitPriceSats (full)');
3020
- expectByte(c, OP_MUL, 'OP_MUL (full)');
3021
- expectByte(c, OP_0, 'OP_0 (payment idx, full)');
3022
- expectByte(c, OP_OUTPUTVALUE, 'OP_OUTPUTVALUE (full)');
3023
- expectByte(c, OP_SWAP, 'OP_SWAP (full)');
3024
- expectByte(c, OP_GREATERTHANOREQUAL, 'OP_GE (full)');
3025
- expectByte(c, OP_VERIFY, 'OP_VERIFY (full)');
3026
- expectByte(c, OP_0, 'OP_0 (payment spk idx, full)');
3027
- expectByte(c, OP_OUTPUTSCRIPT, 'OP_OUTPUTSCRIPT (payment full)');
3028
- const paymentScriptPubKeyFull = readPush(c, 'paymentScriptPubKey (full)');
3029
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (payment full)');
3030
- expectByte(c, OP_DUP, 'OP_DUP (buyer amount, full)');
3031
- expectByte(c, OP_1, 'OP_1 (buyer idx, full)');
3032
- expectByte(c, OP_2, 'OP_2 (AMOUNT sel, full)');
3033
- expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer amount, full)');
3034
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer amount, full)');
3035
- expectByte(c, OP_1, 'OP_1 (buyer idx, full)');
3036
- expectByte(c, OP_1, 'OP_1 (NAME sel, full)');
3037
- expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer name, full)');
3038
- const tokenIdFull = readPush(c, 'tokenId (full)');
3039
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer name, full)');
3040
- expectByte(c, OP_DROP, 'OP_DROP (full)');
3041
- expectByte(c, OP_1, 'OP_1 (true, full)');
3042
- expectByte(c, OP_ELSE, 'OP_ELSE (inner → partial fill)');
3043
- // ═════ Inner ELSE — Partial-fill branch ═════
3044
- const expirationPartial = readOptionalExpirationGate(c, OP_DUP, 'partial');
3045
- expectByte(c, OP_DUP, 'OP_DUP (price, partial)');
3046
- const unitPriceSatsPartial = readPushPositiveInt(c, 'unitPriceSats (partial)');
3047
- expectByte(c, OP_MUL, 'OP_MUL (partial)');
3048
- expectByte(c, OP_0, 'OP_0 (payment idx, partial)');
3049
- expectByte(c, OP_OUTPUTVALUE, 'OP_OUTPUTVALUE (partial)');
3050
- expectByte(c, OP_SWAP, 'OP_SWAP (partial)');
3051
- expectByte(c, OP_GREATERTHANOREQUAL, 'OP_GE (partial)');
3052
- expectByte(c, OP_VERIFY, 'OP_VERIFY (partial)');
3053
- expectByte(c, OP_0, 'OP_0 (payment spk idx, partial)');
3054
- expectByte(c, OP_OUTPUTSCRIPT, 'OP_OUTPUTSCRIPT (payment partial)');
3055
- const paymentScriptPubKeyPartial = readPush(c, 'paymentScriptPubKey (partial)');
3056
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (payment partial)');
3057
- expectByte(c, OP_DUP, 'OP_DUP (buyer amount, partial)');
3058
- expectByte(c, OP_1, 'OP_1 (buyer idx, partial)');
3059
- expectByte(c, OP_2, 'OP_2 (AMOUNT sel, partial)');
3060
- expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer amount, partial)');
3061
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer amount, partial)');
3062
- expectByte(c, OP_1, 'OP_1 (buyer idx, partial)');
3063
- expectByte(c, OP_1, 'OP_1 (NAME sel, partial)');
3064
- expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer name, partial)');
3065
- const tokenIdPartial1 = readPush(c, 'tokenId partial #1');
3066
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer name, partial)');
3067
- // Continuation commitment
3068
- expectByte(c, OP_2, 'OP_2 (remainder idx)');
3069
- expectByte(c, OP_OUTPUTAUTHCOMMITMENT, 'OP_OUTPUTAUTHCOMMITMENT (remainder)');
3070
- expectByte(c, OP_2, 'OP_2 (TXFIELD sel: AUTHSCRIPT_COMMITMENT)');
3071
- expectByte(c, OP_TXFIELD, 'OP_TXFIELD');
3072
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder auth)');
3073
- expectByte(c, OP_2, 'OP_2 (remainder idx)');
3074
- expectByte(c, OP_1, 'OP_1 (NAME sel)');
3075
- expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (remainder name)');
3076
- const tokenIdPartial2 = readPush(c, 'tokenId partial #2');
3077
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder name)');
3078
- expectByte(c, OP_2, 'OP_2 (remainder idx)');
3079
- expectByte(c, OP_2, 'OP_2 (AMOUNT sel)');
3080
- expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (remainder amount)');
3081
- expectByte(c, OP_OVER, 'OP_OVER');
3082
- expectByte(c, OP_0, 'OP_0 (input idx)');
3083
- expectByte(c, OP_2, 'OP_2 (AMOUNT sel)');
3084
- expectByte(c, OP_INPUTASSETFIELD, 'OP_INPUTASSETFIELD');
3085
- expectByte(c, OP_SWAP, 'OP_SWAP');
3086
- expectByte(c, OP_SUB, 'OP_SUB');
3087
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder amount)');
3088
- expectByte(c, OP_DROP, 'OP_DROP (partial)');
3089
- expectByte(c, OP_1, 'OP_1 (true, partial)');
3090
- expectByte(c, OP_ENDIF, 'OP_ENDIF (inner)');
2971
+ // ═════ Outer ELSE — input-0 binding + fill branches ═════
2972
+ const fill = readFillBranches(c);
3091
2973
  expectByte(c, OP_ENDIF, 'OP_ENDIF (outer)');
3092
2974
  assertTrailing(c);
3093
- // Cross-branch consistency.
3094
- if (!bytesEqual(paymentScriptPubKeyFull, paymentScriptPubKeyPartial)) {
3095
- throw new Error('parse-pq: paymentScriptPubKey differs between full-fill and partial-fill branches');
3096
- }
3097
- if (!bytesEqual(tokenIdFull, tokenIdPartial1)) {
3098
- throw new Error('parse-pq: tokenId differs between full-fill and partial-fill branches');
3099
- }
3100
- if (!bytesEqual(tokenIdPartial1, tokenIdPartial2)) {
3101
- throw new Error('parse-pq: tokenId differs between buyer and remainder partial checks');
3102
- }
3103
- if (unitPriceSatsFull !== unitPriceSatsPartial) {
3104
- throw new Error('parse-pq: unitPriceSats differs between full-fill and partial-fill branches');
3105
- }
3106
- assertSameExpiration(expirationFull, expirationPartial, 'full-fill and partial-fill branches');
3107
- const tokenId = new TextDecoder('utf-8', { fatal: true }).decode(tokenIdFull);
3108
2975
  return {
3109
2976
  network,
3110
2977
  pubKeyCommitment,
3111
- tokenId,
3112
- unitPriceSats: unitPriceSatsFull,
2978
+ tokenId: fill.tokenId,
2979
+ unitPriceSats: fill.unitPriceSats,
3113
2980
  txHashSelector,
3114
- expiration: expirationFull,
3115
- paymentScriptPubKey: paymentScriptPubKeyFull,
2981
+ expiration: fill.expiration,
2982
+ paymentScriptPubKey: fill.paymentScriptPubKey,
3116
2983
  scriptHex: bytesToHex(bytes)
3117
2984
  };
3118
2985
  }