@neuraiproject/neurai-scripts 0.4.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.
@@ -1747,35 +1747,42 @@ var NeuraiScriptsBundle = (function (exports) {
1747
1747
  }
1748
1748
 
1749
1749
  /**
1750
- * Partial-Fill Sell Order covenant script.
1750
+ * Partial-Fill Sell Order covenant script (three-branch).
1751
1751
  *
1752
- * 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:
1753
1753
  *
1754
- * OP_IF (unlock pushed 1) → Cancel: seller signs, recovers remainder.
1755
- * OP_ELSE (unlock pushed 0) → Public partial fill: buyer provides the
1756
- * fill amount `N` and the tx layout is
1757
- * 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.
1758
1760
  *
1759
- * The partial-fill branch enforces a fixed output layout so that the script
1760
- * stays compact and parseable:
1761
+ * Output layout:
1761
1762
  *
1762
1763
  * output[0] = XNA payment to seller (value >= N * unitPriceSats)
1763
1764
  * output[1] = asset to buyer (tokenId, amount == N)
1764
- * output[2] = covenant remainder (same AuthScript commitment, amount == in - N)
1765
+ * output[2] = covenant continuation (only for partial fill;
1766
+ * same AuthScript commitment,
1767
+ * amount == inputAmount - N)
1765
1768
  * output[3+] = optional buyer change (not constrained by the covenant)
1766
1769
  *
1767
- * The remainder UTXO reuses the spent covenant's AuthScript v1 commitment
1768
- * via `OP_OUTPUTAUTHCOMMITMENT(2) == OP_TXFIELD(0x02)` (NIP-023). Comparing
1769
- * commitments rather than full scriptPubKeys is mandatory here: the
1770
- * remainder output's asset wrapper (`OP_XNA_ASSET ... OP_DROP`) encodes a
1771
- * smaller `amountRaw` than the spent UTXO's wrapper, so the full-spk
1772
- * equality used by earlier drafts was vacuously unsatisfiable on any
1773
- * asset-wrapped covenant UTXO.
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)
1774
1781
  *
1775
1782
  * The cancel branch uses classical ECDSA (`OP_HASH160 + OP_CHECKSIG`) and
1776
- * commits to a 20-byte PKH. As a consequence this variant only accepts
1777
- * legacy P2PKH addresses as the seller destination. For AuthScript bech32m
1778
- * 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`.
1779
1786
  */
1780
1787
  const ASSET_NAME_MAX$1 = 32;
1781
1788
  function decodeSellerAddress(sellerAddress) {
@@ -1811,8 +1818,6 @@ var NeuraiScriptsBundle = (function (exports) {
1811
1818
  if (priceSats <= 0n) {
1812
1819
  throw new Error('unitPriceSats must be > 0');
1813
1820
  }
1814
- // 8-byte signed int ceiling: values beyond this cannot be produced by OP_MUL
1815
- // inside the covenant without overflow (CScriptNum is int64).
1816
1821
  if (priceSats > 0x7fffffffffffffffn) {
1817
1822
  throw new Error('unitPriceSats exceeds int64 range');
1818
1823
  }
@@ -1829,19 +1834,28 @@ var NeuraiScriptsBundle = (function (exports) {
1829
1834
  const sellerScriptPubKey = encodeP2PKHScriptPubKey(sellerPubKeyHash);
1830
1835
  const tokenIdBytes = new TextEncoder().encode(tokenId);
1831
1836
  const b = new ScriptBuilder();
1832
- // ───────── Cancel branch (IF) ─────────
1833
- // scriptSig expected: <sig> <pubkey> OP_1
1837
+ // ════════ Cancel branch (outer IF) ════════
1838
+ // scriptSig: <sig> <pubkey> <1>
1834
1839
  b.op(OP_IF)
1835
1840
  .op(OP_DUP, OP_HASH160)
1836
1841
  .pushBytes(sellerPubKeyHash)
1837
1842
  .op(OP_EQUALVERIFY, OP_CHECKSIG)
1838
1843
  .op(OP_ELSE);
1839
- // ───────── Partial-fill branch (ELSE) ─────────
1840
- // 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: [ ]
1841
1849
  //
1842
- // Stack invariant entering the ELSE branch: [ N ]
1843
- // 1. Payment value to seller (output 0)
1844
- // 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
1845
1859
  b.op(OP_DUP) // [ N, N ]
1846
1860
  .pushInt(unitPriceSats) // [ N, N, price ]
1847
1861
  .op(OP_MUL) // [ N, N*price ]
@@ -1850,52 +1864,83 @@ var NeuraiScriptsBundle = (function (exports) {
1850
1864
  .op(OP_SWAP) // [ N, value, N*price ]
1851
1865
  .op(OP_GREATERTHANOREQUAL)
1852
1866
  .op(OP_VERIFY); // [ N ]
1853
- // 2. Payment scriptPubKey must equal Alice's (output 0)
1867
+ // 3. output[0].scriptPubKey == sellerScriptPubKey
1854
1868
  b.pushInt(0)
1855
- .op(OP_OUTPUTSCRIPT) // [ N, scriptPubKey_out0 ]
1869
+ .op(OP_OUTPUTSCRIPT) // [ N, spk_out0 ]
1856
1870
  .pushBytes(sellerScriptPubKey)
1857
1871
  .op(OP_EQUALVERIFY); // [ N ]
1858
- // 3. Asset delivered to buyer (output 1): amount == N
1872
+ // 4. output[1].asset.amount == N
1859
1873
  b.op(OP_DUP) // [ N, N ]
1860
- .pushInt(1) // [ N, N, 1 ]
1874
+ .pushInt(1)
1861
1875
  .pushInt(ASSETFIELD_AMOUNT)
1862
1876
  .op(OP_OUTPUTASSETFIELD) // [ N, N, amount_out1 ]
1863
1877
  .op(OP_EQUALVERIFY); // [ N ]
1864
- // 4. Asset delivered to buyer (output 1): name == tokenId
1878
+ // 5. output[1].asset.name == tokenId
1865
1879
  b.pushInt(1)
1866
1880
  .pushInt(ASSETFIELD_NAME)
1867
1881
  .op(OP_OUTPUTASSETFIELD) // [ N, name_out1 ]
1868
1882
  .pushBytes(tokenIdBytes)
1869
1883
  .op(OP_EQUALVERIFY); // [ N ]
1870
- // 5. Remainder covenant continuity (output 2): same AuthScript commitment
1871
- // as spent. NIP-023: comparing 32-byte commitments rather than full
1872
- // scriptPubKeys, because the remainder's asset wrapper carries a
1873
- // different `amountRaw` than the spent UTXO's wrapper.
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).
1874
1918
  b.pushInt(2)
1875
- .op(OP_OUTPUTAUTHCOMMITMENT) // [ N, auth_out2 ]
1919
+ .op(OP_OUTPUTAUTHCOMMITMENT)
1876
1920
  .pushInt(TXFIELD_AUTHSCRIPT_COMMITMENT)
1877
- .op(OP_TXFIELD) // [ N, auth_out2, spent_auth ]
1921
+ .op(OP_TXFIELD)
1878
1922
  .op(OP_EQUALVERIFY); // [ N ]
1879
- // 6. Remainder output: same tokenId
1923
+ // 6. Continuation name == tokenId
1880
1924
  b.pushInt(2)
1881
1925
  .pushInt(ASSETFIELD_NAME)
1882
- .op(OP_OUTPUTASSETFIELD) // [ N, name_out2 ]
1926
+ .op(OP_OUTPUTASSETFIELD)
1883
1927
  .pushBytes(tokenIdBytes)
1884
1928
  .op(OP_EQUALVERIFY); // [ N ]
1885
- // 7. Remainder output: amount == inputAssetAmount - N
1929
+ // 7. Continuation amount == inputAmount - N
1886
1930
  b.pushInt(2)
1887
1931
  .pushInt(ASSETFIELD_AMOUNT)
1888
1932
  .op(OP_OUTPUTASSETFIELD) // [ N, rem_amount ]
1889
- .op(OP_OVER) // [ N, rem_amount, N ] -- copy N under top
1933
+ .op(OP_OVER) // [ N, rem_amount, N ]
1890
1934
  .pushInt(0) // [ N, rem_amount, N, 0 ]
1891
1935
  .pushInt(ASSETFIELD_AMOUNT)
1892
1936
  .op(OP_INPUTASSETFIELD) // [ N, rem_amount, N, in_amount ]
1893
1937
  .op(OP_SWAP) // [ N, rem_amount, in_amount, N ]
1894
1938
  .op(OP_SUB) // [ N, rem_amount, in_amount - N ]
1895
1939
  .op(OP_EQUALVERIFY); // [ N ]
1896
- // Drop N, leave TRUE on the stack so the ELSE branch evaluates as success.
1940
+ // 8. Drop N, leave TRUE on the stack.
1897
1941
  b.op(OP_DROP).pushInt(1);
1898
- b.op(OP_ENDIF);
1942
+ b.op(OP_ENDIF); // closes full/partial inner IF
1943
+ b.op(OP_ENDIF); // closes outer cancel/fill IF
1899
1944
  return b.build();
1900
1945
  }
1901
1946
  /** Hex convenience wrapper for `buildPartialFillScript`. */
@@ -1906,9 +1951,9 @@ var NeuraiScriptsBundle = (function (exports) {
1906
1951
  /**
1907
1952
  * PQ (post-quantum) variant of the Partial-Fill Sell Order covenant.
1908
1953
  *
1909
- * Identical partial-fill branch to the legacy covenant, but the **cancel**
1910
- * branch accepts an ML-DSA-44 signature instead of an ECDSA one. This
1911
- * 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:
1912
1957
  * - `SCRIPT_VERIFY_CHECKSIGFROMSTACK` (for OP_CSFS) active
1913
1958
  * - `SCRIPT_VERIFY_TXHASH` (for OP_TXHASH) active
1914
1959
  * - NIP-18 (`MAX_PQ_SCRIPT_ELEMENT_SIZE = 3072`) active, so the ~2.4 KB
@@ -1926,6 +1971,9 @@ var NeuraiScriptsBundle = (function (exports) {
1926
1971
  * single-SHA256s its message argument before verification, and OP_TXHASH
1927
1972
  * produces its own 32-byte hash, so the seller computes:
1928
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.
1929
1977
  */
1930
1978
  const ASSET_NAME_MAX = 32;
1931
1979
  const DEFAULT_PQ_TXHASH_SELECTOR = 0xff;
@@ -1976,29 +2024,64 @@ var NeuraiScriptsBundle = (function (exports) {
1976
2024
  const payment = encodeSellerScriptPubKey(paymentAddress);
1977
2025
  const tokenIdBytes = new TextEncoder().encode(tokenId);
1978
2026
  const b = new ScriptBuilder();
1979
- // ───────── Cancel branch (PQ via OP_CHECKSIGFROMSTACK) ─────────
1980
- // scriptSig expected: <sigPQ> <pubKeyPQ> OP_1
2027
+ // ════════ Cancel branch (PQ via OP_CHECKSIGFROMSTACK) ════════
2028
+ // scriptSig: <sigPQ> <pubKeyPQ> <1>
1981
2029
  // After OP_IF consumes the flag: [ sig, pubKey ]
1982
2030
  //
1983
- // The selector MUST be pushed as a raw 1-byte element — consensus
1984
- // (`interpreter.cpp` OP_TXHASH case) rejects any stack item of
1985
- // size ≠ 1. Using `pushInt(selector)` would work for 1..127 but emit a
1986
- // 2-byte CScriptNum for 0x80..0xff (the sign-disambiguation pad), which
1987
- // makes OP_TXHASH fail with SCRIPT_ERR_TXHASH. See
1988
- // `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.
1989
2035
  b.op(OP_IF)
1990
2036
  .op(OP_DUP) // [ sig, pubKey, pubKey ]
1991
2037
  .op(OP_SHA256) // [ sig, pubKey, H(pubKey) ]
1992
2038
  .pushBytes(pubKeyCommitment) // [ sig, pubKey, H(pubKey), commitment ]
1993
2039
  .op(OP_EQUALVERIFY) // [ sig, pubKey ]
1994
- .pushBytes(Uint8Array.of(txHashSelector)) // [ sig, pubKey, selector ] — always 1-byte stack item
2040
+ .pushBytes(Uint8Array.of(txHashSelector)) // [ sig, pubKey, selector ]
1995
2041
  .op(OP_TXHASH) // [ sig, pubKey, txHash ]
1996
2042
  .op(OP_SWAP) // [ sig, txHash, pubKey ]
1997
2043
  .op(OP_CHECKSIGFROMSTACK) // [ 1 | 0 ]
1998
2044
  .op(OP_ELSE);
1999
- // ───────── Partial-fill branch (identical layout to legacy) ─────────
2000
- // scriptSig expected: <N> OP_0
2001
- // 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);
2002
2085
  // 1. Payment value (output 0) >= N * unitPriceSats
2003
2086
  b.op(OP_DUP)
2004
2087
  .pushInt(unitPriceSats)
@@ -2008,7 +2091,7 @@ var NeuraiScriptsBundle = (function (exports) {
2008
2091
  .op(OP_SWAP)
2009
2092
  .op(OP_GREATERTHANOREQUAL)
2010
2093
  .op(OP_VERIFY);
2011
- // 2. Payment scriptPubKey (output 0) == payment.bytes
2094
+ // 2. Payment scriptPubKey (output 0)
2012
2095
  b.pushInt(0)
2013
2096
  .op(OP_OUTPUTSCRIPT)
2014
2097
  .pushBytes(payment.bytes)
@@ -2025,21 +2108,19 @@ var NeuraiScriptsBundle = (function (exports) {
2025
2108
  .op(OP_OUTPUTASSETFIELD)
2026
2109
  .pushBytes(tokenIdBytes)
2027
2110
  .op(OP_EQUALVERIFY);
2028
- // 5. Remainder continuity (output 2) — same AuthScript commitment as spent
2029
- // (NIP-023; see script.ts for why full-spk equality is unsatisfiable on
2030
- // asset-wrapped covenant UTXOs).
2111
+ // 5. Continuation (output 2): same AuthScript commitment as spent (NIP-023)
2031
2112
  b.pushInt(2)
2032
2113
  .op(OP_OUTPUTAUTHCOMMITMENT)
2033
2114
  .pushInt(TXFIELD_AUTHSCRIPT_COMMITMENT)
2034
2115
  .op(OP_TXFIELD)
2035
2116
  .op(OP_EQUALVERIFY);
2036
- // 6. Remainder tokenId
2117
+ // 6. Continuation name == tokenId
2037
2118
  b.pushInt(2)
2038
2119
  .pushInt(ASSETFIELD_NAME)
2039
2120
  .op(OP_OUTPUTASSETFIELD)
2040
2121
  .pushBytes(tokenIdBytes)
2041
2122
  .op(OP_EQUALVERIFY);
2042
- // 7. Remainder amount == inputAssetAmount - N
2123
+ // 7. Continuation amount == inputAmount - N
2043
2124
  b.pushInt(2)
2044
2125
  .pushInt(ASSETFIELD_AMOUNT)
2045
2126
  .op(OP_OUTPUTASSETFIELD)
@@ -2051,7 +2132,8 @@ var NeuraiScriptsBundle = (function (exports) {
2051
2132
  .op(OP_SUB)
2052
2133
  .op(OP_EQUALVERIFY);
2053
2134
  b.op(OP_DROP).pushInt(1);
2054
- b.op(OP_ENDIF);
2135
+ b.op(OP_ENDIF); // full / partial
2136
+ b.op(OP_ENDIF); // cancel / fill
2055
2137
  return b.build();
2056
2138
  }
2057
2139
  function buildPartialFillScriptPQHex(params) {
@@ -2067,33 +2149,52 @@ var NeuraiScriptsBundle = (function (exports) {
2067
2149
  * with `@neuraiproject/neurai-create-transaction`.
2068
2150
  */
2069
2151
  /**
2070
- * Unlock the covenant via the public partial-fill branch.
2152
+ * Unlock the covenant via the public fill branches.
2071
2153
  *
2072
- * Layout on the stack before OP_IF executes (top → bottom):
2073
- * 0 ← selects the OP_ELSE (fill) branch
2074
- * 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).
2075
2158
  *
2076
- * so the scriptSig pushes `<N>` then `<0>`.
2159
+ * Unlock stack shapes (pushed bottom → top):
2077
2160
  *
2078
- * @param amount units of the asset the buyer is taking. Must be > 0 and
2079
- * strictly less than the amount locked in the order UTXO
2080
- * (equal would leave a zero-asset remainder, which isn't a
2081
- * 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.
2082
2170
  */
2083
- function buildFillScriptSig(amount) {
2084
- if (typeof amount !== 'bigint') {
2085
- 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');
2086
2174
  }
2087
2175
  if (amount <= 0n) {
2088
2176
  throw new Error('fill amount must be > 0');
2089
2177
  }
2090
- return new ScriptBuilder()
2091
- .pushInt(amount)
2092
- .pushInt(0)
2093
- .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();
2094
2195
  }
2095
- function buildFillScriptSigHex(amount) {
2096
- return bytesToHex(buildFillScriptSig(amount));
2196
+ function buildFillScriptSigHex(amount, total) {
2197
+ return bytesToHex(buildFillScriptSig(amount, total));
2097
2198
  }
2098
2199
  /**
2099
2200
  * Unlock the covenant via the seller's cancel branch.
@@ -2317,13 +2418,17 @@ var NeuraiScriptsBundle = (function (exports) {
2317
2418
  }
2318
2419
 
2319
2420
  /**
2320
- * Parser for the Partial-Fill Sell Order covenant.
2421
+ * Parser for the Partial-Fill Sell Order covenant (three-branch).
2321
2422
  *
2322
2423
  * Extracts `(sellerPubKeyHash, unitPriceSats, tokenId)` from a scriptPubKey
2323
- * that was produced by `buildPartialFillScript`. The parser walks the exact
2324
- * 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
2325
2426
  * deliberate, so a downstream indexer can unambiguously classify a UTXO as
2326
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.
2327
2432
  */
2328
2433
  /**
2329
2434
  * Parse a covenant scriptPubKey and extract its parameters. Throws with a
@@ -2332,8 +2437,8 @@ var NeuraiScriptsBundle = (function (exports) {
2332
2437
  function parsePartialFillScript(script, network = 'xna-test') {
2333
2438
  const bytes = typeof script === 'string' ? hexToBytes(script) : script;
2334
2439
  const c = makeCursor(bytes);
2335
- // ───── Cancel branch prefix ─────
2336
- expectByte(c, OP_IF, 'OP_IF');
2440
+ // ═════ Outer IF — Cancel branch ═════
2441
+ expectByte(c, OP_IF, 'OP_IF (outer cancel)');
2337
2442
  expectByte(c, OP_DUP, 'OP_DUP (cancel)');
2338
2443
  expectByte(c, OP_HASH160, 'OP_HASH160');
2339
2444
  const sellerPubKeyHash = readPush(c, 'sellerPubKeyHash');
@@ -2342,86 +2447,139 @@ var NeuraiScriptsBundle = (function (exports) {
2342
2447
  }
2343
2448
  expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (cancel)');
2344
2449
  expectByte(c, OP_CHECKSIG, 'OP_CHECKSIG (cancel)');
2345
- expectByte(c, OP_ELSE, 'OP_ELSE');
2346
- // ───── Payment value check ─────
2347
- expectByte(c, OP_DUP, 'OP_DUP (price)');
2348
- const unitPriceSats = readPushPositiveInt(c, 'unitPriceSats');
2349
- expectByte(c, OP_MUL, 'OP_MUL');
2350
- expectByte(c, OP_0, 'OP_0 (payment idx)');
2351
- expectByte(c, OP_OUTPUTVALUE, 'OP_OUTPUTVALUE');
2352
- expectByte(c, OP_SWAP, 'OP_SWAP');
2353
- expectByte(c, OP_GREATERTHANOREQUAL, 'OP_GREATERTHANOREQUAL');
2354
- expectByte(c, OP_VERIFY, 'OP_VERIFY (payment)');
2355
- // ───── Payment scriptPubKey check ─────
2356
- expectByte(c, OP_0, 'OP_0 (spk idx)');
2357
- expectByte(c, OP_OUTPUTSCRIPT, 'OP_OUTPUTSCRIPT (payment)');
2358
- const sellerSpk = readPush(c, 'sellerScriptPubKey');
2359
- // 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)');
2360
2470
  const expectedSpk = new Uint8Array([
2361
2471
  OP_DUP, OP_HASH160, 0x14, ...sellerPubKeyHash, OP_EQUALVERIFY, OP_CHECKSIG
2362
2472
  ]);
2363
- if (!bytesEqual(sellerSpk, expectedSpk)) {
2364
- throw new Error('parse: embedded seller scriptPubKey does not match the cancel PKH');
2365
- }
2366
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (payment spk)');
2367
- // ───── Buyer asset amount check (output 1) ─────
2368
- expectByte(c, OP_DUP, 'OP_DUP (buyer amount)');
2369
- expectByte(c, OP_1, 'OP_1 (buyer idx)');
2370
- expectByte(c, OP_2, 'OP_2 (AMOUNT selector)');
2371
- expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer amount)');
2372
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer amount)');
2373
- // ───── Buyer asset name check (output 1) ─────
2374
- expectByte(c, OP_1, 'OP_1 (buyer idx)');
2375
- expectByte(c, OP_1, 'OP_1 (NAME selector)');
2376
- expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer name)');
2377
- const tokenIdBytes1 = readPush(c, 'tokenId #1');
2378
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer name)');
2379
- // ───── Remainder continuity: same AuthScript commitment (NIP-023) ─────
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
2380
2524
  expectByte(c, OP_2, 'OP_2 (remainder idx)');
2381
2525
  expectByte(c, OP_OUTPUTAUTHCOMMITMENT, 'OP_OUTPUTAUTHCOMMITMENT (remainder)');
2382
- expectByte(c, OP_2, 'OP_2 (TXFIELD selector: AUTHSCRIPT_COMMITMENT)');
2526
+ expectByte(c, OP_2, 'OP_2 (TXFIELD sel: AUTHSCRIPT_COMMITMENT)');
2383
2527
  expectByte(c, OP_TXFIELD, 'OP_TXFIELD');
2384
2528
  expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder auth)');
2385
- // ───── Remainder tokenId check ─────
2529
+ // Continuation name
2386
2530
  expectByte(c, OP_2, 'OP_2 (remainder idx)');
2387
- expectByte(c, OP_1, 'OP_1 (NAME selector)');
2531
+ expectByte(c, OP_1, 'OP_1 (NAME sel)');
2388
2532
  expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (remainder name)');
2389
- const tokenIdBytes2 = readPush(c, 'tokenId #2');
2533
+ const tokenIdPartial2 = readPush(c, 'tokenId partial #2');
2390
2534
  expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder name)');
2391
- // ───── Remainder amount == input amount - N ─────
2535
+ // Continuation amount
2392
2536
  expectByte(c, OP_2, 'OP_2 (remainder idx)');
2393
- expectByte(c, OP_2, 'OP_2 (AMOUNT selector)');
2537
+ expectByte(c, OP_2, 'OP_2 (AMOUNT sel)');
2394
2538
  expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (remainder amount)');
2395
2539
  expectByte(c, OP_OVER, 'OP_OVER');
2396
2540
  expectByte(c, OP_0, 'OP_0 (input idx)');
2397
- expectByte(c, OP_2, 'OP_2 (AMOUNT selector)');
2541
+ expectByte(c, OP_2, 'OP_2 (AMOUNT sel)');
2398
2542
  expectByte(c, OP_INPUTASSETFIELD, 'OP_INPUTASSETFIELD');
2399
2543
  expectByte(c, OP_SWAP, 'OP_SWAP');
2400
2544
  expectByte(c, OP_SUB, 'OP_SUB');
2401
2545
  expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder amount)');
2402
- // ───── Tail ─────
2403
- expectByte(c, OP_DROP, 'OP_DROP');
2404
- expectByte(c, OP_1, 'OP_1 (true)');
2405
- 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)');
2406
2552
  assertTrailing(c);
2407
- if (!bytesEqual(tokenIdBytes1, tokenIdBytes2)) {
2408
- 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');
2409
2560
  }
2410
- 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);
2411
2565
  return {
2412
2566
  network,
2413
2567
  sellerPubKeyHash,
2414
- unitPriceSats,
2568
+ unitPriceSats: unitPriceSatsFull,
2415
2569
  tokenId,
2416
2570
  scriptHex: bytesToHex(bytes)
2417
2571
  };
2418
2572
  }
2419
2573
 
2420
2574
  /**
2421
- * Parser for the PQ Partial-Fill Sell Order covenant. Returns the same
2422
- * economic parameters as the legacy parser, plus the payment scriptPubKey
2423
- * bytes (which may be P2PKH or AuthScript) and the configured TXHASH
2424
- * 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.
2425
2583
  */
2426
2584
  /** Quick discriminator without throwing — useful for indexers. */
2427
2585
  function isPartialFillScriptPQ(script) {
@@ -2439,8 +2597,8 @@ var NeuraiScriptsBundle = (function (exports) {
2439
2597
  function parsePartialFillScriptPQ(script, network = 'xna-test') {
2440
2598
  const bytes = typeof script === 'string' ? hexToBytes(script) : script;
2441
2599
  const c = makeCursor(bytes);
2442
- // ───── Cancel branch (PQ) ─────
2443
- expectByte(c, OP_IF, 'OP_IF');
2600
+ // ═════ Outer IF — Cancel branch (PQ) ═════
2601
+ expectByte(c, OP_IF, 'OP_IF (outer cancel)');
2444
2602
  expectByte(c, OP_DUP, 'OP_DUP (cancel)');
2445
2603
  expectByte(c, OP_SHA256, 'OP_SHA256');
2446
2604
  const pubKeyCommitment = readPush(c, 'pubKeyCommitment');
@@ -2448,9 +2606,6 @@ var NeuraiScriptsBundle = (function (exports) {
2448
2606
  throw new Error(`parse-pq: pubKeyCommitment must be 32 bytes, got ${pubKeyCommitment.length}`);
2449
2607
  }
2450
2608
  expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (cancel)');
2451
- // Selector is read as an unsigned byte — consensus OP_TXHASH treats the
2452
- // on-stack element as uint8, and the builder emits a raw 1-byte push so
2453
- // selectors 0x80..0xff round-trip correctly (plan v3 bug B).
2454
2609
  const txHashSelector = readPushUint8(c, 'txHashSelector');
2455
2610
  if (txHashSelector < 1) {
2456
2611
  throw new Error(`parse-pq: txHashSelector 0x00 is rejected by OP_TXHASH`);
@@ -2458,66 +2613,107 @@ var NeuraiScriptsBundle = (function (exports) {
2458
2613
  expectByte(c, OP_TXHASH, 'OP_TXHASH');
2459
2614
  expectByte(c, OP_SWAP, 'OP_SWAP');
2460
2615
  expectByte(c, OP_CHECKSIGFROMSTACK, 'OP_CHECKSIGFROMSTACK');
2461
- expectByte(c, OP_ELSE, 'OP_ELSE');
2462
- // ───── Fill branch ─────
2463
- expectByte(c, OP_DUP, 'OP_DUP (price)');
2464
- const unitPriceSats = readPushPositiveInt(c, 'unitPriceSats');
2465
- expectByte(c, OP_MUL, 'OP_MUL');
2466
- expectByte(c, OP_0, 'OP_0 (payment idx)');
2467
- expectByte(c, OP_OUTPUTVALUE, 'OP_OUTPUTVALUE');
2468
- expectByte(c, OP_SWAP, 'OP_SWAP');
2469
- expectByte(c, OP_GREATERTHANOREQUAL, 'OP_GE');
2470
- expectByte(c, OP_VERIFY, 'OP_VERIFY');
2471
- expectByte(c, OP_0, 'OP_0 (payment spk idx)');
2472
- expectByte(c, OP_OUTPUTSCRIPT, 'OP_OUTPUTSCRIPT (payment)');
2473
- const paymentScriptPubKey = readPush(c, 'paymentScriptPubKey');
2474
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (payment)');
2475
- expectByte(c, OP_DUP, 'OP_DUP (buyer amount)');
2476
- expectByte(c, OP_1, 'OP_1 (buyer idx)');
2477
- expectByte(c, OP_2, 'OP_2 (AMOUNT selector)');
2478
- expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer amount)');
2479
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer amount)');
2480
- expectByte(c, OP_1, 'OP_1 (buyer idx)');
2481
- expectByte(c, OP_1, 'OP_1 (NAME selector)');
2482
- expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer name)');
2483
- const tokenIdBytes1 = readPush(c, 'tokenId #1');
2484
- expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer name)');
2485
- // Remainder continuity: same AuthScript commitment (NIP-023)
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
2486
2671
  expectByte(c, OP_2, 'OP_2 (remainder idx)');
2487
2672
  expectByte(c, OP_OUTPUTAUTHCOMMITMENT, 'OP_OUTPUTAUTHCOMMITMENT (remainder)');
2488
- expectByte(c, OP_2, 'OP_2 (TXFIELD selector: AUTHSCRIPT_COMMITMENT)');
2673
+ expectByte(c, OP_2, 'OP_2 (TXFIELD sel: AUTHSCRIPT_COMMITMENT)');
2489
2674
  expectByte(c, OP_TXFIELD, 'OP_TXFIELD');
2490
2675
  expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder auth)');
2491
2676
  expectByte(c, OP_2, 'OP_2 (remainder idx)');
2492
- expectByte(c, OP_1, 'OP_1 (NAME selector)');
2677
+ expectByte(c, OP_1, 'OP_1 (NAME sel)');
2493
2678
  expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (remainder name)');
2494
- const tokenIdBytes2 = readPush(c, 'tokenId #2');
2679
+ const tokenIdPartial2 = readPush(c, 'tokenId partial #2');
2495
2680
  expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder name)');
2496
2681
  expectByte(c, OP_2, 'OP_2 (remainder idx)');
2497
- expectByte(c, OP_2, 'OP_2 (AMOUNT selector)');
2682
+ expectByte(c, OP_2, 'OP_2 (AMOUNT sel)');
2498
2683
  expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (remainder amount)');
2499
2684
  expectByte(c, OP_OVER, 'OP_OVER');
2500
2685
  expectByte(c, OP_0, 'OP_0 (input idx)');
2501
- expectByte(c, OP_2, 'OP_2 (AMOUNT selector)');
2686
+ expectByte(c, OP_2, 'OP_2 (AMOUNT sel)');
2502
2687
  expectByte(c, OP_INPUTASSETFIELD, 'OP_INPUTASSETFIELD');
2503
2688
  expectByte(c, OP_SWAP, 'OP_SWAP');
2504
2689
  expectByte(c, OP_SUB, 'OP_SUB');
2505
2690
  expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder amount)');
2506
- expectByte(c, OP_DROP, 'OP_DROP');
2507
- expectByte(c, OP_1, 'OP_1 (true)');
2508
- 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)');
2509
2695
  assertTrailing(c);
2510
- if (!bytesEqual(tokenIdBytes1, tokenIdBytes2)) {
2511
- 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');
2512
2708
  }
2513
- const tokenId = new TextDecoder('utf-8', { fatal: true }).decode(tokenIdBytes1);
2709
+ const tokenId = new TextDecoder('utf-8', { fatal: true }).decode(tokenIdFull);
2514
2710
  return {
2515
2711
  network,
2516
2712
  pubKeyCommitment,
2517
2713
  tokenId,
2518
- unitPriceSats,
2714
+ unitPriceSats: unitPriceSatsFull,
2519
2715
  txHashSelector,
2520
- paymentScriptPubKey,
2716
+ paymentScriptPubKey: paymentScriptPubKeyFull,
2521
2717
  scriptHex: bytesToHex(bytes)
2522
2718
  };
2523
2719
  }