@neuraiproject/neurai-scripts 0.1.1 → 0.2.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.
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Asset-transfer wrapper split helper.
3
+ *
4
+ * Neurai asset UTXOs have a scriptPubKey of the form
5
+ *
6
+ * <prefix scriptPubKey bytes> OP_XNA_ASSET <pushdata(payload)> OP_DROP
7
+ *
8
+ * where `prefix` is the recipient's standard script (typically a P2PKH, an
9
+ * AuthScript witness v1, or a bare covenant such as the partial-fill sell
10
+ * order), and `payload` serializes a `CAssetTransfer`:
11
+ *
12
+ * payload = rvn_prefix (0x72 0x76 0x6e) || type_marker (0x74 transfer)
13
+ * || VarStr(assetName)
14
+ * || int64LE(amountRaw)
15
+ * [ || messageRef (optional) || int64LE(expireTime) (optional) ]
16
+ *
17
+ * This helper separates the two halves so consumers can validate the prefix
18
+ * (e.g. feed it to `parsePartialFillScript`) while independently reading the
19
+ * asset data displayed to the user. The optional payload tail
20
+ * (`message` + `expireTime`) is tolerated but not exposed — the first
21
+ * version only needs `assetName` and `amountRaw`.
22
+ *
23
+ * Bare (non-wrapped) scriptPubKeys round-trip through this helper by
24
+ * returning `prefixHex === input` and `assetTransfer === null`.
25
+ */
26
+ export interface AssetTransferPayload {
27
+ /** ASCII asset name as declared in the VarStr field. */
28
+ assetName: string;
29
+ /** Raw satoshi-scaled amount from the int64LE field. Display units = raw / 1e8. */
30
+ amountRaw: bigint;
31
+ /** Hex of the full payload bytes (starting at the `rvn` magic, ending at
32
+ * the last byte before OP_DROP). Includes optional tail when present. */
33
+ payloadHex: string;
34
+ }
35
+ export interface SplitAssetWrappedResult {
36
+ /** Bytes of the scriptPubKey BEFORE the OP_XNA_ASSET wrapper, hex. Equal
37
+ * to the input when the script carries no asset wrapper. */
38
+ prefixHex: string;
39
+ /** Parsed asset-transfer data, or null if the input has no asset wrapper. */
40
+ assetTransfer: AssetTransferPayload | null;
41
+ }
42
+ /**
43
+ * Parse an asset-transfer-wrapped scriptPubKey. Accepts both wrapped and
44
+ * bare forms. Throws on structural malformation (truncated pushdata,
45
+ * missing OP_DROP after payload, bad magic, unsupported pushdata width).
46
+ */
47
+ export declare function splitAssetWrappedScriptPubKey(spkHex: string): SplitAssetWrappedResult;
package/dist/browser.js CHANGED
@@ -1539,6 +1539,204 @@ function encodeSellerScriptPubKey(address) {
1539
1539
  throw new Error(`Unsupported seller address type for "${address}"`);
1540
1540
  }
1541
1541
 
1542
+ /**
1543
+ * Asset-transfer wrapper split helper.
1544
+ *
1545
+ * Neurai asset UTXOs have a scriptPubKey of the form
1546
+ *
1547
+ * <prefix scriptPubKey bytes> OP_XNA_ASSET <pushdata(payload)> OP_DROP
1548
+ *
1549
+ * where `prefix` is the recipient's standard script (typically a P2PKH, an
1550
+ * AuthScript witness v1, or a bare covenant such as the partial-fill sell
1551
+ * order), and `payload` serializes a `CAssetTransfer`:
1552
+ *
1553
+ * payload = rvn_prefix (0x72 0x76 0x6e) || type_marker (0x74 transfer)
1554
+ * || VarStr(assetName)
1555
+ * || int64LE(amountRaw)
1556
+ * [ || messageRef (optional) || int64LE(expireTime) (optional) ]
1557
+ *
1558
+ * This helper separates the two halves so consumers can validate the prefix
1559
+ * (e.g. feed it to `parsePartialFillScript`) while independently reading the
1560
+ * asset data displayed to the user. The optional payload tail
1561
+ * (`message` + `expireTime`) is tolerated but not exposed — the first
1562
+ * version only needs `assetName` and `amountRaw`.
1563
+ *
1564
+ * Bare (non-wrapped) scriptPubKeys round-trip through this helper by
1565
+ * returning `prefixHex === input` and `assetTransfer === null`.
1566
+ */
1567
+ const RVN_MAGIC = Uint8Array.from([0x72, 0x76, 0x6e]); // "rvn"
1568
+ const TRANSFER_TYPE = 0x74;
1569
+ /**
1570
+ * Walk the script one opcode at a time, skipping pushdata payload bytes,
1571
+ * until either OP_XNA_ASSET is reached at top level or the end of the
1572
+ * script. Returns the byte offset of OP_XNA_ASSET, or -1 if not found.
1573
+ * Throws on truncated pushdata.
1574
+ */
1575
+ function findTopLevelAssetOpcode(bytes) {
1576
+ let i = 0;
1577
+ while (i < bytes.length) {
1578
+ const op = bytes[i];
1579
+ if (op === OP_XNA_ASSET)
1580
+ return i;
1581
+ // Short direct push: 0x01..0x4b
1582
+ if (op >= 0x01 && op <= 0x4b) {
1583
+ const len = op;
1584
+ const next = i + 1 + len;
1585
+ if (next > bytes.length) {
1586
+ throw new Error(`splitAssetWrappedScriptPubKey: short push of ${len} bytes at offset ${i} exceeds script length`);
1587
+ }
1588
+ i = next;
1589
+ continue;
1590
+ }
1591
+ if (op === OP_PUSHDATA1) {
1592
+ if (i + 1 >= bytes.length) {
1593
+ throw new Error(`splitAssetWrappedScriptPubKey: truncated PUSHDATA1 length at offset ${i}`);
1594
+ }
1595
+ const len = bytes[i + 1];
1596
+ const next = i + 2 + len;
1597
+ if (next > bytes.length) {
1598
+ throw new Error(`splitAssetWrappedScriptPubKey: PUSHDATA1 of ${len} bytes at offset ${i} exceeds script length`);
1599
+ }
1600
+ i = next;
1601
+ continue;
1602
+ }
1603
+ if (op === OP_PUSHDATA2) {
1604
+ if (i + 2 >= bytes.length) {
1605
+ throw new Error(`splitAssetWrappedScriptPubKey: truncated PUSHDATA2 length at offset ${i}`);
1606
+ }
1607
+ const len = bytes[i + 1] | (bytes[i + 2] << 8);
1608
+ const next = i + 3 + len;
1609
+ if (next > bytes.length) {
1610
+ throw new Error(`splitAssetWrappedScriptPubKey: PUSHDATA2 of ${len} bytes at offset ${i} exceeds script length`);
1611
+ }
1612
+ i = next;
1613
+ continue;
1614
+ }
1615
+ if (op === OP_PUSHDATA4) {
1616
+ if (i + 4 >= bytes.length) {
1617
+ throw new Error(`splitAssetWrappedScriptPubKey: truncated PUSHDATA4 length at offset ${i}`);
1618
+ }
1619
+ const len = (bytes[i + 1] |
1620
+ (bytes[i + 2] << 8) |
1621
+ (bytes[i + 3] << 16) |
1622
+ (bytes[i + 4] << 24)) >>>
1623
+ 0;
1624
+ const next = i + 5 + len;
1625
+ if (next > bytes.length) {
1626
+ throw new Error(`splitAssetWrappedScriptPubKey: PUSHDATA4 of ${len} bytes at offset ${i} exceeds script length`);
1627
+ }
1628
+ i = next;
1629
+ continue;
1630
+ }
1631
+ // Any other opcode (including OP_1..OP_16, OP_DUP, OP_HASH160, etc.) is
1632
+ // one byte wide in Neurai Script. Advance.
1633
+ i += 1;
1634
+ }
1635
+ return -1;
1636
+ }
1637
+ /**
1638
+ * Read the pushdata element that sits immediately after OP_XNA_ASSET and
1639
+ * return its payload bytes together with the cursor position after the
1640
+ * element. Only direct pushes (1..75) and PUSHDATA1 are accepted — a
1641
+ * well-formed asset transfer payload fits within 75 bytes for the common
1642
+ * case and comfortably within 255 bytes even with long names and tails.
1643
+ * PUSHDATA2 and PUSHDATA4 would signal a malformed or adversarial wrapper.
1644
+ */
1645
+ function readPayloadPush(bytes, start) {
1646
+ if (start >= bytes.length) {
1647
+ throw new Error('splitAssetWrappedScriptPubKey: truncated wrapper — missing payload push');
1648
+ }
1649
+ const op = bytes[start];
1650
+ if (op >= 0x01 && op <= 0x4b) {
1651
+ const len = op;
1652
+ const dataStart = start + 1;
1653
+ const dataEnd = dataStart + len;
1654
+ if (dataEnd > bytes.length) {
1655
+ throw new Error(`splitAssetWrappedScriptPubKey: asset payload short push of ${len} bytes exceeds script length`);
1656
+ }
1657
+ return { payload: bytes.slice(dataStart, dataEnd), after: dataEnd };
1658
+ }
1659
+ if (op === OP_PUSHDATA1) {
1660
+ if (start + 1 >= bytes.length) {
1661
+ throw new Error('splitAssetWrappedScriptPubKey: truncated PUSHDATA1 in asset payload');
1662
+ }
1663
+ const len = bytes[start + 1];
1664
+ const dataStart = start + 2;
1665
+ const dataEnd = dataStart + len;
1666
+ if (dataEnd > bytes.length) {
1667
+ throw new Error(`splitAssetWrappedScriptPubKey: asset payload PUSHDATA1 of ${len} bytes exceeds script length`);
1668
+ }
1669
+ return { payload: bytes.slice(dataStart, dataEnd), after: dataEnd };
1670
+ }
1671
+ throw new Error(`splitAssetWrappedScriptPubKey: asset payload push opcode 0x${op.toString(16)} not accepted (expected 0x01..0x4b or PUSHDATA1)`);
1672
+ }
1673
+ function parseAssetTransferPayload(payload) {
1674
+ if (payload.length < 4 + 1 + 8) {
1675
+ // 4 magic+type, 1 varstr length, 8 int64LE amount
1676
+ throw new Error(`splitAssetWrappedScriptPubKey: asset payload of ${payload.length} bytes is too short`);
1677
+ }
1678
+ for (let i = 0; i < 3; i += 1) {
1679
+ if (payload[i] !== RVN_MAGIC[i]) {
1680
+ 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)}`);
1681
+ }
1682
+ }
1683
+ if (payload[3] !== TRANSFER_TYPE) {
1684
+ throw new Error(`splitAssetWrappedScriptPubKey: asset payload type marker 0x${payload[3].toString(16)} is not a transfer (0x74)`);
1685
+ }
1686
+ const nameLen = payload[4];
1687
+ const nameStart = 5;
1688
+ const nameEnd = nameStart + nameLen;
1689
+ if (nameEnd + 8 > payload.length) {
1690
+ throw new Error(`splitAssetWrappedScriptPubKey: asset payload truncated — name length ${nameLen} does not leave room for amount`);
1691
+ }
1692
+ let assetName = '';
1693
+ for (let i = nameStart; i < nameEnd; i += 1) {
1694
+ assetName += String.fromCharCode(payload[i]);
1695
+ }
1696
+ let amountRaw = 0n;
1697
+ for (let i = 0; i < 8; i += 1) {
1698
+ amountRaw |= BigInt(payload[nameEnd + i]) << BigInt(8 * i);
1699
+ }
1700
+ // Any bytes after nameEnd + 8 form the optional tail (message +
1701
+ // expireTime). We intentionally ignore them in this first version; the
1702
+ // raw payload remains available via `payloadHex` if a consumer later
1703
+ // needs to inspect them.
1704
+ return { assetName, amountRaw };
1705
+ }
1706
+ /**
1707
+ * Parse an asset-transfer-wrapped scriptPubKey. Accepts both wrapped and
1708
+ * bare forms. Throws on structural malformation (truncated pushdata,
1709
+ * missing OP_DROP after payload, bad magic, unsupported pushdata width).
1710
+ */
1711
+ function splitAssetWrappedScriptPubKey(spkHex) {
1712
+ const normalized = ensureHex(spkHex, 'scriptPubKey');
1713
+ const bytes = hexToBytes(normalized);
1714
+ const assetOpAt = findTopLevelAssetOpcode(bytes);
1715
+ if (assetOpAt < 0) {
1716
+ return { prefixHex: normalized, assetTransfer: null };
1717
+ }
1718
+ const prefix = bytes.slice(0, assetOpAt);
1719
+ const { payload, after } = readPayloadPush(bytes, assetOpAt + 1);
1720
+ if (after >= bytes.length) {
1721
+ throw new Error('splitAssetWrappedScriptPubKey: asset wrapper missing trailing OP_DROP');
1722
+ }
1723
+ if (bytes[after] !== OP_DROP) {
1724
+ throw new Error(`splitAssetWrappedScriptPubKey: expected OP_DROP at offset ${after}, got 0x${bytes[after].toString(16)}`);
1725
+ }
1726
+ if (after + 1 !== bytes.length) {
1727
+ throw new Error(`splitAssetWrappedScriptPubKey: ${bytes.length - after - 1} trailing bytes after OP_DROP`);
1728
+ }
1729
+ const { assetName, amountRaw } = parseAssetTransferPayload(payload);
1730
+ return {
1731
+ prefixHex: bytesToHex(prefix),
1732
+ assetTransfer: {
1733
+ assetName,
1734
+ amountRaw,
1735
+ payloadHex: bytesToHex(payload),
1736
+ },
1737
+ };
1738
+ }
1739
+
1542
1740
  /**
1543
1741
  * Partial-Fill Sell Order covenant script.
1544
1742
  *
@@ -2269,5 +2467,5 @@ function parsePartialFillScriptPQ(script, network = 'xna-test') {
2269
2467
  };
2270
2468
  }
2271
2469
 
2272
- export { AUTHSCRIPT_LEGACY, AUTHSCRIPT_NOAUTH, AUTHSCRIPT_PQ, AUTHSCRIPT_REF, DEFAULT_PQ_TXHASH_SELECTOR, MULTISIG_MAX_PUBKEYS, NULLDATA_STANDARD_MAX_SIZE, ScriptBuilder, buildAuthScriptWitnessLegacy, buildAuthScriptWitnessNoAuth, buildAuthScriptWitnessPQ, buildAuthScriptWitnessRef, buildCancelScriptSig, buildCancelScriptSigHex, buildCancelScriptSigPQ, buildCancelScriptSigPQHex, buildFillScriptSig, buildFillScriptSigHex, buildPartialFillScript, buildPartialFillScriptHex, buildPartialFillScriptPQ, buildPartialFillScriptPQHex, bytesEqual, bytesToHex, concatBytes, encodeAuthScriptScriptPubKey, encodeMultisigRedeemScript, encodeMultisigRedeemScriptHex, encodeNullDataScript, encodeP2PKHScriptPubKey, encodeP2SHScriptPubKey, encodeP2WPKHScriptPubKey, encodeP2WSHScriptPubKey, encodeScriptNum, encodeSellerScriptPubKey, ensureHex, hexToBytes, isPartialFillScriptPQ, opcodes, parsePartialFillScript, parsePartialFillScriptPQ, pushBytes, pushHex, pushInt };
2470
+ export { AUTHSCRIPT_LEGACY, AUTHSCRIPT_NOAUTH, AUTHSCRIPT_PQ, AUTHSCRIPT_REF, DEFAULT_PQ_TXHASH_SELECTOR, MULTISIG_MAX_PUBKEYS, NULLDATA_STANDARD_MAX_SIZE, ScriptBuilder, buildAuthScriptWitnessLegacy, buildAuthScriptWitnessNoAuth, buildAuthScriptWitnessPQ, buildAuthScriptWitnessRef, buildCancelScriptSig, buildCancelScriptSigHex, buildCancelScriptSigPQ, buildCancelScriptSigPQHex, buildFillScriptSig, buildFillScriptSigHex, buildPartialFillScript, buildPartialFillScriptHex, buildPartialFillScriptPQ, buildPartialFillScriptPQHex, bytesEqual, bytesToHex, concatBytes, encodeAuthScriptScriptPubKey, encodeMultisigRedeemScript, encodeMultisigRedeemScriptHex, encodeNullDataScript, encodeP2PKHScriptPubKey, encodeP2SHScriptPubKey, encodeP2WPKHScriptPubKey, encodeP2WSHScriptPubKey, encodeScriptNum, encodeSellerScriptPubKey, ensureHex, hexToBytes, isPartialFillScriptPQ, opcodes, parsePartialFillScript, parsePartialFillScriptPQ, pushBytes, pushHex, pushInt, splitAssetWrappedScriptPubKey };
2273
2471
  //# sourceMappingURL=browser.js.map