@neuraiproject/neurai-scripts 0.6.0 → 0.6.2
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 +30 -6
- package/dist/NeuraiScripts.global.js +237 -146
- package/dist/NeuraiScripts.global.js.map +1 -1
- package/dist/browser.js +237 -146
- package/dist/browser.js.map +1 -1
- package/dist/core/opcodes.d.ts +3 -0
- package/dist/covenants/partial-fill/expiration.d.ts +7 -0
- package/dist/index.cjs +237 -146
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +237 -146
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +29 -0
- package/package.json +1 -1
package/dist/browser.js
CHANGED
|
@@ -219,6 +219,12 @@ const OP_INPUTVALUE = 0xd6;
|
|
|
219
219
|
// Pushes chain-context fields (block height, median time, etc.) selected
|
|
220
220
|
// by a single-byte selector consumed from the stack.
|
|
221
221
|
const OP_CHAINCONTEXT = 0xd7;
|
|
222
|
+
// ---------- Selectors for OP_CHAINCONTEXT ----------
|
|
223
|
+
// Valid selector bytes consumed by OP_CHAINCONTEXT. HEIGHT is the candidate
|
|
224
|
+
// confirmation block height; MTP is the previous block's median time past.
|
|
225
|
+
const CHAINCONTEXT_HEIGHT = 0x01;
|
|
226
|
+
const CHAINCONTEXT_MTP = 0x02;
|
|
227
|
+
const CHAINCONTEXT_CHAIN_ID = 0x03;
|
|
222
228
|
// ---------- Merkle inclusion (NIP-031) ----------
|
|
223
229
|
// Native Merkle inclusion verifier. Consumes (leaf, scheme_id, proof,
|
|
224
230
|
// root) and pushes a boolean. Flag off → bad-opcode.
|
|
@@ -268,6 +274,9 @@ var opcodes = /*#__PURE__*/Object.freeze({
|
|
|
268
274
|
ASSETFIELD_REISSUABLE: ASSETFIELD_REISSUABLE,
|
|
269
275
|
ASSETFIELD_TYPE: ASSETFIELD_TYPE,
|
|
270
276
|
ASSETFIELD_UNITS: ASSETFIELD_UNITS,
|
|
277
|
+
CHAINCONTEXT_CHAIN_ID: CHAINCONTEXT_CHAIN_ID,
|
|
278
|
+
CHAINCONTEXT_HEIGHT: CHAINCONTEXT_HEIGHT,
|
|
279
|
+
CHAINCONTEXT_MTP: CHAINCONTEXT_MTP,
|
|
271
280
|
OP_0: OP_0,
|
|
272
281
|
OP_0NOTEQUAL: OP_0NOTEQUAL,
|
|
273
282
|
OP_1: OP_1,
|
|
@@ -1785,6 +1794,220 @@ function splitAssetWrappedScriptPubKey(spkHex) {
|
|
|
1785
1794
|
};
|
|
1786
1795
|
}
|
|
1787
1796
|
|
|
1797
|
+
/**
|
|
1798
|
+
* Shared parsing primitives for strict covenant parsers. Each covenant
|
|
1799
|
+
* parser walks the exact byte layout emitted by its builder and fails on
|
|
1800
|
+
* any deviation; the primitives here centralize the cursor arithmetic,
|
|
1801
|
+
* pushdata decoding, and CScriptNum decoding so the legacy and PQ parsers
|
|
1802
|
+
* (and future covenant parsers) cannot drift in rigor.
|
|
1803
|
+
*/
|
|
1804
|
+
function makeCursor(bytes) {
|
|
1805
|
+
return { bytes, pos: 0 };
|
|
1806
|
+
}
|
|
1807
|
+
/** Consume one byte and verify it equals `expected`. */
|
|
1808
|
+
function expectByte(c, expected, label) {
|
|
1809
|
+
if (c.pos >= c.bytes.length) {
|
|
1810
|
+
throw new Error(`parse: unexpected end of script while reading ${label}`);
|
|
1811
|
+
}
|
|
1812
|
+
const got = c.bytes[c.pos];
|
|
1813
|
+
if (got !== expected) {
|
|
1814
|
+
throw new Error(`parse: expected ${label} = 0x${expected.toString(16)} at offset ${c.pos}, got 0x${got.toString(16)}`);
|
|
1815
|
+
}
|
|
1816
|
+
c.pos += 1;
|
|
1817
|
+
}
|
|
1818
|
+
/** Fail if the cursor has not consumed every byte of the script. */
|
|
1819
|
+
function assertTrailing(c) {
|
|
1820
|
+
if (c.pos !== c.bytes.length) {
|
|
1821
|
+
throw new Error(`parse: ${c.bytes.length - c.pos} trailing bytes after end of script`);
|
|
1822
|
+
}
|
|
1823
|
+
}
|
|
1824
|
+
/**
|
|
1825
|
+
* Read one pushdata element from the cursor. Supports direct pushes
|
|
1826
|
+
* (1..75 bytes), `OP_PUSHDATA1` and `OP_PUSHDATA2`. `OP_PUSHDATA4` is not
|
|
1827
|
+
* supported by any current covenant script and would overflow the
|
|
1828
|
+
* per-element cap anyway. Truncation is checked for all length fields
|
|
1829
|
+
* and payload ranges.
|
|
1830
|
+
*/
|
|
1831
|
+
function readPush(c, label) {
|
|
1832
|
+
if (c.pos >= c.bytes.length) {
|
|
1833
|
+
throw new Error(`parse: unexpected end of script while reading push for ${label}`);
|
|
1834
|
+
}
|
|
1835
|
+
const opcode = c.bytes[c.pos];
|
|
1836
|
+
c.pos += 1;
|
|
1837
|
+
// Short direct push: 1..75 bytes
|
|
1838
|
+
if (opcode >= 0x01 && opcode <= 0x4b) {
|
|
1839
|
+
const len = opcode;
|
|
1840
|
+
if (c.pos + len > c.bytes.length) {
|
|
1841
|
+
throw new Error(`parse: short push of ${len} bytes exceeds script length at ${label}`);
|
|
1842
|
+
}
|
|
1843
|
+
const data = c.bytes.slice(c.pos, c.pos + len);
|
|
1844
|
+
c.pos += len;
|
|
1845
|
+
return data;
|
|
1846
|
+
}
|
|
1847
|
+
// OP_PUSHDATA1
|
|
1848
|
+
if (opcode === 0x4c) {
|
|
1849
|
+
if (c.pos >= c.bytes.length) {
|
|
1850
|
+
throw new Error(`parse: truncated PUSHDATA1 length at ${label}`);
|
|
1851
|
+
}
|
|
1852
|
+
const len = c.bytes[c.pos];
|
|
1853
|
+
c.pos += 1;
|
|
1854
|
+
if (c.pos + len > c.bytes.length) {
|
|
1855
|
+
throw new Error(`parse: PUSHDATA1 of ${len} bytes exceeds script length at ${label}`);
|
|
1856
|
+
}
|
|
1857
|
+
const data = c.bytes.slice(c.pos, c.pos + len);
|
|
1858
|
+
c.pos += len;
|
|
1859
|
+
return data;
|
|
1860
|
+
}
|
|
1861
|
+
// OP_PUSHDATA2
|
|
1862
|
+
if (opcode === 0x4d) {
|
|
1863
|
+
if (c.pos + 2 > c.bytes.length) {
|
|
1864
|
+
throw new Error(`parse: truncated PUSHDATA2 length at ${label}`);
|
|
1865
|
+
}
|
|
1866
|
+
const len = c.bytes[c.pos] | (c.bytes[c.pos + 1] << 8);
|
|
1867
|
+
c.pos += 2;
|
|
1868
|
+
if (c.pos + len > c.bytes.length) {
|
|
1869
|
+
throw new Error(`parse: PUSHDATA2 of ${len} bytes exceeds script length at ${label}`);
|
|
1870
|
+
}
|
|
1871
|
+
const data = c.bytes.slice(c.pos, c.pos + len);
|
|
1872
|
+
c.pos += len;
|
|
1873
|
+
return data;
|
|
1874
|
+
}
|
|
1875
|
+
throw new Error(`parse: expected a pushdata opcode at ${label}, got 0x${opcode.toString(16)} at offset ${c.pos - 1}`);
|
|
1876
|
+
}
|
|
1877
|
+
/**
|
|
1878
|
+
* Decode a `CScriptNum` byte vector (little-endian sign-magnitude, up to
|
|
1879
|
+
* 8 bytes) into a BigInt. Empty vector encodes 0.
|
|
1880
|
+
*/
|
|
1881
|
+
function decodeScriptNum(data, label) {
|
|
1882
|
+
if (data.length === 0)
|
|
1883
|
+
return 0n;
|
|
1884
|
+
if (data.length > 8) {
|
|
1885
|
+
throw new Error(`parse: CScriptNum at ${label} exceeds 8 bytes`);
|
|
1886
|
+
}
|
|
1887
|
+
let n = 0n;
|
|
1888
|
+
for (let i = 0; i < data.length - 1; i += 1) {
|
|
1889
|
+
n |= BigInt(data[i]) << BigInt(8 * i);
|
|
1890
|
+
}
|
|
1891
|
+
const last = data[data.length - 1];
|
|
1892
|
+
n |= BigInt(last & 0x7f) << BigInt(8 * (data.length - 1));
|
|
1893
|
+
if (last & 0x80) {
|
|
1894
|
+
n = -n;
|
|
1895
|
+
}
|
|
1896
|
+
return n;
|
|
1897
|
+
}
|
|
1898
|
+
/**
|
|
1899
|
+
* Read a push as a non-negative CScriptNum. Recognises OP_1..OP_16
|
|
1900
|
+
* shorthand. `OP_0` is not accepted because the covenant callers use this
|
|
1901
|
+
* only for values that are strictly positive (prices, selectors, indices).
|
|
1902
|
+
*/
|
|
1903
|
+
function readPushPositiveInt(c, label) {
|
|
1904
|
+
if (c.pos >= c.bytes.length) {
|
|
1905
|
+
throw new Error(`parse: end of script at ${label}`);
|
|
1906
|
+
}
|
|
1907
|
+
const opcode = c.bytes[c.pos];
|
|
1908
|
+
if (opcode >= OP_1 && opcode <= 0x60) {
|
|
1909
|
+
c.pos += 1;
|
|
1910
|
+
return BigInt(opcode - OP_1 + 1);
|
|
1911
|
+
}
|
|
1912
|
+
const data = readPush(c, label);
|
|
1913
|
+
return decodeScriptNum(data, label);
|
|
1914
|
+
}
|
|
1915
|
+
/**
|
|
1916
|
+
* Read a 1-byte selector as an UNSIGNED 8-bit integer (0..255). Accepts
|
|
1917
|
+
* two on-wire encodings, because old vs new covenant builders differ:
|
|
1918
|
+
* - `OP_1..OP_16` shorthand (single opcode) → values 1..16.
|
|
1919
|
+
* - `0x01 <byte>` raw 1-byte push → any value 1..255.
|
|
1920
|
+
*
|
|
1921
|
+
* Values 0x80..0xff MUST use the raw-push form; the CScriptNum encoding
|
|
1922
|
+
* would need a 0x00 padding byte and become 2 bytes on-stack, which
|
|
1923
|
+
* consensus `OP_TXHASH` rejects. The builder in `script-pq.ts` emits the
|
|
1924
|
+
* raw-push form unconditionally; the parser stays lenient so covenants
|
|
1925
|
+
* built by older tools (using OP_N for small values) still round-trip.
|
|
1926
|
+
*/
|
|
1927
|
+
function readPushUint8(c, label) {
|
|
1928
|
+
if (c.pos >= c.bytes.length) {
|
|
1929
|
+
throw new Error(`parse: end of script at ${label}`);
|
|
1930
|
+
}
|
|
1931
|
+
const opcode = c.bytes[c.pos];
|
|
1932
|
+
if (opcode >= OP_1 && opcode <= 0x60) {
|
|
1933
|
+
c.pos += 1;
|
|
1934
|
+
return opcode - OP_1 + 1;
|
|
1935
|
+
}
|
|
1936
|
+
const data = readPush(c, label);
|
|
1937
|
+
if (data.length !== 1) {
|
|
1938
|
+
throw new Error(`parse: ${label} must be a single-byte push, got ${data.length} bytes`);
|
|
1939
|
+
}
|
|
1940
|
+
return data[0];
|
|
1941
|
+
}
|
|
1942
|
+
|
|
1943
|
+
const MAX_INT64 = 0x7fffffffffffffffn;
|
|
1944
|
+
function selectorForMode(mode) {
|
|
1945
|
+
if (mode === 'height')
|
|
1946
|
+
return CHAINCONTEXT_HEIGHT;
|
|
1947
|
+
if (mode === 'mtp')
|
|
1948
|
+
return CHAINCONTEXT_MTP;
|
|
1949
|
+
throw new Error('expiration.mode must be "height" or "mtp"');
|
|
1950
|
+
}
|
|
1951
|
+
function modeForSelector(selector) {
|
|
1952
|
+
if (selector === CHAINCONTEXT_HEIGHT)
|
|
1953
|
+
return 'height';
|
|
1954
|
+
if (selector === CHAINCONTEXT_MTP)
|
|
1955
|
+
return 'mtp';
|
|
1956
|
+
throw new Error('expiration OP_CHAINCONTEXT selector must be HEIGHT (0x01) or MTP (0x02)');
|
|
1957
|
+
}
|
|
1958
|
+
function assertExpiration(expiration) {
|
|
1959
|
+
if (expiration === undefined)
|
|
1960
|
+
return undefined;
|
|
1961
|
+
if (expiration === null || typeof expiration !== 'object') {
|
|
1962
|
+
throw new Error('expiration must be an object');
|
|
1963
|
+
}
|
|
1964
|
+
selectorForMode(expiration.mode);
|
|
1965
|
+
if (typeof expiration.value !== 'bigint') {
|
|
1966
|
+
throw new Error('expiration.value must be a bigint');
|
|
1967
|
+
}
|
|
1968
|
+
if (expiration.value <= 0n) {
|
|
1969
|
+
throw new Error('expiration.value must be > 0');
|
|
1970
|
+
}
|
|
1971
|
+
if (expiration.value > MAX_INT64) {
|
|
1972
|
+
throw new Error('expiration.value exceeds int64 range');
|
|
1973
|
+
}
|
|
1974
|
+
return expiration;
|
|
1975
|
+
}
|
|
1976
|
+
function appendExpirationGate(b, expiration) {
|
|
1977
|
+
if (!expiration)
|
|
1978
|
+
return;
|
|
1979
|
+
// Selectors 1 (HEIGHT) and 2 (MTP) must be pushed as OP_1 / OP_2 so the
|
|
1980
|
+
// resulting script is consensus-minimal (MINIMALDATA). `pushBytes(0x01 N)`
|
|
1981
|
+
// would emit `01 0N`, which the consensus rule rejects for N in 1..16.
|
|
1982
|
+
// Parser side stays lenient (`readPushUint8` accepts both forms) so old
|
|
1983
|
+
// covenants in the raw `01 0N` form keep round-tripping.
|
|
1984
|
+
b.pushInt(expiration.value)
|
|
1985
|
+
.pushInt(selectorForMode(expiration.mode))
|
|
1986
|
+
.op(OP_CHAINCONTEXT, OP_GREATERTHAN, OP_VERIFY);
|
|
1987
|
+
}
|
|
1988
|
+
function readOptionalExpirationGate(c, nextOpcodeWithoutGate, label) {
|
|
1989
|
+
if (c.pos >= c.bytes.length || c.bytes[c.pos] === nextOpcodeWithoutGate) {
|
|
1990
|
+
return undefined;
|
|
1991
|
+
}
|
|
1992
|
+
const value = readPushPositiveInt(c, `expiration value (${label})`);
|
|
1993
|
+
if (value <= 0n) {
|
|
1994
|
+
throw new Error(`parse: expiration value (${label}) must be > 0`);
|
|
1995
|
+
}
|
|
1996
|
+
const selector = readPushUint8(c, `expiration selector (${label})`);
|
|
1997
|
+
const mode = modeForSelector(selector);
|
|
1998
|
+
expectByte(c, OP_CHAINCONTEXT, `OP_CHAINCONTEXT (${label})`);
|
|
1999
|
+
expectByte(c, OP_GREATERTHAN, `OP_GREATERTHAN (${label})`);
|
|
2000
|
+
expectByte(c, OP_VERIFY, `OP_VERIFY (${label})`);
|
|
2001
|
+
return { mode, value };
|
|
2002
|
+
}
|
|
2003
|
+
function assertSameExpiration(a, b, label) {
|
|
2004
|
+
if (a === undefined && b === undefined)
|
|
2005
|
+
return;
|
|
2006
|
+
if (a === undefined || b === undefined || a.mode !== b.mode || a.value !== b.value) {
|
|
2007
|
+
throw new Error(`parse: expiration differs between ${label}`);
|
|
2008
|
+
}
|
|
2009
|
+
}
|
|
2010
|
+
|
|
1788
2011
|
/**
|
|
1789
2012
|
* Partial-Fill Sell Order covenant script (three-branch).
|
|
1790
2013
|
*
|
|
@@ -1870,6 +2093,7 @@ function buildPartialFillScript(params) {
|
|
|
1870
2093
|
const sellerPubKeyHash = decodeSellerAddress(sellerAddress);
|
|
1871
2094
|
assertTokenId$1(tokenId);
|
|
1872
2095
|
assertPrice$1(unitPriceSats);
|
|
2096
|
+
const expiration = assertExpiration(params.expiration);
|
|
1873
2097
|
const sellerScriptPubKey = encodeP2PKHScriptPubKey(sellerPubKeyHash);
|
|
1874
2098
|
const tokenIdBytes = new TextEncoder().encode(tokenId);
|
|
1875
2099
|
const b = new ScriptBuilder();
|
|
@@ -1890,6 +2114,7 @@ function buildPartialFillScript(params) {
|
|
|
1890
2114
|
// push N. The entire covenant is drained to vout[1]; no vout[2] is
|
|
1891
2115
|
// constrained and none is required (consensus forbids zero-amount asset
|
|
1892
2116
|
// transfers, so the partial-fill continuity check is simply skipped).
|
|
2117
|
+
appendExpirationGate(b, expiration);
|
|
1893
2118
|
// 1. N = inputAmount
|
|
1894
2119
|
b.pushInt(0)
|
|
1895
2120
|
.pushInt(ASSETFIELD_AMOUNT)
|
|
@@ -1926,6 +2151,7 @@ function buildPartialFillScript(params) {
|
|
|
1926
2151
|
// scriptSig: <N> <0> <0> ( N, full-flag=0, cancel-flag=0 )
|
|
1927
2152
|
// Stack entering: [ N ]
|
|
1928
2153
|
b.op(OP_ELSE);
|
|
2154
|
+
appendExpirationGate(b, expiration);
|
|
1929
2155
|
// 1. Payment value (output 0) >= N * unitPriceSats
|
|
1930
2156
|
b.op(OP_DUP) // [ N, N ]
|
|
1931
2157
|
.pushInt(unitPriceSats) // [ N, N, price ]
|
|
@@ -2060,6 +2286,7 @@ function buildPartialFillScriptPQ(params) {
|
|
|
2060
2286
|
assertTokenId(tokenId);
|
|
2061
2287
|
assertPrice(unitPriceSats);
|
|
2062
2288
|
assertSelector(txHashSelector);
|
|
2289
|
+
const expiration = assertExpiration(params.expiration);
|
|
2063
2290
|
const payment = encodeSellerScriptPubKey(paymentAddress);
|
|
2064
2291
|
const tokenIdBytes = new TextEncoder().encode(tokenId);
|
|
2065
2292
|
const b = new ScriptBuilder();
|
|
@@ -2087,6 +2314,7 @@ function buildPartialFillScriptPQ(params) {
|
|
|
2087
2314
|
// scriptSig: <1> <0>
|
|
2088
2315
|
// Stack entering: [ ]
|
|
2089
2316
|
// 1. N = inputAmount
|
|
2317
|
+
appendExpirationGate(b, expiration);
|
|
2090
2318
|
b.pushInt(0)
|
|
2091
2319
|
.pushInt(ASSETFIELD_AMOUNT)
|
|
2092
2320
|
.op(OP_INPUTASSETFIELD);
|
|
@@ -2121,6 +2349,7 @@ function buildPartialFillScriptPQ(params) {
|
|
|
2121
2349
|
// scriptSig: <N> <0> <0>
|
|
2122
2350
|
// Stack entering: [ N ]
|
|
2123
2351
|
b.op(OP_ELSE);
|
|
2352
|
+
appendExpirationGate(b, expiration);
|
|
2124
2353
|
// 1. Payment value (output 0) >= N * unitPriceSats
|
|
2125
2354
|
b.op(OP_DUP)
|
|
2126
2355
|
.pushInt(unitPriceSats)
|
|
@@ -2310,152 +2539,6 @@ function buildCancelScriptSigPQHex(sigPQ, pubKey) {
|
|
|
2310
2539
|
return bytesToHex(buildCancelScriptSigPQ(sigPQ, pubKey));
|
|
2311
2540
|
}
|
|
2312
2541
|
|
|
2313
|
-
/**
|
|
2314
|
-
* Shared parsing primitives for strict covenant parsers. Each covenant
|
|
2315
|
-
* parser walks the exact byte layout emitted by its builder and fails on
|
|
2316
|
-
* any deviation; the primitives here centralize the cursor arithmetic,
|
|
2317
|
-
* pushdata decoding, and CScriptNum decoding so the legacy and PQ parsers
|
|
2318
|
-
* (and future covenant parsers) cannot drift in rigor.
|
|
2319
|
-
*/
|
|
2320
|
-
function makeCursor(bytes) {
|
|
2321
|
-
return { bytes, pos: 0 };
|
|
2322
|
-
}
|
|
2323
|
-
/** Consume one byte and verify it equals `expected`. */
|
|
2324
|
-
function expectByte(c, expected, label) {
|
|
2325
|
-
if (c.pos >= c.bytes.length) {
|
|
2326
|
-
throw new Error(`parse: unexpected end of script while reading ${label}`);
|
|
2327
|
-
}
|
|
2328
|
-
const got = c.bytes[c.pos];
|
|
2329
|
-
if (got !== expected) {
|
|
2330
|
-
throw new Error(`parse: expected ${label} = 0x${expected.toString(16)} at offset ${c.pos}, got 0x${got.toString(16)}`);
|
|
2331
|
-
}
|
|
2332
|
-
c.pos += 1;
|
|
2333
|
-
}
|
|
2334
|
-
/** Fail if the cursor has not consumed every byte of the script. */
|
|
2335
|
-
function assertTrailing(c) {
|
|
2336
|
-
if (c.pos !== c.bytes.length) {
|
|
2337
|
-
throw new Error(`parse: ${c.bytes.length - c.pos} trailing bytes after end of script`);
|
|
2338
|
-
}
|
|
2339
|
-
}
|
|
2340
|
-
/**
|
|
2341
|
-
* Read one pushdata element from the cursor. Supports direct pushes
|
|
2342
|
-
* (1..75 bytes), `OP_PUSHDATA1` and `OP_PUSHDATA2`. `OP_PUSHDATA4` is not
|
|
2343
|
-
* supported by any current covenant script and would overflow the
|
|
2344
|
-
* per-element cap anyway. Truncation is checked for all length fields
|
|
2345
|
-
* and payload ranges.
|
|
2346
|
-
*/
|
|
2347
|
-
function readPush(c, label) {
|
|
2348
|
-
if (c.pos >= c.bytes.length) {
|
|
2349
|
-
throw new Error(`parse: unexpected end of script while reading push for ${label}`);
|
|
2350
|
-
}
|
|
2351
|
-
const opcode = c.bytes[c.pos];
|
|
2352
|
-
c.pos += 1;
|
|
2353
|
-
// Short direct push: 1..75 bytes
|
|
2354
|
-
if (opcode >= 0x01 && opcode <= 0x4b) {
|
|
2355
|
-
const len = opcode;
|
|
2356
|
-
if (c.pos + len > c.bytes.length) {
|
|
2357
|
-
throw new Error(`parse: short push of ${len} bytes exceeds script length at ${label}`);
|
|
2358
|
-
}
|
|
2359
|
-
const data = c.bytes.slice(c.pos, c.pos + len);
|
|
2360
|
-
c.pos += len;
|
|
2361
|
-
return data;
|
|
2362
|
-
}
|
|
2363
|
-
// OP_PUSHDATA1
|
|
2364
|
-
if (opcode === 0x4c) {
|
|
2365
|
-
if (c.pos >= c.bytes.length) {
|
|
2366
|
-
throw new Error(`parse: truncated PUSHDATA1 length at ${label}`);
|
|
2367
|
-
}
|
|
2368
|
-
const len = c.bytes[c.pos];
|
|
2369
|
-
c.pos += 1;
|
|
2370
|
-
if (c.pos + len > c.bytes.length) {
|
|
2371
|
-
throw new Error(`parse: PUSHDATA1 of ${len} bytes exceeds script length at ${label}`);
|
|
2372
|
-
}
|
|
2373
|
-
const data = c.bytes.slice(c.pos, c.pos + len);
|
|
2374
|
-
c.pos += len;
|
|
2375
|
-
return data;
|
|
2376
|
-
}
|
|
2377
|
-
// OP_PUSHDATA2
|
|
2378
|
-
if (opcode === 0x4d) {
|
|
2379
|
-
if (c.pos + 2 > c.bytes.length) {
|
|
2380
|
-
throw new Error(`parse: truncated PUSHDATA2 length at ${label}`);
|
|
2381
|
-
}
|
|
2382
|
-
const len = c.bytes[c.pos] | (c.bytes[c.pos + 1] << 8);
|
|
2383
|
-
c.pos += 2;
|
|
2384
|
-
if (c.pos + len > c.bytes.length) {
|
|
2385
|
-
throw new Error(`parse: PUSHDATA2 of ${len} bytes exceeds script length at ${label}`);
|
|
2386
|
-
}
|
|
2387
|
-
const data = c.bytes.slice(c.pos, c.pos + len);
|
|
2388
|
-
c.pos += len;
|
|
2389
|
-
return data;
|
|
2390
|
-
}
|
|
2391
|
-
throw new Error(`parse: expected a pushdata opcode at ${label}, got 0x${opcode.toString(16)} at offset ${c.pos - 1}`);
|
|
2392
|
-
}
|
|
2393
|
-
/**
|
|
2394
|
-
* Decode a `CScriptNum` byte vector (little-endian sign-magnitude, up to
|
|
2395
|
-
* 8 bytes) into a BigInt. Empty vector encodes 0.
|
|
2396
|
-
*/
|
|
2397
|
-
function decodeScriptNum(data, label) {
|
|
2398
|
-
if (data.length === 0)
|
|
2399
|
-
return 0n;
|
|
2400
|
-
if (data.length > 8) {
|
|
2401
|
-
throw new Error(`parse: CScriptNum at ${label} exceeds 8 bytes`);
|
|
2402
|
-
}
|
|
2403
|
-
let n = 0n;
|
|
2404
|
-
for (let i = 0; i < data.length - 1; i += 1) {
|
|
2405
|
-
n |= BigInt(data[i]) << BigInt(8 * i);
|
|
2406
|
-
}
|
|
2407
|
-
const last = data[data.length - 1];
|
|
2408
|
-
n |= BigInt(last & 0x7f) << BigInt(8 * (data.length - 1));
|
|
2409
|
-
if (last & 0x80) {
|
|
2410
|
-
n = -n;
|
|
2411
|
-
}
|
|
2412
|
-
return n;
|
|
2413
|
-
}
|
|
2414
|
-
/**
|
|
2415
|
-
* Read a push as a non-negative CScriptNum. Recognises OP_1..OP_16
|
|
2416
|
-
* shorthand. `OP_0` is not accepted because the covenant callers use this
|
|
2417
|
-
* only for values that are strictly positive (prices, selectors, indices).
|
|
2418
|
-
*/
|
|
2419
|
-
function readPushPositiveInt(c, label) {
|
|
2420
|
-
if (c.pos >= c.bytes.length) {
|
|
2421
|
-
throw new Error(`parse: end of script at ${label}`);
|
|
2422
|
-
}
|
|
2423
|
-
const opcode = c.bytes[c.pos];
|
|
2424
|
-
if (opcode >= OP_1 && opcode <= 0x60) {
|
|
2425
|
-
c.pos += 1;
|
|
2426
|
-
return BigInt(opcode - OP_1 + 1);
|
|
2427
|
-
}
|
|
2428
|
-
const data = readPush(c, label);
|
|
2429
|
-
return decodeScriptNum(data, label);
|
|
2430
|
-
}
|
|
2431
|
-
/**
|
|
2432
|
-
* Read a 1-byte selector as an UNSIGNED 8-bit integer (0..255). Accepts
|
|
2433
|
-
* two on-wire encodings, because old vs new covenant builders differ:
|
|
2434
|
-
* - `OP_1..OP_16` shorthand (single opcode) → values 1..16.
|
|
2435
|
-
* - `0x01 <byte>` raw 1-byte push → any value 1..255.
|
|
2436
|
-
*
|
|
2437
|
-
* Values 0x80..0xff MUST use the raw-push form; the CScriptNum encoding
|
|
2438
|
-
* would need a 0x00 padding byte and become 2 bytes on-stack, which
|
|
2439
|
-
* consensus `OP_TXHASH` rejects. The builder in `script-pq.ts` emits the
|
|
2440
|
-
* raw-push form unconditionally; the parser stays lenient so covenants
|
|
2441
|
-
* built by older tools (using OP_N for small values) still round-trip.
|
|
2442
|
-
*/
|
|
2443
|
-
function readPushUint8(c, label) {
|
|
2444
|
-
if (c.pos >= c.bytes.length) {
|
|
2445
|
-
throw new Error(`parse: end of script at ${label}`);
|
|
2446
|
-
}
|
|
2447
|
-
const opcode = c.bytes[c.pos];
|
|
2448
|
-
if (opcode >= OP_1 && opcode <= 0x60) {
|
|
2449
|
-
c.pos += 1;
|
|
2450
|
-
return opcode - OP_1 + 1;
|
|
2451
|
-
}
|
|
2452
|
-
const data = readPush(c, label);
|
|
2453
|
-
if (data.length !== 1) {
|
|
2454
|
-
throw new Error(`parse: ${label} must be a single-byte push, got ${data.length} bytes`);
|
|
2455
|
-
}
|
|
2456
|
-
return data[0];
|
|
2457
|
-
}
|
|
2458
|
-
|
|
2459
2542
|
/**
|
|
2460
2543
|
* Parser for the Partial-Fill Sell Order covenant (three-branch).
|
|
2461
2544
|
*
|
|
@@ -2489,6 +2572,7 @@ function parsePartialFillScript(script, network = 'xna-test') {
|
|
|
2489
2572
|
expectByte(c, OP_ELSE, 'OP_ELSE (outer → fill)');
|
|
2490
2573
|
// ═════ Inner IF — Full-fill branch ═════
|
|
2491
2574
|
expectByte(c, OP_IF, 'OP_IF (inner full-fill)');
|
|
2575
|
+
const expirationFull = readOptionalExpirationGate(c, OP_0, 'full');
|
|
2492
2576
|
// N = inputAmount
|
|
2493
2577
|
expectByte(c, OP_0, 'OP_0 (input idx, full)');
|
|
2494
2578
|
expectByte(c, OP_2, 'OP_2 (AMOUNT sel, full)');
|
|
@@ -2530,6 +2614,7 @@ function parsePartialFillScript(script, network = 'xna-test') {
|
|
|
2530
2614
|
expectByte(c, OP_1, 'OP_1 (true, full)');
|
|
2531
2615
|
expectByte(c, OP_ELSE, 'OP_ELSE (inner → partial fill)');
|
|
2532
2616
|
// ═════ Inner ELSE — Partial-fill branch ═════
|
|
2617
|
+
const expirationPartial = readOptionalExpirationGate(c, OP_DUP, 'partial');
|
|
2533
2618
|
// Payment value
|
|
2534
2619
|
expectByte(c, OP_DUP, 'OP_DUP (price, partial)');
|
|
2535
2620
|
const unitPriceSatsPartial = readPushPositiveInt(c, 'unitPriceSats (partial)');
|
|
@@ -2600,12 +2685,14 @@ function parsePartialFillScript(script, network = 'xna-test') {
|
|
|
2600
2685
|
if (unitPriceSatsFull !== unitPriceSatsPartial) {
|
|
2601
2686
|
throw new Error('parse: unitPriceSats differs between full-fill and partial-fill branches');
|
|
2602
2687
|
}
|
|
2688
|
+
assertSameExpiration(expirationFull, expirationPartial, 'full-fill and partial-fill branches');
|
|
2603
2689
|
const tokenId = new TextDecoder('utf-8', { fatal: true }).decode(tokenIdFull);
|
|
2604
2690
|
return {
|
|
2605
2691
|
network,
|
|
2606
2692
|
sellerPubKeyHash,
|
|
2607
2693
|
unitPriceSats: unitPriceSatsFull,
|
|
2608
2694
|
tokenId,
|
|
2695
|
+
expiration: expirationFull,
|
|
2609
2696
|
scriptHex: bytesToHex(bytes)
|
|
2610
2697
|
};
|
|
2611
2698
|
}
|
|
@@ -2655,6 +2742,7 @@ function parsePartialFillScriptPQ(script, network = 'xna-test') {
|
|
|
2655
2742
|
expectByte(c, OP_ELSE, 'OP_ELSE (outer → fill)');
|
|
2656
2743
|
// ═════ Inner IF — Full-fill branch ═════
|
|
2657
2744
|
expectByte(c, OP_IF, 'OP_IF (inner full-fill)');
|
|
2745
|
+
const expirationFull = readOptionalExpirationGate(c, OP_0, 'full');
|
|
2658
2746
|
expectByte(c, OP_0, 'OP_0 (input idx, full)');
|
|
2659
2747
|
expectByte(c, OP_2, 'OP_2 (AMOUNT sel, full)');
|
|
2660
2748
|
expectByte(c, OP_INPUTASSETFIELD, 'OP_INPUTASSETFIELD (full)');
|
|
@@ -2684,6 +2772,7 @@ function parsePartialFillScriptPQ(script, network = 'xna-test') {
|
|
|
2684
2772
|
expectByte(c, OP_1, 'OP_1 (true, full)');
|
|
2685
2773
|
expectByte(c, OP_ELSE, 'OP_ELSE (inner → partial fill)');
|
|
2686
2774
|
// ═════ Inner ELSE — Partial-fill branch ═════
|
|
2775
|
+
const expirationPartial = readOptionalExpirationGate(c, OP_DUP, 'partial');
|
|
2687
2776
|
expectByte(c, OP_DUP, 'OP_DUP (price, partial)');
|
|
2688
2777
|
const unitPriceSatsPartial = readPushPositiveInt(c, 'unitPriceSats (partial)');
|
|
2689
2778
|
expectByte(c, OP_MUL, 'OP_MUL (partial)');
|
|
@@ -2745,6 +2834,7 @@ function parsePartialFillScriptPQ(script, network = 'xna-test') {
|
|
|
2745
2834
|
if (unitPriceSatsFull !== unitPriceSatsPartial) {
|
|
2746
2835
|
throw new Error('parse-pq: unitPriceSats differs between full-fill and partial-fill branches');
|
|
2747
2836
|
}
|
|
2837
|
+
assertSameExpiration(expirationFull, expirationPartial, 'full-fill and partial-fill branches');
|
|
2748
2838
|
const tokenId = new TextDecoder('utf-8', { fatal: true }).decode(tokenIdFull);
|
|
2749
2839
|
return {
|
|
2750
2840
|
network,
|
|
@@ -2752,6 +2842,7 @@ function parsePartialFillScriptPQ(script, network = 'xna-test') {
|
|
|
2752
2842
|
tokenId,
|
|
2753
2843
|
unitPriceSats: unitPriceSatsFull,
|
|
2754
2844
|
txHashSelector,
|
|
2845
|
+
expiration: expirationFull,
|
|
2755
2846
|
paymentScriptPubKey: paymentScriptPubKeyFull,
|
|
2756
2847
|
scriptHex: bytesToHex(bytes)
|
|
2757
2848
|
};
|