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