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