@secondlayer/stacks 0.0.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 +92 -0
- package/dist/shared/stacks-02actsrx.cjs +17 -0
- package/dist/shared/stacks-02actsrx.cjs.map +10 -0
- package/dist/shared/stacks-1aqm7c2h.cjs +110 -0
- package/dist/shared/stacks-1aqm7c2h.cjs.map +11 -0
- package/dist/shared/stacks-24h5k94q.cjs +74 -0
- package/dist/shared/stacks-24h5k94q.cjs.map +11 -0
- package/dist/shared/stacks-2ar4prfz.cjs +328 -0
- package/dist/shared/stacks-2ar4prfz.cjs.map +11 -0
- package/dist/shared/stacks-4py5pm0r.cjs +237 -0
- package/dist/shared/stacks-4py5pm0r.cjs.map +10 -0
- package/dist/shared/stacks-5tprhns9.cjs +54 -0
- package/dist/shared/stacks-5tprhns9.cjs.map +11 -0
- package/dist/shared/stacks-61rvzkz1.cjs +132 -0
- package/dist/shared/stacks-61rvzkz1.cjs.map +17 -0
- package/dist/shared/stacks-709tra19.js +30 -0
- package/dist/shared/stacks-709tra19.js.map +10 -0
- package/dist/shared/stacks-74k8cxgb.cjs +567 -0
- package/dist/shared/stacks-74k8cxgb.cjs.map +13 -0
- package/dist/shared/stacks-8m9byk2j.js +7 -0
- package/dist/shared/stacks-8m9byk2j.js.map +9 -0
- package/dist/shared/stacks-8yykgdww.js +125 -0
- package/dist/shared/stacks-8yykgdww.js.map +11 -0
- package/dist/shared/stacks-9g0jhab3.cjs +40 -0
- package/dist/shared/stacks-9g0jhab3.cjs.map +10 -0
- package/dist/shared/stacks-an5k8n4t.cjs +291 -0
- package/dist/shared/stacks-an5k8n4t.cjs.map +23 -0
- package/dist/shared/stacks-b008c3w8.cjs +214 -0
- package/dist/shared/stacks-b008c3w8.cjs.map +11 -0
- package/dist/shared/stacks-bnvfmt7y.cjs +31 -0
- package/dist/shared/stacks-bnvfmt7y.cjs.map +9 -0
- package/dist/shared/stacks-bycy6t9k.js +22 -0
- package/dist/shared/stacks-bycy6t9k.js.map +10 -0
- package/dist/shared/stacks-d1fmkv3p.js +258 -0
- package/dist/shared/stacks-d1fmkv3p.js.map +13 -0
- package/dist/shared/stacks-dr5en54r.cjs +119 -0
- package/dist/shared/stacks-dr5en54r.cjs.map +10 -0
- package/dist/shared/stacks-dxhntr76.js +278 -0
- package/dist/shared/stacks-dxhntr76.js.map +10 -0
- package/dist/shared/stacks-evnzth1q.cjs +35 -0
- package/dist/shared/stacks-evnzth1q.cjs.map +10 -0
- package/dist/shared/stacks-f3tk2k5s.js +27 -0
- package/dist/shared/stacks-f3tk2k5s.js.map +9 -0
- package/dist/shared/stacks-f7wdrzs3.js +285 -0
- package/dist/shared/stacks-f7wdrzs3.js.map +11 -0
- package/dist/shared/stacks-fcaefc5b.cjs +16 -0
- package/dist/shared/stacks-fcaefc5b.cjs.map +10 -0
- package/dist/shared/stacks-g9mcjcqg.cjs +3 -0
- package/dist/shared/stacks-g9mcjcqg.cjs.map +9 -0
- package/dist/shared/stacks-gck1sq7h.cjs +28 -0
- package/dist/shared/stacks-gck1sq7h.cjs.map +10 -0
- package/dist/shared/stacks-gjvbq6w7.js +46 -0
- package/dist/shared/stacks-gjvbq6w7.js.map +10 -0
- package/dist/shared/stacks-j2cy7qtt.js +237 -0
- package/dist/shared/stacks-j2cy7qtt.js.map +11 -0
- package/dist/shared/stacks-k08tgzyp.cjs +232 -0
- package/dist/shared/stacks-k08tgzyp.cjs.map +13 -0
- package/dist/shared/stacks-k2p5tv6x.js +37 -0
- package/dist/shared/stacks-k2p5tv6x.js.map +10 -0
- package/dist/shared/stacks-k58k970n.js +234 -0
- package/dist/shared/stacks-k58k970n.js.map +10 -0
- package/dist/shared/stacks-n2g56wgg.js +338 -0
- package/dist/shared/stacks-n2g56wgg.js.map +23 -0
- package/dist/shared/stacks-nqytwfcc.js +243 -0
- package/dist/shared/stacks-nqytwfcc.js.map +11 -0
- package/dist/shared/stacks-qcae5852.js +19 -0
- package/dist/shared/stacks-qcae5852.js.map +10 -0
- package/dist/shared/stacks-qt8ehebn.js +76 -0
- package/dist/shared/stacks-qt8ehebn.js.map +11 -0
- package/dist/shared/stacks-qvg7c24g.cjs +232 -0
- package/dist/shared/stacks-qvg7c24g.cjs.map +10 -0
- package/dist/shared/stacks-r1t6g3c3.cjs +241 -0
- package/dist/shared/stacks-r1t6g3c3.cjs.map +11 -0
- package/dist/shared/stacks-tjxmrssa.js +282 -0
- package/dist/shared/stacks-tjxmrssa.js.map +11 -0
- package/dist/shared/stacks-v9jqcpd4.js +18 -0
- package/dist/shared/stacks-v9jqcpd4.js.map +10 -0
- package/dist/shared/stacks-vdfag81a.cjs +121 -0
- package/dist/shared/stacks-vdfag81a.cjs.map +10 -0
- package/dist/shared/stacks-vdfs3e5v.js +134 -0
- package/dist/shared/stacks-vdfs3e5v.js.map +10 -0
- package/dist/shared/stacks-y3dh7hv1.js +60 -0
- package/dist/shared/stacks-y3dh7hv1.js.map +11 -0
- package/dist/shared/stacks-ya3x01yd.cjs +16 -0
- package/dist/shared/stacks-ya3x01yd.cjs.map +10 -0
- package/dist/shared/stacks-yfgsm6ap.js +599 -0
- package/dist/shared/stacks-yfgsm6ap.js.map +13 -0
- package/dist/shared/stacks-yfveb68e.js +121 -0
- package/dist/shared/stacks-yfveb68e.js.map +10 -0
- package/dist/shared/stacks-zm0b3tgy.js +160 -0
- package/dist/shared/stacks-zm0b3tgy.js.map +17 -0
- package/dist/shared/stacks-zyv9qqme.cjs +249 -0
- package/dist/shared/stacks-zyv9qqme.cjs.map +11 -0
- package/dist/src/accounts/index.cjs +90 -0
- package/dist/src/accounts/index.cjs.map +12 -0
- package/dist/src/accounts/index.d.cts +47 -0
- package/dist/src/accounts/index.d.ts +47 -0
- package/dist/src/accounts/index.js +104 -0
- package/dist/src/accounts/index.js.map +12 -0
- package/dist/src/actions/index.cjs +36 -0
- package/dist/src/actions/index.cjs.map +9 -0
- package/dist/src/actions/index.d.cts +886 -0
- package/dist/src/actions/index.d.ts +886 -0
- package/dist/src/actions/index.js +81 -0
- package/dist/src/actions/index.js.map +9 -0
- package/dist/src/bns/index.cjs +590 -0
- package/dist/src/bns/index.cjs.map +15 -0
- package/dist/src/bns/index.d.cts +241 -0
- package/dist/src/bns/index.d.ts +241 -0
- package/dist/src/bns/index.js +611 -0
- package/dist/src/bns/index.js.map +15 -0
- package/dist/src/chains/index.cjs +13 -0
- package/dist/src/chains/index.cjs.map +9 -0
- package/dist/src/chains/index.d.cts +53 -0
- package/dist/src/chains/index.d.ts +53 -0
- package/dist/src/chains/index.js +18 -0
- package/dist/src/chains/index.js.map +9 -0
- package/dist/src/clarity/index.cjs +3 -0
- package/dist/src/clarity/index.cjs.map +9 -0
- package/dist/src/clarity/index.d.cts +821 -0
- package/dist/src/clarity/index.d.ts +821 -0
- package/dist/src/clarity/index.js +145 -0
- package/dist/src/clarity/index.js.map +9 -0
- package/dist/src/connect/index.cjs +480 -0
- package/dist/src/connect/index.cjs.map +19 -0
- package/dist/src/connect/index.d.cts +110 -0
- package/dist/src/connect/index.d.ts +110 -0
- package/dist/src/connect/index.js +448 -0
- package/dist/src/connect/index.js.map +19 -0
- package/dist/src/connect/walletconnect/index.cjs +1322 -0
- package/dist/src/connect/walletconnect/index.cjs.map +18 -0
- package/dist/src/connect/walletconnect/index.d.cts +132 -0
- package/dist/src/connect/walletconnect/index.d.ts +132 -0
- package/dist/src/connect/walletconnect/index.js +1290 -0
- package/dist/src/connect/walletconnect/index.js.map +18 -0
- package/dist/src/index.cjs +557 -0
- package/dist/src/index.cjs.map +21 -0
- package/dist/src/index.d.cts +847 -0
- package/dist/src/index.d.ts +847 -0
- package/dist/src/index.js +629 -0
- package/dist/src/index.js.map +21 -0
- package/dist/src/postconditions/index.cjs +3 -0
- package/dist/src/postconditions/index.cjs.map +9 -0
- package/dist/src/postconditions/index.d.cts +91 -0
- package/dist/src/postconditions/index.d.ts +91 -0
- package/dist/src/postconditions/index.js +12 -0
- package/dist/src/postconditions/index.js.map +9 -0
- package/dist/src/pox/index.cjs +860 -0
- package/dist/src/pox/index.cjs.map +15 -0
- package/dist/src/pox/index.d.cts +244 -0
- package/dist/src/pox/index.d.ts +244 -0
- package/dist/src/pox/index.js +877 -0
- package/dist/src/pox/index.js.map +15 -0
- package/dist/src/stackingdao/index.cjs +300 -0
- package/dist/src/stackingdao/index.cjs.map +13 -0
- package/dist/src/stackingdao/index.d.cts +179 -0
- package/dist/src/stackingdao/index.d.ts +179 -0
- package/dist/src/stackingdao/index.js +318 -0
- package/dist/src/stackingdao/index.js.map +13 -0
- package/dist/src/subscriptions/index.cjs +14 -0
- package/dist/src/subscriptions/index.cjs.map +9 -0
- package/dist/src/subscriptions/index.d.cts +250 -0
- package/dist/src/subscriptions/index.d.ts +250 -0
- package/dist/src/subscriptions/index.js +21 -0
- package/dist/src/subscriptions/index.js.map +9 -0
- package/dist/src/transactions/index.cjs +50 -0
- package/dist/src/transactions/index.cjs.map +9 -0
- package/dist/src/transactions/index.d.cts +412 -0
- package/dist/src/transactions/index.d.ts +412 -0
- package/dist/src/transactions/index.js +101 -0
- package/dist/src/transactions/index.js.map +9 -0
- package/dist/src/utils/index.cjs +168 -0
- package/dist/src/utils/index.cjs.map +11 -0
- package/dist/src/utils/index.d.cts +140 -0
- package/dist/src/utils/index.d.ts +140 -0
- package/dist/src/utils/index.js +222 -0
- package/dist/src/utils/index.js.map +11 -0
- package/package.json +105 -0
package/README.md
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# @secondlayer/stacks
|
|
2
|
+
|
|
3
|
+
A viem-style SDK for the Stacks blockchain. One package, zero polyfills, full tree-shaking.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bun add @secondlayer/stacks
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { createPublicClient, http } from "@secondlayer/stacks";
|
|
15
|
+
import { mainnet } from "@secondlayer/stacks/chains";
|
|
16
|
+
|
|
17
|
+
const client = createPublicClient({
|
|
18
|
+
chain: mainnet,
|
|
19
|
+
transport: http(),
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
const balance = await client.getBalance({
|
|
23
|
+
address: "SP2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKNRV9EJ7",
|
|
24
|
+
});
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Exports
|
|
28
|
+
|
|
29
|
+
| Module | Description |
|
|
30
|
+
|---|---|
|
|
31
|
+
| `@secondlayer/stacks` | Clients, transports, re-exports |
|
|
32
|
+
| `@secondlayer/stacks/accounts` | `privateKeyToAccount`, `mnemonicToAccount`, wallet providers |
|
|
33
|
+
| `@secondlayer/stacks/chains` | `mainnet`, `testnet`, `devnet`, `defineChain` |
|
|
34
|
+
| `@secondlayer/stacks/clarity` | `Cl.*` constructors, serialization, ABI type system |
|
|
35
|
+
| `@secondlayer/stacks/actions` | `readContract`, `callContract`, `transferStx`, `getContract`, `multicall` |
|
|
36
|
+
| `@secondlayer/stacks/transactions` | Build, sign, serialize transactions, multi-sig |
|
|
37
|
+
| `@secondlayer/stacks/postconditions` | `Pc` fluent builder for post-conditions |
|
|
38
|
+
| `@secondlayer/stacks/connect` | Wallet connection — browser extensions + `setProvider` |
|
|
39
|
+
| `@secondlayer/stacks/connect/walletconnect` | WalletConnect v2 — native relay, QR, modal |
|
|
40
|
+
| `@secondlayer/stacks/subscriptions` | `watchBlocks`, `watchMempool`, `watchTransaction` |
|
|
41
|
+
| `@secondlayer/stacks/utils` | Encoding, hashing, addresses, unit formatting |
|
|
42
|
+
| `@secondlayer/stacks/bns` | BNS name registration, resolution, zonefiles |
|
|
43
|
+
| `@secondlayer/stacks/pox` | PoX stacking — solo and delegated |
|
|
44
|
+
| `@secondlayer/stacks/stackingdao` | StackingDAO liquid staking (STX/stSTX) |
|
|
45
|
+
|
|
46
|
+
## WalletConnect v2
|
|
47
|
+
|
|
48
|
+
Native WC v2 implementation — X25519 ECDH, AES-256-GCM envelope encryption, Ed25519 JWT relay auth. Zero cost if you don't import it, tree-shakes completely.
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
import { connect, setProvider } from "@secondlayer/stacks/connect";
|
|
52
|
+
import { WalletConnectProvider, showModal } from "@secondlayer/stacks/connect/walletconnect";
|
|
53
|
+
|
|
54
|
+
const wc = new WalletConnectProvider({
|
|
55
|
+
projectId: "your-reown-project-id", // from cloud.reown.com
|
|
56
|
+
metadata: { name: "My App", description: "...", url: "https://myapp.com", icons: [] },
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// Restore existing session or pair new one
|
|
60
|
+
if (!wc.restore()) {
|
|
61
|
+
const { uri, approval } = await wc.pair();
|
|
62
|
+
showModal({ wcUri: uri, onClose: () => {} });
|
|
63
|
+
await approval;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
setProvider(wc);
|
|
67
|
+
const { addresses } = await connect();
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
The built-in modal shows browser extension wallets alongside the WC QR code — users pick whichever they prefer.
|
|
71
|
+
|
|
72
|
+
## Bundle Size
|
|
73
|
+
|
|
74
|
+
Measured with `bun build --minify --target=browser`, gzipped. Compared against stacks.js v7.3.1 + @stacks/connect v8.2.4.
|
|
75
|
+
|
|
76
|
+
```
|
|
77
|
+
@secondlayer/stacks stacks.js + connect
|
|
78
|
+
─────────────────── ───────────────────
|
|
79
|
+
SDK (gzipped) 23.8 KB 189 KB
|
|
80
|
+
+ Connect +7.9 KB +347 KB
|
|
81
|
+
+ WalletConnect v2 +25.5 KB (included)
|
|
82
|
+
─────────────────── ───────────────────
|
|
83
|
+
Total 46.1 KB 536 KB ← 11.6x
|
|
84
|
+
Dependencies 6 294 ← 49x
|
|
85
|
+
node_modules 7 MB 351 MB ← 50x
|
|
86
|
+
Polyfills needed none Buffer, crypto
|
|
87
|
+
Packages to install 1 5+
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
6 runtime deps, all `@noble`/`@scure`: `@noble/hashes`, `@noble/secp256k1`, `@noble/curves`, `@noble/ciphers`, `@scure/bip32`, `@scure/bip39`.
|
|
91
|
+
|
|
92
|
+
Connect and WalletConnect are separate entry points — import only what you use. An app that just reads contracts pays 23.8 KB. Full wallet connection + WC v2 pays 46.1 KB. The equivalent stacks.js setup is 536 KB regardless.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// src/accounts/providerToAccount.ts
|
|
2
|
+
async function providerToAccount(provider) {
|
|
3
|
+
const result = await provider.request("stx_getAddresses");
|
|
4
|
+
const entry = result?.addresses?.[0];
|
|
5
|
+
if (!entry?.address || !entry?.publicKey) {
|
|
6
|
+
throw new Error("Provider did not return a valid address");
|
|
7
|
+
}
|
|
8
|
+
return {
|
|
9
|
+
type: "provider",
|
|
10
|
+
address: entry.address,
|
|
11
|
+
publicKey: entry.publicKey,
|
|
12
|
+
provider
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
//# debugId=92C250F1EE7F3F3064756E2164756E21
|
|
17
|
+
//# sourceMappingURL=stacks-02actsrx.cjs.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/accounts/providerToAccount.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import type { StacksProvider, ProviderAccount } from \"./types.ts\";\n\n/** Create a ProviderAccount by querying the wallet for addresses */\nexport async function providerToAccount(\n provider: StacksProvider\n): Promise<ProviderAccount> {\n const result = await provider.request(\"stx_getAddresses\");\n\n const entry = result?.addresses?.[0];\n if (!entry?.address || !entry?.publicKey) {\n throw new Error(\"Provider did not return a valid address\");\n }\n\n return {\n type: \"provider\",\n address: entry.address,\n publicKey: entry.publicKey,\n provider,\n };\n}\n"
|
|
6
|
+
],
|
|
7
|
+
"mappings": ";AAGA,eAAsB,iBAAiB,CACrC,UAC0B;AAAA,EAC1B,MAAM,SAAS,MAAM,SAAS,QAAQ,kBAAkB;AAAA,EAExD,MAAM,QAAQ,QAAQ,YAAY;AAAA,EAClC,IAAI,CAAC,OAAO,WAAW,CAAC,OAAO,WAAW;AAAA,IACxC,MAAM,IAAI,MAAM,yCAAyC;AAAA,EAC3D;AAAA,EAEA,OAAO;AAAA,IACL,MAAM;AAAA,IACN,SAAS,MAAM;AAAA,IACf,WAAW,MAAM;AAAA,IACjB;AAAA,EACF;AAAA;",
|
|
8
|
+
"debugId": "92C250F1EE7F3F3064756E2164756E21",
|
|
9
|
+
"names": []
|
|
10
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// src/clarity/types.ts
|
|
2
|
+
var ClarityWireType = {
|
|
3
|
+
int: 0,
|
|
4
|
+
uint: 1,
|
|
5
|
+
buffer: 2,
|
|
6
|
+
true: 3,
|
|
7
|
+
false: 4,
|
|
8
|
+
address: 5,
|
|
9
|
+
contract: 6,
|
|
10
|
+
ok: 7,
|
|
11
|
+
err: 8,
|
|
12
|
+
none: 9,
|
|
13
|
+
some: 10,
|
|
14
|
+
list: 11,
|
|
15
|
+
tuple: 12,
|
|
16
|
+
ascii: 13,
|
|
17
|
+
utf8: 14
|
|
18
|
+
};
|
|
19
|
+
var wireTypeToClarity = Object.fromEntries(Object.entries(ClarityWireType).map(([k, v]) => [v, k]));
|
|
20
|
+
function clarityTypeFromByte(byte) {
|
|
21
|
+
const type = wireTypeToClarity[byte];
|
|
22
|
+
if (!type)
|
|
23
|
+
throw new Error(`Unknown clarity wire type: ${byte}`);
|
|
24
|
+
return type;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// src/clarity/serialize.ts
|
|
28
|
+
var CLARITY_INT_SIZE = 128n;
|
|
29
|
+
var CLARITY_INT_BYTE_SIZE = 16;
|
|
30
|
+
function typeIdByte(type) {
|
|
31
|
+
return ClarityWireType[type];
|
|
32
|
+
}
|
|
33
|
+
function withTypeId(type, bytes) {
|
|
34
|
+
return concatBytes(new Uint8Array([typeIdByte(type)]), bytes);
|
|
35
|
+
}
|
|
36
|
+
function serializeAddress(c32Address) {
|
|
37
|
+
const [version, hash160] = c32addressDecode(c32Address);
|
|
38
|
+
return concatBytes(new Uint8Array([version]), hexToBytes(hash160));
|
|
39
|
+
}
|
|
40
|
+
function serializeLPString(str, prefixBytes = 1) {
|
|
41
|
+
const content = utf8ToBytes(str);
|
|
42
|
+
const lengthPrefix = hexToBytes(intToHex(content.byteLength, prefixBytes));
|
|
43
|
+
return concatBytes(lengthPrefix, content);
|
|
44
|
+
}
|
|
45
|
+
function serializeCVBytes(value) {
|
|
46
|
+
switch (value.type) {
|
|
47
|
+
case "true":
|
|
48
|
+
case "false":
|
|
49
|
+
return new Uint8Array([typeIdByte(value.type)]);
|
|
50
|
+
case "int": {
|
|
51
|
+
const bytes = bigIntToBytes(toTwos(BigInt(value.value), CLARITY_INT_SIZE), CLARITY_INT_BYTE_SIZE);
|
|
52
|
+
return withTypeId(value.type, bytes);
|
|
53
|
+
}
|
|
54
|
+
case "uint": {
|
|
55
|
+
const bytes = bigIntToBytes(BigInt(value.value), CLARITY_INT_BYTE_SIZE);
|
|
56
|
+
return withTypeId(value.type, bytes);
|
|
57
|
+
}
|
|
58
|
+
case "buffer": {
|
|
59
|
+
const bufBytes = hexToBytes(value.value);
|
|
60
|
+
return withTypeId(value.type, concatBytes(writeUInt32BE(bufBytes.length), bufBytes));
|
|
61
|
+
}
|
|
62
|
+
case "none":
|
|
63
|
+
return new Uint8Array([typeIdByte(value.type)]);
|
|
64
|
+
case "some":
|
|
65
|
+
return withTypeId(value.type, serializeCVBytes(value.value));
|
|
66
|
+
case "ok":
|
|
67
|
+
case "err":
|
|
68
|
+
return withTypeId(value.type, serializeCVBytes(value.value));
|
|
69
|
+
case "address":
|
|
70
|
+
return withTypeId(value.type, serializeAddress(value.value));
|
|
71
|
+
case "contract": {
|
|
72
|
+
const [addr, name] = value.value.split(".");
|
|
73
|
+
if (!addr || !name)
|
|
74
|
+
throw new Error(`Invalid contract principal: ${value.value}`);
|
|
75
|
+
return withTypeId(value.type, concatBytes(serializeAddress(addr), serializeLPString(name)));
|
|
76
|
+
}
|
|
77
|
+
case "list": {
|
|
78
|
+
const parts = [writeUInt32BE(value.value.length)];
|
|
79
|
+
for (const item of value.value) {
|
|
80
|
+
parts.push(serializeCVBytes(item));
|
|
81
|
+
}
|
|
82
|
+
return withTypeId(value.type, concatBytes(...parts));
|
|
83
|
+
}
|
|
84
|
+
case "tuple": {
|
|
85
|
+
const keys = Object.keys(value.value).sort((a, b) => a.localeCompare(b));
|
|
86
|
+
const parts = [writeUInt32BE(keys.length)];
|
|
87
|
+
for (const key of keys) {
|
|
88
|
+
parts.push(serializeLPString(key));
|
|
89
|
+
parts.push(serializeCVBytes(value.value[key]));
|
|
90
|
+
}
|
|
91
|
+
return withTypeId(value.type, concatBytes(...parts));
|
|
92
|
+
}
|
|
93
|
+
case "ascii": {
|
|
94
|
+
const strBytes = asciiToBytes(value.value);
|
|
95
|
+
return withTypeId(value.type, concatBytes(writeUInt32BE(strBytes.length), strBytes));
|
|
96
|
+
}
|
|
97
|
+
case "utf8": {
|
|
98
|
+
const strBytes = utf8ToBytes(value.value);
|
|
99
|
+
return withTypeId(value.type, concatBytes(writeUInt32BE(strBytes.length), strBytes));
|
|
100
|
+
}
|
|
101
|
+
default:
|
|
102
|
+
throw new Error(`Cannot serialize unknown clarity type`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
function serializeCV(value) {
|
|
106
|
+
return bytesToHex(serializeCVBytes(value));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
//# debugId=2B3A693ED24B1BAF64756E2164756E21
|
|
110
|
+
//# sourceMappingURL=stacks-1aqm7c2h.cjs.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/clarity/types.ts", "../src/clarity/serialize.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"export type ClarityType =\n | \"int\"\n | \"uint\"\n | \"buffer\"\n | \"true\"\n | \"false\"\n | \"address\"\n | \"contract\"\n | \"ok\"\n | \"err\"\n | \"none\"\n | \"some\"\n | \"list\"\n | \"tuple\"\n | \"ascii\"\n | \"utf8\";\n\n/** Wire type IDs matching the Stacks binary format (SIP-005) */\nexport const ClarityWireType = {\n int: 0x00,\n uint: 0x01,\n buffer: 0x02,\n true: 0x03,\n false: 0x04,\n address: 0x05,\n contract: 0x06,\n ok: 0x07,\n err: 0x08,\n none: 0x09,\n some: 0x0a,\n list: 0x0b,\n tuple: 0x0c,\n ascii: 0x0d,\n utf8: 0x0e,\n} as const satisfies Record<ClarityType, number>;\n\n/** Reverse lookup: wire byte → ClarityType string */\nconst wireTypeToClarity = Object.fromEntries(\n Object.entries(ClarityWireType).map(([k, v]) => [v, k])\n) as Record<number, ClarityType>;\n\nexport function clarityTypeFromByte(byte: number): ClarityType {\n const type = wireTypeToClarity[byte];\n if (!type) throw new Error(`Unknown clarity wire type: ${byte}`);\n return type;\n}\n\n// CV types\n\nexport type IntCV = { readonly type: \"int\"; readonly value: bigint };\nexport type UIntCV = { readonly type: \"uint\"; readonly value: bigint };\nexport type BooleanCV = TrueCV | FalseCV;\nexport type TrueCV = { readonly type: \"true\" };\nexport type FalseCV = { readonly type: \"false\" };\nexport type BufferCV = { readonly type: \"buffer\"; readonly value: string }; // hex\nexport type NoneCV = { readonly type: \"none\" };\nexport type SomeCV = {\n readonly type: \"some\";\n readonly value: ClarityValue;\n};\nexport type OptionalCV = NoneCV | SomeCV;\nexport type ResponseOkCV = {\n readonly type: \"ok\";\n readonly value: ClarityValue;\n};\nexport type ResponseErrorCV = {\n readonly type: \"err\";\n readonly value: ClarityValue;\n};\nexport type ResponseCV = ResponseOkCV | ResponseErrorCV;\nexport type StandardPrincipalCV = {\n readonly type: \"address\";\n readonly value: string;\n};\nexport type ContractPrincipalCV = {\n readonly type: \"contract\";\n readonly value: string; // \"address.name\"\n};\nexport type PrincipalCV = StandardPrincipalCV | ContractPrincipalCV;\nexport type ListCV = {\n type: \"list\";\n value: ClarityValue[];\n};\nexport type TupleData = {\n [key: string]: ClarityValue;\n};\nexport type TupleCV = {\n type: \"tuple\";\n value: TupleData;\n};\nexport type StringAsciiCV = {\n readonly type: \"ascii\";\n readonly value: string;\n};\nexport type StringUtf8CV = {\n readonly type: \"utf8\";\n readonly value: string;\n};\n\nexport type ClarityValue =\n | IntCV\n | UIntCV\n | BooleanCV\n | BufferCV\n | NoneCV\n | SomeCV\n | ResponseOkCV\n | ResponseErrorCV\n | StandardPrincipalCV\n | ContractPrincipalCV\n | ListCV\n | TupleCV\n | StringAsciiCV\n | StringUtf8CV;\n",
|
|
6
|
+
"import {\n bytesToHex,\n hexToBytes,\n concatBytes,\n bigIntToBytes,\n toTwos,\n asciiToBytes,\n utf8ToBytes,\n writeUInt32BE,\n intToHex,\n} from \"../utils/encoding.ts\";\nimport { c32addressDecode } from \"../utils/c32.ts\";\nimport { ClarityWireType, type ClarityValue, type ClarityType } from \"./types.ts\";\n\nconst CLARITY_INT_SIZE = 128n;\nconst CLARITY_INT_BYTE_SIZE = 16;\n\nfunction typeIdByte(type: ClarityType): number {\n return ClarityWireType[type];\n}\n\nfunction withTypeId(type: ClarityType, bytes: Uint8Array): Uint8Array {\n return concatBytes(new Uint8Array([typeIdByte(type)]), bytes);\n}\n\nfunction serializeAddress(c32Address: string): Uint8Array {\n const [version, hash160] = c32addressDecode(c32Address);\n return concatBytes(new Uint8Array([version]), hexToBytes(hash160));\n}\n\nfunction serializeLPString(str: string, prefixBytes = 1): Uint8Array {\n const content = utf8ToBytes(str);\n const lengthPrefix = hexToBytes(intToHex(content.byteLength, prefixBytes));\n return concatBytes(lengthPrefix, content);\n}\n\nexport function serializeCVBytes(value: ClarityValue): Uint8Array {\n switch (value.type) {\n case \"true\":\n case \"false\":\n return new Uint8Array([typeIdByte(value.type)]);\n\n case \"int\": {\n const bytes = bigIntToBytes(\n toTwos(BigInt(value.value), CLARITY_INT_SIZE),\n CLARITY_INT_BYTE_SIZE\n );\n return withTypeId(value.type, bytes);\n }\n\n case \"uint\": {\n const bytes = bigIntToBytes(BigInt(value.value), CLARITY_INT_BYTE_SIZE);\n return withTypeId(value.type, bytes);\n }\n\n case \"buffer\": {\n const bufBytes = hexToBytes(value.value);\n return withTypeId(\n value.type,\n concatBytes(writeUInt32BE(bufBytes.length), bufBytes)\n );\n }\n\n case \"none\":\n return new Uint8Array([typeIdByte(value.type)]);\n\n case \"some\":\n return withTypeId(value.type, serializeCVBytes(value.value));\n\n case \"ok\":\n case \"err\":\n return withTypeId(value.type, serializeCVBytes(value.value));\n\n case \"address\":\n return withTypeId(value.type, serializeAddress(value.value));\n\n case \"contract\": {\n const [addr, name] = value.value.split(\".\");\n if (!addr || !name) throw new Error(`Invalid contract principal: ${value.value}`);\n return withTypeId(\n value.type,\n concatBytes(serializeAddress(addr), serializeLPString(name))\n );\n }\n\n case \"list\": {\n const parts: Uint8Array[] = [writeUInt32BE(value.value.length)];\n for (const item of value.value) {\n parts.push(serializeCVBytes(item));\n }\n return withTypeId(value.type, concatBytes(...parts));\n }\n\n case \"tuple\": {\n const keys = Object.keys(value.value).sort((a, b) => a.localeCompare(b));\n const parts: Uint8Array[] = [writeUInt32BE(keys.length)];\n for (const key of keys) {\n parts.push(serializeLPString(key));\n parts.push(serializeCVBytes(value.value[key]!));\n }\n return withTypeId(value.type, concatBytes(...parts));\n }\n\n case \"ascii\": {\n const strBytes = asciiToBytes(value.value);\n return withTypeId(\n value.type,\n concatBytes(writeUInt32BE(strBytes.length), strBytes)\n );\n }\n\n case \"utf8\": {\n const strBytes = utf8ToBytes(value.value);\n return withTypeId(\n value.type,\n concatBytes(writeUInt32BE(strBytes.length), strBytes)\n );\n }\n\n default:\n throw new Error(`Cannot serialize unknown clarity type`);\n }\n}\n\nexport function serializeCV(value: ClarityValue): string {\n return bytesToHex(serializeCVBytes(value));\n}\n"
|
|
7
|
+
],
|
|
8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AAI,CAAC;AAAA,MACnC;AAAA,MACA,OAAO,WAAW,MAAM,MAAM,YAAY,GAAG,KAAK,CAAC;AAAA,IACrD;AAAA,SAEK,SAAS;AAAA,MACZ,MAAM,OAAO,OAAO,KAAK,MAAM,KAAK,EAAE,KAAK,CAAC,GAAG,MAAM,EAAE,cAAc,CAAC,CAAC;AAAA,MACvE,MAAM,QAAsB,CAAC,cAAc,KAAK,MAAM,CAAC;AAAA,MACvD,WAAW,OAAO,MAAM;AAAA,QACtB,MAAM,KAAK,kBAAkB,GAAG,CAAC;AAAA,QACjC,MAAM,KAAK,iBAAiB,MAAM,MAAM,IAAK,CAAC;AAAA,MAChD;AAAA,MACA,OAAO,WAAW,MAAM,MAAM,YAAY,GAAG,KAAK,CAAC;AAAA,IACrD;AAAA,SAEK,SAAS;AAAA,MACZ,MAAM,WAAW,aAAa,MAAM,KAAK;AAAA,MACzC,OAAO,WACL,MAAM,MACN,YAAY,cAAc,SAAS,MAAM,GAAG,QAAQ,CACtD;AAAA,IACF;AAAA,SAEK,QAAQ;AAAA,MACX,MAAM,WAAW,YAAY,MAAM,KAAK;AAAA,MACxC,OAAO,WACL,MAAM,MACN,YAAY,cAAc,SAAS,MAAM,GAAG,QAAQ,CACtD;AAAA,IACF;AAAA;AAAA,MAGE,MAAM,IAAI,MAAM,uCAAuC;AAAA;AAAA;AAItD,SAAS,WAAW,CAAC,OAA6B;AAAA,EACvD,OAAO,WAAW,iBAAiB,KAAK,CAAC;AAAA;",
|
|
9
|
+
"debugId": "2B3A693ED24B1BAF64756E2164756E21",
|
|
10
|
+
"names": []
|
|
11
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// src/chains/definitions.ts
|
|
2
|
+
var mainnet = {
|
|
3
|
+
id: 1,
|
|
4
|
+
name: "Stacks Mainnet",
|
|
5
|
+
network: "mainnet",
|
|
6
|
+
transactionVersion: 0,
|
|
7
|
+
peerNetworkId: 385875968,
|
|
8
|
+
addressVersion: { singleSig: 22, multiSig: 20 },
|
|
9
|
+
magicBytes: "X2",
|
|
10
|
+
bootAddress: "SP000000000000000000002Q6VF78",
|
|
11
|
+
nativeCurrency: { name: "Stacks", symbol: "STX", decimals: 6 },
|
|
12
|
+
rpcUrls: {
|
|
13
|
+
default: {
|
|
14
|
+
http: ["https://api.mainnet.hiro.so"],
|
|
15
|
+
ws: ["wss://api.mainnet.hiro.so/extended/v1/ws"]
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
blockExplorers: {
|
|
19
|
+
default: { name: "Hiro Explorer", url: "https://explorer.hiro.so" }
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
var testnet = {
|
|
23
|
+
id: 2147483648,
|
|
24
|
+
name: "Stacks Testnet",
|
|
25
|
+
network: "testnet",
|
|
26
|
+
transactionVersion: 128,
|
|
27
|
+
peerNetworkId: 4278190080,
|
|
28
|
+
addressVersion: { singleSig: 26, multiSig: 21 },
|
|
29
|
+
magicBytes: "T2",
|
|
30
|
+
bootAddress: "ST000000000000000000002AMW42H",
|
|
31
|
+
nativeCurrency: { name: "Stacks", symbol: "STX", decimals: 6 },
|
|
32
|
+
rpcUrls: {
|
|
33
|
+
default: {
|
|
34
|
+
http: ["https://api.testnet.hiro.so"],
|
|
35
|
+
ws: ["wss://api.testnet.hiro.so/extended/v1/ws"]
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
blockExplorers: {
|
|
39
|
+
default: {
|
|
40
|
+
name: "Hiro Explorer",
|
|
41
|
+
url: "https://explorer.hiro.so/?chain=testnet"
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
var devnet = {
|
|
46
|
+
id: 2147483648,
|
|
47
|
+
name: "Stacks Devnet",
|
|
48
|
+
network: "testnet",
|
|
49
|
+
transactionVersion: 128,
|
|
50
|
+
peerNetworkId: 4278190080,
|
|
51
|
+
addressVersion: { singleSig: 26, multiSig: 21 },
|
|
52
|
+
magicBytes: "id",
|
|
53
|
+
bootAddress: "ST000000000000000000002AMW42H",
|
|
54
|
+
nativeCurrency: { name: "Stacks", symbol: "STX", decimals: 6 },
|
|
55
|
+
rpcUrls: {
|
|
56
|
+
default: {
|
|
57
|
+
http: ["http://localhost:3999"],
|
|
58
|
+
ws: ["ws://localhost:3999/extended/v1/ws"]
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
var mocknet = {
|
|
63
|
+
...devnet,
|
|
64
|
+
name: "Stacks Mocknet",
|
|
65
|
+
addressVersion: { ...devnet.addressVersion }
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
// src/chains/defineChain.ts
|
|
69
|
+
function defineChain(chain) {
|
|
70
|
+
return chain;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
//# debugId=2AFA2816A3EAB92964756E2164756E21
|
|
74
|
+
//# sourceMappingURL=stacks-24h5k94q.cjs.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/chains/definitions.ts", "../src/chains/defineChain.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import type { StacksChain } from \"./types.ts\";\n\n/** Stacks mainnet chain definition (Hiro API). */\nexport const mainnet: StacksChain = {\n id: 0x00000001,\n name: \"Stacks Mainnet\",\n network: \"mainnet\",\n transactionVersion: 0x00,\n peerNetworkId: 0x17000000,\n addressVersion: { singleSig: 22, multiSig: 20 },\n magicBytes: \"X2\",\n bootAddress: \"SP000000000000000000002Q6VF78\",\n nativeCurrency: { name: \"Stacks\", symbol: \"STX\", decimals: 6 },\n rpcUrls: {\n default: {\n http: [\"https://api.mainnet.hiro.so\"],\n ws: [\"wss://api.mainnet.hiro.so/extended/v1/ws\"],\n },\n },\n blockExplorers: {\n default: { name: \"Hiro Explorer\", url: \"https://explorer.hiro.so\" },\n },\n};\n\n/** Stacks testnet chain definition (Hiro API). */\nexport const testnet: StacksChain = {\n id: 0x80000000,\n name: \"Stacks Testnet\",\n network: \"testnet\",\n transactionVersion: 0x80,\n peerNetworkId: 0xff000000,\n addressVersion: { singleSig: 26, multiSig: 21 },\n magicBytes: \"T2\",\n bootAddress: \"ST000000000000000000002AMW42H\",\n nativeCurrency: { name: \"Stacks\", symbol: \"STX\", decimals: 6 },\n rpcUrls: {\n default: {\n http: [\"https://api.testnet.hiro.so\"],\n ws: [\"wss://api.testnet.hiro.so/extended/v1/ws\"],\n },\n },\n blockExplorers: {\n default: {\n name: \"Hiro Explorer\",\n url: \"https://explorer.hiro.so/?chain=testnet\",\n },\n },\n};\n\n/** Local development chain definition (localhost:3999). */\nexport const devnet: StacksChain = {\n id: 0x80000000,\n name: \"Stacks Devnet\",\n network: \"testnet\",\n transactionVersion: 0x80,\n peerNetworkId: 0xff000000,\n addressVersion: { singleSig: 26, multiSig: 21 },\n magicBytes: \"id\",\n bootAddress: \"ST000000000000000000002AMW42H\",\n nativeCurrency: { name: \"Stacks\", symbol: \"STX\", decimals: 6 },\n rpcUrls: {\n default: {\n http: [\"http://localhost:3999\"],\n ws: [\"ws://localhost:3999/extended/v1/ws\"],\n },\n },\n};\n\n/** Alias for devnet used in mock/test environments. */\nexport const mocknet: StacksChain = {\n ...devnet,\n name: \"Stacks Mocknet\",\n addressVersion: { ...devnet.addressVersion },\n};\n",
|
|
6
|
+
"import type { StacksChain } from \"./types.ts\";\n\n/** Identity helper for defining a custom {@link StacksChain} with full type inference. */\nexport function defineChain(chain: StacksChain): StacksChain {\n return chain;\n}\n"
|
|
7
|
+
],
|
|
8
|
+
"mappings": ";AAGO,IAAM,UAAuB;AAAA,EAClC,IAAI;AAAA,EACJ,MAAM;AAAA,EACN,SAAS;AAAA,EACT,oBAAoB;AAAA,EACpB,eAAe;AAAA,EACf,gBAAgB,EAAE,WAAW,IAAI,UAAU,GAAG;AAAA,EAC9C,YAAY;AAAA,EACZ,aAAa;AAAA,EACb,gBAAgB,EAAE,MAAM,UAAU,QAAQ,OAAO,UAAU,EAAE;AAAA,EA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|
+
"debugId": "2AFA2816A3EAB92964756E2164756E21",
|
|
10
|
+
"names": []
|
|
11
|
+
}
|
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
// src/clarity/index.ts
|
|
2
|
+
var exports_clarity = {};
|
|
3
|
+
__export(exports_clarity, {
|
|
4
|
+
validateArgsArray: () => validateArgsArray,
|
|
5
|
+
validateArgs: () => validateArgs,
|
|
6
|
+
uintCV: () => uintCV,
|
|
7
|
+
tupleCV: () => tupleCV,
|
|
8
|
+
trueCV: () => trueCV,
|
|
9
|
+
toCamelCase: () => toCamelCase,
|
|
10
|
+
stringUtf8CV: () => stringUtf8CV,
|
|
11
|
+
stringAsciiCV: () => stringAsciiCV,
|
|
12
|
+
standardPrincipalCV: () => standardPrincipalCV,
|
|
13
|
+
someCV: () => someCV,
|
|
14
|
+
sip013Abi: () => sip013Abi,
|
|
15
|
+
sip010Abi: () => sip010Abi,
|
|
16
|
+
sip009Abi: () => sip009Abi,
|
|
17
|
+
serializeCVBytes: () => serializeCVBytes,
|
|
18
|
+
serializeCV: () => serializeCV,
|
|
19
|
+
responseOkCV: () => responseOkCV,
|
|
20
|
+
responseErrorCV: () => responseErrorCV,
|
|
21
|
+
prettyPrint: () => prettyPrint,
|
|
22
|
+
prepareArgs: () => prepareArgs,
|
|
23
|
+
noneCV: () => noneCV,
|
|
24
|
+
listCV: () => listCV,
|
|
25
|
+
jsToClarityValue: () => jsToClarityValue,
|
|
26
|
+
jsToClarity: () => jsToClarity,
|
|
27
|
+
isUint8Array: () => isUint8Array,
|
|
28
|
+
isUint128: () => isUint128,
|
|
29
|
+
isTraitReference: () => isTraitReference,
|
|
30
|
+
isString: () => isString,
|
|
31
|
+
isStandardPrincipal: () => isStandardPrincipal,
|
|
32
|
+
isResponse: () => isResponse,
|
|
33
|
+
isPrincipal: () => isPrincipal,
|
|
34
|
+
isOptional: () => isOptional,
|
|
35
|
+
isOkResponse: () => isOkResponse,
|
|
36
|
+
isInt128: () => isInt128,
|
|
37
|
+
isErrResponse: () => isErrResponse,
|
|
38
|
+
isContractPrincipal: () => isContractPrincipal,
|
|
39
|
+
isBool: () => isBool,
|
|
40
|
+
isArray: () => isArray,
|
|
41
|
+
isAbiTuple: () => isAbiTuple,
|
|
42
|
+
isAbiTraitReference: () => isAbiTraitReference,
|
|
43
|
+
isAbiStringUtf8: () => isAbiStringUtf8,
|
|
44
|
+
isAbiStringAscii: () => isAbiStringAscii,
|
|
45
|
+
isAbiResponse: () => isAbiResponse,
|
|
46
|
+
isAbiOptional: () => isAbiOptional,
|
|
47
|
+
isAbiList: () => isAbiList,
|
|
48
|
+
isAbiBuffer: () => isAbiBuffer,
|
|
49
|
+
intCV: () => intCV,
|
|
50
|
+
falseCV: () => falseCV,
|
|
51
|
+
deserializeCVBytes: () => deserializeCVBytes,
|
|
52
|
+
deserializeCV: () => deserializeCV,
|
|
53
|
+
cvToValue: () => cvToValue,
|
|
54
|
+
cvToJSON: () => cvToJSON,
|
|
55
|
+
contractPrincipalCV: () => contractPrincipalCV,
|
|
56
|
+
clarityValueToJS: () => clarityValueToJS,
|
|
57
|
+
clarityTypeFromByte: () => clarityTypeFromByte,
|
|
58
|
+
bufferCV: () => bufferCV,
|
|
59
|
+
boolCV: () => boolCV,
|
|
60
|
+
SIP013_ABI: () => SIP013_ABI,
|
|
61
|
+
SIP010_ABI: () => SIP010_ABI,
|
|
62
|
+
SIP009_ABI: () => SIP009_ABI,
|
|
63
|
+
ClarityWireType: () => ClarityWireType,
|
|
64
|
+
ClarityConversionError: () => ClarityConversionError,
|
|
65
|
+
Cl: () => Cl,
|
|
66
|
+
CONTRACT_NAME_REGEX: () => CONTRACT_NAME_REGEX
|
|
67
|
+
});
|
|
68
|
+
module.exports = __toCommonJS(exports_clarity);
|
|
69
|
+
|
|
70
|
+
// src/clarity/prettyPrint.ts
|
|
71
|
+
function prettyPrint(val, encoding = "hex") {
|
|
72
|
+
switch (val.type) {
|
|
73
|
+
case "true":
|
|
74
|
+
return "true";
|
|
75
|
+
case "false":
|
|
76
|
+
return "false";
|
|
77
|
+
case "int":
|
|
78
|
+
return val.value.toString();
|
|
79
|
+
case "uint":
|
|
80
|
+
return `u${val.value.toString()}`;
|
|
81
|
+
case "buffer":
|
|
82
|
+
if (encoding === "tryAscii") {
|
|
83
|
+
const str = bytesToAscii(hexToBytes(val.value));
|
|
84
|
+
if (/^[ -~]*$/.test(str))
|
|
85
|
+
return JSON.stringify(str);
|
|
86
|
+
}
|
|
87
|
+
return `0x${val.value}`;
|
|
88
|
+
case "none":
|
|
89
|
+
return "none";
|
|
90
|
+
case "some":
|
|
91
|
+
return `(some ${prettyPrint(val.value, encoding)})`;
|
|
92
|
+
case "ok":
|
|
93
|
+
return `(ok ${prettyPrint(val.value, encoding)})`;
|
|
94
|
+
case "err":
|
|
95
|
+
return `(err ${prettyPrint(val.value, encoding)})`;
|
|
96
|
+
case "address":
|
|
97
|
+
case "contract":
|
|
98
|
+
return val.value;
|
|
99
|
+
case "list":
|
|
100
|
+
return `(list ${val.value.map((v) => prettyPrint(v, encoding)).join(" ")})`;
|
|
101
|
+
case "tuple":
|
|
102
|
+
return `(tuple ${Object.keys(val.value).map((key) => `(${key} ${prettyPrint(val.value[key], encoding)})`).join(" ")})`;
|
|
103
|
+
case "ascii":
|
|
104
|
+
return `"${val.value}"`;
|
|
105
|
+
case "utf8":
|
|
106
|
+
return `u"${val.value}"`;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
function cvToJSON(val) {
|
|
110
|
+
switch (val.type) {
|
|
111
|
+
case "true":
|
|
112
|
+
return { type: "bool", value: true };
|
|
113
|
+
case "false":
|
|
114
|
+
return { type: "bool", value: false };
|
|
115
|
+
case "int":
|
|
116
|
+
return { type: "int", value: val.value.toString() };
|
|
117
|
+
case "uint":
|
|
118
|
+
return { type: "uint", value: val.value.toString() };
|
|
119
|
+
case "buffer":
|
|
120
|
+
return { type: `(buff ${Math.ceil(val.value.length / 2)})`, value: `0x${val.value}` };
|
|
121
|
+
case "none":
|
|
122
|
+
return { type: "(optional none)", value: null };
|
|
123
|
+
case "some":
|
|
124
|
+
return { type: `(optional)`, value: cvToJSON(val.value) };
|
|
125
|
+
case "ok":
|
|
126
|
+
return { type: "(response)", value: cvToJSON(val.value), success: true };
|
|
127
|
+
case "err":
|
|
128
|
+
return { type: "(response)", value: cvToJSON(val.value), success: false };
|
|
129
|
+
case "address":
|
|
130
|
+
case "contract":
|
|
131
|
+
return { type: "principal", value: val.value };
|
|
132
|
+
case "list":
|
|
133
|
+
return { type: "(list)", value: val.value.map(cvToJSON) };
|
|
134
|
+
case "tuple": {
|
|
135
|
+
const result = {};
|
|
136
|
+
for (const key of Object.keys(val.value)) {
|
|
137
|
+
result[key] = cvToJSON(val.value[key]);
|
|
138
|
+
}
|
|
139
|
+
return { type: "(tuple)", value: result };
|
|
140
|
+
}
|
|
141
|
+
case "ascii":
|
|
142
|
+
return { type: `(string-ascii ${asciiToBytes(val.value).length})`, value: val.value };
|
|
143
|
+
case "utf8":
|
|
144
|
+
return { type: `(string-utf8 ${utf8ToBytes(val.value).length})`, value: val.value };
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
function cvToValue(val) {
|
|
148
|
+
switch (val.type) {
|
|
149
|
+
case "true":
|
|
150
|
+
return true;
|
|
151
|
+
case "false":
|
|
152
|
+
return false;
|
|
153
|
+
case "int":
|
|
154
|
+
case "uint":
|
|
155
|
+
return val.value;
|
|
156
|
+
case "buffer":
|
|
157
|
+
return `0x${val.value}`;
|
|
158
|
+
case "none":
|
|
159
|
+
return null;
|
|
160
|
+
case "some":
|
|
161
|
+
return cvToValue(val.value);
|
|
162
|
+
case "ok":
|
|
163
|
+
case "err":
|
|
164
|
+
return cvToValue(val.value);
|
|
165
|
+
case "address":
|
|
166
|
+
case "contract":
|
|
167
|
+
return val.value;
|
|
168
|
+
case "list":
|
|
169
|
+
return val.value.map(cvToValue);
|
|
170
|
+
case "tuple": {
|
|
171
|
+
const result = {};
|
|
172
|
+
for (const key of Object.keys(val.value)) {
|
|
173
|
+
result[key] = cvToValue(val.value[key]);
|
|
174
|
+
}
|
|
175
|
+
return result;
|
|
176
|
+
}
|
|
177
|
+
case "ascii":
|
|
178
|
+
case "utf8":
|
|
179
|
+
return val.value;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
// src/clarity/abi/converters.ts
|
|
183
|
+
class ClarityConversionError extends Error {
|
|
184
|
+
type;
|
|
185
|
+
value;
|
|
186
|
+
constructor(message, type, value) {
|
|
187
|
+
super(message);
|
|
188
|
+
this.type = type;
|
|
189
|
+
this.value = value;
|
|
190
|
+
this.name = "ClarityConversionError";
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
function jsToClarity(type, value) {
|
|
194
|
+
if (type === "none")
|
|
195
|
+
return null;
|
|
196
|
+
if (type === "uint128") {
|
|
197
|
+
if (!isUint128(value))
|
|
198
|
+
throw new ClarityConversionError("Invalid uint128 value", type, value);
|
|
199
|
+
return value;
|
|
200
|
+
}
|
|
201
|
+
if (type === "int128") {
|
|
202
|
+
if (!isInt128(value))
|
|
203
|
+
throw new ClarityConversionError("Invalid int128 value", type, value);
|
|
204
|
+
return value;
|
|
205
|
+
}
|
|
206
|
+
if (type === "bool") {
|
|
207
|
+
if (!isBool(value))
|
|
208
|
+
throw new ClarityConversionError("Invalid bool value", type, value);
|
|
209
|
+
return value;
|
|
210
|
+
}
|
|
211
|
+
if (type === "principal") {
|
|
212
|
+
if (!isPrincipal(value))
|
|
213
|
+
throw new ClarityConversionError("Invalid principal value", type, value);
|
|
214
|
+
return value;
|
|
215
|
+
}
|
|
216
|
+
if (type === "trait_reference") {
|
|
217
|
+
if (!isPrincipal(value))
|
|
218
|
+
throw new ClarityConversionError("Invalid trait_reference value", type, value);
|
|
219
|
+
return value;
|
|
220
|
+
}
|
|
221
|
+
if (isAbiStringAscii(type)) {
|
|
222
|
+
if (!isString(value))
|
|
223
|
+
throw new ClarityConversionError("Invalid string-ascii value", type, value);
|
|
224
|
+
const str = value;
|
|
225
|
+
for (let i = 0;i < str.length; i++) {
|
|
226
|
+
if (str.charCodeAt(i) > 127) {
|
|
227
|
+
throw new ClarityConversionError(`Non-ASCII character at position ${i}: '${str[i]}' (code ${str.charCodeAt(i)})`, type, value);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
if (str.length > type["string-ascii"].length) {
|
|
231
|
+
throw new ClarityConversionError(`String length ${str.length} exceeds max ${type["string-ascii"].length}`, type, value);
|
|
232
|
+
}
|
|
233
|
+
return value;
|
|
234
|
+
}
|
|
235
|
+
if (isAbiStringUtf8(type)) {
|
|
236
|
+
if (!isString(value))
|
|
237
|
+
throw new ClarityConversionError("Invalid string-utf8 value", type, value);
|
|
238
|
+
const str = value;
|
|
239
|
+
const byteLength = new TextEncoder().encode(str).length;
|
|
240
|
+
if (byteLength > type["string-utf8"].length) {
|
|
241
|
+
throw new ClarityConversionError(`String byte length ${byteLength} exceeds max ${type["string-utf8"].length}`, type, value);
|
|
242
|
+
}
|
|
243
|
+
return value;
|
|
244
|
+
}
|
|
245
|
+
if (isAbiBuffer(type)) {
|
|
246
|
+
if (!isUint8Array(value))
|
|
247
|
+
throw new ClarityConversionError("Invalid buffer value", type, value);
|
|
248
|
+
const buff = value;
|
|
249
|
+
if (buff.length > type.buff.length) {
|
|
250
|
+
throw new ClarityConversionError(`Buffer length ${buff.length} exceeds max ${type.buff.length}`, type, value);
|
|
251
|
+
}
|
|
252
|
+
return value;
|
|
253
|
+
}
|
|
254
|
+
if (isAbiList(type)) {
|
|
255
|
+
if (!Array.isArray(value))
|
|
256
|
+
throw new ClarityConversionError("Expected array for list type", type, value);
|
|
257
|
+
if (value.length > type.list.length) {
|
|
258
|
+
throw new ClarityConversionError(`List length ${value.length} exceeds max ${type.list.length}`, type, value);
|
|
259
|
+
}
|
|
260
|
+
return value.map((item) => jsToClarity(type.list.type, item));
|
|
261
|
+
}
|
|
262
|
+
if (isAbiTuple(type)) {
|
|
263
|
+
if (typeof value !== "object" || value === null)
|
|
264
|
+
throw new ClarityConversionError("Expected object for tuple type", type, value);
|
|
265
|
+
const result = {};
|
|
266
|
+
for (const field of type.tuple) {
|
|
267
|
+
const camelKey = toCamelCase(field.name);
|
|
268
|
+
const hasOriginal = field.name in value;
|
|
269
|
+
const hasCamel = camelKey in value;
|
|
270
|
+
if (!hasOriginal && !hasCamel) {
|
|
271
|
+
throw new ClarityConversionError(`Missing tuple field: ${field.name}`, type, value);
|
|
272
|
+
}
|
|
273
|
+
const fieldValue = hasOriginal ? value[field.name] : value[camelKey];
|
|
274
|
+
result[field.name] = jsToClarity(field.type, fieldValue);
|
|
275
|
+
}
|
|
276
|
+
return result;
|
|
277
|
+
}
|
|
278
|
+
if (isAbiOptional(type)) {
|
|
279
|
+
if (value === null || value === undefined)
|
|
280
|
+
return null;
|
|
281
|
+
return jsToClarity(type.optional, value);
|
|
282
|
+
}
|
|
283
|
+
if (isAbiResponse(type)) {
|
|
284
|
+
if (typeof value !== "object" || value === null)
|
|
285
|
+
throw new ClarityConversionError("Expected object for response type", type, value);
|
|
286
|
+
const hasOk = "ok" in value;
|
|
287
|
+
const hasErr = "err" in value;
|
|
288
|
+
if (hasOk && !hasErr) {
|
|
289
|
+
return {
|
|
290
|
+
ok: jsToClarity(type.response.ok, value.ok)
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
if (hasErr && !hasOk) {
|
|
294
|
+
return {
|
|
295
|
+
err: jsToClarity(type.response.error, value.err)
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
throw new ClarityConversionError("Response must have exactly 'ok' or 'err' property", type, value);
|
|
299
|
+
}
|
|
300
|
+
throw new ClarityConversionError(`Unknown Clarity type`, type, value);
|
|
301
|
+
}
|
|
302
|
+
function prepareArgs(func, args) {
|
|
303
|
+
return func.args.map((arg) => {
|
|
304
|
+
if (!(arg.name in args)) {
|
|
305
|
+
throw new ClarityConversionError(`Missing argument: ${arg.name}`, arg.type, undefined);
|
|
306
|
+
}
|
|
307
|
+
return jsToClarity(arg.type, args[arg.name]);
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
function validateArgs(func, args) {
|
|
311
|
+
for (const arg of func.args) {
|
|
312
|
+
if (!(arg.name in args)) {
|
|
313
|
+
throw new ClarityConversionError(`Missing argument: ${arg.name}`, arg.type, undefined);
|
|
314
|
+
}
|
|
315
|
+
jsToClarity(arg.type, args[arg.name]);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
function validateArgsArray(func, args) {
|
|
319
|
+
if (args.length !== func.args.length) {
|
|
320
|
+
throw new ClarityConversionError(`Expected ${func.args.length} arguments, got ${args.length}`, func.args[0]?.type ?? "bool", args);
|
|
321
|
+
}
|
|
322
|
+
func.args.forEach((arg, i) => {
|
|
323
|
+
jsToClarity(arg.type, args[i]);
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
//# debugId=72D7A0CDC9AC36C164756E2164756E21
|
|
328
|
+
//# sourceMappingURL=stacks-2ar4prfz.cjs.map
|