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