@neuraiproject/neurai-scripts 0.3.0 → 0.5.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.
@@ -190,6 +190,11 @@ var NeuraiScriptsBundle = (function (exports) {
190
190
  const OP_REFINPUTFIELD = 0xd2;
191
191
  const OP_REFINPUTASSETFIELD = 0xd3;
192
192
  const OP_REFINPUTCOUNT = 0xd4;
193
+ // ---------- Output auth commitment introspection (NIP-023) ----------
194
+ // Pushes the 32-byte AuthScript v1 commitment of a selected output's
195
+ // scriptPubKey. Symmetric to TXFIELD_AUTHSCRIPT_COMMITMENT for inputs;
196
+ // ignores any trailing OP_XNA_ASSET asset wrapper bytes.
197
+ const OP_OUTPUTAUTHCOMMITMENT = 0xd5;
193
198
  // ---------- Byte manipulation (DePIN-Test) ----------
194
199
  const OP_CAT = 0x7e;
195
200
  const OP_SPLIT = 0xb7;
@@ -312,6 +317,7 @@ var NeuraiScriptsBundle = (function (exports) {
312
317
  OP_NUMEQUALVERIFY: OP_NUMEQUALVERIFY,
313
318
  OP_NUMNOTEQUAL: OP_NUMNOTEQUAL,
314
319
  OP_OUTPUTASSETFIELD: OP_OUTPUTASSETFIELD,
320
+ OP_OUTPUTAUTHCOMMITMENT: OP_OUTPUTAUTHCOMMITMENT,
315
321
  OP_OUTPUTCOUNT: OP_OUTPUTCOUNT,
316
322
  OP_OUTPUTSCRIPT: OP_OUTPUTSCRIPT,
317
323
  OP_OUTPUTVALUE: OP_OUTPUTVALUE,
@@ -1741,31 +1747,42 @@ var NeuraiScriptsBundle = (function (exports) {
1741
1747
  }
1742
1748
 
1743
1749
  /**
1744
- * Partial-Fill Sell Order covenant script.
1750
+ * Partial-Fill Sell Order covenant script (three-branch).
1745
1751
  *
1746
- * The covenant has two branches selected by the top of the unlock stack:
1752
+ * The covenant has three branches selected by the top of the unlock stack:
1747
1753
  *
1748
- * OP_IF (unlock pushed 1) → Cancel: seller signs, recovers remainder.
1749
- * OP_ELSE (unlock pushed 0) → Public partial fill: buyer provides the
1750
- * fill amount `N` and the tx layout is
1751
- * validated byte by byte.
1754
+ * OP_IF ← Cancel: seller signs, recovers remainder.
1755
+ * OP_ELSE OP_IF ← Full fill: buyer drains the covenant entirely.
1756
+ * No continuation output is required.
1757
+ * OP_ELSE OP_ELSE ← Partial fill: buyer takes N < total. A
1758
+ * continuation UTXO at vout[2] preserves the
1759
+ * covenant with amount = total - N.
1752
1760
  *
1753
- * The partial-fill branch enforces a fixed output layout so that the script
1754
- * stays compact and parseable:
1761
+ * Output layout:
1755
1762
  *
1756
1763
  * output[0] = XNA payment to seller (value >= N * unitPriceSats)
1757
1764
  * output[1] = asset to buyer (tokenId, amount == N)
1758
- * output[2] = covenant remainder (same scriptPubKey, amount == in - N)
1765
+ * output[2] = covenant continuation (only for partial fill;
1766
+ * same AuthScript commitment,
1767
+ * amount == inputAmount - N)
1759
1768
  * output[3+] = optional buyer change (not constrained by the covenant)
1760
1769
  *
1761
- * The remainder UTXO reuses the spent scriptPubKey via
1762
- * `OP_OUTPUTSCRIPT == OP_TXFIELD 0x03`, so the covenant is self-replicating
1763
- * without hardcoding its own hash.
1770
+ * Continuity — the remainder UTXO reuses the spent covenant's AuthScript v1
1771
+ * commitment via `OP_OUTPUTAUTHCOMMITMENT(2) == OP_TXFIELD(0x02)` (NIP-023).
1772
+ * Comparing commitments rather than full scriptPubKeys is mandatory because
1773
+ * the remainder's asset wrapper carries a different `amountRaw` than the
1774
+ * spent UTXO's wrapper.
1775
+ *
1776
+ * Unlock stack shapes (scriptSig pushes, top → bottom):
1777
+ *
1778
+ * Cancel: <sig> <pubkey> <1>
1779
+ * Full fill: <1> <0> (full-flag=1, cancel-flag=0)
1780
+ * Partial fill: <N> <0> <0> (N, full-flag=0, cancel-flag=0)
1764
1781
  *
1765
1782
  * The cancel branch uses classical ECDSA (`OP_HASH160 + OP_CHECKSIG`) and
1766
- * commits to a 20-byte PKH. As a consequence this variant only accepts
1767
- * legacy P2PKH addresses as the seller destination. For AuthScript bech32m
1768
- * destinations and post-quantum signing, use `buildPartialFillScriptPQ`.
1783
+ * commits to a 20-byte PKH — this variant only accepts legacy P2PKH
1784
+ * addresses as the seller destination. For AuthScript bech32m destinations
1785
+ * and post-quantum signing, use `buildPartialFillScriptPQ`.
1769
1786
  */
1770
1787
  const ASSET_NAME_MAX$1 = 32;
1771
1788
  function decodeSellerAddress(sellerAddress) {
@@ -1801,8 +1818,6 @@ var NeuraiScriptsBundle = (function (exports) {
1801
1818
  if (priceSats <= 0n) {
1802
1819
  throw new Error('unitPriceSats must be > 0');
1803
1820
  }
1804
- // 8-byte signed int ceiling: values beyond this cannot be produced by OP_MUL
1805
- // inside the covenant without overflow (CScriptNum is int64).
1806
1821
  if (priceSats > 0x7fffffffffffffffn) {
1807
1822
  throw new Error('unitPriceSats exceeds int64 range');
1808
1823
  }
@@ -1819,19 +1834,28 @@ var NeuraiScriptsBundle = (function (exports) {
1819
1834
  const sellerScriptPubKey = encodeP2PKHScriptPubKey(sellerPubKeyHash);
1820
1835
  const tokenIdBytes = new TextEncoder().encode(tokenId);
1821
1836
  const b = new ScriptBuilder();
1822
- // ───────── Cancel branch (IF) ─────────
1823
- // scriptSig expected: <sig> <pubkey> OP_1
1837
+ // ════════ Cancel branch (outer IF) ════════
1838
+ // scriptSig: <sig> <pubkey> <1>
1824
1839
  b.op(OP_IF)
1825
1840
  .op(OP_DUP, OP_HASH160)
1826
1841
  .pushBytes(sellerPubKeyHash)
1827
1842
  .op(OP_EQUALVERIFY, OP_CHECKSIG)
1828
1843
  .op(OP_ELSE);
1829
- // ───────── Partial-fill branch (ELSE) ─────────
1830
- // scriptSig expected: <N> OP_0
1844
+ // ════════ Fill branches (outer ELSE: inner IF/ELSE) ════════
1845
+ b.op(OP_IF);
1846
+ // ──────── Full-fill branch (inner IF) ────────
1847
+ // scriptSig: <1> <0> ( full-flag=1, cancel-flag=0 )
1848
+ // Stack entering: [ ]
1831
1849
  //
1832
- // Stack invariant entering the ELSE branch: [ N ]
1833
- // 1. Payment value to seller (output 0)
1834
- // require outputValue(0) >= N * unitPriceSats
1850
+ // N is derived from the spent UTXO's asset amount, so the buyer does not
1851
+ // push N. The entire covenant is drained to vout[1]; no vout[2] is
1852
+ // constrained and none is required (consensus forbids zero-amount asset
1853
+ // transfers, so the partial-fill continuity check is simply skipped).
1854
+ // 1. N = inputAmount
1855
+ b.pushInt(0)
1856
+ .pushInt(ASSETFIELD_AMOUNT)
1857
+ .op(OP_INPUTASSETFIELD); // [ N ]
1858
+ // 2. output[0].value >= N * unitPriceSats
1835
1859
  b.op(OP_DUP) // [ N, N ]
1836
1860
  .pushInt(unitPriceSats) // [ N, N, price ]
1837
1861
  .op(OP_MUL) // [ N, N*price ]
@@ -1840,49 +1864,83 @@ var NeuraiScriptsBundle = (function (exports) {
1840
1864
  .op(OP_SWAP) // [ N, value, N*price ]
1841
1865
  .op(OP_GREATERTHANOREQUAL)
1842
1866
  .op(OP_VERIFY); // [ N ]
1843
- // 2. Payment scriptPubKey must equal Alice's (output 0)
1867
+ // 3. output[0].scriptPubKey == sellerScriptPubKey
1844
1868
  b.pushInt(0)
1845
- .op(OP_OUTPUTSCRIPT) // [ N, scriptPubKey_out0 ]
1869
+ .op(OP_OUTPUTSCRIPT) // [ N, spk_out0 ]
1846
1870
  .pushBytes(sellerScriptPubKey)
1847
1871
  .op(OP_EQUALVERIFY); // [ N ]
1848
- // 3. Asset delivered to buyer (output 1): amount == N
1872
+ // 4. output[1].asset.amount == N
1849
1873
  b.op(OP_DUP) // [ N, N ]
1850
- .pushInt(1) // [ N, N, 1 ]
1874
+ .pushInt(1)
1851
1875
  .pushInt(ASSETFIELD_AMOUNT)
1852
1876
  .op(OP_OUTPUTASSETFIELD) // [ N, N, amount_out1 ]
1853
1877
  .op(OP_EQUALVERIFY); // [ N ]
1854
- // 4. Asset delivered to buyer (output 1): name == tokenId
1878
+ // 5. output[1].asset.name == tokenId
1855
1879
  b.pushInt(1)
1856
1880
  .pushInt(ASSETFIELD_NAME)
1857
1881
  .op(OP_OUTPUTASSETFIELD) // [ N, name_out1 ]
1858
1882
  .pushBytes(tokenIdBytes)
1859
1883
  .op(OP_EQUALVERIFY); // [ N ]
1860
- // 5. Remainder covenant continuity (output 2): same scriptPubKey as spent
1884
+ // 6. Drop N, leave TRUE on the stack.
1885
+ b.op(OP_DROP).pushInt(1);
1886
+ // ──────── Partial-fill branch (inner ELSE) ────────
1887
+ // scriptSig: <N> <0> <0> ( N, full-flag=0, cancel-flag=0 )
1888
+ // Stack entering: [ N ]
1889
+ b.op(OP_ELSE);
1890
+ // 1. Payment value (output 0) >= N * unitPriceSats
1891
+ b.op(OP_DUP) // [ N, N ]
1892
+ .pushInt(unitPriceSats) // [ N, N, price ]
1893
+ .op(OP_MUL) // [ N, N*price ]
1894
+ .pushInt(0) // [ N, N*price, 0 ]
1895
+ .op(OP_OUTPUTVALUE) // [ N, N*price, value ]
1896
+ .op(OP_SWAP) // [ N, value, N*price ]
1897
+ .op(OP_GREATERTHANOREQUAL)
1898
+ .op(OP_VERIFY); // [ N ]
1899
+ // 2. Payment scriptPubKey (output 0)
1900
+ b.pushInt(0)
1901
+ .op(OP_OUTPUTSCRIPT) // [ N, spk_out0 ]
1902
+ .pushBytes(sellerScriptPubKey)
1903
+ .op(OP_EQUALVERIFY); // [ N ]
1904
+ // 3. Asset to buyer (output 1) amount == N
1905
+ b.op(OP_DUP) // [ N, N ]
1906
+ .pushInt(1)
1907
+ .pushInt(ASSETFIELD_AMOUNT)
1908
+ .op(OP_OUTPUTASSETFIELD)
1909
+ .op(OP_EQUALVERIFY); // [ N ]
1910
+ // 4. Asset to buyer (output 1) name == tokenId
1911
+ b.pushInt(1)
1912
+ .pushInt(ASSETFIELD_NAME)
1913
+ .op(OP_OUTPUTASSETFIELD)
1914
+ .pushBytes(tokenIdBytes)
1915
+ .op(OP_EQUALVERIFY); // [ N ]
1916
+ // 5. Continuation (output 2): same AuthScript commitment as spent UTXO
1917
+ // (NIP-023 — see file-level comment).
1861
1918
  b.pushInt(2)
1862
- .op(OP_OUTPUTSCRIPT) // [ N, spk_out2 ]
1863
- .pushInt(TXFIELD_SCRIPTPUBKEY)
1864
- .op(OP_TXFIELD) // [ N, spk_out2, spent_spk ]
1919
+ .op(OP_OUTPUTAUTHCOMMITMENT)
1920
+ .pushInt(TXFIELD_AUTHSCRIPT_COMMITMENT)
1921
+ .op(OP_TXFIELD)
1865
1922
  .op(OP_EQUALVERIFY); // [ N ]
1866
- // 6. Remainder output: same tokenId
1923
+ // 6. Continuation name == tokenId
1867
1924
  b.pushInt(2)
1868
1925
  .pushInt(ASSETFIELD_NAME)
1869
- .op(OP_OUTPUTASSETFIELD) // [ N, name_out2 ]
1926
+ .op(OP_OUTPUTASSETFIELD)
1870
1927
  .pushBytes(tokenIdBytes)
1871
1928
  .op(OP_EQUALVERIFY); // [ N ]
1872
- // 7. Remainder output: amount == inputAssetAmount - N
1929
+ // 7. Continuation amount == inputAmount - N
1873
1930
  b.pushInt(2)
1874
1931
  .pushInt(ASSETFIELD_AMOUNT)
1875
1932
  .op(OP_OUTPUTASSETFIELD) // [ N, rem_amount ]
1876
- .op(OP_OVER) // [ N, rem_amount, N ] -- copy N under top
1933
+ .op(OP_OVER) // [ N, rem_amount, N ]
1877
1934
  .pushInt(0) // [ N, rem_amount, N, 0 ]
1878
1935
  .pushInt(ASSETFIELD_AMOUNT)
1879
1936
  .op(OP_INPUTASSETFIELD) // [ N, rem_amount, N, in_amount ]
1880
1937
  .op(OP_SWAP) // [ N, rem_amount, in_amount, N ]
1881
1938
  .op(OP_SUB) // [ N, rem_amount, in_amount - N ]
1882
1939
  .op(OP_EQUALVERIFY); // [ N ]
1883
- // Drop N, leave TRUE on the stack so the ELSE branch evaluates as success.
1940
+ // 8. Drop N, leave TRUE on the stack.
1884
1941
  b.op(OP_DROP).pushInt(1);
1885
- b.op(OP_ENDIF);
1942
+ b.op(OP_ENDIF); // closes full/partial inner IF
1943
+ b.op(OP_ENDIF); // closes outer cancel/fill IF
1886
1944
  return b.build();
1887
1945
  }
1888
1946
  /** Hex convenience wrapper for `buildPartialFillScript`. */
@@ -1893,9 +1951,9 @@ var NeuraiScriptsBundle = (function (exports) {
1893
1951
  /**
1894
1952
  * PQ (post-quantum) variant of the Partial-Fill Sell Order covenant.
1895
1953
  *
1896
- * Identical partial-fill branch to the legacy covenant, but the **cancel**
1897
- * branch accepts an ML-DSA-44 signature instead of an ECDSA one. This
1898
- * requires:
1954
+ * Identical fill branches (full + partial) to the legacy covenant, but the
1955
+ * **cancel** branch accepts an ML-DSA-44 signature instead of an ECDSA one.
1956
+ * This requires:
1899
1957
  * - `SCRIPT_VERIFY_CHECKSIGFROMSTACK` (for OP_CSFS) active
1900
1958
  * - `SCRIPT_VERIFY_TXHASH` (for OP_TXHASH) active
1901
1959
  * - NIP-18 (`MAX_PQ_SCRIPT_ELEMENT_SIZE = 3072`) active, so the ~2.4 KB
@@ -1913,6 +1971,9 @@ var NeuraiScriptsBundle = (function (exports) {
1913
1971
  * single-SHA256s its message argument before verification, and OP_TXHASH
1914
1972
  * produces its own 32-byte hash, so the seller computes:
1915
1973
  * `sign(pqSeckey, SHA256(doubleSHA256(selected_tx_fields)))`
1974
+ *
1975
+ * Fill branches are identical in structure to the legacy variant — see
1976
+ * `./script.ts` for the three-branch layout description.
1916
1977
  */
1917
1978
  const ASSET_NAME_MAX = 32;
1918
1979
  const DEFAULT_PQ_TXHASH_SELECTOR = 0xff;
@@ -1963,29 +2024,64 @@ var NeuraiScriptsBundle = (function (exports) {
1963
2024
  const payment = encodeSellerScriptPubKey(paymentAddress);
1964
2025
  const tokenIdBytes = new TextEncoder().encode(tokenId);
1965
2026
  const b = new ScriptBuilder();
1966
- // ───────── Cancel branch (PQ via OP_CHECKSIGFROMSTACK) ─────────
1967
- // scriptSig expected: <sigPQ> <pubKeyPQ> OP_1
2027
+ // ════════ Cancel branch (PQ via OP_CHECKSIGFROMSTACK) ════════
2028
+ // scriptSig: <sigPQ> <pubKeyPQ> <1>
1968
2029
  // After OP_IF consumes the flag: [ sig, pubKey ]
1969
2030
  //
1970
- // The selector MUST be pushed as a raw 1-byte element — consensus
1971
- // (`interpreter.cpp` OP_TXHASH case) rejects any stack item of
1972
- // size ≠ 1. Using `pushInt(selector)` would work for 1..127 but emit a
1973
- // 2-byte CScriptNum for 0x80..0xff (the sign-disambiguation pad), which
1974
- // makes OP_TXHASH fail with SCRIPT_ERR_TXHASH. See
1975
- // `memory/project_covenant_v3_findings.md` bug B for details.
2031
+ // The selector MUST be pushed as a raw 1-byte element — consensus rejects
2032
+ // any stack item of size ≠ 1. Using `pushInt(selector)` would work for
2033
+ // 1..127 but emit a 2-byte CScriptNum for 0x80..0xff (sign-disambiguation
2034
+ // pad), which makes OP_TXHASH fail with SCRIPT_ERR_TXHASH.
1976
2035
  b.op(OP_IF)
1977
2036
  .op(OP_DUP) // [ sig, pubKey, pubKey ]
1978
2037
  .op(OP_SHA256) // [ sig, pubKey, H(pubKey) ]
1979
2038
  .pushBytes(pubKeyCommitment) // [ sig, pubKey, H(pubKey), commitment ]
1980
2039
  .op(OP_EQUALVERIFY) // [ sig, pubKey ]
1981
- .pushBytes(Uint8Array.of(txHashSelector)) // [ sig, pubKey, selector ] — always 1-byte stack item
2040
+ .pushBytes(Uint8Array.of(txHashSelector)) // [ sig, pubKey, selector ]
1982
2041
  .op(OP_TXHASH) // [ sig, pubKey, txHash ]
1983
2042
  .op(OP_SWAP) // [ sig, txHash, pubKey ]
1984
2043
  .op(OP_CHECKSIGFROMSTACK) // [ 1 | 0 ]
1985
2044
  .op(OP_ELSE);
1986
- // ───────── Partial-fill branch (identical layout to legacy) ─────────
1987
- // scriptSig expected: <N> OP_0
1988
- // Stack entering ELSE: [ N ]
2045
+ // ════════ Fill branches (inner IF: full / ELSE: partial) ════════
2046
+ b.op(OP_IF);
2047
+ // ──────── Full-fill branch ────────
2048
+ // scriptSig: <1> <0>
2049
+ // Stack entering: [ ]
2050
+ // 1. N = inputAmount
2051
+ b.pushInt(0)
2052
+ .pushInt(ASSETFIELD_AMOUNT)
2053
+ .op(OP_INPUTASSETFIELD);
2054
+ // 2. Payment value (output 0) >= N * unitPriceSats
2055
+ b.op(OP_DUP)
2056
+ .pushInt(unitPriceSats)
2057
+ .op(OP_MUL)
2058
+ .pushInt(0)
2059
+ .op(OP_OUTPUTVALUE)
2060
+ .op(OP_SWAP)
2061
+ .op(OP_GREATERTHANOREQUAL)
2062
+ .op(OP_VERIFY);
2063
+ // 3. Payment scriptPubKey (output 0)
2064
+ b.pushInt(0)
2065
+ .op(OP_OUTPUTSCRIPT)
2066
+ .pushBytes(payment.bytes)
2067
+ .op(OP_EQUALVERIFY);
2068
+ // 4. Asset to buyer (output 1) amount == N
2069
+ b.op(OP_DUP)
2070
+ .pushInt(1)
2071
+ .pushInt(ASSETFIELD_AMOUNT)
2072
+ .op(OP_OUTPUTASSETFIELD)
2073
+ .op(OP_EQUALVERIFY);
2074
+ // 5. Asset to buyer (output 1) name == tokenId
2075
+ b.pushInt(1)
2076
+ .pushInt(ASSETFIELD_NAME)
2077
+ .op(OP_OUTPUTASSETFIELD)
2078
+ .pushBytes(tokenIdBytes)
2079
+ .op(OP_EQUALVERIFY);
2080
+ b.op(OP_DROP).pushInt(1);
2081
+ // ──────── Partial-fill branch ────────
2082
+ // scriptSig: <N> <0> <0>
2083
+ // Stack entering: [ N ]
2084
+ b.op(OP_ELSE);
1989
2085
  // 1. Payment value (output 0) >= N * unitPriceSats
1990
2086
  b.op(OP_DUP)
1991
2087
  .pushInt(unitPriceSats)
@@ -1995,7 +2091,7 @@ var NeuraiScriptsBundle = (function (exports) {
1995
2091
  .op(OP_SWAP)
1996
2092
  .op(OP_GREATERTHANOREQUAL)
1997
2093
  .op(OP_VERIFY);
1998
- // 2. Payment scriptPubKey (output 0) == payment.bytes
2094
+ // 2. Payment scriptPubKey (output 0)
1999
2095
  b.pushInt(0)
2000
2096
  .op(OP_OUTPUTSCRIPT)
2001
2097
  .pushBytes(payment.bytes)
@@ -2012,19 +2108,19 @@ var NeuraiScriptsBundle = (function (exports) {
2012
2108
  .op(OP_OUTPUTASSETFIELD)
2013
2109
  .pushBytes(tokenIdBytes)
2014
2110
  .op(OP_EQUALVERIFY);
2015
- // 5. Remainder continuity (output 2) — same scriptPubKey as spent
2111
+ // 5. Continuation (output 2): same AuthScript commitment as spent (NIP-023)
2016
2112
  b.pushInt(2)
2017
- .op(OP_OUTPUTSCRIPT)
2018
- .pushInt(TXFIELD_SCRIPTPUBKEY)
2113
+ .op(OP_OUTPUTAUTHCOMMITMENT)
2114
+ .pushInt(TXFIELD_AUTHSCRIPT_COMMITMENT)
2019
2115
  .op(OP_TXFIELD)
2020
2116
  .op(OP_EQUALVERIFY);
2021
- // 6. Remainder tokenId
2117
+ // 6. Continuation name == tokenId
2022
2118
  b.pushInt(2)
2023
2119
  .pushInt(ASSETFIELD_NAME)
2024
2120
  .op(OP_OUTPUTASSETFIELD)
2025
2121
  .pushBytes(tokenIdBytes)
2026
2122
  .op(OP_EQUALVERIFY);
2027
- // 7. Remainder amount == inputAssetAmount - N
2123
+ // 7. Continuation amount == inputAmount - N
2028
2124
  b.pushInt(2)
2029
2125
  .pushInt(ASSETFIELD_AMOUNT)
2030
2126
  .op(OP_OUTPUTASSETFIELD)
@@ -2036,7 +2132,8 @@ var NeuraiScriptsBundle = (function (exports) {
2036
2132
  .op(OP_SUB)
2037
2133
  .op(OP_EQUALVERIFY);
2038
2134
  b.op(OP_DROP).pushInt(1);
2039
- b.op(OP_ENDIF);
2135
+ b.op(OP_ENDIF); // full / partial
2136
+ b.op(OP_ENDIF); // cancel / fill
2040
2137
  return b.build();
2041
2138
  }
2042
2139
  function buildPartialFillScriptPQHex(params) {
@@ -2052,33 +2149,52 @@ var NeuraiScriptsBundle = (function (exports) {
2052
2149
  * with `@neuraiproject/neurai-create-transaction`.
2053
2150
  */
2054
2151
  /**
2055
- * Unlock the covenant via the public partial-fill branch.
2152
+ * Unlock the covenant via the public fill branches.
2056
2153
  *
2057
- * Layout on the stack before OP_IF executes (top → bottom):
2058
- * 0 ← selects the OP_ELSE (fill) branch
2059
- * N ← amount of asset the buyer is taking from this order
2154
+ * amount === total → full-fill branch. The entire covenant drains to
2155
+ * vout[1]; no vout[2] continuation is emitted.
2156
+ * amount < total → partial-fill branch. vout[2] re-locks the
2157
+ * continuation (`total - amount` units).
2060
2158
  *
2061
- * so the scriptSig pushes `<N>` then `<0>`.
2159
+ * Unlock stack shapes (pushed bottom → top):
2062
2160
  *
2063
- * @param amount units of the asset the buyer is taking. Must be > 0 and
2064
- * strictly less than the amount locked in the order UTXO
2065
- * (equal would leave a zero-asset remainder, which isn't a
2066
- * valid Neurai transfer output).
2161
+ * Full fill: <1> <0> ( full-flag=1, cancel-flag=0 )
2162
+ * Partial fill: <N> <0> <0> ( N, full-flag=0, cancel-flag=0 )
2163
+ *
2164
+ * @param amount units the buyer is taking. Must be > 0 and ≤ total.
2165
+ * @param total current asset amount locked in the covenant UTXO. The
2166
+ * builder reads this to decide whether to emit the full-fill
2167
+ * or partial-fill witness — consensus uses `OP_INPUTASSETFIELD`
2168
+ * inside the covenant to check it independently, so the value
2169
+ * passed here must match on-chain reality or the script fails.
2067
2170
  */
2068
- function buildFillScriptSig(amount) {
2069
- if (typeof amount !== 'bigint') {
2070
- throw new Error('amount must be a bigint');
2171
+ function buildFillScriptSig(amount, total) {
2172
+ if (typeof amount !== 'bigint' || typeof total !== 'bigint') {
2173
+ throw new Error('amount and total must be bigint');
2071
2174
  }
2072
2175
  if (amount <= 0n) {
2073
2176
  throw new Error('fill amount must be > 0');
2074
2177
  }
2075
- return new ScriptBuilder()
2076
- .pushInt(amount)
2077
- .pushInt(0)
2078
- .build();
2178
+ if (total <= 0n) {
2179
+ throw new Error('total must be > 0');
2180
+ }
2181
+ if (amount > total) {
2182
+ throw new Error('fill amount exceeds the covenant total');
2183
+ }
2184
+ const b = new ScriptBuilder();
2185
+ if (amount === total) {
2186
+ // Full fill: covenant drains entirely. Buyer does not push N — the
2187
+ // covenant reads it via OP_INPUTASSETFIELD.
2188
+ b.pushInt(1).pushInt(0);
2189
+ }
2190
+ else {
2191
+ // Partial fill: buyer pushes N, then both flag bytes.
2192
+ b.pushInt(amount).pushInt(0).pushInt(0);
2193
+ }
2194
+ return b.build();
2079
2195
  }
2080
- function buildFillScriptSigHex(amount) {
2081
- return bytesToHex(buildFillScriptSig(amount));
2196
+ function buildFillScriptSigHex(amount, total) {
2197
+ return bytesToHex(buildFillScriptSig(amount, total));
2082
2198
  }
2083
2199
  /**
2084
2200
  * Unlock the covenant via the seller's cancel branch.
@@ -2302,13 +2418,17 @@ var NeuraiScriptsBundle = (function (exports) {
2302
2418
  }
2303
2419
 
2304
2420
  /**
2305
- * Parser for the Partial-Fill Sell Order covenant.
2421
+ * Parser for the Partial-Fill Sell Order covenant (three-branch).
2306
2422
  *
2307
2423
  * Extracts `(sellerPubKeyHash, unitPriceSats, tokenId)` from a scriptPubKey
2308
- * that was produced by `buildPartialFillScript`. The parser walks the exact
2309
- * byte layout emitted by the builder and fails on any deviation — this is
2424
+ * that was produced by `buildPartialFillScript`. Walks the exact byte
2425
+ * layout emitted by the builder and fails on any deviation — this is
2310
2426
  * deliberate, so a downstream indexer can unambiguously classify a UTXO as
2311
2427
  * "partial-fill order" or "unknown script" with no false positives.
2428
+ *
2429
+ * The full-fill and partial-fill branches share `(sellerScriptPubKey,
2430
+ * unitPriceSats, tokenId)`. The parser reads both branches and verifies
2431
+ * consistency; inconsistency throws.
2312
2432
  */
2313
2433
  /**
2314
2434
  * Parse a covenant scriptPubKey and extract its parameters. Throws with a
@@ -2317,8 +2437,8 @@ var NeuraiScriptsBundle = (function (exports) {
2317
2437
  function parsePartialFillScript(script, network = 'xna-test') {
2318
2438
  const bytes = typeof script === 'string' ? hexToBytes(script) : script;
2319
2439
  const c = makeCursor(bytes);
2320
- // ───── Cancel branch prefix ─────
2321
- expectByte(c, OP_IF, 'OP_IF');
2440
+ // ═════ Outer IF — Cancel branch ═════
2441
+ expectByte(c, OP_IF, 'OP_IF (outer cancel)');
2322
2442
  expectByte(c, OP_DUP, 'OP_DUP (cancel)');
2323
2443
  expectByte(c, OP_HASH160, 'OP_HASH160');
2324
2444
  const sellerPubKeyHash = readPush(c, 'sellerPubKeyHash');
@@ -2327,86 +2447,139 @@ var NeuraiScriptsBundle = (function (exports) {
2327
2447
  }
2328
2448
  expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (cancel)');
2329
2449
  expectByte(c, OP_CHECKSIG, 'OP_CHECKSIG (cancel)');
2330
- expectByte(c, OP_ELSE, 'OP_ELSE');
2331
- // ───── Payment value check ─────
2332
- expectByte(c, OP_DUP, 'OP_DUP (price)');
2333
- const unitPriceSats = readPushPositiveInt(c, 'unitPriceSats');
2334
- expectByte(c, OP_MUL, 'OP_MUL');
2335
- expectByte(c, OP_0, 'OP_0 (payment idx)');
2336
- expectByte(c, OP_OUTPUTVALUE, 'OP_OUTPUTVALUE');
2337
- expectByte(c, OP_SWAP, 'OP_SWAP');
2338
- expectByte(c, OP_GREATERTHANOREQUAL, 'OP_GREATERTHANOREQUAL');
2339
- expectByte(c, OP_VERIFY, 'OP_VERIFY (payment)');
2340
- // ───── Payment scriptPubKey check ─────
2341
- expectByte(c, OP_0, 'OP_0 (spk idx)');
2342
- expectByte(c, OP_OUTPUTSCRIPT, 'OP_OUTPUTSCRIPT (payment)');
2343
- const sellerSpk = readPush(c, 'sellerScriptPubKey');
2344
- // Must be P2PKH with our PKH.
2450
+ expectByte(c, OP_ELSE, 'OP_ELSE (outer → fill)');
2451
+ // ═════ Inner IF — Full-fill branch ═════
2452
+ expectByte(c, OP_IF, 'OP_IF (inner full-fill)');
2453
+ // N = inputAmount
2454
+ expectByte(c, OP_0, 'OP_0 (input idx, full)');
2455
+ expectByte(c, OP_2, 'OP_2 (AMOUNT sel, full)');
2456
+ expectByte(c, OP_INPUTASSETFIELD, 'OP_INPUTASSETFIELD (full)');
2457
+ // payment value check
2458
+ expectByte(c, OP_DUP, 'OP_DUP (price, full)');
2459
+ const unitPriceSatsFull = readPushPositiveInt(c, 'unitPriceSats (full)');
2460
+ expectByte(c, OP_MUL, 'OP_MUL (full)');
2461
+ expectByte(c, OP_0, 'OP_0 (payment idx, full)');
2462
+ expectByte(c, OP_OUTPUTVALUE, 'OP_OUTPUTVALUE (full)');
2463
+ expectByte(c, OP_SWAP, 'OP_SWAP (full)');
2464
+ expectByte(c, OP_GREATERTHANOREQUAL, 'OP_GREATERTHANOREQUAL (full)');
2465
+ expectByte(c, OP_VERIFY, 'OP_VERIFY (payment full)');
2466
+ // payment spk
2467
+ expectByte(c, OP_0, 'OP_0 (spk idx, full)');
2468
+ expectByte(c, OP_OUTPUTSCRIPT, 'OP_OUTPUTSCRIPT (payment full)');
2469
+ const sellerSpkFull = readPush(c, 'sellerScriptPubKey (full)');
2345
2470
  const expectedSpk = new Uint8Array([
2346
2471
  OP_DUP, OP_HASH160, 0x14, ...sellerPubKeyHash, OP_EQUALVERIFY, OP_CHECKSIG
2347
2472
  ]);
2348
- if (!bytesEqual(sellerSpk, expectedSpk)) {
2349
- throw new Error('parse: embedded seller scriptPubKey does not match the cancel PKH');
2350
- }
2351
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (payment spk)');
2352
- // ───── Buyer asset amount check (output 1) ─────
2353
- expectByte(c, OP_DUP, 'OP_DUP (buyer amount)');
2354
- expectByte(c, OP_1, 'OP_1 (buyer idx)');
2355
- expectByte(c, OP_2, 'OP_2 (AMOUNT selector)');
2356
- expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer amount)');
2357
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer amount)');
2358
- // ───── Buyer asset name check (output 1) ─────
2359
- expectByte(c, OP_1, 'OP_1 (buyer idx)');
2360
- expectByte(c, OP_1, 'OP_1 (NAME selector)');
2361
- expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer name)');
2362
- const tokenIdBytes1 = readPush(c, 'tokenId #1');
2363
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer name)');
2364
- // ───── Remainder continuity: same scriptPubKey ─────
2473
+ if (!bytesEqual(sellerSpkFull, expectedSpk)) {
2474
+ throw new Error('parse: full-fill seller scriptPubKey does not match the cancel PKH');
2475
+ }
2476
+ expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (payment spk full)');
2477
+ // buyer amount == N (full)
2478
+ expectByte(c, OP_DUP, 'OP_DUP (buyer amount, full)');
2479
+ expectByte(c, OP_1, 'OP_1 (buyer idx, full)');
2480
+ expectByte(c, OP_2, 'OP_2 (AMOUNT sel, full)');
2481
+ expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer amount, full)');
2482
+ expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer amount, full)');
2483
+ // buyer name == tokenId (full)
2484
+ expectByte(c, OP_1, 'OP_1 (buyer idx, full)');
2485
+ expectByte(c, OP_1, 'OP_1 (NAME sel, full)');
2486
+ expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer name, full)');
2487
+ const tokenIdFull = readPush(c, 'tokenId (full)');
2488
+ expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer name, full)');
2489
+ // tail (full)
2490
+ expectByte(c, OP_DROP, 'OP_DROP (full)');
2491
+ expectByte(c, OP_1, 'OP_1 (true, full)');
2492
+ expectByte(c, OP_ELSE, 'OP_ELSE (inner → partial fill)');
2493
+ // ═════ Inner ELSE — Partial-fill branch ═════
2494
+ // Payment value
2495
+ expectByte(c, OP_DUP, 'OP_DUP (price, partial)');
2496
+ const unitPriceSatsPartial = readPushPositiveInt(c, 'unitPriceSats (partial)');
2497
+ expectByte(c, OP_MUL, 'OP_MUL (partial)');
2498
+ expectByte(c, OP_0, 'OP_0 (pay idx, partial)');
2499
+ expectByte(c, OP_OUTPUTVALUE, 'OP_OUTPUTVALUE (partial)');
2500
+ expectByte(c, OP_SWAP, 'OP_SWAP (partial)');
2501
+ expectByte(c, OP_GREATERTHANOREQUAL, 'OP_GREATERTHANOREQUAL (partial)');
2502
+ expectByte(c, OP_VERIFY, 'OP_VERIFY (payment partial)');
2503
+ // Payment spk
2504
+ expectByte(c, OP_0, 'OP_0 (spk idx, partial)');
2505
+ expectByte(c, OP_OUTPUTSCRIPT, 'OP_OUTPUTSCRIPT (payment partial)');
2506
+ const sellerSpkPartial = readPush(c, 'sellerScriptPubKey (partial)');
2507
+ if (!bytesEqual(sellerSpkPartial, expectedSpk)) {
2508
+ throw new Error('parse: partial-fill seller scriptPubKey does not match the cancel PKH');
2509
+ }
2510
+ expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (payment spk partial)');
2511
+ // Buyer amount
2512
+ expectByte(c, OP_DUP, 'OP_DUP (buyer amount, partial)');
2513
+ expectByte(c, OP_1, 'OP_1 (buyer idx, partial)');
2514
+ expectByte(c, OP_2, 'OP_2 (AMOUNT sel, partial)');
2515
+ expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer amount, partial)');
2516
+ expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer amount, partial)');
2517
+ // Buyer name
2518
+ expectByte(c, OP_1, 'OP_1 (buyer idx, partial)');
2519
+ expectByte(c, OP_1, 'OP_1 (NAME sel, partial)');
2520
+ expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer name, partial)');
2521
+ const tokenIdPartial1 = readPush(c, 'tokenId partial #1');
2522
+ expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer name, partial)');
2523
+ // Continuation commitment
2365
2524
  expectByte(c, OP_2, 'OP_2 (remainder idx)');
2366
- expectByte(c, OP_OUTPUTSCRIPT, 'OP_OUTPUTSCRIPT (remainder)');
2367
- expectByte(c, OP_3, 'OP_3 (TXFIELD selector)');
2525
+ expectByte(c, OP_OUTPUTAUTHCOMMITMENT, 'OP_OUTPUTAUTHCOMMITMENT (remainder)');
2526
+ expectByte(c, OP_2, 'OP_2 (TXFIELD sel: AUTHSCRIPT_COMMITMENT)');
2368
2527
  expectByte(c, OP_TXFIELD, 'OP_TXFIELD');
2369
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder spk)');
2370
- // ───── Remainder tokenId check ─────
2528
+ expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder auth)');
2529
+ // Continuation name
2371
2530
  expectByte(c, OP_2, 'OP_2 (remainder idx)');
2372
- expectByte(c, OP_1, 'OP_1 (NAME selector)');
2531
+ expectByte(c, OP_1, 'OP_1 (NAME sel)');
2373
2532
  expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (remainder name)');
2374
- const tokenIdBytes2 = readPush(c, 'tokenId #2');
2533
+ const tokenIdPartial2 = readPush(c, 'tokenId partial #2');
2375
2534
  expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder name)');
2376
- // ───── Remainder amount == input amount - N ─────
2535
+ // Continuation amount
2377
2536
  expectByte(c, OP_2, 'OP_2 (remainder idx)');
2378
- expectByte(c, OP_2, 'OP_2 (AMOUNT selector)');
2537
+ expectByte(c, OP_2, 'OP_2 (AMOUNT sel)');
2379
2538
  expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (remainder amount)');
2380
2539
  expectByte(c, OP_OVER, 'OP_OVER');
2381
2540
  expectByte(c, OP_0, 'OP_0 (input idx)');
2382
- expectByte(c, OP_2, 'OP_2 (AMOUNT selector)');
2541
+ expectByte(c, OP_2, 'OP_2 (AMOUNT sel)');
2383
2542
  expectByte(c, OP_INPUTASSETFIELD, 'OP_INPUTASSETFIELD');
2384
2543
  expectByte(c, OP_SWAP, 'OP_SWAP');
2385
2544
  expectByte(c, OP_SUB, 'OP_SUB');
2386
2545
  expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder amount)');
2387
- // ───── Tail ─────
2388
- expectByte(c, OP_DROP, 'OP_DROP');
2389
- expectByte(c, OP_1, 'OP_1 (true)');
2390
- expectByte(c, OP_ENDIF, 'OP_ENDIF');
2546
+ // Tail (partial)
2547
+ expectByte(c, OP_DROP, 'OP_DROP (partial)');
2548
+ expectByte(c, OP_1, 'OP_1 (true, partial)');
2549
+ // Close the nested structure
2550
+ expectByte(c, OP_ENDIF, 'OP_ENDIF (inner)');
2551
+ expectByte(c, OP_ENDIF, 'OP_ENDIF (outer)');
2391
2552
  assertTrailing(c);
2392
- if (!bytesEqual(tokenIdBytes1, tokenIdBytes2)) {
2393
- throw new Error('parse: tokenId bytes differ between buyer and remainder checks');
2553
+ // Cross-branch consistency: all three (full token, partial name #1,
2554
+ // partial name #2) must agree, and the two unit prices must match.
2555
+ if (!bytesEqual(tokenIdFull, tokenIdPartial1)) {
2556
+ throw new Error('parse: tokenId differs between full-fill and partial-fill branches');
2557
+ }
2558
+ if (!bytesEqual(tokenIdPartial1, tokenIdPartial2)) {
2559
+ throw new Error('parse: tokenId bytes differ between buyer and remainder partial checks');
2394
2560
  }
2395
- const tokenId = new TextDecoder('utf-8', { fatal: true }).decode(tokenIdBytes1);
2561
+ if (unitPriceSatsFull !== unitPriceSatsPartial) {
2562
+ throw new Error('parse: unitPriceSats differs between full-fill and partial-fill branches');
2563
+ }
2564
+ const tokenId = new TextDecoder('utf-8', { fatal: true }).decode(tokenIdFull);
2396
2565
  return {
2397
2566
  network,
2398
2567
  sellerPubKeyHash,
2399
- unitPriceSats,
2568
+ unitPriceSats: unitPriceSatsFull,
2400
2569
  tokenId,
2401
2570
  scriptHex: bytesToHex(bytes)
2402
2571
  };
2403
2572
  }
2404
2573
 
2405
2574
  /**
2406
- * Parser for the PQ Partial-Fill Sell Order covenant. Returns the same
2407
- * economic parameters as the legacy parser, plus the payment scriptPubKey
2408
- * bytes (which may be P2PKH or AuthScript) and the configured TXHASH
2409
- * selector.
2575
+ * Parser for the PQ Partial-Fill Sell Order covenant (three-branch).
2576
+ * Returns the same economic parameters as the legacy parser, plus the
2577
+ * payment scriptPubKey bytes (which may be P2PKH or AuthScript) and the
2578
+ * configured TXHASH selector.
2579
+ *
2580
+ * The full-fill and partial-fill branches share `(paymentScriptPubKey,
2581
+ * unitPriceSats, tokenId)`. The parser reads both branches and verifies
2582
+ * consistency; inconsistency throws.
2410
2583
  */
2411
2584
  /** Quick discriminator without throwing — useful for indexers. */
2412
2585
  function isPartialFillScriptPQ(script) {
@@ -2424,8 +2597,8 @@ var NeuraiScriptsBundle = (function (exports) {
2424
2597
  function parsePartialFillScriptPQ(script, network = 'xna-test') {
2425
2598
  const bytes = typeof script === 'string' ? hexToBytes(script) : script;
2426
2599
  const c = makeCursor(bytes);
2427
- // ───── Cancel branch (PQ) ─────
2428
- expectByte(c, OP_IF, 'OP_IF');
2600
+ // ═════ Outer IF — Cancel branch (PQ) ═════
2601
+ expectByte(c, OP_IF, 'OP_IF (outer cancel)');
2429
2602
  expectByte(c, OP_DUP, 'OP_DUP (cancel)');
2430
2603
  expectByte(c, OP_SHA256, 'OP_SHA256');
2431
2604
  const pubKeyCommitment = readPush(c, 'pubKeyCommitment');
@@ -2433,9 +2606,6 @@ var NeuraiScriptsBundle = (function (exports) {
2433
2606
  throw new Error(`parse-pq: pubKeyCommitment must be 32 bytes, got ${pubKeyCommitment.length}`);
2434
2607
  }
2435
2608
  expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (cancel)');
2436
- // Selector is read as an unsigned byte — consensus OP_TXHASH treats the
2437
- // on-stack element as uint8, and the builder emits a raw 1-byte push so
2438
- // selectors 0x80..0xff round-trip correctly (plan v3 bug B).
2439
2609
  const txHashSelector = readPushUint8(c, 'txHashSelector');
2440
2610
  if (txHashSelector < 1) {
2441
2611
  throw new Error(`parse-pq: txHashSelector 0x00 is rejected by OP_TXHASH`);
@@ -2443,65 +2613,107 @@ var NeuraiScriptsBundle = (function (exports) {
2443
2613
  expectByte(c, OP_TXHASH, 'OP_TXHASH');
2444
2614
  expectByte(c, OP_SWAP, 'OP_SWAP');
2445
2615
  expectByte(c, OP_CHECKSIGFROMSTACK, 'OP_CHECKSIGFROMSTACK');
2446
- expectByte(c, OP_ELSE, 'OP_ELSE');
2447
- // ───── Fill branch ─────
2448
- expectByte(c, OP_DUP, 'OP_DUP (price)');
2449
- const unitPriceSats = readPushPositiveInt(c, 'unitPriceSats');
2450
- expectByte(c, OP_MUL, 'OP_MUL');
2451
- expectByte(c, OP_0, 'OP_0 (payment idx)');
2452
- expectByte(c, OP_OUTPUTVALUE, 'OP_OUTPUTVALUE');
2453
- expectByte(c, OP_SWAP, 'OP_SWAP');
2454
- expectByte(c, OP_GREATERTHANOREQUAL, 'OP_GE');
2455
- expectByte(c, OP_VERIFY, 'OP_VERIFY');
2456
- expectByte(c, OP_0, 'OP_0 (payment spk idx)');
2457
- expectByte(c, OP_OUTPUTSCRIPT, 'OP_OUTPUTSCRIPT (payment)');
2458
- const paymentScriptPubKey = readPush(c, 'paymentScriptPubKey');
2459
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (payment)');
2460
- expectByte(c, OP_DUP, 'OP_DUP (buyer amount)');
2461
- expectByte(c, OP_1, 'OP_1 (buyer idx)');
2462
- expectByte(c, OP_2, 'OP_2 (AMOUNT selector)');
2463
- expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer amount)');
2464
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer amount)');
2465
- expectByte(c, OP_1, 'OP_1 (buyer idx)');
2466
- expectByte(c, OP_1, 'OP_1 (NAME selector)');
2467
- expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer name)');
2468
- const tokenIdBytes1 = readPush(c, 'tokenId #1');
2469
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer name)');
2616
+ expectByte(c, OP_ELSE, 'OP_ELSE (outer → fill)');
2617
+ // ═════ Inner IF — Full-fill branch ═════
2618
+ expectByte(c, OP_IF, 'OP_IF (inner full-fill)');
2619
+ expectByte(c, OP_0, 'OP_0 (input idx, full)');
2620
+ expectByte(c, OP_2, 'OP_2 (AMOUNT sel, full)');
2621
+ expectByte(c, OP_INPUTASSETFIELD, 'OP_INPUTASSETFIELD (full)');
2622
+ expectByte(c, OP_DUP, 'OP_DUP (price, full)');
2623
+ const unitPriceSatsFull = readPushPositiveInt(c, 'unitPriceSats (full)');
2624
+ expectByte(c, OP_MUL, 'OP_MUL (full)');
2625
+ expectByte(c, OP_0, 'OP_0 (payment idx, full)');
2626
+ expectByte(c, OP_OUTPUTVALUE, 'OP_OUTPUTVALUE (full)');
2627
+ expectByte(c, OP_SWAP, 'OP_SWAP (full)');
2628
+ expectByte(c, OP_GREATERTHANOREQUAL, 'OP_GE (full)');
2629
+ expectByte(c, OP_VERIFY, 'OP_VERIFY (full)');
2630
+ expectByte(c, OP_0, 'OP_0 (payment spk idx, full)');
2631
+ expectByte(c, OP_OUTPUTSCRIPT, 'OP_OUTPUTSCRIPT (payment full)');
2632
+ const paymentScriptPubKeyFull = readPush(c, 'paymentScriptPubKey (full)');
2633
+ expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (payment full)');
2634
+ expectByte(c, OP_DUP, 'OP_DUP (buyer amount, full)');
2635
+ expectByte(c, OP_1, 'OP_1 (buyer idx, full)');
2636
+ expectByte(c, OP_2, 'OP_2 (AMOUNT sel, full)');
2637
+ expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer amount, full)');
2638
+ expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer amount, full)');
2639
+ expectByte(c, OP_1, 'OP_1 (buyer idx, full)');
2640
+ expectByte(c, OP_1, 'OP_1 (NAME sel, full)');
2641
+ expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer name, full)');
2642
+ const tokenIdFull = readPush(c, 'tokenId (full)');
2643
+ expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer name, full)');
2644
+ expectByte(c, OP_DROP, 'OP_DROP (full)');
2645
+ expectByte(c, OP_1, 'OP_1 (true, full)');
2646
+ expectByte(c, OP_ELSE, 'OP_ELSE (inner → partial fill)');
2647
+ // ═════ Inner ELSE — Partial-fill branch ═════
2648
+ expectByte(c, OP_DUP, 'OP_DUP (price, partial)');
2649
+ const unitPriceSatsPartial = readPushPositiveInt(c, 'unitPriceSats (partial)');
2650
+ expectByte(c, OP_MUL, 'OP_MUL (partial)');
2651
+ expectByte(c, OP_0, 'OP_0 (payment idx, partial)');
2652
+ expectByte(c, OP_OUTPUTVALUE, 'OP_OUTPUTVALUE (partial)');
2653
+ expectByte(c, OP_SWAP, 'OP_SWAP (partial)');
2654
+ expectByte(c, OP_GREATERTHANOREQUAL, 'OP_GE (partial)');
2655
+ expectByte(c, OP_VERIFY, 'OP_VERIFY (partial)');
2656
+ expectByte(c, OP_0, 'OP_0 (payment spk idx, partial)');
2657
+ expectByte(c, OP_OUTPUTSCRIPT, 'OP_OUTPUTSCRIPT (payment partial)');
2658
+ const paymentScriptPubKeyPartial = readPush(c, 'paymentScriptPubKey (partial)');
2659
+ expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (payment partial)');
2660
+ expectByte(c, OP_DUP, 'OP_DUP (buyer amount, partial)');
2661
+ expectByte(c, OP_1, 'OP_1 (buyer idx, partial)');
2662
+ expectByte(c, OP_2, 'OP_2 (AMOUNT sel, partial)');
2663
+ expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer amount, partial)');
2664
+ expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer amount, partial)');
2665
+ expectByte(c, OP_1, 'OP_1 (buyer idx, partial)');
2666
+ expectByte(c, OP_1, 'OP_1 (NAME sel, partial)');
2667
+ expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer name, partial)');
2668
+ const tokenIdPartial1 = readPush(c, 'tokenId partial #1');
2669
+ expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer name, partial)');
2670
+ // Continuation commitment
2470
2671
  expectByte(c, OP_2, 'OP_2 (remainder idx)');
2471
- expectByte(c, OP_OUTPUTSCRIPT, 'OP_OUTPUTSCRIPT (remainder)');
2472
- expectByte(c, OP_3, 'OP_3 (TXFIELD selector)');
2672
+ expectByte(c, OP_OUTPUTAUTHCOMMITMENT, 'OP_OUTPUTAUTHCOMMITMENT (remainder)');
2673
+ expectByte(c, OP_2, 'OP_2 (TXFIELD sel: AUTHSCRIPT_COMMITMENT)');
2473
2674
  expectByte(c, OP_TXFIELD, 'OP_TXFIELD');
2474
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder spk)');
2675
+ expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder auth)');
2475
2676
  expectByte(c, OP_2, 'OP_2 (remainder idx)');
2476
- expectByte(c, OP_1, 'OP_1 (NAME selector)');
2677
+ expectByte(c, OP_1, 'OP_1 (NAME sel)');
2477
2678
  expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (remainder name)');
2478
- const tokenIdBytes2 = readPush(c, 'tokenId #2');
2679
+ const tokenIdPartial2 = readPush(c, 'tokenId partial #2');
2479
2680
  expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder name)');
2480
2681
  expectByte(c, OP_2, 'OP_2 (remainder idx)');
2481
- expectByte(c, OP_2, 'OP_2 (AMOUNT selector)');
2682
+ expectByte(c, OP_2, 'OP_2 (AMOUNT sel)');
2482
2683
  expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (remainder amount)');
2483
2684
  expectByte(c, OP_OVER, 'OP_OVER');
2484
2685
  expectByte(c, OP_0, 'OP_0 (input idx)');
2485
- expectByte(c, OP_2, 'OP_2 (AMOUNT selector)');
2686
+ expectByte(c, OP_2, 'OP_2 (AMOUNT sel)');
2486
2687
  expectByte(c, OP_INPUTASSETFIELD, 'OP_INPUTASSETFIELD');
2487
2688
  expectByte(c, OP_SWAP, 'OP_SWAP');
2488
2689
  expectByte(c, OP_SUB, 'OP_SUB');
2489
2690
  expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder amount)');
2490
- expectByte(c, OP_DROP, 'OP_DROP');
2491
- expectByte(c, OP_1, 'OP_1 (true)');
2492
- expectByte(c, OP_ENDIF, 'OP_ENDIF');
2691
+ expectByte(c, OP_DROP, 'OP_DROP (partial)');
2692
+ expectByte(c, OP_1, 'OP_1 (true, partial)');
2693
+ expectByte(c, OP_ENDIF, 'OP_ENDIF (inner)');
2694
+ expectByte(c, OP_ENDIF, 'OP_ENDIF (outer)');
2493
2695
  assertTrailing(c);
2494
- if (!bytesEqual(tokenIdBytes1, tokenIdBytes2)) {
2495
- throw new Error('parse-pq: tokenId differs between buyer and remainder checks');
2696
+ // Cross-branch consistency.
2697
+ if (!bytesEqual(paymentScriptPubKeyFull, paymentScriptPubKeyPartial)) {
2698
+ throw new Error('parse-pq: paymentScriptPubKey differs between full-fill and partial-fill branches');
2699
+ }
2700
+ if (!bytesEqual(tokenIdFull, tokenIdPartial1)) {
2701
+ throw new Error('parse-pq: tokenId differs between full-fill and partial-fill branches');
2702
+ }
2703
+ if (!bytesEqual(tokenIdPartial1, tokenIdPartial2)) {
2704
+ throw new Error('parse-pq: tokenId differs between buyer and remainder partial checks');
2705
+ }
2706
+ if (unitPriceSatsFull !== unitPriceSatsPartial) {
2707
+ throw new Error('parse-pq: unitPriceSats differs between full-fill and partial-fill branches');
2496
2708
  }
2497
- const tokenId = new TextDecoder('utf-8', { fatal: true }).decode(tokenIdBytes1);
2709
+ const tokenId = new TextDecoder('utf-8', { fatal: true }).decode(tokenIdFull);
2498
2710
  return {
2499
2711
  network,
2500
2712
  pubKeyCommitment,
2501
2713
  tokenId,
2502
- unitPriceSats,
2714
+ unitPriceSats: unitPriceSatsFull,
2503
2715
  txHashSelector,
2504
- paymentScriptPubKey,
2716
+ paymentScriptPubKey: paymentScriptPubKeyFull,
2505
2717
  scriptHex: bytesToHex(bytes)
2506
2718
  };
2507
2719
  }