@neuraiproject/neurai-scripts 0.6.0 → 0.6.1
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 +232 -146
- package/dist/NeuraiScripts.global.js.map +1 -1
- package/dist/browser.js +232 -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 +232 -146
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +232 -146
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +29 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type { Network, OrderUtxo, ParsedPartialFillOrder, ParsedPartialFillOrderPQ, PartialFillOrderParams, PartialFillOrderPQParams, TxInputRef } from './types.js';
|
|
1
|
+
export type { Network, OrderUtxo, ParsedPartialFillOrder, ParsedPartialFillOrderPQ, PartialFillExpiration, PartialFillExpirationMode, PartialFillOrderParams, PartialFillOrderPQParams, TxInputRef } from './types.js';
|
|
2
2
|
export { bytesToHex, hexToBytes, concatBytes, bytesEqual, ensureHex } from './core/bytes.js';
|
|
3
3
|
export { ScriptBuilder, encodeScriptNum, pushBytes, pushInt, pushHex } from './core/script-builder.js';
|
|
4
4
|
export * as opcodes from './core/opcodes.js';
|
package/dist/index.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,215 @@ 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
|
+
b.pushInt(expiration.value)
|
|
1980
|
+
.pushBytes(Uint8Array.of(selectorForMode(expiration.mode)))
|
|
1981
|
+
.op(OP_CHAINCONTEXT, OP_GREATERTHAN, OP_VERIFY);
|
|
1982
|
+
}
|
|
1983
|
+
function readOptionalExpirationGate(c, nextOpcodeWithoutGate, label) {
|
|
1984
|
+
if (c.pos >= c.bytes.length || c.bytes[c.pos] === nextOpcodeWithoutGate) {
|
|
1985
|
+
return undefined;
|
|
1986
|
+
}
|
|
1987
|
+
const value = readPushPositiveInt(c, `expiration value (${label})`);
|
|
1988
|
+
if (value <= 0n) {
|
|
1989
|
+
throw new Error(`parse: expiration value (${label}) must be > 0`);
|
|
1990
|
+
}
|
|
1991
|
+
const selector = readPushUint8(c, `expiration selector (${label})`);
|
|
1992
|
+
const mode = modeForSelector(selector);
|
|
1993
|
+
expectByte(c, OP_CHAINCONTEXT, `OP_CHAINCONTEXT (${label})`);
|
|
1994
|
+
expectByte(c, OP_GREATERTHAN, `OP_GREATERTHAN (${label})`);
|
|
1995
|
+
expectByte(c, OP_VERIFY, `OP_VERIFY (${label})`);
|
|
1996
|
+
return { mode, value };
|
|
1997
|
+
}
|
|
1998
|
+
function assertSameExpiration(a, b, label) {
|
|
1999
|
+
if (a === undefined && b === undefined)
|
|
2000
|
+
return;
|
|
2001
|
+
if (a === undefined || b === undefined || a.mode !== b.mode || a.value !== b.value) {
|
|
2002
|
+
throw new Error(`parse: expiration differs between ${label}`);
|
|
2003
|
+
}
|
|
2004
|
+
}
|
|
2005
|
+
|
|
1788
2006
|
/**
|
|
1789
2007
|
* Partial-Fill Sell Order covenant script (three-branch).
|
|
1790
2008
|
*
|
|
@@ -1870,6 +2088,7 @@ function buildPartialFillScript(params) {
|
|
|
1870
2088
|
const sellerPubKeyHash = decodeSellerAddress(sellerAddress);
|
|
1871
2089
|
assertTokenId$1(tokenId);
|
|
1872
2090
|
assertPrice$1(unitPriceSats);
|
|
2091
|
+
const expiration = assertExpiration(params.expiration);
|
|
1873
2092
|
const sellerScriptPubKey = encodeP2PKHScriptPubKey(sellerPubKeyHash);
|
|
1874
2093
|
const tokenIdBytes = new TextEncoder().encode(tokenId);
|
|
1875
2094
|
const b = new ScriptBuilder();
|
|
@@ -1890,6 +2109,7 @@ function buildPartialFillScript(params) {
|
|
|
1890
2109
|
// push N. The entire covenant is drained to vout[1]; no vout[2] is
|
|
1891
2110
|
// constrained and none is required (consensus forbids zero-amount asset
|
|
1892
2111
|
// transfers, so the partial-fill continuity check is simply skipped).
|
|
2112
|
+
appendExpirationGate(b, expiration);
|
|
1893
2113
|
// 1. N = inputAmount
|
|
1894
2114
|
b.pushInt(0)
|
|
1895
2115
|
.pushInt(ASSETFIELD_AMOUNT)
|
|
@@ -1926,6 +2146,7 @@ function buildPartialFillScript(params) {
|
|
|
1926
2146
|
// scriptSig: <N> <0> <0> ( N, full-flag=0, cancel-flag=0 )
|
|
1927
2147
|
// Stack entering: [ N ]
|
|
1928
2148
|
b.op(OP_ELSE);
|
|
2149
|
+
appendExpirationGate(b, expiration);
|
|
1929
2150
|
// 1. Payment value (output 0) >= N * unitPriceSats
|
|
1930
2151
|
b.op(OP_DUP) // [ N, N ]
|
|
1931
2152
|
.pushInt(unitPriceSats) // [ N, N, price ]
|
|
@@ -2060,6 +2281,7 @@ function buildPartialFillScriptPQ(params) {
|
|
|
2060
2281
|
assertTokenId(tokenId);
|
|
2061
2282
|
assertPrice(unitPriceSats);
|
|
2062
2283
|
assertSelector(txHashSelector);
|
|
2284
|
+
const expiration = assertExpiration(params.expiration);
|
|
2063
2285
|
const payment = encodeSellerScriptPubKey(paymentAddress);
|
|
2064
2286
|
const tokenIdBytes = new TextEncoder().encode(tokenId);
|
|
2065
2287
|
const b = new ScriptBuilder();
|
|
@@ -2087,6 +2309,7 @@ function buildPartialFillScriptPQ(params) {
|
|
|
2087
2309
|
// scriptSig: <1> <0>
|
|
2088
2310
|
// Stack entering: [ ]
|
|
2089
2311
|
// 1. N = inputAmount
|
|
2312
|
+
appendExpirationGate(b, expiration);
|
|
2090
2313
|
b.pushInt(0)
|
|
2091
2314
|
.pushInt(ASSETFIELD_AMOUNT)
|
|
2092
2315
|
.op(OP_INPUTASSETFIELD);
|
|
@@ -2121,6 +2344,7 @@ function buildPartialFillScriptPQ(params) {
|
|
|
2121
2344
|
// scriptSig: <N> <0> <0>
|
|
2122
2345
|
// Stack entering: [ N ]
|
|
2123
2346
|
b.op(OP_ELSE);
|
|
2347
|
+
appendExpirationGate(b, expiration);
|
|
2124
2348
|
// 1. Payment value (output 0) >= N * unitPriceSats
|
|
2125
2349
|
b.op(OP_DUP)
|
|
2126
2350
|
.pushInt(unitPriceSats)
|
|
@@ -2310,152 +2534,6 @@ function buildCancelScriptSigPQHex(sigPQ, pubKey) {
|
|
|
2310
2534
|
return bytesToHex(buildCancelScriptSigPQ(sigPQ, pubKey));
|
|
2311
2535
|
}
|
|
2312
2536
|
|
|
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
2537
|
/**
|
|
2460
2538
|
* Parser for the Partial-Fill Sell Order covenant (three-branch).
|
|
2461
2539
|
*
|
|
@@ -2489,6 +2567,7 @@ function parsePartialFillScript(script, network = 'xna-test') {
|
|
|
2489
2567
|
expectByte(c, OP_ELSE, 'OP_ELSE (outer → fill)');
|
|
2490
2568
|
// ═════ Inner IF — Full-fill branch ═════
|
|
2491
2569
|
expectByte(c, OP_IF, 'OP_IF (inner full-fill)');
|
|
2570
|
+
const expirationFull = readOptionalExpirationGate(c, OP_0, 'full');
|
|
2492
2571
|
// N = inputAmount
|
|
2493
2572
|
expectByte(c, OP_0, 'OP_0 (input idx, full)');
|
|
2494
2573
|
expectByte(c, OP_2, 'OP_2 (AMOUNT sel, full)');
|
|
@@ -2530,6 +2609,7 @@ function parsePartialFillScript(script, network = 'xna-test') {
|
|
|
2530
2609
|
expectByte(c, OP_1, 'OP_1 (true, full)');
|
|
2531
2610
|
expectByte(c, OP_ELSE, 'OP_ELSE (inner → partial fill)');
|
|
2532
2611
|
// ═════ Inner ELSE — Partial-fill branch ═════
|
|
2612
|
+
const expirationPartial = readOptionalExpirationGate(c, OP_DUP, 'partial');
|
|
2533
2613
|
// Payment value
|
|
2534
2614
|
expectByte(c, OP_DUP, 'OP_DUP (price, partial)');
|
|
2535
2615
|
const unitPriceSatsPartial = readPushPositiveInt(c, 'unitPriceSats (partial)');
|
|
@@ -2600,12 +2680,14 @@ function parsePartialFillScript(script, network = 'xna-test') {
|
|
|
2600
2680
|
if (unitPriceSatsFull !== unitPriceSatsPartial) {
|
|
2601
2681
|
throw new Error('parse: unitPriceSats differs between full-fill and partial-fill branches');
|
|
2602
2682
|
}
|
|
2683
|
+
assertSameExpiration(expirationFull, expirationPartial, 'full-fill and partial-fill branches');
|
|
2603
2684
|
const tokenId = new TextDecoder('utf-8', { fatal: true }).decode(tokenIdFull);
|
|
2604
2685
|
return {
|
|
2605
2686
|
network,
|
|
2606
2687
|
sellerPubKeyHash,
|
|
2607
2688
|
unitPriceSats: unitPriceSatsFull,
|
|
2608
2689
|
tokenId,
|
|
2690
|
+
expiration: expirationFull,
|
|
2609
2691
|
scriptHex: bytesToHex(bytes)
|
|
2610
2692
|
};
|
|
2611
2693
|
}
|
|
@@ -2655,6 +2737,7 @@ function parsePartialFillScriptPQ(script, network = 'xna-test') {
|
|
|
2655
2737
|
expectByte(c, OP_ELSE, 'OP_ELSE (outer → fill)');
|
|
2656
2738
|
// ═════ Inner IF — Full-fill branch ═════
|
|
2657
2739
|
expectByte(c, OP_IF, 'OP_IF (inner full-fill)');
|
|
2740
|
+
const expirationFull = readOptionalExpirationGate(c, OP_0, 'full');
|
|
2658
2741
|
expectByte(c, OP_0, 'OP_0 (input idx, full)');
|
|
2659
2742
|
expectByte(c, OP_2, 'OP_2 (AMOUNT sel, full)');
|
|
2660
2743
|
expectByte(c, OP_INPUTASSETFIELD, 'OP_INPUTASSETFIELD (full)');
|
|
@@ -2684,6 +2767,7 @@ function parsePartialFillScriptPQ(script, network = 'xna-test') {
|
|
|
2684
2767
|
expectByte(c, OP_1, 'OP_1 (true, full)');
|
|
2685
2768
|
expectByte(c, OP_ELSE, 'OP_ELSE (inner → partial fill)');
|
|
2686
2769
|
// ═════ Inner ELSE — Partial-fill branch ═════
|
|
2770
|
+
const expirationPartial = readOptionalExpirationGate(c, OP_DUP, 'partial');
|
|
2687
2771
|
expectByte(c, OP_DUP, 'OP_DUP (price, partial)');
|
|
2688
2772
|
const unitPriceSatsPartial = readPushPositiveInt(c, 'unitPriceSats (partial)');
|
|
2689
2773
|
expectByte(c, OP_MUL, 'OP_MUL (partial)');
|
|
@@ -2745,6 +2829,7 @@ function parsePartialFillScriptPQ(script, network = 'xna-test') {
|
|
|
2745
2829
|
if (unitPriceSatsFull !== unitPriceSatsPartial) {
|
|
2746
2830
|
throw new Error('parse-pq: unitPriceSats differs between full-fill and partial-fill branches');
|
|
2747
2831
|
}
|
|
2832
|
+
assertSameExpiration(expirationFull, expirationPartial, 'full-fill and partial-fill branches');
|
|
2748
2833
|
const tokenId = new TextDecoder('utf-8', { fatal: true }).decode(tokenIdFull);
|
|
2749
2834
|
return {
|
|
2750
2835
|
network,
|
|
@@ -2752,6 +2837,7 @@ function parsePartialFillScriptPQ(script, network = 'xna-test') {
|
|
|
2752
2837
|
tokenId,
|
|
2753
2838
|
unitPriceSats: unitPriceSatsFull,
|
|
2754
2839
|
txHashSelector,
|
|
2840
|
+
expiration: expirationFull,
|
|
2755
2841
|
paymentScriptPubKey: paymentScriptPubKeyFull,
|
|
2756
2842
|
scriptHex: bytesToHex(bytes)
|
|
2757
2843
|
};
|