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