@neuraiproject/neurai-scripts 0.9.2 → 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.
@@ -2212,7 +2212,224 @@ var NeuraiScriptsBundle = (function (exports) {
2212
2212
  }
2213
2213
 
2214
2214
  /**
2215
- * 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.
2216
2433
  *
2217
2434
  * The covenant has three branches selected by the top of the unlock stack:
2218
2435
  *
@@ -2232,11 +2449,8 @@ var NeuraiScriptsBundle = (function (exports) {
2232
2449
  * amount == inputAmount - N)
2233
2450
  * output[3+] = optional buyer change (not constrained by the covenant)
2234
2451
  *
2235
- * Continuity — the remainder UTXO reuses the spent covenant's AuthScript v1
2236
- * commitment via `OP_OUTPUTAUTHCOMMITMENT(2) == OP_TXFIELD(0x02)` (NIP-023).
2237
- * Comparing commitments rather than full scriptPubKeys is mandatory because
2238
- * the remainder's asset wrapper carries a different `amountRaw` than the
2239
- * 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`.
2240
2454
  *
2241
2455
  * Unlock stack shapes (scriptSig pushes, top → bottom):
2242
2456
  *
@@ -2244,10 +2458,12 @@ var NeuraiScriptsBundle = (function (exports) {
2244
2458
  * Full fill: <1> <0> (full-flag=1, cancel-flag=0)
2245
2459
  * Partial fill: <N> <0> <0> (N, full-flag=0, cancel-flag=0)
2246
2460
  *
2247
- * The cancel branch uses classical ECDSA (`OP_HASH160 + OP_CHECKSIG`) and
2248
- * commits to a 20-byte PKH — this variant only accepts legacy P2PKH
2249
- * addresses as the seller destination. For AuthScript bech32m destinations
2250
- * 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`.
2251
2467
  */
2252
2468
  const ASSET_NAME_MAX$1 = 32;
2253
2469
  function decodeSellerAddress(sellerAddress) {
@@ -2256,8 +2472,8 @@ var NeuraiScriptsBundle = (function (exports) {
2256
2472
  }
2257
2473
  const decoded = decodeAddress(sellerAddress);
2258
2474
  if (decoded.type !== 'p2pkh') {
2259
- throw new Error(`sellerAddress must be a legacy P2PKH address (the legacy partial-fill covenant uses OP_HASH160 + OP_CHECKSIG); ` +
2260
- `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.`);
2261
2477
  }
2262
2478
  const pkh = Uint8Array.from(decoded.hash);
2263
2479
  if (pkh.length !== 20) {
@@ -2265,6 +2481,27 @@ var NeuraiScriptsBundle = (function (exports) {
2265
2481
  }
2266
2482
  return pkh;
2267
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
+ }
2268
2505
  function assertTokenId$1(tokenId) {
2269
2506
  if (typeof tokenId !== 'string' || tokenId.length === 0) {
2270
2507
  throw new Error('tokenId is required');
@@ -2292,13 +2529,11 @@ var NeuraiScriptsBundle = (function (exports) {
2292
2529
  * Returns raw bytes; wrap with `bytesToHex` for wire format.
2293
2530
  */
2294
2531
  function buildPartialFillScript(params) {
2295
- const { sellerAddress, tokenId, unitPriceSats } = params;
2296
- const sellerPubKeyHash = decodeSellerAddress(sellerAddress);
2532
+ const { tokenId, unitPriceSats } = params;
2533
+ const { sellerPubKeyHash, paymentScriptPubKey } = resolveSeller(params);
2297
2534
  assertTokenId$1(tokenId);
2298
2535
  assertPrice$1(unitPriceSats);
2299
2536
  const expiration = assertExpiration(params.expiration);
2300
- const sellerScriptPubKey = encodeP2PKHScriptPubKey(sellerPubKeyHash);
2301
- const tokenIdBytes = new TextEncoder().encode(tokenId);
2302
2537
  const b = new ScriptBuilder();
2303
2538
  // ════════ Cancel branch (outer IF) ════════
2304
2539
  // scriptSig: <sig> <pubkey> <1>
@@ -2307,107 +2542,13 @@ var NeuraiScriptsBundle = (function (exports) {
2307
2542
  .pushBytes(sellerPubKeyHash)
2308
2543
  .op(OP_EQUALVERIFY, OP_CHECKSIG)
2309
2544
  .op(OP_ELSE);
2310
- // ════════ Fill branches (outer ELSE: inner IF/ELSE) ════════
2311
- b.op(OP_IF);
2312
- // ──────── Full-fill branch (inner IF) ────────
2313
- // scriptSig: <1> <0> ( full-flag=1, cancel-flag=0 )
2314
- // Stack entering: [ ]
2315
- //
2316
- // N is derived from the spent UTXO's asset amount, so the buyer does not
2317
- // push N. The entire covenant is drained to vout[1]; no vout[2] is
2318
- // constrained and none is required (consensus forbids zero-amount asset
2319
- // transfers, so the partial-fill continuity check is simply skipped).
2320
- appendExpirationGate(b, expiration);
2321
- // 1. N = inputAmount
2322
- b.pushInt(0)
2323
- .pushInt(ASSETFIELD_AMOUNT)
2324
- .op(OP_INPUTASSETFIELD); // [ N ]
2325
- // 2. output[0].value >= N * unitPriceSats
2326
- b.op(OP_DUP) // [ N, N ]
2327
- .pushInt(unitPriceSats) // [ N, N, price ]
2328
- .op(OP_MUL) // [ N, N*price ]
2329
- .pushInt(0) // [ N, N*price, 0 ]
2330
- .op(OP_OUTPUTVALUE) // [ N, N*price, value ]
2331
- .op(OP_SWAP) // [ N, value, N*price ]
2332
- .op(OP_GREATERTHANOREQUAL)
2333
- .op(OP_VERIFY); // [ N ]
2334
- // 3. output[0].scriptPubKey == sellerScriptPubKey
2335
- b.pushInt(0)
2336
- .op(OP_OUTPUTSCRIPT) // [ N, spk_out0 ]
2337
- .pushBytes(sellerScriptPubKey)
2338
- .op(OP_EQUALVERIFY); // [ N ]
2339
- // 4. output[1].asset.amount == N
2340
- b.op(OP_DUP) // [ N, N ]
2341
- .pushInt(1)
2342
- .pushInt(ASSETFIELD_AMOUNT)
2343
- .op(OP_OUTPUTASSETFIELD) // [ N, N, amount_out1 ]
2344
- .op(OP_EQUALVERIFY); // [ N ]
2345
- // 5. output[1].asset.name == tokenId
2346
- b.pushInt(1)
2347
- .pushInt(ASSETFIELD_NAME)
2348
- .op(OP_OUTPUTASSETFIELD) // [ N, name_out1 ]
2349
- .pushBytes(tokenIdBytes)
2350
- .op(OP_EQUALVERIFY); // [ N ]
2351
- // 6. Drop N, leave TRUE on the stack.
2352
- b.op(OP_DROP).pushInt(1);
2353
- // ──────── Partial-fill branch (inner ELSE) ────────
2354
- // scriptSig: <N> <0> <0> ( N, full-flag=0, cancel-flag=0 )
2355
- // Stack entering: [ N ]
2356
- b.op(OP_ELSE);
2357
- appendExpirationGate(b, expiration);
2358
- // 1. Payment value (output 0) >= N * unitPriceSats
2359
- b.op(OP_DUP) // [ N, N ]
2360
- .pushInt(unitPriceSats) // [ N, N, price ]
2361
- .op(OP_MUL) // [ N, N*price ]
2362
- .pushInt(0) // [ N, N*price, 0 ]
2363
- .op(OP_OUTPUTVALUE) // [ N, N*price, value ]
2364
- .op(OP_SWAP) // [ N, value, N*price ]
2365
- .op(OP_GREATERTHANOREQUAL)
2366
- .op(OP_VERIFY); // [ N ]
2367
- // 2. Payment scriptPubKey (output 0)
2368
- b.pushInt(0)
2369
- .op(OP_OUTPUTSCRIPT) // [ N, spk_out0 ]
2370
- .pushBytes(sellerScriptPubKey)
2371
- .op(OP_EQUALVERIFY); // [ N ]
2372
- // 3. Asset to buyer (output 1) amount == N
2373
- b.op(OP_DUP) // [ N, N ]
2374
- .pushInt(1)
2375
- .pushInt(ASSETFIELD_AMOUNT)
2376
- .op(OP_OUTPUTASSETFIELD)
2377
- .op(OP_EQUALVERIFY); // [ N ]
2378
- // 4. Asset to buyer (output 1) name == tokenId
2379
- b.pushInt(1)
2380
- .pushInt(ASSETFIELD_NAME)
2381
- .op(OP_OUTPUTASSETFIELD)
2382
- .pushBytes(tokenIdBytes)
2383
- .op(OP_EQUALVERIFY); // [ N ]
2384
- // 5. Continuation (output 2): same AuthScript commitment as spent UTXO
2385
- // (NIP-023 — see file-level comment).
2386
- b.pushInt(2)
2387
- .op(OP_OUTPUTAUTHCOMMITMENT)
2388
- .pushInt(TXFIELD_AUTHSCRIPT_COMMITMENT)
2389
- .op(OP_TXFIELD)
2390
- .op(OP_EQUALVERIFY); // [ N ]
2391
- // 6. Continuation name == tokenId
2392
- b.pushInt(2)
2393
- .pushInt(ASSETFIELD_NAME)
2394
- .op(OP_OUTPUTASSETFIELD)
2395
- .pushBytes(tokenIdBytes)
2396
- .op(OP_EQUALVERIFY); // [ N ]
2397
- // 7. Continuation amount == inputAmount - N
2398
- b.pushInt(2)
2399
- .pushInt(ASSETFIELD_AMOUNT)
2400
- .op(OP_OUTPUTASSETFIELD) // [ N, rem_amount ]
2401
- .op(OP_OVER) // [ N, rem_amount, N ]
2402
- .pushInt(0) // [ N, rem_amount, N, 0 ]
2403
- .pushInt(ASSETFIELD_AMOUNT)
2404
- .op(OP_INPUTASSETFIELD) // [ N, rem_amount, N, in_amount ]
2405
- .op(OP_SWAP) // [ N, rem_amount, in_amount, N ]
2406
- .op(OP_SUB) // [ N, rem_amount, in_amount - N ]
2407
- .op(OP_EQUALVERIFY); // [ N ]
2408
- // 8. Drop N, leave TRUE on the stack.
2409
- b.op(OP_DROP).pushInt(1);
2410
- 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
+ });
2411
2552
  b.op(OP_ENDIF); // closes outer cancel/fill IF
2412
2553
  return b.build();
2413
2554
  }
@@ -2437,11 +2578,11 @@ var NeuraiScriptsBundle = (function (exports) {
2437
2578
  *
2438
2579
  * The message that gets signed is **SHA256(OP_TXHASH(selector))** — CSFS
2439
2580
  * single-SHA256s its message argument before verification, and OP_TXHASH
2440
- * produces its own 32-byte hash, so the seller computes:
2441
- * `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)))`
2442
2583
  *
2443
- * Fill branches are identical in structure to the legacy variant — see
2444
- * `./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`.
2445
2586
  */
2446
2587
  const ASSET_NAME_MAX = 32;
2447
2588
  const DEFAULT_PQ_TXHASH_SELECTOR = 0x1ff;
@@ -2509,99 +2650,13 @@ var NeuraiScriptsBundle = (function (exports) {
2509
2650
  .op(OP_SWAP) // [ sig, txHash, pubKey ]
2510
2651
  .op(OP_CHECKSIGFROMSTACK) // [ 1 | 0 ]
2511
2652
  .op(OP_ELSE);
2512
- // ════════ Fill branches (inner IF: full / ELSE: partial) ════════
2513
- b.op(OP_IF);
2514
- // ──────── Full-fill branch ────────
2515
- // scriptSig: <1> <0>
2516
- // Stack entering: [ ]
2517
- // 1. N = inputAmount
2518
- appendExpirationGate(b, expiration);
2519
- b.pushInt(0)
2520
- .pushInt(ASSETFIELD_AMOUNT)
2521
- .op(OP_INPUTASSETFIELD);
2522
- // 2. Payment value (output 0) >= N * unitPriceSats
2523
- b.op(OP_DUP)
2524
- .pushInt(unitPriceSats)
2525
- .op(OP_MUL)
2526
- .pushInt(0)
2527
- .op(OP_OUTPUTVALUE)
2528
- .op(OP_SWAP)
2529
- .op(OP_GREATERTHANOREQUAL)
2530
- .op(OP_VERIFY);
2531
- // 3. Payment scriptPubKey (output 0)
2532
- b.pushInt(0)
2533
- .op(OP_OUTPUTSCRIPT)
2534
- .pushBytes(payment.bytes)
2535
- .op(OP_EQUALVERIFY);
2536
- // 4. Asset to buyer (output 1) amount == N
2537
- b.op(OP_DUP)
2538
- .pushInt(1)
2539
- .pushInt(ASSETFIELD_AMOUNT)
2540
- .op(OP_OUTPUTASSETFIELD)
2541
- .op(OP_EQUALVERIFY);
2542
- // 5. Asset to buyer (output 1) name == tokenId
2543
- b.pushInt(1)
2544
- .pushInt(ASSETFIELD_NAME)
2545
- .op(OP_OUTPUTASSETFIELD)
2546
- .pushBytes(tokenIdBytes)
2547
- .op(OP_EQUALVERIFY);
2548
- b.op(OP_DROP).pushInt(1);
2549
- // ──────── Partial-fill branch ────────
2550
- // scriptSig: <N> <0> <0>
2551
- // Stack entering: [ N ]
2552
- b.op(OP_ELSE);
2553
- appendExpirationGate(b, expiration);
2554
- // 1. Payment value (output 0) >= N * unitPriceSats
2555
- b.op(OP_DUP)
2556
- .pushInt(unitPriceSats)
2557
- .op(OP_MUL)
2558
- .pushInt(0)
2559
- .op(OP_OUTPUTVALUE)
2560
- .op(OP_SWAP)
2561
- .op(OP_GREATERTHANOREQUAL)
2562
- .op(OP_VERIFY);
2563
- // 2. Payment scriptPubKey (output 0)
2564
- b.pushInt(0)
2565
- .op(OP_OUTPUTSCRIPT)
2566
- .pushBytes(payment.bytes)
2567
- .op(OP_EQUALVERIFY);
2568
- // 3. Asset to buyer (output 1) amount == N
2569
- b.op(OP_DUP)
2570
- .pushInt(1)
2571
- .pushInt(ASSETFIELD_AMOUNT)
2572
- .op(OP_OUTPUTASSETFIELD)
2573
- .op(OP_EQUALVERIFY);
2574
- // 4. Asset to buyer (output 1) name == tokenId
2575
- b.pushInt(1)
2576
- .pushInt(ASSETFIELD_NAME)
2577
- .op(OP_OUTPUTASSETFIELD)
2578
- .pushBytes(tokenIdBytes)
2579
- .op(OP_EQUALVERIFY);
2580
- // 5. Continuation (output 2): same AuthScript commitment as spent (NIP-023)
2581
- b.pushInt(2)
2582
- .op(OP_OUTPUTAUTHCOMMITMENT)
2583
- .pushInt(TXFIELD_AUTHSCRIPT_COMMITMENT)
2584
- .op(OP_TXFIELD)
2585
- .op(OP_EQUALVERIFY);
2586
- // 6. Continuation name == tokenId
2587
- b.pushInt(2)
2588
- .pushInt(ASSETFIELD_NAME)
2589
- .op(OP_OUTPUTASSETFIELD)
2590
- .pushBytes(tokenIdBytes)
2591
- .op(OP_EQUALVERIFY);
2592
- // 7. Continuation amount == inputAmount - N
2593
- b.pushInt(2)
2594
- .pushInt(ASSETFIELD_AMOUNT)
2595
- .op(OP_OUTPUTASSETFIELD)
2596
- .op(OP_OVER)
2597
- .pushInt(0)
2598
- .pushInt(ASSETFIELD_AMOUNT)
2599
- .op(OP_INPUTASSETFIELD)
2600
- .op(OP_SWAP)
2601
- .op(OP_SUB)
2602
- .op(OP_EQUALVERIFY);
2603
- b.op(OP_DROP).pushInt(1);
2604
- 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
+ });
2605
2660
  b.op(OP_ENDIF); // cancel / fill
2606
2661
  return b.build();
2607
2662
  }
@@ -2824,17 +2879,15 @@ var NeuraiScriptsBundle = (function (exports) {
2824
2879
  }
2825
2880
 
2826
2881
  /**
2827
- * Parser for the Partial-Fill Sell Order covenant (three-branch).
2828
- *
2829
- * Extracts `(sellerPubKeyHash, unitPriceSats, tokenId)` from a scriptPubKey
2830
- * that was produced by `buildPartialFillScript`. Walks the exact byte
2831
- * layout emitted by the builder and fails on any deviation — this is
2832
- * deliberate, so a downstream indexer can unambiguously classify a UTXO as
2833
- * "partial-fill order" or "unknown script" with no false positives.
2834
- *
2835
- * The full-fill and partial-fill branches share `(sellerScriptPubKey,
2836
- * unitPriceSats, tokenId)`. The parser reads both branches and verifies
2837
- * 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.
2838
2891
  */
2839
2892
  /**
2840
2893
  * Parse a covenant scriptPubKey and extract its parameters. Throws with a
@@ -2854,129 +2907,17 @@ var NeuraiScriptsBundle = (function (exports) {
2854
2907
  expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (cancel)');
2855
2908
  expectByte(c, OP_CHECKSIG, 'OP_CHECKSIG (cancel)');
2856
2909
  expectByte(c, OP_ELSE, 'OP_ELSE (outer → fill)');
2857
- // ═════ Inner IF — Full-fill branch ═════
2858
- expectByte(c, OP_IF, 'OP_IF (inner full-fill)');
2859
- const expirationFull = readOptionalExpirationGate(c, OP_0, 'full');
2860
- // N = inputAmount
2861
- expectByte(c, OP_0, 'OP_0 (input idx, full)');
2862
- expectByte(c, OP_2, 'OP_2 (AMOUNT sel, full)');
2863
- expectByte(c, OP_INPUTASSETFIELD, 'OP_INPUTASSETFIELD (full)');
2864
- // payment value check
2865
- expectByte(c, OP_DUP, 'OP_DUP (price, full)');
2866
- const unitPriceSatsFull = readPushPositiveInt(c, 'unitPriceSats (full)');
2867
- expectByte(c, OP_MUL, 'OP_MUL (full)');
2868
- expectByte(c, OP_0, 'OP_0 (payment idx, full)');
2869
- expectByte(c, OP_OUTPUTVALUE, 'OP_OUTPUTVALUE (full)');
2870
- expectByte(c, OP_SWAP, 'OP_SWAP (full)');
2871
- expectByte(c, OP_GREATERTHANOREQUAL, 'OP_GREATERTHANOREQUAL (full)');
2872
- expectByte(c, OP_VERIFY, 'OP_VERIFY (payment full)');
2873
- // payment spk
2874
- expectByte(c, OP_0, 'OP_0 (spk idx, full)');
2875
- expectByte(c, OP_OUTPUTSCRIPT, 'OP_OUTPUTSCRIPT (payment full)');
2876
- const sellerSpkFull = readPush(c, 'sellerScriptPubKey (full)');
2877
- const expectedSpk = new Uint8Array([
2878
- OP_DUP, OP_HASH160, 0x14, ...sellerPubKeyHash, OP_EQUALVERIFY, OP_CHECKSIG
2879
- ]);
2880
- if (!bytesEqual(sellerSpkFull, expectedSpk)) {
2881
- throw new Error('parse: full-fill seller scriptPubKey does not match the cancel PKH');
2882
- }
2883
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (payment spk full)');
2884
- // buyer amount == N (full)
2885
- expectByte(c, OP_DUP, 'OP_DUP (buyer amount, full)');
2886
- expectByte(c, OP_1, 'OP_1 (buyer idx, full)');
2887
- expectByte(c, OP_2, 'OP_2 (AMOUNT sel, full)');
2888
- expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer amount, full)');
2889
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer amount, full)');
2890
- // buyer name == tokenId (full)
2891
- expectByte(c, OP_1, 'OP_1 (buyer idx, full)');
2892
- expectByte(c, OP_1, 'OP_1 (NAME sel, full)');
2893
- expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer name, full)');
2894
- const tokenIdFull = readPush(c, 'tokenId (full)');
2895
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer name, full)');
2896
- // tail (full)
2897
- expectByte(c, OP_DROP, 'OP_DROP (full)');
2898
- expectByte(c, OP_1, 'OP_1 (true, full)');
2899
- expectByte(c, OP_ELSE, 'OP_ELSE (inner → partial fill)');
2900
- // ═════ Inner ELSE — Partial-fill branch ═════
2901
- const expirationPartial = readOptionalExpirationGate(c, OP_DUP, 'partial');
2902
- // Payment value
2903
- expectByte(c, OP_DUP, 'OP_DUP (price, partial)');
2904
- const unitPriceSatsPartial = readPushPositiveInt(c, 'unitPriceSats (partial)');
2905
- expectByte(c, OP_MUL, 'OP_MUL (partial)');
2906
- expectByte(c, OP_0, 'OP_0 (pay idx, partial)');
2907
- expectByte(c, OP_OUTPUTVALUE, 'OP_OUTPUTVALUE (partial)');
2908
- expectByte(c, OP_SWAP, 'OP_SWAP (partial)');
2909
- expectByte(c, OP_GREATERTHANOREQUAL, 'OP_GREATERTHANOREQUAL (partial)');
2910
- expectByte(c, OP_VERIFY, 'OP_VERIFY (payment partial)');
2911
- // Payment spk
2912
- expectByte(c, OP_0, 'OP_0 (spk idx, partial)');
2913
- expectByte(c, OP_OUTPUTSCRIPT, 'OP_OUTPUTSCRIPT (payment partial)');
2914
- const sellerSpkPartial = readPush(c, 'sellerScriptPubKey (partial)');
2915
- if (!bytesEqual(sellerSpkPartial, expectedSpk)) {
2916
- throw new Error('parse: partial-fill seller scriptPubKey does not match the cancel PKH');
2917
- }
2918
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (payment spk partial)');
2919
- // Buyer amount
2920
- expectByte(c, OP_DUP, 'OP_DUP (buyer amount, partial)');
2921
- expectByte(c, OP_1, 'OP_1 (buyer idx, partial)');
2922
- expectByte(c, OP_2, 'OP_2 (AMOUNT sel, partial)');
2923
- expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer amount, partial)');
2924
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer amount, partial)');
2925
- // Buyer name
2926
- expectByte(c, OP_1, 'OP_1 (buyer idx, partial)');
2927
- expectByte(c, OP_1, 'OP_1 (NAME sel, partial)');
2928
- expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer name, partial)');
2929
- const tokenIdPartial1 = readPush(c, 'tokenId partial #1');
2930
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer name, partial)');
2931
- // Continuation commitment
2932
- expectByte(c, OP_2, 'OP_2 (remainder idx)');
2933
- expectByte(c, OP_OUTPUTAUTHCOMMITMENT, 'OP_OUTPUTAUTHCOMMITMENT (remainder)');
2934
- expectByte(c, OP_2, 'OP_2 (TXFIELD sel: AUTHSCRIPT_COMMITMENT)');
2935
- expectByte(c, OP_TXFIELD, 'OP_TXFIELD');
2936
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder auth)');
2937
- // Continuation name
2938
- expectByte(c, OP_2, 'OP_2 (remainder idx)');
2939
- expectByte(c, OP_1, 'OP_1 (NAME sel)');
2940
- expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (remainder name)');
2941
- const tokenIdPartial2 = readPush(c, 'tokenId partial #2');
2942
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder name)');
2943
- // Continuation amount
2944
- expectByte(c, OP_2, 'OP_2 (remainder idx)');
2945
- expectByte(c, OP_2, 'OP_2 (AMOUNT sel)');
2946
- expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (remainder amount)');
2947
- expectByte(c, OP_OVER, 'OP_OVER');
2948
- expectByte(c, OP_0, 'OP_0 (input idx)');
2949
- expectByte(c, OP_2, 'OP_2 (AMOUNT sel)');
2950
- expectByte(c, OP_INPUTASSETFIELD, 'OP_INPUTASSETFIELD');
2951
- expectByte(c, OP_SWAP, 'OP_SWAP');
2952
- expectByte(c, OP_SUB, 'OP_SUB');
2953
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder amount)');
2954
- // Tail (partial)
2955
- expectByte(c, OP_DROP, 'OP_DROP (partial)');
2956
- expectByte(c, OP_1, 'OP_1 (true, partial)');
2957
- // Close the nested structure
2958
- expectByte(c, OP_ENDIF, 'OP_ENDIF (inner)');
2910
+ // ═════ Outer ELSE — input-0 binding + fill branches ═════
2911
+ const fill = readFillBranches(c);
2959
2912
  expectByte(c, OP_ENDIF, 'OP_ENDIF (outer)');
2960
2913
  assertTrailing(c);
2961
- // Cross-branch consistency: all three (full token, partial name #1,
2962
- // partial name #2) must agree, and the two unit prices must match.
2963
- if (!bytesEqual(tokenIdFull, tokenIdPartial1)) {
2964
- throw new Error('parse: tokenId differs between full-fill and partial-fill branches');
2965
- }
2966
- if (!bytesEqual(tokenIdPartial1, tokenIdPartial2)) {
2967
- throw new Error('parse: tokenId bytes differ between buyer and remainder partial checks');
2968
- }
2969
- if (unitPriceSatsFull !== unitPriceSatsPartial) {
2970
- throw new Error('parse: unitPriceSats differs between full-fill and partial-fill branches');
2971
- }
2972
- assertSameExpiration(expirationFull, expirationPartial, 'full-fill and partial-fill branches');
2973
- const tokenId = new TextDecoder('utf-8', { fatal: true }).decode(tokenIdFull);
2974
2914
  return {
2975
2915
  network,
2976
2916
  sellerPubKeyHash,
2977
- unitPriceSats: unitPriceSatsFull,
2978
- tokenId,
2979
- expiration: expirationFull,
2917
+ paymentScriptPubKey: fill.paymentScriptPubKey,
2918
+ unitPriceSats: fill.unitPriceSats,
2919
+ tokenId: fill.tokenId,
2920
+ expiration: fill.expiration,
2980
2921
  scriptHex: bytesToHex(bytes)
2981
2922
  };
2982
2923
  }
@@ -2987,9 +2928,8 @@ var NeuraiScriptsBundle = (function (exports) {
2987
2928
  * payment scriptPubKey bytes (which may be P2PKH or AuthScript) and the
2988
2929
  * configured TXHASH selector.
2989
2930
  *
2990
- * The full-fill and partial-fill branches share `(paymentScriptPubKey,
2991
- * unitPriceSats, tokenId)`. The parser reads both branches and verifies
2992
- * 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.
2993
2933
  */
2994
2934
  /** Quick discriminator without throwing — useful for indexers. */
2995
2935
  function isPartialFillScriptPQ(script) {
@@ -3028,110 +2968,18 @@ var NeuraiScriptsBundle = (function (exports) {
3028
2968
  expectByte(c, OP_SWAP, 'OP_SWAP');
3029
2969
  expectByte(c, OP_CHECKSIGFROMSTACK, 'OP_CHECKSIGFROMSTACK');
3030
2970
  expectByte(c, OP_ELSE, 'OP_ELSE (outer → fill)');
3031
- // ═════ Inner IF — Full-fill branch ═════
3032
- expectByte(c, OP_IF, 'OP_IF (inner full-fill)');
3033
- const expirationFull = readOptionalExpirationGate(c, OP_0, 'full');
3034
- expectByte(c, OP_0, 'OP_0 (input idx, full)');
3035
- expectByte(c, OP_2, 'OP_2 (AMOUNT sel, full)');
3036
- expectByte(c, OP_INPUTASSETFIELD, 'OP_INPUTASSETFIELD (full)');
3037
- expectByte(c, OP_DUP, 'OP_DUP (price, full)');
3038
- const unitPriceSatsFull = readPushPositiveInt(c, 'unitPriceSats (full)');
3039
- expectByte(c, OP_MUL, 'OP_MUL (full)');
3040
- expectByte(c, OP_0, 'OP_0 (payment idx, full)');
3041
- expectByte(c, OP_OUTPUTVALUE, 'OP_OUTPUTVALUE (full)');
3042
- expectByte(c, OP_SWAP, 'OP_SWAP (full)');
3043
- expectByte(c, OP_GREATERTHANOREQUAL, 'OP_GE (full)');
3044
- expectByte(c, OP_VERIFY, 'OP_VERIFY (full)');
3045
- expectByte(c, OP_0, 'OP_0 (payment spk idx, full)');
3046
- expectByte(c, OP_OUTPUTSCRIPT, 'OP_OUTPUTSCRIPT (payment full)');
3047
- const paymentScriptPubKeyFull = readPush(c, 'paymentScriptPubKey (full)');
3048
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (payment full)');
3049
- expectByte(c, OP_DUP, 'OP_DUP (buyer amount, full)');
3050
- expectByte(c, OP_1, 'OP_1 (buyer idx, full)');
3051
- expectByte(c, OP_2, 'OP_2 (AMOUNT sel, full)');
3052
- expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer amount, full)');
3053
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer amount, full)');
3054
- expectByte(c, OP_1, 'OP_1 (buyer idx, full)');
3055
- expectByte(c, OP_1, 'OP_1 (NAME sel, full)');
3056
- expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer name, full)');
3057
- const tokenIdFull = readPush(c, 'tokenId (full)');
3058
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer name, full)');
3059
- expectByte(c, OP_DROP, 'OP_DROP (full)');
3060
- expectByte(c, OP_1, 'OP_1 (true, full)');
3061
- expectByte(c, OP_ELSE, 'OP_ELSE (inner → partial fill)');
3062
- // ═════ Inner ELSE — Partial-fill branch ═════
3063
- const expirationPartial = readOptionalExpirationGate(c, OP_DUP, 'partial');
3064
- expectByte(c, OP_DUP, 'OP_DUP (price, partial)');
3065
- const unitPriceSatsPartial = readPushPositiveInt(c, 'unitPriceSats (partial)');
3066
- expectByte(c, OP_MUL, 'OP_MUL (partial)');
3067
- expectByte(c, OP_0, 'OP_0 (payment idx, partial)');
3068
- expectByte(c, OP_OUTPUTVALUE, 'OP_OUTPUTVALUE (partial)');
3069
- expectByte(c, OP_SWAP, 'OP_SWAP (partial)');
3070
- expectByte(c, OP_GREATERTHANOREQUAL, 'OP_GE (partial)');
3071
- expectByte(c, OP_VERIFY, 'OP_VERIFY (partial)');
3072
- expectByte(c, OP_0, 'OP_0 (payment spk idx, partial)');
3073
- expectByte(c, OP_OUTPUTSCRIPT, 'OP_OUTPUTSCRIPT (payment partial)');
3074
- const paymentScriptPubKeyPartial = readPush(c, 'paymentScriptPubKey (partial)');
3075
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (payment partial)');
3076
- expectByte(c, OP_DUP, 'OP_DUP (buyer amount, partial)');
3077
- expectByte(c, OP_1, 'OP_1 (buyer idx, partial)');
3078
- expectByte(c, OP_2, 'OP_2 (AMOUNT sel, partial)');
3079
- expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer amount, partial)');
3080
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer amount, partial)');
3081
- expectByte(c, OP_1, 'OP_1 (buyer idx, partial)');
3082
- expectByte(c, OP_1, 'OP_1 (NAME sel, partial)');
3083
- expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer name, partial)');
3084
- const tokenIdPartial1 = readPush(c, 'tokenId partial #1');
3085
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer name, partial)');
3086
- // Continuation commitment
3087
- expectByte(c, OP_2, 'OP_2 (remainder idx)');
3088
- expectByte(c, OP_OUTPUTAUTHCOMMITMENT, 'OP_OUTPUTAUTHCOMMITMENT (remainder)');
3089
- expectByte(c, OP_2, 'OP_2 (TXFIELD sel: AUTHSCRIPT_COMMITMENT)');
3090
- expectByte(c, OP_TXFIELD, 'OP_TXFIELD');
3091
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder auth)');
3092
- expectByte(c, OP_2, 'OP_2 (remainder idx)');
3093
- expectByte(c, OP_1, 'OP_1 (NAME sel)');
3094
- expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (remainder name)');
3095
- const tokenIdPartial2 = readPush(c, 'tokenId partial #2');
3096
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder name)');
3097
- expectByte(c, OP_2, 'OP_2 (remainder idx)');
3098
- expectByte(c, OP_2, 'OP_2 (AMOUNT sel)');
3099
- expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (remainder amount)');
3100
- expectByte(c, OP_OVER, 'OP_OVER');
3101
- expectByte(c, OP_0, 'OP_0 (input idx)');
3102
- expectByte(c, OP_2, 'OP_2 (AMOUNT sel)');
3103
- expectByte(c, OP_INPUTASSETFIELD, 'OP_INPUTASSETFIELD');
3104
- expectByte(c, OP_SWAP, 'OP_SWAP');
3105
- expectByte(c, OP_SUB, 'OP_SUB');
3106
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder amount)');
3107
- expectByte(c, OP_DROP, 'OP_DROP (partial)');
3108
- expectByte(c, OP_1, 'OP_1 (true, partial)');
3109
- expectByte(c, OP_ENDIF, 'OP_ENDIF (inner)');
2971
+ // ═════ Outer ELSE — input-0 binding + fill branches ═════
2972
+ const fill = readFillBranches(c);
3110
2973
  expectByte(c, OP_ENDIF, 'OP_ENDIF (outer)');
3111
2974
  assertTrailing(c);
3112
- // Cross-branch consistency.
3113
- if (!bytesEqual(paymentScriptPubKeyFull, paymentScriptPubKeyPartial)) {
3114
- throw new Error('parse-pq: paymentScriptPubKey differs between full-fill and partial-fill branches');
3115
- }
3116
- if (!bytesEqual(tokenIdFull, tokenIdPartial1)) {
3117
- throw new Error('parse-pq: tokenId differs between full-fill and partial-fill branches');
3118
- }
3119
- if (!bytesEqual(tokenIdPartial1, tokenIdPartial2)) {
3120
- throw new Error('parse-pq: tokenId differs between buyer and remainder partial checks');
3121
- }
3122
- if (unitPriceSatsFull !== unitPriceSatsPartial) {
3123
- throw new Error('parse-pq: unitPriceSats differs between full-fill and partial-fill branches');
3124
- }
3125
- assertSameExpiration(expirationFull, expirationPartial, 'full-fill and partial-fill branches');
3126
- const tokenId = new TextDecoder('utf-8', { fatal: true }).decode(tokenIdFull);
3127
2975
  return {
3128
2976
  network,
3129
2977
  pubKeyCommitment,
3130
- tokenId,
3131
- unitPriceSats: unitPriceSatsFull,
2978
+ tokenId: fill.tokenId,
2979
+ unitPriceSats: fill.unitPriceSats,
3132
2980
  txHashSelector,
3133
- expiration: expirationFull,
3134
- paymentScriptPubKey: paymentScriptPubKeyFull,
2981
+ expiration: fill.expiration,
2982
+ paymentScriptPubKey: fill.paymentScriptPubKey,
3135
2983
  scriptHex: bytesToHex(bytes)
3136
2984
  };
3137
2985
  }