@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.
@@ -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
  *
@@ -2319,7 +2517,8 @@ var NeuraiScriptsBundle = (function (exports) {
2319
2517
  parsePartialFillScriptPQ: parsePartialFillScriptPQ,
2320
2518
  pushBytes: pushBytes,
2321
2519
  pushHex: pushHex,
2322
- pushInt: pushInt
2520
+ pushInt: pushInt,
2521
+ splitAssetWrappedScriptPubKey: splitAssetWrappedScriptPubKey
2323
2522
  });
2324
2523
 
2325
2524
  const globalTarget = globalThis;