@neuraiproject/neurai-scripts 0.1.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/NeuraiScripts.global.js +241 -6
- package/dist/NeuraiScripts.global.js.map +1 -1
- package/dist/asset-wrapper.d.ts +47 -0
- package/dist/browser.js +240 -6
- package/dist/browser.js.map +1 -1
- package/dist/core/script-parser.d.ts +13 -0
- package/dist/index.cjs +240 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +240 -6
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
|
@@ -1542,6 +1542,204 @@ var NeuraiScriptsBundle = (function (exports) {
|
|
|
1542
1542
|
throw new Error(`Unsupported seller address type for "${address}"`);
|
|
1543
1543
|
}
|
|
1544
1544
|
|
|
1545
|
+
/**
|
|
1546
|
+
* Asset-transfer wrapper split helper.
|
|
1547
|
+
*
|
|
1548
|
+
* Neurai asset UTXOs have a scriptPubKey of the form
|
|
1549
|
+
*
|
|
1550
|
+
* <prefix scriptPubKey bytes> OP_XNA_ASSET <pushdata(payload)> OP_DROP
|
|
1551
|
+
*
|
|
1552
|
+
* where `prefix` is the recipient's standard script (typically a P2PKH, an
|
|
1553
|
+
* AuthScript witness v1, or a bare covenant such as the partial-fill sell
|
|
1554
|
+
* order), and `payload` serializes a `CAssetTransfer`:
|
|
1555
|
+
*
|
|
1556
|
+
* payload = rvn_prefix (0x72 0x76 0x6e) || type_marker (0x74 transfer)
|
|
1557
|
+
* || VarStr(assetName)
|
|
1558
|
+
* || int64LE(amountRaw)
|
|
1559
|
+
* [ || messageRef (optional) || int64LE(expireTime) (optional) ]
|
|
1560
|
+
*
|
|
1561
|
+
* This helper separates the two halves so consumers can validate the prefix
|
|
1562
|
+
* (e.g. feed it to `parsePartialFillScript`) while independently reading the
|
|
1563
|
+
* asset data displayed to the user. The optional payload tail
|
|
1564
|
+
* (`message` + `expireTime`) is tolerated but not exposed — the first
|
|
1565
|
+
* version only needs `assetName` and `amountRaw`.
|
|
1566
|
+
*
|
|
1567
|
+
* Bare (non-wrapped) scriptPubKeys round-trip through this helper by
|
|
1568
|
+
* returning `prefixHex === input` and `assetTransfer === null`.
|
|
1569
|
+
*/
|
|
1570
|
+
const RVN_MAGIC = Uint8Array.from([0x72, 0x76, 0x6e]); // "rvn"
|
|
1571
|
+
const TRANSFER_TYPE = 0x74;
|
|
1572
|
+
/**
|
|
1573
|
+
* Walk the script one opcode at a time, skipping pushdata payload bytes,
|
|
1574
|
+
* until either OP_XNA_ASSET is reached at top level or the end of the
|
|
1575
|
+
* script. Returns the byte offset of OP_XNA_ASSET, or -1 if not found.
|
|
1576
|
+
* Throws on truncated pushdata.
|
|
1577
|
+
*/
|
|
1578
|
+
function findTopLevelAssetOpcode(bytes) {
|
|
1579
|
+
let i = 0;
|
|
1580
|
+
while (i < bytes.length) {
|
|
1581
|
+
const op = bytes[i];
|
|
1582
|
+
if (op === OP_XNA_ASSET)
|
|
1583
|
+
return i;
|
|
1584
|
+
// Short direct push: 0x01..0x4b
|
|
1585
|
+
if (op >= 0x01 && op <= 0x4b) {
|
|
1586
|
+
const len = op;
|
|
1587
|
+
const next = i + 1 + len;
|
|
1588
|
+
if (next > bytes.length) {
|
|
1589
|
+
throw new Error(`splitAssetWrappedScriptPubKey: short push of ${len} bytes at offset ${i} exceeds script length`);
|
|
1590
|
+
}
|
|
1591
|
+
i = next;
|
|
1592
|
+
continue;
|
|
1593
|
+
}
|
|
1594
|
+
if (op === OP_PUSHDATA1) {
|
|
1595
|
+
if (i + 1 >= bytes.length) {
|
|
1596
|
+
throw new Error(`splitAssetWrappedScriptPubKey: truncated PUSHDATA1 length at offset ${i}`);
|
|
1597
|
+
}
|
|
1598
|
+
const len = bytes[i + 1];
|
|
1599
|
+
const next = i + 2 + len;
|
|
1600
|
+
if (next > bytes.length) {
|
|
1601
|
+
throw new Error(`splitAssetWrappedScriptPubKey: PUSHDATA1 of ${len} bytes at offset ${i} exceeds script length`);
|
|
1602
|
+
}
|
|
1603
|
+
i = next;
|
|
1604
|
+
continue;
|
|
1605
|
+
}
|
|
1606
|
+
if (op === OP_PUSHDATA2) {
|
|
1607
|
+
if (i + 2 >= bytes.length) {
|
|
1608
|
+
throw new Error(`splitAssetWrappedScriptPubKey: truncated PUSHDATA2 length at offset ${i}`);
|
|
1609
|
+
}
|
|
1610
|
+
const len = bytes[i + 1] | (bytes[i + 2] << 8);
|
|
1611
|
+
const next = i + 3 + len;
|
|
1612
|
+
if (next > bytes.length) {
|
|
1613
|
+
throw new Error(`splitAssetWrappedScriptPubKey: PUSHDATA2 of ${len} bytes at offset ${i} exceeds script length`);
|
|
1614
|
+
}
|
|
1615
|
+
i = next;
|
|
1616
|
+
continue;
|
|
1617
|
+
}
|
|
1618
|
+
if (op === OP_PUSHDATA4) {
|
|
1619
|
+
if (i + 4 >= bytes.length) {
|
|
1620
|
+
throw new Error(`splitAssetWrappedScriptPubKey: truncated PUSHDATA4 length at offset ${i}`);
|
|
1621
|
+
}
|
|
1622
|
+
const len = (bytes[i + 1] |
|
|
1623
|
+
(bytes[i + 2] << 8) |
|
|
1624
|
+
(bytes[i + 3] << 16) |
|
|
1625
|
+
(bytes[i + 4] << 24)) >>>
|
|
1626
|
+
0;
|
|
1627
|
+
const next = i + 5 + len;
|
|
1628
|
+
if (next > bytes.length) {
|
|
1629
|
+
throw new Error(`splitAssetWrappedScriptPubKey: PUSHDATA4 of ${len} bytes at offset ${i} exceeds script length`);
|
|
1630
|
+
}
|
|
1631
|
+
i = next;
|
|
1632
|
+
continue;
|
|
1633
|
+
}
|
|
1634
|
+
// Any other opcode (including OP_1..OP_16, OP_DUP, OP_HASH160, etc.) is
|
|
1635
|
+
// one byte wide in Neurai Script. Advance.
|
|
1636
|
+
i += 1;
|
|
1637
|
+
}
|
|
1638
|
+
return -1;
|
|
1639
|
+
}
|
|
1640
|
+
/**
|
|
1641
|
+
* Read the pushdata element that sits immediately after OP_XNA_ASSET and
|
|
1642
|
+
* return its payload bytes together with the cursor position after the
|
|
1643
|
+
* element. Only direct pushes (1..75) and PUSHDATA1 are accepted — a
|
|
1644
|
+
* well-formed asset transfer payload fits within 75 bytes for the common
|
|
1645
|
+
* case and comfortably within 255 bytes even with long names and tails.
|
|
1646
|
+
* PUSHDATA2 and PUSHDATA4 would signal a malformed or adversarial wrapper.
|
|
1647
|
+
*/
|
|
1648
|
+
function readPayloadPush(bytes, start) {
|
|
1649
|
+
if (start >= bytes.length) {
|
|
1650
|
+
throw new Error('splitAssetWrappedScriptPubKey: truncated wrapper — missing payload push');
|
|
1651
|
+
}
|
|
1652
|
+
const op = bytes[start];
|
|
1653
|
+
if (op >= 0x01 && op <= 0x4b) {
|
|
1654
|
+
const len = op;
|
|
1655
|
+
const dataStart = start + 1;
|
|
1656
|
+
const dataEnd = dataStart + len;
|
|
1657
|
+
if (dataEnd > bytes.length) {
|
|
1658
|
+
throw new Error(`splitAssetWrappedScriptPubKey: asset payload short push of ${len} bytes exceeds script length`);
|
|
1659
|
+
}
|
|
1660
|
+
return { payload: bytes.slice(dataStart, dataEnd), after: dataEnd };
|
|
1661
|
+
}
|
|
1662
|
+
if (op === OP_PUSHDATA1) {
|
|
1663
|
+
if (start + 1 >= bytes.length) {
|
|
1664
|
+
throw new Error('splitAssetWrappedScriptPubKey: truncated PUSHDATA1 in asset payload');
|
|
1665
|
+
}
|
|
1666
|
+
const len = bytes[start + 1];
|
|
1667
|
+
const dataStart = start + 2;
|
|
1668
|
+
const dataEnd = dataStart + len;
|
|
1669
|
+
if (dataEnd > bytes.length) {
|
|
1670
|
+
throw new Error(`splitAssetWrappedScriptPubKey: asset payload PUSHDATA1 of ${len} bytes exceeds script length`);
|
|
1671
|
+
}
|
|
1672
|
+
return { payload: bytes.slice(dataStart, dataEnd), after: dataEnd };
|
|
1673
|
+
}
|
|
1674
|
+
throw new Error(`splitAssetWrappedScriptPubKey: asset payload push opcode 0x${op.toString(16)} not accepted (expected 0x01..0x4b or PUSHDATA1)`);
|
|
1675
|
+
}
|
|
1676
|
+
function parseAssetTransferPayload(payload) {
|
|
1677
|
+
if (payload.length < 4 + 1 + 8) {
|
|
1678
|
+
// 4 magic+type, 1 varstr length, 8 int64LE amount
|
|
1679
|
+
throw new Error(`splitAssetWrappedScriptPubKey: asset payload of ${payload.length} bytes is too short`);
|
|
1680
|
+
}
|
|
1681
|
+
for (let i = 0; i < 3; i += 1) {
|
|
1682
|
+
if (payload[i] !== RVN_MAGIC[i]) {
|
|
1683
|
+
throw new Error(`splitAssetWrappedScriptPubKey: asset payload magic mismatch — expected "rvn" got 0x${payload[0].toString(16)} 0x${payload[1].toString(16)} 0x${payload[2].toString(16)}`);
|
|
1684
|
+
}
|
|
1685
|
+
}
|
|
1686
|
+
if (payload[3] !== TRANSFER_TYPE) {
|
|
1687
|
+
throw new Error(`splitAssetWrappedScriptPubKey: asset payload type marker 0x${payload[3].toString(16)} is not a transfer (0x74)`);
|
|
1688
|
+
}
|
|
1689
|
+
const nameLen = payload[4];
|
|
1690
|
+
const nameStart = 5;
|
|
1691
|
+
const nameEnd = nameStart + nameLen;
|
|
1692
|
+
if (nameEnd + 8 > payload.length) {
|
|
1693
|
+
throw new Error(`splitAssetWrappedScriptPubKey: asset payload truncated — name length ${nameLen} does not leave room for amount`);
|
|
1694
|
+
}
|
|
1695
|
+
let assetName = '';
|
|
1696
|
+
for (let i = nameStart; i < nameEnd; i += 1) {
|
|
1697
|
+
assetName += String.fromCharCode(payload[i]);
|
|
1698
|
+
}
|
|
1699
|
+
let amountRaw = 0n;
|
|
1700
|
+
for (let i = 0; i < 8; i += 1) {
|
|
1701
|
+
amountRaw |= BigInt(payload[nameEnd + i]) << BigInt(8 * i);
|
|
1702
|
+
}
|
|
1703
|
+
// Any bytes after nameEnd + 8 form the optional tail (message +
|
|
1704
|
+
// expireTime). We intentionally ignore them in this first version; the
|
|
1705
|
+
// raw payload remains available via `payloadHex` if a consumer later
|
|
1706
|
+
// needs to inspect them.
|
|
1707
|
+
return { assetName, amountRaw };
|
|
1708
|
+
}
|
|
1709
|
+
/**
|
|
1710
|
+
* Parse an asset-transfer-wrapped scriptPubKey. Accepts both wrapped and
|
|
1711
|
+
* bare forms. Throws on structural malformation (truncated pushdata,
|
|
1712
|
+
* missing OP_DROP after payload, bad magic, unsupported pushdata width).
|
|
1713
|
+
*/
|
|
1714
|
+
function splitAssetWrappedScriptPubKey(spkHex) {
|
|
1715
|
+
const normalized = ensureHex(spkHex, 'scriptPubKey');
|
|
1716
|
+
const bytes = hexToBytes(normalized);
|
|
1717
|
+
const assetOpAt = findTopLevelAssetOpcode(bytes);
|
|
1718
|
+
if (assetOpAt < 0) {
|
|
1719
|
+
return { prefixHex: normalized, assetTransfer: null };
|
|
1720
|
+
}
|
|
1721
|
+
const prefix = bytes.slice(0, assetOpAt);
|
|
1722
|
+
const { payload, after } = readPayloadPush(bytes, assetOpAt + 1);
|
|
1723
|
+
if (after >= bytes.length) {
|
|
1724
|
+
throw new Error('splitAssetWrappedScriptPubKey: asset wrapper missing trailing OP_DROP');
|
|
1725
|
+
}
|
|
1726
|
+
if (bytes[after] !== OP_DROP) {
|
|
1727
|
+
throw new Error(`splitAssetWrappedScriptPubKey: expected OP_DROP at offset ${after}, got 0x${bytes[after].toString(16)}`);
|
|
1728
|
+
}
|
|
1729
|
+
if (after + 1 !== bytes.length) {
|
|
1730
|
+
throw new Error(`splitAssetWrappedScriptPubKey: ${bytes.length - after - 1} trailing bytes after OP_DROP`);
|
|
1731
|
+
}
|
|
1732
|
+
const { assetName, amountRaw } = parseAssetTransferPayload(payload);
|
|
1733
|
+
return {
|
|
1734
|
+
prefixHex: bytesToHex(prefix),
|
|
1735
|
+
assetTransfer: {
|
|
1736
|
+
assetName,
|
|
1737
|
+
amountRaw,
|
|
1738
|
+
payloadHex: bytesToHex(payload),
|
|
1739
|
+
},
|
|
1740
|
+
};
|
|
1741
|
+
}
|
|
1742
|
+
|
|
1545
1743
|
/**
|
|
1546
1744
|
* Partial-Fill Sell Order covenant script.
|
|
1547
1745
|
*
|
|
@@ -1768,12 +1966,19 @@ var NeuraiScriptsBundle = (function (exports) {
|
|
|
1768
1966
|
// ───────── Cancel branch (PQ via OP_CHECKSIGFROMSTACK) ─────────
|
|
1769
1967
|
// scriptSig expected: <sigPQ> <pubKeyPQ> OP_1
|
|
1770
1968
|
// After OP_IF consumes the flag: [ sig, pubKey ]
|
|
1969
|
+
//
|
|
1970
|
+
// The selector MUST be pushed as a raw 1-byte element — consensus
|
|
1971
|
+
// (`interpreter.cpp` OP_TXHASH case) rejects any stack item of
|
|
1972
|
+
// size ≠ 1. Using `pushInt(selector)` would work for 1..127 but emit a
|
|
1973
|
+
// 2-byte CScriptNum for 0x80..0xff (the sign-disambiguation pad), which
|
|
1974
|
+
// makes OP_TXHASH fail with SCRIPT_ERR_TXHASH. See
|
|
1975
|
+
// `memory/project_covenant_v3_findings.md` bug B for details.
|
|
1771
1976
|
b.op(OP_IF)
|
|
1772
1977
|
.op(OP_DUP) // [ sig, pubKey, pubKey ]
|
|
1773
1978
|
.op(OP_SHA256) // [ sig, pubKey, H(pubKey) ]
|
|
1774
1979
|
.pushBytes(pubKeyCommitment) // [ sig, pubKey, H(pubKey), commitment ]
|
|
1775
1980
|
.op(OP_EQUALVERIFY) // [ sig, pubKey ]
|
|
1776
|
-
.
|
|
1981
|
+
.pushBytes(Uint8Array.of(txHashSelector)) // [ sig, pubKey, selector ] — always 1-byte stack item
|
|
1777
1982
|
.op(OP_TXHASH) // [ sig, pubKey, txHash ]
|
|
1778
1983
|
.op(OP_SWAP) // [ sig, txHash, pubKey ]
|
|
1779
1984
|
.op(OP_CHECKSIGFROMSTACK) // [ 1 | 0 ]
|
|
@@ -2068,6 +2273,33 @@ var NeuraiScriptsBundle = (function (exports) {
|
|
|
2068
2273
|
const data = readPush(c, label);
|
|
2069
2274
|
return decodeScriptNum(data, label);
|
|
2070
2275
|
}
|
|
2276
|
+
/**
|
|
2277
|
+
* Read a 1-byte selector as an UNSIGNED 8-bit integer (0..255). Accepts
|
|
2278
|
+
* two on-wire encodings, because old vs new covenant builders differ:
|
|
2279
|
+
* - `OP_1..OP_16` shorthand (single opcode) → values 1..16.
|
|
2280
|
+
* - `0x01 <byte>` raw 1-byte push → any value 1..255.
|
|
2281
|
+
*
|
|
2282
|
+
* Values 0x80..0xff MUST use the raw-push form; the CScriptNum encoding
|
|
2283
|
+
* would need a 0x00 padding byte and become 2 bytes on-stack, which
|
|
2284
|
+
* consensus `OP_TXHASH` rejects. The builder in `script-pq.ts` emits the
|
|
2285
|
+
* raw-push form unconditionally; the parser stays lenient so covenants
|
|
2286
|
+
* built by older tools (using OP_N for small values) still round-trip.
|
|
2287
|
+
*/
|
|
2288
|
+
function readPushUint8(c, label) {
|
|
2289
|
+
if (c.pos >= c.bytes.length) {
|
|
2290
|
+
throw new Error(`parse: end of script at ${label}`);
|
|
2291
|
+
}
|
|
2292
|
+
const opcode = c.bytes[c.pos];
|
|
2293
|
+
if (opcode >= OP_1 && opcode <= 0x60) {
|
|
2294
|
+
c.pos += 1;
|
|
2295
|
+
return opcode - OP_1 + 1;
|
|
2296
|
+
}
|
|
2297
|
+
const data = readPush(c, label);
|
|
2298
|
+
if (data.length !== 1) {
|
|
2299
|
+
throw new Error(`parse: ${label} must be a single-byte push, got ${data.length} bytes`);
|
|
2300
|
+
}
|
|
2301
|
+
return data[0];
|
|
2302
|
+
}
|
|
2071
2303
|
|
|
2072
2304
|
/**
|
|
2073
2305
|
* Parser for the Partial-Fill Sell Order covenant.
|
|
@@ -2201,11 +2433,13 @@ var NeuraiScriptsBundle = (function (exports) {
|
|
|
2201
2433
|
throw new Error(`parse-pq: pubKeyCommitment must be 32 bytes, got ${pubKeyCommitment.length}`);
|
|
2202
2434
|
}
|
|
2203
2435
|
expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (cancel)');
|
|
2204
|
-
|
|
2205
|
-
|
|
2206
|
-
|
|
2436
|
+
// Selector is read as an unsigned byte — consensus OP_TXHASH treats the
|
|
2437
|
+
// on-stack element as uint8, and the builder emits a raw 1-byte push so
|
|
2438
|
+
// selectors 0x80..0xff round-trip correctly (plan v3 bug B).
|
|
2439
|
+
const txHashSelector = readPushUint8(c, 'txHashSelector');
|
|
2440
|
+
if (txHashSelector < 1) {
|
|
2441
|
+
throw new Error(`parse-pq: txHashSelector 0x00 is rejected by OP_TXHASH`);
|
|
2207
2442
|
}
|
|
2208
|
-
const txHashSelector = Number(txHashSelectorBig);
|
|
2209
2443
|
expectByte(c, OP_TXHASH, 'OP_TXHASH');
|
|
2210
2444
|
expectByte(c, OP_SWAP, 'OP_SWAP');
|
|
2211
2445
|
expectByte(c, OP_CHECKSIGFROMSTACK, 'OP_CHECKSIGFROMSTACK');
|
|
@@ -2319,7 +2553,8 @@ var NeuraiScriptsBundle = (function (exports) {
|
|
|
2319
2553
|
parsePartialFillScriptPQ: parsePartialFillScriptPQ,
|
|
2320
2554
|
pushBytes: pushBytes,
|
|
2321
2555
|
pushHex: pushHex,
|
|
2322
|
-
pushInt: pushInt
|
|
2556
|
+
pushInt: pushInt,
|
|
2557
|
+
splitAssetWrappedScriptPubKey: splitAssetWrappedScriptPubKey
|
|
2323
2558
|
});
|
|
2324
2559
|
|
|
2325
2560
|
const globalTarget = globalThis;
|