@neuraiproject/neurai-scripts 0.7.2 → 0.8.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 +26 -0
- package/dist/NeuraiScripts.global.js +22 -9
- package/dist/NeuraiScripts.global.js.map +1 -1
- package/dist/asset-wrapper.d.ts +11 -2
- package/dist/browser.js +22 -9
- package/dist/browser.js.map +1 -1
- package/dist/index.cjs +22 -9
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +22 -9
- package/dist/index.js.map +1 -1
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -85,6 +85,32 @@ import { ScriptBuilder, opcodes, encodeP2WPKHScriptPubKey, buildPartialFillScrip
|
|
|
85
85
|
|
|
86
86
|
---
|
|
87
87
|
|
|
88
|
+
## Asset-transfer wrappers and NIP-040
|
|
89
|
+
|
|
90
|
+
`splitAssetWrappedScriptPubKey(scriptPubKeyHex)` separates a spendable script
|
|
91
|
+
prefix from its `OP_XNA_ASSET <payload> OP_DROP` transfer wrapper. It returns
|
|
92
|
+
the original prefix as `prefixHex` and either `assetTransfer: null` or:
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
{
|
|
96
|
+
assetName: string;
|
|
97
|
+
amountRaw: bigint;
|
|
98
|
+
marker: 'rvn' | 'xna';
|
|
99
|
+
payloadHex: string;
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
NIP-040 changed the marker used by newly created asset outputs from `rvn` to
|
|
104
|
+
`xna`. The parser always accepts both because pre-activation UTXOs remain part
|
|
105
|
+
of chain history and stay spendable. Parsing therefore needs no network,
|
|
106
|
+
height, or activation flag; `marker` reports what was actually encoded.
|
|
107
|
+
|
|
108
|
+
The helper remains intentionally limited to transfer payloads (`0x74`).
|
|
109
|
+
Issuance, owner, and reissue payload types are outside this API and continue to
|
|
110
|
+
raise `not a transfer`.
|
|
111
|
+
|
|
112
|
+
---
|
|
113
|
+
|
|
88
114
|
## Core primitives
|
|
89
115
|
|
|
90
116
|
### Opcode constants (`./core/opcodes.ts`)
|
|
@@ -1610,11 +1610,16 @@ var NeuraiScriptsBundle = (function (exports) {
|
|
|
1610
1610
|
* AuthScript witness v1, or a bare covenant such as the partial-fill sell
|
|
1611
1611
|
* order), and `payload` serializes a `CAssetTransfer`:
|
|
1612
1612
|
*
|
|
1613
|
-
* payload =
|
|
1613
|
+
* payload = marker ("rvn" 0x72 0x76 0x6e | "xna" 0x78 0x6e 0x61)
|
|
1614
|
+
* || type_marker (0x74 transfer)
|
|
1614
1615
|
* || VarStr(assetName)
|
|
1615
1616
|
* || int64LE(amountRaw)
|
|
1616
1617
|
* [ || messageRef (optional) || int64LE(expireTime) (optional) ]
|
|
1617
1618
|
*
|
|
1619
|
+
* NIP-040: mainnet still emits the Ravencoin-inherited "rvn" marker;
|
|
1620
|
+
* testnet/regtest emit "xna" after activation. Both are accepted when
|
|
1621
|
+
* reading — the detected marker is surfaced on the parsed payload.
|
|
1622
|
+
*
|
|
1618
1623
|
* This helper separates the two halves so consumers can validate the prefix
|
|
1619
1624
|
* (e.g. feed it to `parsePartialFillScript`) while independently reading the
|
|
1620
1625
|
* asset data displayed to the user. The optional payload tail
|
|
@@ -1624,7 +1629,10 @@ var NeuraiScriptsBundle = (function (exports) {
|
|
|
1624
1629
|
* Bare (non-wrapped) scriptPubKeys round-trip through this helper by
|
|
1625
1630
|
* returning `prefixHex === input` and `assetTransfer === null`.
|
|
1626
1631
|
*/
|
|
1627
|
-
const
|
|
1632
|
+
const ASSET_MAGICS = [
|
|
1633
|
+
{ marker: 'rvn', bytes: Uint8Array.from([0x72, 0x76, 0x6e]) },
|
|
1634
|
+
{ marker: 'xna', bytes: Uint8Array.from([0x78, 0x6e, 0x61]) },
|
|
1635
|
+
];
|
|
1628
1636
|
const TRANSFER_TYPE = 0x74;
|
|
1629
1637
|
/**
|
|
1630
1638
|
* Walk the script one opcode at a time, skipping pushdata payload bytes,
|
|
@@ -1730,16 +1738,20 @@ var NeuraiScriptsBundle = (function (exports) {
|
|
|
1730
1738
|
}
|
|
1731
1739
|
throw new Error(`splitAssetWrappedScriptPubKey: asset payload push opcode 0x${op.toString(16)} not accepted (expected 0x01..0x4b or PUSHDATA1)`);
|
|
1732
1740
|
}
|
|
1741
|
+
function detectAssetMarker(payload) {
|
|
1742
|
+
for (const { marker, bytes } of ASSET_MAGICS) {
|
|
1743
|
+
if (payload[0] === bytes[0] && payload[1] === bytes[1] && payload[2] === bytes[2]) {
|
|
1744
|
+
return marker;
|
|
1745
|
+
}
|
|
1746
|
+
}
|
|
1747
|
+
throw new Error(`splitAssetWrappedScriptPubKey: asset payload magic mismatch — expected "rvn" or "xna" got 0x${payload[0].toString(16)} 0x${payload[1].toString(16)} 0x${payload[2].toString(16)}`);
|
|
1748
|
+
}
|
|
1733
1749
|
function parseAssetTransferPayload(payload) {
|
|
1734
1750
|
if (payload.length < 4 + 1 + 8) {
|
|
1735
1751
|
// 4 magic+type, 1 varstr length, 8 int64LE amount
|
|
1736
1752
|
throw new Error(`splitAssetWrappedScriptPubKey: asset payload of ${payload.length} bytes is too short`);
|
|
1737
1753
|
}
|
|
1738
|
-
|
|
1739
|
-
if (payload[i] !== RVN_MAGIC[i]) {
|
|
1740
|
-
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)}`);
|
|
1741
|
-
}
|
|
1742
|
-
}
|
|
1754
|
+
const marker = detectAssetMarker(payload);
|
|
1743
1755
|
if (payload[3] !== TRANSFER_TYPE) {
|
|
1744
1756
|
throw new Error(`splitAssetWrappedScriptPubKey: asset payload type marker 0x${payload[3].toString(16)} is not a transfer (0x74)`);
|
|
1745
1757
|
}
|
|
@@ -1761,7 +1773,7 @@ var NeuraiScriptsBundle = (function (exports) {
|
|
|
1761
1773
|
// expireTime). We intentionally ignore them in this first version; the
|
|
1762
1774
|
// raw payload remains available via `payloadHex` if a consumer later
|
|
1763
1775
|
// needs to inspect them.
|
|
1764
|
-
return { assetName, amountRaw };
|
|
1776
|
+
return { assetName, amountRaw, marker };
|
|
1765
1777
|
}
|
|
1766
1778
|
/**
|
|
1767
1779
|
* Parse an asset-transfer-wrapped scriptPubKey. Accepts both wrapped and
|
|
@@ -1786,12 +1798,13 @@ var NeuraiScriptsBundle = (function (exports) {
|
|
|
1786
1798
|
if (after + 1 !== bytes.length) {
|
|
1787
1799
|
throw new Error(`splitAssetWrappedScriptPubKey: ${bytes.length - after - 1} trailing bytes after OP_DROP`);
|
|
1788
1800
|
}
|
|
1789
|
-
const { assetName, amountRaw } = parseAssetTransferPayload(payload);
|
|
1801
|
+
const { assetName, amountRaw, marker } = parseAssetTransferPayload(payload);
|
|
1790
1802
|
return {
|
|
1791
1803
|
prefixHex: bytesToHex(prefix),
|
|
1792
1804
|
assetTransfer: {
|
|
1793
1805
|
assetName,
|
|
1794
1806
|
amountRaw,
|
|
1807
|
+
marker,
|
|
1795
1808
|
payloadHex: bytesToHex(payload),
|
|
1796
1809
|
},
|
|
1797
1810
|
};
|