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