@neuraiproject/neurai-scripts 0.5.1 → 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 +274 -146
- package/dist/NeuraiScripts.global.js.map +1 -1
- package/dist/browser.js +274 -146
- package/dist/browser.js.map +1 -1
- package/dist/core/opcodes.d.ts +14 -0
- package/dist/covenants/partial-fill/expiration.d.ts +7 -0
- package/dist/index.cjs +274 -146
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +274 -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
|
@@ -153,6 +153,26 @@ const OP_CHECKSIG = 0xac;
|
|
|
153
153
|
const OP_CHECKSIGVERIFY = 0xad;
|
|
154
154
|
const OP_CHECKMULTISIG = 0xae;
|
|
155
155
|
const OP_CHECKMULTISIGVERIFY = 0xaf;
|
|
156
|
+
// ---------- Extended hash opcodes ----------
|
|
157
|
+
// NIP-030: KECCAK256 / BLAKE2B in previously unassigned slots.
|
|
158
|
+
const OP_KECCAK256 = 0xba;
|
|
159
|
+
const OP_BLAKE2B = 0xbb;
|
|
160
|
+
// NIP-034a: modern hash opcodes (BLAKE3, SHA3-256, SHA-512).
|
|
161
|
+
// NIP-036: OP_POSEIDON — SNARK-friendly Poseidon hash over the BN254
|
|
162
|
+
// scalar field. Occupies slot 0xc9.
|
|
163
|
+
const OP_BLAKE3 = 0xc8;
|
|
164
|
+
const OP_POSEIDON = 0xc9;
|
|
165
|
+
const OP_SHA3_256 = 0xca;
|
|
166
|
+
const OP_SHA512 = 0xcb;
|
|
167
|
+
// ---------- Extended signature opcodes ----------
|
|
168
|
+
// NIP-035: strict-profile RFC 8032 PureEd25519 verifier in slot 0xdd.
|
|
169
|
+
// Stack contract: (sig64, msg, pubkey32 -- 0|1). Slots 0xd8..0xdc are
|
|
170
|
+
// reserved by NIP-033 (BLS12-381 family), unassigned in current code.
|
|
171
|
+
const OP_CHECKSIG_ED25519 = 0xdd;
|
|
172
|
+
// NIP-039: generic OP_CHECKSIG-compatible signature accumulator in slot
|
|
173
|
+
// 0xde. Stack contract: (sig, count, pubkey -- count + 0|1). Works with
|
|
174
|
+
// both legacy secp256k1 and PQ ML-DSA-44 keys via CPubKey::Verify().
|
|
175
|
+
const OP_CHECKSIGADD = 0xde;
|
|
156
176
|
// ---------- Expansion / NOPs ----------
|
|
157
177
|
// Only the NOPs not aliased by DePIN-Test opcodes are exported as NOPs.
|
|
158
178
|
// OP_NOP2 == OP_CHECKLOCKTIMEVERIFY (0xb1)
|
|
@@ -192,6 +212,23 @@ const OP_REFINPUTCOUNT = 0xd4;
|
|
|
192
212
|
// scriptPubKey. Symmetric to TXFIELD_AUTHSCRIPT_COMMITMENT for inputs;
|
|
193
213
|
// ignores any trailing OP_XNA_ASSET asset wrapper bytes.
|
|
194
214
|
const OP_OUTPUTAUTHCOMMITMENT = 0xd5;
|
|
215
|
+
// ---------- Input value introspection (NIP-024) ----------
|
|
216
|
+
// Pushes the satoshi value of a selected input's spent prevout.
|
|
217
|
+
const OP_INPUTVALUE = 0xd6;
|
|
218
|
+
// ---------- Chain context introspection (NIP-026) ----------
|
|
219
|
+
// Pushes chain-context fields (block height, median time, etc.) selected
|
|
220
|
+
// by a single-byte selector consumed from the stack.
|
|
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;
|
|
228
|
+
// ---------- Merkle inclusion (NIP-031) ----------
|
|
229
|
+
// Native Merkle inclusion verifier. Consumes (leaf, scheme_id, proof,
|
|
230
|
+
// root) and pushes a boolean. Flag off → bad-opcode.
|
|
231
|
+
const OP_CHECKMERKLEINCLUSION = 0xc1;
|
|
195
232
|
// ---------- Byte manipulation (DePIN-Test) ----------
|
|
196
233
|
const OP_CAT = 0x7e;
|
|
197
234
|
const OP_SPLIT = 0xb7;
|
|
@@ -237,6 +274,9 @@ var opcodes = /*#__PURE__*/Object.freeze({
|
|
|
237
274
|
ASSETFIELD_REISSUABLE: ASSETFIELD_REISSUABLE,
|
|
238
275
|
ASSETFIELD_TYPE: ASSETFIELD_TYPE,
|
|
239
276
|
ASSETFIELD_UNITS: ASSETFIELD_UNITS,
|
|
277
|
+
CHAINCONTEXT_CHAIN_ID: CHAINCONTEXT_CHAIN_ID,
|
|
278
|
+
CHAINCONTEXT_HEIGHT: CHAINCONTEXT_HEIGHT,
|
|
279
|
+
CHAINCONTEXT_MTP: CHAINCONTEXT_MTP,
|
|
240
280
|
OP_0: OP_0,
|
|
241
281
|
OP_0NOTEQUAL: OP_0NOTEQUAL,
|
|
242
282
|
OP_1: OP_1,
|
|
@@ -266,16 +306,22 @@ var opcodes = /*#__PURE__*/Object.freeze({
|
|
|
266
306
|
OP_9: OP_9,
|
|
267
307
|
OP_ABS: OP_ABS,
|
|
268
308
|
OP_ADD: OP_ADD,
|
|
309
|
+
OP_BLAKE2B: OP_BLAKE2B,
|
|
310
|
+
OP_BLAKE3: OP_BLAKE3,
|
|
269
311
|
OP_BOOLAND: OP_BOOLAND,
|
|
270
312
|
OP_BOOLOR: OP_BOOLOR,
|
|
271
313
|
OP_CAT: OP_CAT,
|
|
314
|
+
OP_CHAINCONTEXT: OP_CHAINCONTEXT,
|
|
272
315
|
OP_CHECKLOCKTIMEVERIFY: OP_CHECKLOCKTIMEVERIFY,
|
|
316
|
+
OP_CHECKMERKLEINCLUSION: OP_CHECKMERKLEINCLUSION,
|
|
273
317
|
OP_CHECKMULTISIG: OP_CHECKMULTISIG,
|
|
274
318
|
OP_CHECKMULTISIGVERIFY: OP_CHECKMULTISIGVERIFY,
|
|
275
319
|
OP_CHECKSEQUENCEVERIFY: OP_CHECKSEQUENCEVERIFY,
|
|
276
320
|
OP_CHECKSIG: OP_CHECKSIG,
|
|
321
|
+
OP_CHECKSIGADD: OP_CHECKSIGADD,
|
|
277
322
|
OP_CHECKSIGFROMSTACK: OP_CHECKSIGFROMSTACK,
|
|
278
323
|
OP_CHECKSIGVERIFY: OP_CHECKSIGVERIFY,
|
|
324
|
+
OP_CHECKSIG_ED25519: OP_CHECKSIG_ED25519,
|
|
279
325
|
OP_CHECKTEMPLATEVERIFY: OP_CHECKTEMPLATEVERIFY,
|
|
280
326
|
OP_CODESEPARATOR: OP_CODESEPARATOR,
|
|
281
327
|
OP_DEPTH: OP_DEPTH,
|
|
@@ -296,6 +342,8 @@ var opcodes = /*#__PURE__*/Object.freeze({
|
|
|
296
342
|
OP_IFDUP: OP_IFDUP,
|
|
297
343
|
OP_INPUTASSETFIELD: OP_INPUTASSETFIELD,
|
|
298
344
|
OP_INPUTCOUNT: OP_INPUTCOUNT,
|
|
345
|
+
OP_INPUTVALUE: OP_INPUTVALUE,
|
|
346
|
+
OP_KECCAK256: OP_KECCAK256,
|
|
299
347
|
OP_LESSTHAN: OP_LESSTHAN,
|
|
300
348
|
OP_LESSTHANOREQUAL: OP_LESSTHANOREQUAL,
|
|
301
349
|
OP_MAX: OP_MAX,
|
|
@@ -320,6 +368,7 @@ var opcodes = /*#__PURE__*/Object.freeze({
|
|
|
320
368
|
OP_OUTPUTVALUE: OP_OUTPUTVALUE,
|
|
321
369
|
OP_OVER: OP_OVER,
|
|
322
370
|
OP_PICK: OP_PICK,
|
|
371
|
+
OP_POSEIDON: OP_POSEIDON,
|
|
323
372
|
OP_PUSHDATA1: OP_PUSHDATA1,
|
|
324
373
|
OP_PUSHDATA2: OP_PUSHDATA2,
|
|
325
374
|
OP_PUSHDATA4: OP_PUSHDATA4,
|
|
@@ -334,6 +383,8 @@ var opcodes = /*#__PURE__*/Object.freeze({
|
|
|
334
383
|
OP_ROT: OP_ROT,
|
|
335
384
|
OP_SHA1: OP_SHA1,
|
|
336
385
|
OP_SHA256: OP_SHA256,
|
|
386
|
+
OP_SHA3_256: OP_SHA3_256,
|
|
387
|
+
OP_SHA512: OP_SHA512,
|
|
337
388
|
OP_SIZE: OP_SIZE,
|
|
338
389
|
OP_SPLIT: OP_SPLIT,
|
|
339
390
|
OP_SUB: OP_SUB,
|
|
@@ -1743,6 +1794,215 @@ function splitAssetWrappedScriptPubKey(spkHex) {
|
|
|
1743
1794
|
};
|
|
1744
1795
|
}
|
|
1745
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
|
+
|
|
1746
2006
|
/**
|
|
1747
2007
|
* Partial-Fill Sell Order covenant script (three-branch).
|
|
1748
2008
|
*
|
|
@@ -1828,6 +2088,7 @@ function buildPartialFillScript(params) {
|
|
|
1828
2088
|
const sellerPubKeyHash = decodeSellerAddress(sellerAddress);
|
|
1829
2089
|
assertTokenId$1(tokenId);
|
|
1830
2090
|
assertPrice$1(unitPriceSats);
|
|
2091
|
+
const expiration = assertExpiration(params.expiration);
|
|
1831
2092
|
const sellerScriptPubKey = encodeP2PKHScriptPubKey(sellerPubKeyHash);
|
|
1832
2093
|
const tokenIdBytes = new TextEncoder().encode(tokenId);
|
|
1833
2094
|
const b = new ScriptBuilder();
|
|
@@ -1848,6 +2109,7 @@ function buildPartialFillScript(params) {
|
|
|
1848
2109
|
// push N. The entire covenant is drained to vout[1]; no vout[2] is
|
|
1849
2110
|
// constrained and none is required (consensus forbids zero-amount asset
|
|
1850
2111
|
// transfers, so the partial-fill continuity check is simply skipped).
|
|
2112
|
+
appendExpirationGate(b, expiration);
|
|
1851
2113
|
// 1. N = inputAmount
|
|
1852
2114
|
b.pushInt(0)
|
|
1853
2115
|
.pushInt(ASSETFIELD_AMOUNT)
|
|
@@ -1884,6 +2146,7 @@ function buildPartialFillScript(params) {
|
|
|
1884
2146
|
// scriptSig: <N> <0> <0> ( N, full-flag=0, cancel-flag=0 )
|
|
1885
2147
|
// Stack entering: [ N ]
|
|
1886
2148
|
b.op(OP_ELSE);
|
|
2149
|
+
appendExpirationGate(b, expiration);
|
|
1887
2150
|
// 1. Payment value (output 0) >= N * unitPriceSats
|
|
1888
2151
|
b.op(OP_DUP) // [ N, N ]
|
|
1889
2152
|
.pushInt(unitPriceSats) // [ N, N, price ]
|
|
@@ -2018,6 +2281,7 @@ function buildPartialFillScriptPQ(params) {
|
|
|
2018
2281
|
assertTokenId(tokenId);
|
|
2019
2282
|
assertPrice(unitPriceSats);
|
|
2020
2283
|
assertSelector(txHashSelector);
|
|
2284
|
+
const expiration = assertExpiration(params.expiration);
|
|
2021
2285
|
const payment = encodeSellerScriptPubKey(paymentAddress);
|
|
2022
2286
|
const tokenIdBytes = new TextEncoder().encode(tokenId);
|
|
2023
2287
|
const b = new ScriptBuilder();
|
|
@@ -2045,6 +2309,7 @@ function buildPartialFillScriptPQ(params) {
|
|
|
2045
2309
|
// scriptSig: <1> <0>
|
|
2046
2310
|
// Stack entering: [ ]
|
|
2047
2311
|
// 1. N = inputAmount
|
|
2312
|
+
appendExpirationGate(b, expiration);
|
|
2048
2313
|
b.pushInt(0)
|
|
2049
2314
|
.pushInt(ASSETFIELD_AMOUNT)
|
|
2050
2315
|
.op(OP_INPUTASSETFIELD);
|
|
@@ -2079,6 +2344,7 @@ function buildPartialFillScriptPQ(params) {
|
|
|
2079
2344
|
// scriptSig: <N> <0> <0>
|
|
2080
2345
|
// Stack entering: [ N ]
|
|
2081
2346
|
b.op(OP_ELSE);
|
|
2347
|
+
appendExpirationGate(b, expiration);
|
|
2082
2348
|
// 1. Payment value (output 0) >= N * unitPriceSats
|
|
2083
2349
|
b.op(OP_DUP)
|
|
2084
2350
|
.pushInt(unitPriceSats)
|
|
@@ -2268,152 +2534,6 @@ function buildCancelScriptSigPQHex(sigPQ, pubKey) {
|
|
|
2268
2534
|
return bytesToHex(buildCancelScriptSigPQ(sigPQ, pubKey));
|
|
2269
2535
|
}
|
|
2270
2536
|
|
|
2271
|
-
/**
|
|
2272
|
-
* Shared parsing primitives for strict covenant parsers. Each covenant
|
|
2273
|
-
* parser walks the exact byte layout emitted by its builder and fails on
|
|
2274
|
-
* any deviation; the primitives here centralize the cursor arithmetic,
|
|
2275
|
-
* pushdata decoding, and CScriptNum decoding so the legacy and PQ parsers
|
|
2276
|
-
* (and future covenant parsers) cannot drift in rigor.
|
|
2277
|
-
*/
|
|
2278
|
-
function makeCursor(bytes) {
|
|
2279
|
-
return { bytes, pos: 0 };
|
|
2280
|
-
}
|
|
2281
|
-
/** Consume one byte and verify it equals `expected`. */
|
|
2282
|
-
function expectByte(c, expected, label) {
|
|
2283
|
-
if (c.pos >= c.bytes.length) {
|
|
2284
|
-
throw new Error(`parse: unexpected end of script while reading ${label}`);
|
|
2285
|
-
}
|
|
2286
|
-
const got = c.bytes[c.pos];
|
|
2287
|
-
if (got !== expected) {
|
|
2288
|
-
throw new Error(`parse: expected ${label} = 0x${expected.toString(16)} at offset ${c.pos}, got 0x${got.toString(16)}`);
|
|
2289
|
-
}
|
|
2290
|
-
c.pos += 1;
|
|
2291
|
-
}
|
|
2292
|
-
/** Fail if the cursor has not consumed every byte of the script. */
|
|
2293
|
-
function assertTrailing(c) {
|
|
2294
|
-
if (c.pos !== c.bytes.length) {
|
|
2295
|
-
throw new Error(`parse: ${c.bytes.length - c.pos} trailing bytes after end of script`);
|
|
2296
|
-
}
|
|
2297
|
-
}
|
|
2298
|
-
/**
|
|
2299
|
-
* Read one pushdata element from the cursor. Supports direct pushes
|
|
2300
|
-
* (1..75 bytes), `OP_PUSHDATA1` and `OP_PUSHDATA2`. `OP_PUSHDATA4` is not
|
|
2301
|
-
* supported by any current covenant script and would overflow the
|
|
2302
|
-
* per-element cap anyway. Truncation is checked for all length fields
|
|
2303
|
-
* and payload ranges.
|
|
2304
|
-
*/
|
|
2305
|
-
function readPush(c, label) {
|
|
2306
|
-
if (c.pos >= c.bytes.length) {
|
|
2307
|
-
throw new Error(`parse: unexpected end of script while reading push for ${label}`);
|
|
2308
|
-
}
|
|
2309
|
-
const opcode = c.bytes[c.pos];
|
|
2310
|
-
c.pos += 1;
|
|
2311
|
-
// Short direct push: 1..75 bytes
|
|
2312
|
-
if (opcode >= 0x01 && opcode <= 0x4b) {
|
|
2313
|
-
const len = opcode;
|
|
2314
|
-
if (c.pos + len > c.bytes.length) {
|
|
2315
|
-
throw new Error(`parse: short push of ${len} bytes exceeds script length at ${label}`);
|
|
2316
|
-
}
|
|
2317
|
-
const data = c.bytes.slice(c.pos, c.pos + len);
|
|
2318
|
-
c.pos += len;
|
|
2319
|
-
return data;
|
|
2320
|
-
}
|
|
2321
|
-
// OP_PUSHDATA1
|
|
2322
|
-
if (opcode === 0x4c) {
|
|
2323
|
-
if (c.pos >= c.bytes.length) {
|
|
2324
|
-
throw new Error(`parse: truncated PUSHDATA1 length at ${label}`);
|
|
2325
|
-
}
|
|
2326
|
-
const len = c.bytes[c.pos];
|
|
2327
|
-
c.pos += 1;
|
|
2328
|
-
if (c.pos + len > c.bytes.length) {
|
|
2329
|
-
throw new Error(`parse: PUSHDATA1 of ${len} bytes exceeds script length at ${label}`);
|
|
2330
|
-
}
|
|
2331
|
-
const data = c.bytes.slice(c.pos, c.pos + len);
|
|
2332
|
-
c.pos += len;
|
|
2333
|
-
return data;
|
|
2334
|
-
}
|
|
2335
|
-
// OP_PUSHDATA2
|
|
2336
|
-
if (opcode === 0x4d) {
|
|
2337
|
-
if (c.pos + 2 > c.bytes.length) {
|
|
2338
|
-
throw new Error(`parse: truncated PUSHDATA2 length at ${label}`);
|
|
2339
|
-
}
|
|
2340
|
-
const len = c.bytes[c.pos] | (c.bytes[c.pos + 1] << 8);
|
|
2341
|
-
c.pos += 2;
|
|
2342
|
-
if (c.pos + len > c.bytes.length) {
|
|
2343
|
-
throw new Error(`parse: PUSHDATA2 of ${len} bytes exceeds script length at ${label}`);
|
|
2344
|
-
}
|
|
2345
|
-
const data = c.bytes.slice(c.pos, c.pos + len);
|
|
2346
|
-
c.pos += len;
|
|
2347
|
-
return data;
|
|
2348
|
-
}
|
|
2349
|
-
throw new Error(`parse: expected a pushdata opcode at ${label}, got 0x${opcode.toString(16)} at offset ${c.pos - 1}`);
|
|
2350
|
-
}
|
|
2351
|
-
/**
|
|
2352
|
-
* Decode a `CScriptNum` byte vector (little-endian sign-magnitude, up to
|
|
2353
|
-
* 8 bytes) into a BigInt. Empty vector encodes 0.
|
|
2354
|
-
*/
|
|
2355
|
-
function decodeScriptNum(data, label) {
|
|
2356
|
-
if (data.length === 0)
|
|
2357
|
-
return 0n;
|
|
2358
|
-
if (data.length > 8) {
|
|
2359
|
-
throw new Error(`parse: CScriptNum at ${label} exceeds 8 bytes`);
|
|
2360
|
-
}
|
|
2361
|
-
let n = 0n;
|
|
2362
|
-
for (let i = 0; i < data.length - 1; i += 1) {
|
|
2363
|
-
n |= BigInt(data[i]) << BigInt(8 * i);
|
|
2364
|
-
}
|
|
2365
|
-
const last = data[data.length - 1];
|
|
2366
|
-
n |= BigInt(last & 0x7f) << BigInt(8 * (data.length - 1));
|
|
2367
|
-
if (last & 0x80) {
|
|
2368
|
-
n = -n;
|
|
2369
|
-
}
|
|
2370
|
-
return n;
|
|
2371
|
-
}
|
|
2372
|
-
/**
|
|
2373
|
-
* Read a push as a non-negative CScriptNum. Recognises OP_1..OP_16
|
|
2374
|
-
* shorthand. `OP_0` is not accepted because the covenant callers use this
|
|
2375
|
-
* only for values that are strictly positive (prices, selectors, indices).
|
|
2376
|
-
*/
|
|
2377
|
-
function readPushPositiveInt(c, label) {
|
|
2378
|
-
if (c.pos >= c.bytes.length) {
|
|
2379
|
-
throw new Error(`parse: end of script at ${label}`);
|
|
2380
|
-
}
|
|
2381
|
-
const opcode = c.bytes[c.pos];
|
|
2382
|
-
if (opcode >= OP_1 && opcode <= 0x60) {
|
|
2383
|
-
c.pos += 1;
|
|
2384
|
-
return BigInt(opcode - OP_1 + 1);
|
|
2385
|
-
}
|
|
2386
|
-
const data = readPush(c, label);
|
|
2387
|
-
return decodeScriptNum(data, label);
|
|
2388
|
-
}
|
|
2389
|
-
/**
|
|
2390
|
-
* Read a 1-byte selector as an UNSIGNED 8-bit integer (0..255). Accepts
|
|
2391
|
-
* two on-wire encodings, because old vs new covenant builders differ:
|
|
2392
|
-
* - `OP_1..OP_16` shorthand (single opcode) → values 1..16.
|
|
2393
|
-
* - `0x01 <byte>` raw 1-byte push → any value 1..255.
|
|
2394
|
-
*
|
|
2395
|
-
* Values 0x80..0xff MUST use the raw-push form; the CScriptNum encoding
|
|
2396
|
-
* would need a 0x00 padding byte and become 2 bytes on-stack, which
|
|
2397
|
-
* consensus `OP_TXHASH` rejects. The builder in `script-pq.ts` emits the
|
|
2398
|
-
* raw-push form unconditionally; the parser stays lenient so covenants
|
|
2399
|
-
* built by older tools (using OP_N for small values) still round-trip.
|
|
2400
|
-
*/
|
|
2401
|
-
function readPushUint8(c, label) {
|
|
2402
|
-
if (c.pos >= c.bytes.length) {
|
|
2403
|
-
throw new Error(`parse: end of script at ${label}`);
|
|
2404
|
-
}
|
|
2405
|
-
const opcode = c.bytes[c.pos];
|
|
2406
|
-
if (opcode >= OP_1 && opcode <= 0x60) {
|
|
2407
|
-
c.pos += 1;
|
|
2408
|
-
return opcode - OP_1 + 1;
|
|
2409
|
-
}
|
|
2410
|
-
const data = readPush(c, label);
|
|
2411
|
-
if (data.length !== 1) {
|
|
2412
|
-
throw new Error(`parse: ${label} must be a single-byte push, got ${data.length} bytes`);
|
|
2413
|
-
}
|
|
2414
|
-
return data[0];
|
|
2415
|
-
}
|
|
2416
|
-
|
|
2417
2537
|
/**
|
|
2418
2538
|
* Parser for the Partial-Fill Sell Order covenant (three-branch).
|
|
2419
2539
|
*
|
|
@@ -2447,6 +2567,7 @@ function parsePartialFillScript(script, network = 'xna-test') {
|
|
|
2447
2567
|
expectByte(c, OP_ELSE, 'OP_ELSE (outer → fill)');
|
|
2448
2568
|
// ═════ Inner IF — Full-fill branch ═════
|
|
2449
2569
|
expectByte(c, OP_IF, 'OP_IF (inner full-fill)');
|
|
2570
|
+
const expirationFull = readOptionalExpirationGate(c, OP_0, 'full');
|
|
2450
2571
|
// N = inputAmount
|
|
2451
2572
|
expectByte(c, OP_0, 'OP_0 (input idx, full)');
|
|
2452
2573
|
expectByte(c, OP_2, 'OP_2 (AMOUNT sel, full)');
|
|
@@ -2488,6 +2609,7 @@ function parsePartialFillScript(script, network = 'xna-test') {
|
|
|
2488
2609
|
expectByte(c, OP_1, 'OP_1 (true, full)');
|
|
2489
2610
|
expectByte(c, OP_ELSE, 'OP_ELSE (inner → partial fill)');
|
|
2490
2611
|
// ═════ Inner ELSE — Partial-fill branch ═════
|
|
2612
|
+
const expirationPartial = readOptionalExpirationGate(c, OP_DUP, 'partial');
|
|
2491
2613
|
// Payment value
|
|
2492
2614
|
expectByte(c, OP_DUP, 'OP_DUP (price, partial)');
|
|
2493
2615
|
const unitPriceSatsPartial = readPushPositiveInt(c, 'unitPriceSats (partial)');
|
|
@@ -2558,12 +2680,14 @@ function parsePartialFillScript(script, network = 'xna-test') {
|
|
|
2558
2680
|
if (unitPriceSatsFull !== unitPriceSatsPartial) {
|
|
2559
2681
|
throw new Error('parse: unitPriceSats differs between full-fill and partial-fill branches');
|
|
2560
2682
|
}
|
|
2683
|
+
assertSameExpiration(expirationFull, expirationPartial, 'full-fill and partial-fill branches');
|
|
2561
2684
|
const tokenId = new TextDecoder('utf-8', { fatal: true }).decode(tokenIdFull);
|
|
2562
2685
|
return {
|
|
2563
2686
|
network,
|
|
2564
2687
|
sellerPubKeyHash,
|
|
2565
2688
|
unitPriceSats: unitPriceSatsFull,
|
|
2566
2689
|
tokenId,
|
|
2690
|
+
expiration: expirationFull,
|
|
2567
2691
|
scriptHex: bytesToHex(bytes)
|
|
2568
2692
|
};
|
|
2569
2693
|
}
|
|
@@ -2613,6 +2737,7 @@ function parsePartialFillScriptPQ(script, network = 'xna-test') {
|
|
|
2613
2737
|
expectByte(c, OP_ELSE, 'OP_ELSE (outer → fill)');
|
|
2614
2738
|
// ═════ Inner IF — Full-fill branch ═════
|
|
2615
2739
|
expectByte(c, OP_IF, 'OP_IF (inner full-fill)');
|
|
2740
|
+
const expirationFull = readOptionalExpirationGate(c, OP_0, 'full');
|
|
2616
2741
|
expectByte(c, OP_0, 'OP_0 (input idx, full)');
|
|
2617
2742
|
expectByte(c, OP_2, 'OP_2 (AMOUNT sel, full)');
|
|
2618
2743
|
expectByte(c, OP_INPUTASSETFIELD, 'OP_INPUTASSETFIELD (full)');
|
|
@@ -2642,6 +2767,7 @@ function parsePartialFillScriptPQ(script, network = 'xna-test') {
|
|
|
2642
2767
|
expectByte(c, OP_1, 'OP_1 (true, full)');
|
|
2643
2768
|
expectByte(c, OP_ELSE, 'OP_ELSE (inner → partial fill)');
|
|
2644
2769
|
// ═════ Inner ELSE — Partial-fill branch ═════
|
|
2770
|
+
const expirationPartial = readOptionalExpirationGate(c, OP_DUP, 'partial');
|
|
2645
2771
|
expectByte(c, OP_DUP, 'OP_DUP (price, partial)');
|
|
2646
2772
|
const unitPriceSatsPartial = readPushPositiveInt(c, 'unitPriceSats (partial)');
|
|
2647
2773
|
expectByte(c, OP_MUL, 'OP_MUL (partial)');
|
|
@@ -2703,6 +2829,7 @@ function parsePartialFillScriptPQ(script, network = 'xna-test') {
|
|
|
2703
2829
|
if (unitPriceSatsFull !== unitPriceSatsPartial) {
|
|
2704
2830
|
throw new Error('parse-pq: unitPriceSats differs between full-fill and partial-fill branches');
|
|
2705
2831
|
}
|
|
2832
|
+
assertSameExpiration(expirationFull, expirationPartial, 'full-fill and partial-fill branches');
|
|
2706
2833
|
const tokenId = new TextDecoder('utf-8', { fatal: true }).decode(tokenIdFull);
|
|
2707
2834
|
return {
|
|
2708
2835
|
network,
|
|
@@ -2710,6 +2837,7 @@ function parsePartialFillScriptPQ(script, network = 'xna-test') {
|
|
|
2710
2837
|
tokenId,
|
|
2711
2838
|
unitPriceSats: unitPriceSatsFull,
|
|
2712
2839
|
txHashSelector,
|
|
2840
|
+
expiration: expirationFull,
|
|
2713
2841
|
paymentScriptPubKey: paymentScriptPubKeyFull,
|
|
2714
2842
|
scriptHex: bytesToHex(bytes)
|
|
2715
2843
|
};
|