@veridex/sdk 1.0.0-beta.8 → 1.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/LICENSE +170 -21
- package/README.md +574 -117
- package/dist/EVMClient-DtqvdfUP.d.mts +376 -0
- package/dist/auth/prepareAuth.d.mts +25 -0
- package/dist/auth/prepareAuth.js +2406 -0
- package/dist/auth/prepareAuth.js.map +1 -0
- package/dist/auth/prepareAuth.mjs +151 -0
- package/dist/auth/prepareAuth.mjs.map +1 -0
- package/dist/chains/aptos/index.d.mts +6 -5
- package/dist/chains/aptos/index.js +66 -39
- package/dist/chains/aptos/index.js.map +1 -1
- package/dist/chains/aptos/index.mjs +5 -547
- package/dist/chains/aptos/index.mjs.map +1 -1
- package/dist/chains/avalanche/index.d.mts +137 -0
- package/dist/chains/avalanche/index.js +1555 -0
- package/dist/chains/avalanche/index.js.map +1 -0
- package/dist/chains/avalanche/index.mjs +10 -0
- package/dist/chains/avalanche/index.mjs.map +1 -0
- package/dist/chains/evm/index.d.mts +5 -3
- package/dist/chains/evm/index.js +165 -3
- package/dist/chains/evm/index.js.map +1 -1
- package/dist/chains/evm/index.mjs +8 -1200
- package/dist/chains/evm/index.mjs.map +1 -1
- package/dist/chains/solana/index.d.mts +1 -1
- package/dist/chains/solana/index.js.map +1 -1
- package/dist/chains/solana/index.mjs +4 -486
- package/dist/chains/solana/index.mjs.map +1 -1
- package/dist/chains/stacks/index.d.mts +559 -0
- package/dist/chains/stacks/index.js +1207 -0
- package/dist/chains/stacks/index.js.map +1 -0
- package/dist/chains/stacks/index.mjs +71 -0
- package/dist/chains/stacks/index.mjs.map +1 -0
- package/dist/chains/starknet/index.d.mts +3 -3
- package/dist/chains/starknet/index.js.map +1 -1
- package/dist/chains/starknet/index.mjs +5 -503
- package/dist/chains/starknet/index.mjs.map +1 -1
- package/dist/chains/sui/index.d.mts +2 -2
- package/dist/chains/sui/index.js.map +1 -1
- package/dist/chains/sui/index.mjs +5 -529
- package/dist/chains/sui/index.mjs.map +1 -1
- package/dist/chunk-5T6KPH7A.mjs +1082 -0
- package/dist/chunk-5T6KPH7A.mjs.map +1 -0
- package/dist/chunk-72ZA3OYQ.mjs +20 -0
- package/dist/chunk-72ZA3OYQ.mjs.map +1 -0
- package/dist/chunk-EFIURACP.mjs +438 -0
- package/dist/chunk-EFIURACP.mjs.map +1 -0
- package/dist/chunk-F3YAGZSW.mjs +269 -0
- package/dist/chunk-F3YAGZSW.mjs.map +1 -0
- package/dist/chunk-GWJRKDSA.mjs +549 -0
- package/dist/chunk-GWJRKDSA.mjs.map +1 -0
- package/dist/chunk-M3MM4YMF.mjs +417 -0
- package/dist/chunk-M3MM4YMF.mjs.map +1 -0
- package/dist/chunk-N4A2RMUN.mjs +216 -0
- package/dist/chunk-N4A2RMUN.mjs.map +1 -0
- package/dist/chunk-NUWSMJFJ.mjs +179 -0
- package/dist/chunk-NUWSMJFJ.mjs.map +1 -0
- package/dist/chunk-OVMMTL6H.mjs +330 -0
- package/dist/chunk-OVMMTL6H.mjs.map +1 -0
- package/dist/chunk-PDHZ5X5O.mjs +565 -0
- package/dist/chunk-PDHZ5X5O.mjs.map +1 -0
- package/dist/chunk-PRHNGA4G.mjs +464 -0
- package/dist/chunk-PRHNGA4G.mjs.map +1 -0
- package/dist/chunk-Q5O3M5LP.mjs +422 -0
- package/dist/chunk-Q5O3M5LP.mjs.map +1 -0
- package/dist/chunk-QDO6NQ7P.mjs +840 -0
- package/dist/chunk-QDO6NQ7P.mjs.map +1 -0
- package/dist/chunk-QT4ZZ4GM.mjs +509 -0
- package/dist/chunk-QT4ZZ4GM.mjs.map +1 -0
- package/dist/chunk-USDA5JTN.mjs +1249 -0
- package/dist/chunk-USDA5JTN.mjs.map +1 -0
- package/dist/chunk-V636MIV3.mjs +52 -0
- package/dist/chunk-V636MIV3.mjs.map +1 -0
- package/dist/chunk-X7BZMSPQ.mjs +407 -0
- package/dist/chunk-X7BZMSPQ.mjs.map +1 -0
- package/dist/chunk-YCUJZ6Z7.mjs +829 -0
- package/dist/chunk-YCUJZ6Z7.mjs.map +1 -0
- package/dist/constants.d.mts +1 -1
- package/dist/constants.js +26 -12
- package/dist/constants.js.map +1 -1
- package/dist/constants.mjs +16 -375
- package/dist/constants.mjs.map +1 -1
- package/dist/index-DDalBhAm.d.mts +243 -0
- package/dist/index.d.mts +2508 -556
- package/dist/index.js +14576 -9628
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +4108 -7840
- package/dist/index.mjs.map +1 -1
- package/dist/passkey.d.mts +182 -0
- package/dist/passkey.js +914 -0
- package/dist/passkey.js.map +1 -0
- package/dist/passkey.mjs +15 -0
- package/dist/passkey.mjs.map +1 -0
- package/dist/payload.js.map +1 -1
- package/dist/payload.mjs +25 -244
- package/dist/payload.mjs.map +1 -1
- package/dist/portfolio-V347KZOL.mjs +13 -0
- package/dist/portfolio-V347KZOL.mjs.map +1 -0
- package/dist/queries/index.js +145 -12
- package/dist/queries/index.js.map +1 -1
- package/dist/queries/index.mjs +14 -1496
- package/dist/queries/index.mjs.map +1 -1
- package/dist/{types-FJL7j6gQ.d.ts → types-B7V5VNbO.d.mts} +6 -2
- package/dist/{types-ChIsqCiw.d.mts → types-DP2CQT8p.d.mts} +12 -1
- package/dist/types.d.mts +16 -0
- package/dist/types.js.map +1 -1
- package/dist/utils.js +25 -11
- package/dist/utils.js.map +1 -1
- package/dist/utils.mjs +19 -371
- package/dist/utils.mjs.map +1 -1
- package/dist/wormhole.js.map +1 -1
- package/dist/wormhole.mjs +25 -397
- package/dist/wormhole.mjs.map +1 -1
- package/package.json +28 -3
- package/scripts/patch-noble-curves.js +78 -0
- package/dist/chains/aptos/index.d.ts +0 -145
- package/dist/chains/evm/index.d.ts +0 -5
- package/dist/chains/solana/index.d.ts +0 -116
- package/dist/chains/starknet/index.d.ts +0 -172
- package/dist/chains/sui/index.d.ts +0 -182
- package/dist/constants.d.ts +0 -150
- package/dist/index-0NXfbk0z.d.ts +0 -637
- package/dist/index-D0dLVjTA.d.mts +0 -637
- package/dist/index.d.ts +0 -3123
- package/dist/payload.d.ts +0 -125
- package/dist/queries/index.d.ts +0 -148
- package/dist/types-ChIsqCiw.d.ts +0 -565
- package/dist/types-FJL7j6gQ.d.mts +0 -172
- package/dist/types.d.ts +0 -407
- package/dist/utils.d.ts +0 -81
- package/dist/wormhole.d.ts +0 -167
|
@@ -0,0 +1,1082 @@
|
|
|
1
|
+
import {
|
|
2
|
+
encodeBridgeAction,
|
|
3
|
+
encodeExecuteAction,
|
|
4
|
+
encodeTransferAction
|
|
5
|
+
} from "./chunk-F3YAGZSW.mjs";
|
|
6
|
+
|
|
7
|
+
// src/chains/stacks/StacksSigner.ts
|
|
8
|
+
var P256_ORDER = BigInt(
|
|
9
|
+
"0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551"
|
|
10
|
+
);
|
|
11
|
+
var P256_HALF_ORDER = P256_ORDER / 2n;
|
|
12
|
+
function compressPublicKey(x, y) {
|
|
13
|
+
const prefix = y % 2n === 0n ? 2 : 3;
|
|
14
|
+
const xBytes = bigintToBytes(x, 32);
|
|
15
|
+
const compressed = new Uint8Array(33);
|
|
16
|
+
compressed[0] = prefix;
|
|
17
|
+
compressed.set(xBytes, 1);
|
|
18
|
+
return compressed;
|
|
19
|
+
}
|
|
20
|
+
function rsToCompactSignature(r, s) {
|
|
21
|
+
const normalizedS = s > P256_HALF_ORDER ? P256_ORDER - s : s;
|
|
22
|
+
const compact = new Uint8Array(64);
|
|
23
|
+
compact.set(bigintToBytes(r, 32), 0);
|
|
24
|
+
compact.set(bigintToBytes(normalizedS, 32), 32);
|
|
25
|
+
return compact;
|
|
26
|
+
}
|
|
27
|
+
function parseDERSignature(der) {
|
|
28
|
+
if (der[0] !== 48) {
|
|
29
|
+
throw new Error("Invalid DER signature: expected SEQUENCE tag 0x30");
|
|
30
|
+
}
|
|
31
|
+
let offset = 2;
|
|
32
|
+
if (der[offset] !== 2) {
|
|
33
|
+
throw new Error("Invalid DER signature: expected INTEGER tag 0x02 for r");
|
|
34
|
+
}
|
|
35
|
+
offset++;
|
|
36
|
+
const rLen = der[offset];
|
|
37
|
+
offset++;
|
|
38
|
+
const rBytes = der.slice(offset, offset + rLen);
|
|
39
|
+
offset += rLen;
|
|
40
|
+
if (der[offset] !== 2) {
|
|
41
|
+
throw new Error("Invalid DER signature: expected INTEGER tag 0x02 for s");
|
|
42
|
+
}
|
|
43
|
+
offset++;
|
|
44
|
+
const sLen = der[offset];
|
|
45
|
+
offset++;
|
|
46
|
+
const sBytes = der.slice(offset, offset + sLen);
|
|
47
|
+
return {
|
|
48
|
+
r: bytesToBigint(rBytes),
|
|
49
|
+
s: bytesToBigint(sBytes)
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
function derToCompactSignature(der) {
|
|
53
|
+
const { r, s } = parseDERSignature(der);
|
|
54
|
+
return rsToCompactSignature(r, s);
|
|
55
|
+
}
|
|
56
|
+
async function computeKeyHash(compressedPubkey) {
|
|
57
|
+
const hashBuffer = await globalThis.crypto.subtle.digest("SHA-256", compressedPubkey.buffer);
|
|
58
|
+
const hashArray = new Uint8Array(hashBuffer);
|
|
59
|
+
return "0x" + bytesToHex(hashArray);
|
|
60
|
+
}
|
|
61
|
+
async function computeKeyHashFromCoords(x, y) {
|
|
62
|
+
const compressed = compressPublicKey(x, y);
|
|
63
|
+
return computeKeyHash(compressed);
|
|
64
|
+
}
|
|
65
|
+
async function buildRegistrationHash(nonce) {
|
|
66
|
+
const message = `veridex:register:${nonce}`;
|
|
67
|
+
const encoded = new TextEncoder().encode(message);
|
|
68
|
+
const hashBuffer = await globalThis.crypto.subtle.digest("SHA-256", encoded.buffer);
|
|
69
|
+
return new Uint8Array(hashBuffer);
|
|
70
|
+
}
|
|
71
|
+
async function buildSessionRegistrationHash(sessionKeyHash, duration, maxValue, nonce) {
|
|
72
|
+
const cleanHash = sessionKeyHash.replace("0x", "");
|
|
73
|
+
const message = `veridex:session:${cleanHash}:${duration}:${maxValue}:${nonce}`;
|
|
74
|
+
const encoded = new TextEncoder().encode(message);
|
|
75
|
+
const hashBuffer = await globalThis.crypto.subtle.digest("SHA-256", encoded.buffer);
|
|
76
|
+
return new Uint8Array(hashBuffer);
|
|
77
|
+
}
|
|
78
|
+
async function buildRevocationHash(sessionHash, nonce) {
|
|
79
|
+
const cleanHash = sessionHash.replace("0x", "");
|
|
80
|
+
const message = `veridex:revoke:${cleanHash}:${nonce}`;
|
|
81
|
+
const encoded = new TextEncoder().encode(message);
|
|
82
|
+
const hashBuffer = await globalThis.crypto.subtle.digest("SHA-256", encoded.buffer);
|
|
83
|
+
return new Uint8Array(hashBuffer);
|
|
84
|
+
}
|
|
85
|
+
async function buildExecuteHash(actionType, amount, recipient, nonce) {
|
|
86
|
+
const message = `veridex:execute:${actionType}:${amount}:${recipient}:${nonce}`;
|
|
87
|
+
const encoded = new TextEncoder().encode(message);
|
|
88
|
+
const hashBuffer = await globalThis.crypto.subtle.digest("SHA-256", encoded.buffer);
|
|
89
|
+
return new Uint8Array(hashBuffer);
|
|
90
|
+
}
|
|
91
|
+
async function buildWithdrawalHash(amount, recipient, nonce) {
|
|
92
|
+
const message = `veridex:withdraw:${amount}:${recipient}:${nonce}`;
|
|
93
|
+
const encoded = new TextEncoder().encode(message);
|
|
94
|
+
const hashBuffer = await globalThis.crypto.subtle.digest("SHA-256", encoded.buffer);
|
|
95
|
+
return new Uint8Array(hashBuffer);
|
|
96
|
+
}
|
|
97
|
+
function bigintToBytes(value, length) {
|
|
98
|
+
const hex = value.toString(16).padStart(length * 2, "0");
|
|
99
|
+
const bytes = new Uint8Array(length);
|
|
100
|
+
for (let i = 0; i < length; i++) {
|
|
101
|
+
bytes[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
|
|
102
|
+
}
|
|
103
|
+
return bytes;
|
|
104
|
+
}
|
|
105
|
+
function bytesToBigint(bytes) {
|
|
106
|
+
let start = 0;
|
|
107
|
+
while (start < bytes.length - 1 && bytes[start] === 0) {
|
|
108
|
+
start++;
|
|
109
|
+
}
|
|
110
|
+
let result = 0n;
|
|
111
|
+
for (let i = start; i < bytes.length; i++) {
|
|
112
|
+
result = result << 8n | BigInt(bytes[i]);
|
|
113
|
+
}
|
|
114
|
+
return result;
|
|
115
|
+
}
|
|
116
|
+
function bytesToHex(bytes) {
|
|
117
|
+
return Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
118
|
+
}
|
|
119
|
+
function hexToBytes(hex) {
|
|
120
|
+
const clean = hex.replace("0x", "");
|
|
121
|
+
const bytes = new Uint8Array(clean.length / 2);
|
|
122
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
123
|
+
bytes[i] = parseInt(clean.slice(i * 2, i * 2 + 2), 16);
|
|
124
|
+
}
|
|
125
|
+
return bytes;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// src/chains/stacks/StacksAddressUtils.ts
|
|
129
|
+
var STACKS_MAINNET_PREFIX = "SP";
|
|
130
|
+
var STACKS_TESTNET_PREFIX = "ST";
|
|
131
|
+
var C32_ALPHABET = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
|
|
132
|
+
function isValidStacksPrincipal(address) {
|
|
133
|
+
if (!address || typeof address !== "string") {
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
const parts = address.split(".");
|
|
137
|
+
if (parts.length > 2) {
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
const standardPart = parts[0];
|
|
141
|
+
const contractName = parts[1];
|
|
142
|
+
if (!isValidStandardPrincipal(standardPart)) {
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
if (contractName !== void 0) {
|
|
146
|
+
if (!isValidContractName(contractName)) {
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
function isValidStandardPrincipal(address) {
|
|
153
|
+
if (!address || address.length < 5) {
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
const prefix = address.slice(0, 2);
|
|
157
|
+
if (prefix !== STACKS_MAINNET_PREFIX && prefix !== STACKS_TESTNET_PREFIX) {
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
160
|
+
if (address.length < 38 || address.length > 42) {
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
const body = address.slice(1).toUpperCase();
|
|
164
|
+
for (const char of body) {
|
|
165
|
+
if (!C32_ALPHABET.includes(char)) {
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return true;
|
|
170
|
+
}
|
|
171
|
+
function isValidContractName(name) {
|
|
172
|
+
if (!name || name.length === 0 || name.length > 128) {
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
if (!/^[a-zA-Z]/.test(name)) {
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
if (!/^[a-zA-Z][a-zA-Z0-9-]*$/.test(name)) {
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
return true;
|
|
182
|
+
}
|
|
183
|
+
function getNetworkFromAddress(address) {
|
|
184
|
+
const prefix = address.slice(0, 2);
|
|
185
|
+
if (prefix === STACKS_MAINNET_PREFIX) {
|
|
186
|
+
return "mainnet";
|
|
187
|
+
}
|
|
188
|
+
return "testnet";
|
|
189
|
+
}
|
|
190
|
+
function getContractPrincipal(deployerAddress, contractName) {
|
|
191
|
+
if (!isValidStandardPrincipal(deployerAddress)) {
|
|
192
|
+
throw new Error(`Invalid deployer address: ${deployerAddress}`);
|
|
193
|
+
}
|
|
194
|
+
if (!isValidContractName(contractName)) {
|
|
195
|
+
throw new Error(`Invalid contract name: ${contractName}`);
|
|
196
|
+
}
|
|
197
|
+
return `${deployerAddress}.${contractName}`;
|
|
198
|
+
}
|
|
199
|
+
function parseContractPrincipal(contractPrincipal) {
|
|
200
|
+
const dotIndex = contractPrincipal.indexOf(".");
|
|
201
|
+
if (dotIndex === -1) {
|
|
202
|
+
throw new Error(
|
|
203
|
+
`Not a contract principal: ${contractPrincipal}. Expected format: address.contract-name`
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
return {
|
|
207
|
+
address: contractPrincipal.slice(0, dotIndex),
|
|
208
|
+
contractName: contractPrincipal.slice(dotIndex + 1)
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
function isContractPrincipal(address) {
|
|
212
|
+
return address.includes(".");
|
|
213
|
+
}
|
|
214
|
+
function getStacksExplorerTxUrl(txId, network = "testnet") {
|
|
215
|
+
const cleanTxId = txId.startsWith("0x") ? txId : `0x${txId}`;
|
|
216
|
+
const chainParam = network === "testnet" ? "&chain=testnet" : "";
|
|
217
|
+
return `https://explorer.hiro.so/txid/${cleanTxId}?${chainParam}`;
|
|
218
|
+
}
|
|
219
|
+
function getStacksExplorerAddressUrl(address, network = "testnet") {
|
|
220
|
+
const chainParam = network === "testnet" ? "?chain=testnet" : "";
|
|
221
|
+
return `https://explorer.hiro.so/address/${address}${chainParam}`;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// src/chains/stacks/StacksClient.ts
|
|
225
|
+
var STACKS_ACTION_TYPES = {
|
|
226
|
+
TRANSFER_STX: 1,
|
|
227
|
+
TRANSFER_SBTC: 2,
|
|
228
|
+
CONTRACT_CALL: 3
|
|
229
|
+
};
|
|
230
|
+
var HIRO_API = {
|
|
231
|
+
testnet: "https://api.testnet.hiro.so",
|
|
232
|
+
mainnet: "https://api.hiro.so"
|
|
233
|
+
};
|
|
234
|
+
var StacksClient = class {
|
|
235
|
+
config;
|
|
236
|
+
rpcUrl;
|
|
237
|
+
spokeContract;
|
|
238
|
+
vaultContract;
|
|
239
|
+
wormholeVerifierContract;
|
|
240
|
+
vaultVaaContract;
|
|
241
|
+
networkType;
|
|
242
|
+
constructor(clientConfig) {
|
|
243
|
+
this.networkType = clientConfig.network || "testnet";
|
|
244
|
+
this.rpcUrl = clientConfig.rpcUrl || HIRO_API[this.networkType];
|
|
245
|
+
if (clientConfig.spokeContractAddress && isContractPrincipal(clientConfig.spokeContractAddress)) {
|
|
246
|
+
const parsed = parseContractPrincipal(clientConfig.spokeContractAddress);
|
|
247
|
+
this.spokeContract = { address: parsed.address, name: parsed.contractName };
|
|
248
|
+
} else {
|
|
249
|
+
this.spokeContract = null;
|
|
250
|
+
}
|
|
251
|
+
if (clientConfig.vaultContractAddress && isContractPrincipal(clientConfig.vaultContractAddress)) {
|
|
252
|
+
const parsed = parseContractPrincipal(clientConfig.vaultContractAddress);
|
|
253
|
+
this.vaultContract = { address: parsed.address, name: parsed.contractName };
|
|
254
|
+
} else {
|
|
255
|
+
this.vaultContract = null;
|
|
256
|
+
}
|
|
257
|
+
if (clientConfig.wormholeVerifierAddress && isContractPrincipal(clientConfig.wormholeVerifierAddress)) {
|
|
258
|
+
const parsed = parseContractPrincipal(clientConfig.wormholeVerifierAddress);
|
|
259
|
+
this.wormholeVerifierContract = { address: parsed.address, name: parsed.contractName };
|
|
260
|
+
} else {
|
|
261
|
+
this.wormholeVerifierContract = null;
|
|
262
|
+
}
|
|
263
|
+
if (clientConfig.vaultVaaContractAddress && isContractPrincipal(clientConfig.vaultVaaContractAddress)) {
|
|
264
|
+
const parsed = parseContractPrincipal(clientConfig.vaultVaaContractAddress);
|
|
265
|
+
this.vaultVaaContract = { address: parsed.address, name: parsed.contractName };
|
|
266
|
+
} else {
|
|
267
|
+
this.vaultVaaContract = null;
|
|
268
|
+
}
|
|
269
|
+
if (this.spokeContract && !this.vaultContract) {
|
|
270
|
+
this.vaultContract = {
|
|
271
|
+
address: this.spokeContract.address,
|
|
272
|
+
name: "veridex-vault"
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
if (this.spokeContract && !this.wormholeVerifierContract) {
|
|
276
|
+
this.wormholeVerifierContract = {
|
|
277
|
+
address: this.spokeContract.address,
|
|
278
|
+
name: "veridex-wormhole-verifier"
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
if (this.spokeContract && !this.vaultVaaContract) {
|
|
282
|
+
this.vaultVaaContract = {
|
|
283
|
+
address: this.spokeContract.address,
|
|
284
|
+
name: "veridex-vault-vaa"
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
this.config = {
|
|
288
|
+
name: `Stacks ${this.networkType}`,
|
|
289
|
+
chainId: this.networkType === "mainnet" ? 1 : 2147483648,
|
|
290
|
+
wormholeChainId: clientConfig.wormholeChainId,
|
|
291
|
+
rpcUrl: this.rpcUrl,
|
|
292
|
+
explorerUrl: this.networkType === "testnet" ? "https://explorer.hiro.so/?chain=testnet" : "https://explorer.hiro.so",
|
|
293
|
+
isEvm: false,
|
|
294
|
+
contracts: {
|
|
295
|
+
hub: clientConfig.spokeContractAddress,
|
|
296
|
+
wormholeCoreBridge: "",
|
|
297
|
+
wormholeVerifier: this.wormholeVerifierContract ? `${this.wormholeVerifierContract.address}.${this.wormholeVerifierContract.name}` : void 0,
|
|
298
|
+
vaultVaa: this.vaultVaaContract ? `${this.vaultVaaContract.address}.${this.vaultVaaContract.name}` : void 0
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
// ========================================================================
|
|
303
|
+
// ChainClient Interface - Configuration
|
|
304
|
+
// ========================================================================
|
|
305
|
+
getConfig() {
|
|
306
|
+
return this.config;
|
|
307
|
+
}
|
|
308
|
+
// ========================================================================
|
|
309
|
+
// ChainClient Interface - Nonce & Fees
|
|
310
|
+
// ========================================================================
|
|
311
|
+
/**
|
|
312
|
+
* Get the current nonce for a user identity from the spoke contract.
|
|
313
|
+
* Calls the read-only function `get-nonce` on veridex-spoke.
|
|
314
|
+
*/
|
|
315
|
+
async getNonce(userKeyHash) {
|
|
316
|
+
if (!this.spokeContract) {
|
|
317
|
+
return 0n;
|
|
318
|
+
}
|
|
319
|
+
try {
|
|
320
|
+
const result = await this.callReadOnly(
|
|
321
|
+
this.spokeContract.address,
|
|
322
|
+
this.spokeContract.name,
|
|
323
|
+
"get-nonce",
|
|
324
|
+
[`0x${userKeyHash.replace("0x", "")}`]
|
|
325
|
+
);
|
|
326
|
+
if (result && result.value !== void 0) {
|
|
327
|
+
return BigInt(result.value);
|
|
328
|
+
}
|
|
329
|
+
return 0n;
|
|
330
|
+
} catch {
|
|
331
|
+
return 0n;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Get the Wormhole message fee.
|
|
336
|
+
* Phase 1: No Wormhole integration, returns 0.
|
|
337
|
+
*/
|
|
338
|
+
async getMessageFee() {
|
|
339
|
+
return 0n;
|
|
340
|
+
}
|
|
341
|
+
// ========================================================================
|
|
342
|
+
// ChainClient Interface - Payload Building
|
|
343
|
+
// ========================================================================
|
|
344
|
+
async buildTransferPayload(params) {
|
|
345
|
+
return encodeTransferAction(params.token, params.recipient, params.amount);
|
|
346
|
+
}
|
|
347
|
+
async buildExecutePayload(params) {
|
|
348
|
+
return encodeExecuteAction(params.target, params.value, params.data);
|
|
349
|
+
}
|
|
350
|
+
async buildBridgePayload(params) {
|
|
351
|
+
return encodeBridgeAction(params.token, params.amount, params.destinationChain, params.recipient);
|
|
352
|
+
}
|
|
353
|
+
// ========================================================================
|
|
354
|
+
// ChainClient Interface - Dispatch
|
|
355
|
+
// ========================================================================
|
|
356
|
+
/**
|
|
357
|
+
* Direct dispatch is not supported on Stacks in Phase 1.
|
|
358
|
+
* Stacks actions are executed via sponsored transactions through the relayer.
|
|
359
|
+
*/
|
|
360
|
+
async dispatch(_signature, _publicKeyX, _publicKeyY, _targetChain, _actionPayload, _nonce, _signer) {
|
|
361
|
+
throw new Error(
|
|
362
|
+
"Direct dispatch not supported on Stacks in Phase 1. Use dispatchGasless() to route through the relayer, which sponsors Stacks transactions. Phase 2 will add Wormhole cross-chain dispatch support."
|
|
363
|
+
);
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Dispatch an action via the relayer (gasless/sponsored).
|
|
367
|
+
*
|
|
368
|
+
* Flow:
|
|
369
|
+
* 1. User signs action with Passkey (on client)
|
|
370
|
+
* 2. SDK submits to relayer with targetChain=60 (Stacks)
|
|
371
|
+
* 3. Relayer builds Clarity contract-call transaction
|
|
372
|
+
* 4. Relayer sponsors the transaction (pays STX gas)
|
|
373
|
+
* 5. Relayer broadcasts to Stacks network
|
|
374
|
+
* 6. Transaction confirmed on Stacks
|
|
375
|
+
*/
|
|
376
|
+
async dispatchGasless(signature, publicKeyX, publicKeyY, targetChain, actionPayload, nonce, relayerUrl) {
|
|
377
|
+
const keyHash = await computeKeyHashFromCoords(publicKeyX, publicKeyY);
|
|
378
|
+
const compressedPubkey = compressPublicKey(publicKeyX, publicKeyY);
|
|
379
|
+
const compactSig = rsToCompactSignature(signature.r, signature.s);
|
|
380
|
+
const request = {
|
|
381
|
+
signature: {
|
|
382
|
+
r: "0x" + signature.r.toString(16).padStart(64, "0"),
|
|
383
|
+
s: "0x" + signature.s.toString(16).padStart(64, "0"),
|
|
384
|
+
authenticatorData: signature.authenticatorData,
|
|
385
|
+
clientDataJSON: signature.clientDataJSON,
|
|
386
|
+
challengeIndex: signature.challengeIndex,
|
|
387
|
+
typeIndex: signature.typeIndex
|
|
388
|
+
},
|
|
389
|
+
publicKeyX: "0x" + publicKeyX.toString(16).padStart(64, "0"),
|
|
390
|
+
publicKeyY: "0x" + publicKeyY.toString(16).padStart(64, "0"),
|
|
391
|
+
compressedPubkey: "0x" + bytesToHex(compressedPubkey),
|
|
392
|
+
compactSignature: "0x" + bytesToHex(compactSig),
|
|
393
|
+
targetChain,
|
|
394
|
+
actionPayload,
|
|
395
|
+
userNonce: Number(nonce)
|
|
396
|
+
};
|
|
397
|
+
const response = await fetch(`${relayerUrl}/api/v1/submit`, {
|
|
398
|
+
method: "POST",
|
|
399
|
+
headers: { "Content-Type": "application/json" },
|
|
400
|
+
body: JSON.stringify(request)
|
|
401
|
+
});
|
|
402
|
+
if (!response.ok) {
|
|
403
|
+
const errorText = await response.text().catch(() => "Unknown error");
|
|
404
|
+
throw new Error(
|
|
405
|
+
`Relayer submission failed: ${response.status} ${response.statusText}. Error: ${errorText}`
|
|
406
|
+
);
|
|
407
|
+
}
|
|
408
|
+
const result = await response.json();
|
|
409
|
+
return {
|
|
410
|
+
transactionHash: result.transactionHash ?? result.txHash ?? result.hubTxHash ?? "",
|
|
411
|
+
sequence: BigInt(result.sequence || 0),
|
|
412
|
+
userKeyHash: keyHash,
|
|
413
|
+
targetChain
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
// ========================================================================
|
|
417
|
+
// ChainClient Interface - Vault Management
|
|
418
|
+
// ========================================================================
|
|
419
|
+
/**
|
|
420
|
+
* Get vault address for a user.
|
|
421
|
+
* On Stacks, vaults are map-based within the vault contract.
|
|
422
|
+
* The "vault address" is the vault contract principal itself.
|
|
423
|
+
*/
|
|
424
|
+
async getVaultAddress(userKeyHash) {
|
|
425
|
+
if (!this.vaultContract) {
|
|
426
|
+
return null;
|
|
427
|
+
}
|
|
428
|
+
const exists = await this.vaultExists(userKeyHash);
|
|
429
|
+
if (!exists) {
|
|
430
|
+
return null;
|
|
431
|
+
}
|
|
432
|
+
return `${this.vaultContract.address}.${this.vaultContract.name}`;
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* Compute vault address deterministically.
|
|
436
|
+
* On Stacks, all vaults live in the same contract (map-based).
|
|
437
|
+
*/
|
|
438
|
+
computeVaultAddress(_userKeyHash) {
|
|
439
|
+
if (!this.vaultContract) {
|
|
440
|
+
throw new Error("Vault contract not configured");
|
|
441
|
+
}
|
|
442
|
+
return `${this.vaultContract.address}.${this.vaultContract.name}`;
|
|
443
|
+
}
|
|
444
|
+
/**
|
|
445
|
+
* Check if a vault (identity) exists for a user.
|
|
446
|
+
* Queries the spoke contract's `identity-exists` read-only function.
|
|
447
|
+
*/
|
|
448
|
+
async vaultExists(userKeyHash) {
|
|
449
|
+
if (!this.spokeContract) {
|
|
450
|
+
return false;
|
|
451
|
+
}
|
|
452
|
+
try {
|
|
453
|
+
const result = await this.callReadOnly(
|
|
454
|
+
this.spokeContract.address,
|
|
455
|
+
this.spokeContract.name,
|
|
456
|
+
"identity-exists",
|
|
457
|
+
[`0x${userKeyHash.replace("0x", "")}`]
|
|
458
|
+
);
|
|
459
|
+
return result === true || result?.value === true;
|
|
460
|
+
} catch {
|
|
461
|
+
return false;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
/**
|
|
465
|
+
* Create a vault (register identity) on Stacks.
|
|
466
|
+
* Must be done via Hub dispatch or relayer in Phase 1.
|
|
467
|
+
*/
|
|
468
|
+
async createVault(userKeyHash, _signer) {
|
|
469
|
+
throw new Error(
|
|
470
|
+
`Vault creation on Stacks requires Passkey signature verification. Use createVaultViaRelayer() for sponsored identity registration, or call register-identity directly with a signed Stacks transaction. KeyHash=${userKeyHash}`
|
|
471
|
+
);
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* Create a vault with a sponsor wallet.
|
|
475
|
+
* On Stacks, this registers an identity via sponsored transaction.
|
|
476
|
+
*/
|
|
477
|
+
async createVaultSponsored(userKeyHash, _sponsorPrivateKey, _rpcUrl) {
|
|
478
|
+
throw new Error(
|
|
479
|
+
`Sponsored vault creation on Stacks requires the user to sign with their Passkey. Use createVaultViaRelayer() which handles the sponsored transaction flow. KeyHash=${userKeyHash}`
|
|
480
|
+
);
|
|
481
|
+
}
|
|
482
|
+
/**
|
|
483
|
+
* Create a vault via the relayer (sponsored/gasless).
|
|
484
|
+
* The relayer will sponsor the register-identity transaction.
|
|
485
|
+
*/
|
|
486
|
+
async createVaultViaRelayer(userKeyHash, relayerUrl) {
|
|
487
|
+
const response = await fetch(`${relayerUrl}/api/v1/stacks/vault`, {
|
|
488
|
+
method: "POST",
|
|
489
|
+
headers: { "Content-Type": "application/json" },
|
|
490
|
+
body: JSON.stringify({
|
|
491
|
+
userKeyHash,
|
|
492
|
+
chainId: this.config.wormholeChainId
|
|
493
|
+
})
|
|
494
|
+
});
|
|
495
|
+
const result = await response.json();
|
|
496
|
+
if (!response.ok || !result.success) {
|
|
497
|
+
throw new Error(result.error || "Failed to create vault via relayer");
|
|
498
|
+
}
|
|
499
|
+
return {
|
|
500
|
+
address: result.vaultAddress || this.computeVaultAddress(userKeyHash),
|
|
501
|
+
transactionHash: result.transactionHash || "",
|
|
502
|
+
blockNumber: 0,
|
|
503
|
+
gasUsed: 0n,
|
|
504
|
+
alreadyExisted: result.alreadyExists || false,
|
|
505
|
+
sponsoredBy: "relayer"
|
|
506
|
+
};
|
|
507
|
+
}
|
|
508
|
+
async estimateVaultCreationGas(_userKeyHash) {
|
|
509
|
+
return 10000n;
|
|
510
|
+
}
|
|
511
|
+
getFactoryAddress() {
|
|
512
|
+
return void 0;
|
|
513
|
+
}
|
|
514
|
+
getImplementationAddress() {
|
|
515
|
+
return void 0;
|
|
516
|
+
}
|
|
517
|
+
// ========================================================================
|
|
518
|
+
// Stacks-Specific: Balance Queries
|
|
519
|
+
// ========================================================================
|
|
520
|
+
/**
|
|
521
|
+
* Get native STX balance for an address.
|
|
522
|
+
*/
|
|
523
|
+
async getNativeBalance(address) {
|
|
524
|
+
try {
|
|
525
|
+
const response = await fetch(
|
|
526
|
+
`${this.rpcUrl}/v2/accounts/${address}?proof=0`
|
|
527
|
+
);
|
|
528
|
+
if (!response.ok) {
|
|
529
|
+
return 0n;
|
|
530
|
+
}
|
|
531
|
+
const data = await response.json();
|
|
532
|
+
return BigInt(data.balance || "0");
|
|
533
|
+
} catch {
|
|
534
|
+
return 0n;
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
* Get vault STX balance for an identity.
|
|
539
|
+
* Queries the vault contract's `get-stx-balance` read-only function.
|
|
540
|
+
*/
|
|
541
|
+
async getVaultStxBalance(keyHash) {
|
|
542
|
+
if (!this.vaultContract) {
|
|
543
|
+
return 0n;
|
|
544
|
+
}
|
|
545
|
+
try {
|
|
546
|
+
const result = await this.callReadOnly(
|
|
547
|
+
this.vaultContract.address,
|
|
548
|
+
this.vaultContract.name,
|
|
549
|
+
"get-stx-balance",
|
|
550
|
+
[`0x${keyHash.replace("0x", "")}`]
|
|
551
|
+
);
|
|
552
|
+
if (result && result.value !== void 0) {
|
|
553
|
+
return BigInt(result.value);
|
|
554
|
+
}
|
|
555
|
+
return 0n;
|
|
556
|
+
} catch {
|
|
557
|
+
return 0n;
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
/**
|
|
561
|
+
* Get vault sBTC balance for an identity.
|
|
562
|
+
*/
|
|
563
|
+
async getVaultSbtcBalance(keyHash) {
|
|
564
|
+
if (!this.vaultContract) {
|
|
565
|
+
return 0n;
|
|
566
|
+
}
|
|
567
|
+
try {
|
|
568
|
+
const result = await this.callReadOnly(
|
|
569
|
+
this.vaultContract.address,
|
|
570
|
+
this.vaultContract.name,
|
|
571
|
+
"get-sbtc-balance",
|
|
572
|
+
[`0x${keyHash.replace("0x", "")}`]
|
|
573
|
+
);
|
|
574
|
+
if (result && result.value !== void 0) {
|
|
575
|
+
return BigInt(result.value);
|
|
576
|
+
}
|
|
577
|
+
return 0n;
|
|
578
|
+
} catch {
|
|
579
|
+
return 0n;
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
// ========================================================================
|
|
583
|
+
// Stacks-Specific: Identity & Session Queries
|
|
584
|
+
// ========================================================================
|
|
585
|
+
/**
|
|
586
|
+
* Get identity info from the spoke contract.
|
|
587
|
+
*/
|
|
588
|
+
async getIdentity(keyHash) {
|
|
589
|
+
if (!this.spokeContract) {
|
|
590
|
+
return null;
|
|
591
|
+
}
|
|
592
|
+
try {
|
|
593
|
+
const result = await this.callReadOnly(
|
|
594
|
+
this.spokeContract.address,
|
|
595
|
+
this.spokeContract.name,
|
|
596
|
+
"get-identity",
|
|
597
|
+
[`0x${keyHash.replace("0x", "")}`]
|
|
598
|
+
);
|
|
599
|
+
if (!result || result.value === void 0) {
|
|
600
|
+
return null;
|
|
601
|
+
}
|
|
602
|
+
const val = result.value;
|
|
603
|
+
return {
|
|
604
|
+
compressedPubkey: val["compressed-pubkey"]?.value || "",
|
|
605
|
+
owner: val.owner?.value || "",
|
|
606
|
+
nonce: BigInt(val.nonce?.value || 0),
|
|
607
|
+
createdAt: BigInt(val["created-at"]?.value || 0)
|
|
608
|
+
};
|
|
609
|
+
} catch {
|
|
610
|
+
return null;
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
/**
|
|
614
|
+
* Get session info from the spoke contract.
|
|
615
|
+
*/
|
|
616
|
+
async getSession(keyHash, sessionHash) {
|
|
617
|
+
if (!this.spokeContract) {
|
|
618
|
+
return null;
|
|
619
|
+
}
|
|
620
|
+
try {
|
|
621
|
+
const cleanKeyHash = `0x${keyHash.replace("0x", "")}`;
|
|
622
|
+
const cleanSessionHash = `0x${sessionHash.replace("0x", "")}`;
|
|
623
|
+
const result = await this.callReadOnly(
|
|
624
|
+
this.spokeContract.address,
|
|
625
|
+
this.spokeContract.name,
|
|
626
|
+
"get-session",
|
|
627
|
+
[cleanKeyHash, cleanSessionHash]
|
|
628
|
+
);
|
|
629
|
+
if (!result || result.value === void 0) {
|
|
630
|
+
return null;
|
|
631
|
+
}
|
|
632
|
+
const val = result.value;
|
|
633
|
+
return {
|
|
634
|
+
sessionPubkey: val["session-pubkey"]?.value || "",
|
|
635
|
+
expiry: BigInt(val.expiry?.value || 0),
|
|
636
|
+
maxValue: BigInt(val["max-value"]?.value || 0),
|
|
637
|
+
spent: BigInt(val.spent?.value || 0),
|
|
638
|
+
revoked: val.revoked?.value === true,
|
|
639
|
+
createdAt: BigInt(val["created-at"]?.value || 0)
|
|
640
|
+
};
|
|
641
|
+
} catch {
|
|
642
|
+
return null;
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
/**
|
|
646
|
+
* Check if a session is currently active.
|
|
647
|
+
*/
|
|
648
|
+
async checkSessionActive(keyHash, sessionHash) {
|
|
649
|
+
if (!this.spokeContract) {
|
|
650
|
+
return false;
|
|
651
|
+
}
|
|
652
|
+
try {
|
|
653
|
+
const cleanKeyHash = `0x${keyHash.replace("0x", "")}`;
|
|
654
|
+
const cleanSessionHash = `0x${sessionHash.replace("0x", "")}`;
|
|
655
|
+
const result = await this.callReadOnly(
|
|
656
|
+
this.spokeContract.address,
|
|
657
|
+
this.spokeContract.name,
|
|
658
|
+
"is-session-active",
|
|
659
|
+
[cleanKeyHash, cleanSessionHash]
|
|
660
|
+
);
|
|
661
|
+
return result?.value === true;
|
|
662
|
+
} catch {
|
|
663
|
+
return false;
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
/**
|
|
667
|
+
* Get remaining spending budget for a session.
|
|
668
|
+
*/
|
|
669
|
+
async getRemainingBudget(keyHash, sessionHash) {
|
|
670
|
+
if (!this.spokeContract) {
|
|
671
|
+
return 0n;
|
|
672
|
+
}
|
|
673
|
+
try {
|
|
674
|
+
const cleanKeyHash = `0x${keyHash.replace("0x", "")}`;
|
|
675
|
+
const cleanSessionHash = `0x${sessionHash.replace("0x", "")}`;
|
|
676
|
+
const result = await this.callReadOnly(
|
|
677
|
+
this.spokeContract.address,
|
|
678
|
+
this.spokeContract.name,
|
|
679
|
+
"get-remaining-budget",
|
|
680
|
+
[cleanKeyHash, cleanSessionHash]
|
|
681
|
+
);
|
|
682
|
+
if (result && result.value !== void 0) {
|
|
683
|
+
return BigInt(result.value);
|
|
684
|
+
}
|
|
685
|
+
return 0n;
|
|
686
|
+
} catch {
|
|
687
|
+
return 0n;
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
// ========================================================================
|
|
691
|
+
// Stacks-Specific: Protocol Status
|
|
692
|
+
// ========================================================================
|
|
693
|
+
/**
|
|
694
|
+
* Check if the spoke contract is paused.
|
|
695
|
+
*/
|
|
696
|
+
async isProtocolPaused() {
|
|
697
|
+
if (!this.spokeContract) {
|
|
698
|
+
return false;
|
|
699
|
+
}
|
|
700
|
+
try {
|
|
701
|
+
const result = await this.callReadOnly(
|
|
702
|
+
this.spokeContract.address,
|
|
703
|
+
this.spokeContract.name,
|
|
704
|
+
"is-paused",
|
|
705
|
+
[]
|
|
706
|
+
);
|
|
707
|
+
return result === true || result?.value === true;
|
|
708
|
+
} catch {
|
|
709
|
+
return false;
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
/**
|
|
713
|
+
* Get global identity count.
|
|
714
|
+
*/
|
|
715
|
+
async getIdentityCount() {
|
|
716
|
+
if (!this.spokeContract) {
|
|
717
|
+
return 0n;
|
|
718
|
+
}
|
|
719
|
+
try {
|
|
720
|
+
const result = await this.callReadOnly(
|
|
721
|
+
this.spokeContract.address,
|
|
722
|
+
this.spokeContract.name,
|
|
723
|
+
"get-identity-count",
|
|
724
|
+
[]
|
|
725
|
+
);
|
|
726
|
+
return BigInt(result?.value || result || 0);
|
|
727
|
+
} catch {
|
|
728
|
+
return 0n;
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
/**
|
|
732
|
+
* Get total STX deposited across all vaults.
|
|
733
|
+
*/
|
|
734
|
+
async getTotalStxDeposited() {
|
|
735
|
+
if (!this.vaultContract) {
|
|
736
|
+
return 0n;
|
|
737
|
+
}
|
|
738
|
+
try {
|
|
739
|
+
const result = await this.callReadOnly(
|
|
740
|
+
this.vaultContract.address,
|
|
741
|
+
this.vaultContract.name,
|
|
742
|
+
"get-total-stx-deposited",
|
|
743
|
+
[]
|
|
744
|
+
);
|
|
745
|
+
return BigInt(result?.value || result || 0);
|
|
746
|
+
} catch {
|
|
747
|
+
return 0n;
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
// ========================================================================
|
|
751
|
+
// Session Management (Issue #13)
|
|
752
|
+
// ========================================================================
|
|
753
|
+
/**
|
|
754
|
+
* Register a session key on the Stacks spoke.
|
|
755
|
+
* On Stacks, sessions are managed directly on the spoke contract
|
|
756
|
+
* (unlike EVM spokes where sessions are on the Hub).
|
|
757
|
+
*/
|
|
758
|
+
async registerSession(_params) {
|
|
759
|
+
throw new Error(
|
|
760
|
+
"Session registration on Stacks requires a Passkey signature. Build a register-session transaction with the Passkey signature, then submit via the relayer for sponsored execution."
|
|
761
|
+
);
|
|
762
|
+
}
|
|
763
|
+
/**
|
|
764
|
+
* Revoke a session key on the Stacks spoke.
|
|
765
|
+
*/
|
|
766
|
+
async revokeSession(_params) {
|
|
767
|
+
throw new Error(
|
|
768
|
+
"Session revocation on Stacks requires a Passkey signature. Build a revoke-session transaction with the Passkey signature, then submit via the relayer for sponsored execution."
|
|
769
|
+
);
|
|
770
|
+
}
|
|
771
|
+
/**
|
|
772
|
+
* Check if a session is active.
|
|
773
|
+
*/
|
|
774
|
+
async isSessionActive(userKeyHash, sessionKeyHash) {
|
|
775
|
+
const active = await this.checkSessionActive(userKeyHash, sessionKeyHash);
|
|
776
|
+
const session = await this.getSession(userKeyHash, sessionKeyHash);
|
|
777
|
+
return {
|
|
778
|
+
isActive: active,
|
|
779
|
+
expiry: session ? Number(session.expiry) : 0,
|
|
780
|
+
maxValue: session?.maxValue ?? 0n,
|
|
781
|
+
chainScopes: [this.config.wormholeChainId]
|
|
782
|
+
};
|
|
783
|
+
}
|
|
784
|
+
/**
|
|
785
|
+
* Get all sessions for a user.
|
|
786
|
+
* Note: Clarity maps don't support enumeration, so this requires
|
|
787
|
+
* off-chain indexing or event log parsing.
|
|
788
|
+
*/
|
|
789
|
+
async getUserSessions(_userKeyHash) {
|
|
790
|
+
throw new Error(
|
|
791
|
+
'Enumerating all sessions is not supported on Stacks (Clarity maps are not iterable). Use checkSessionActive() with a known session hash, or query the Stacks event log for "session-registered" print events via the Hiro API.'
|
|
792
|
+
);
|
|
793
|
+
}
|
|
794
|
+
// ========================================================================
|
|
795
|
+
// Stacks-Specific: Transaction Status
|
|
796
|
+
// ========================================================================
|
|
797
|
+
/**
|
|
798
|
+
* Get the status of a Stacks transaction.
|
|
799
|
+
*/
|
|
800
|
+
async getTransactionStatus(txId) {
|
|
801
|
+
try {
|
|
802
|
+
const cleanTxId = txId.startsWith("0x") ? txId : `0x${txId}`;
|
|
803
|
+
const response = await fetch(
|
|
804
|
+
`${this.rpcUrl}/extended/v1/tx/${cleanTxId}`
|
|
805
|
+
);
|
|
806
|
+
if (!response.ok) {
|
|
807
|
+
return { status: "not_found" };
|
|
808
|
+
}
|
|
809
|
+
const data = await response.json();
|
|
810
|
+
const txStatus = data.tx_status;
|
|
811
|
+
if (txStatus === "success") {
|
|
812
|
+
return {
|
|
813
|
+
status: "success",
|
|
814
|
+
blockHeight: data.block_height
|
|
815
|
+
};
|
|
816
|
+
}
|
|
817
|
+
if (txStatus === "pending") {
|
|
818
|
+
return { status: "pending" };
|
|
819
|
+
}
|
|
820
|
+
if (txStatus === "abort_by_response" || txStatus === "abort_by_post_condition") {
|
|
821
|
+
return {
|
|
822
|
+
status: "failed",
|
|
823
|
+
error: `Transaction aborted: ${txStatus}`
|
|
824
|
+
};
|
|
825
|
+
}
|
|
826
|
+
return { status: "pending" };
|
|
827
|
+
} catch {
|
|
828
|
+
return { status: "not_found" };
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
/**
|
|
832
|
+
* Wait for a transaction to be confirmed.
|
|
833
|
+
*
|
|
834
|
+
* @param txId - Transaction ID
|
|
835
|
+
* @param maxAttempts - Maximum polling attempts (default: 60)
|
|
836
|
+
* @param pollIntervalMs - Polling interval in milliseconds (default: 5000)
|
|
837
|
+
*/
|
|
838
|
+
async waitForConfirmation(txId, maxAttempts = 60, pollIntervalMs = 5e3) {
|
|
839
|
+
for (let i = 0; i < maxAttempts; i++) {
|
|
840
|
+
const status = await this.getTransactionStatus(txId);
|
|
841
|
+
if (status.status === "success") {
|
|
842
|
+
return { confirmed: true, blockHeight: status.blockHeight };
|
|
843
|
+
}
|
|
844
|
+
if (status.status === "failed") {
|
|
845
|
+
throw new Error(`Transaction failed: ${status.error}`);
|
|
846
|
+
}
|
|
847
|
+
await new Promise((resolve) => setTimeout(resolve, pollIntervalMs));
|
|
848
|
+
}
|
|
849
|
+
return { confirmed: false };
|
|
850
|
+
}
|
|
851
|
+
// ========================================================================
|
|
852
|
+
// Stacks-Specific: Network Info
|
|
853
|
+
// ========================================================================
|
|
854
|
+
/**
|
|
855
|
+
* Get Stacks network info (block height, network version, etc.).
|
|
856
|
+
*/
|
|
857
|
+
async getNetworkInfo() {
|
|
858
|
+
const response = await fetch(`${this.rpcUrl}/v2/info`);
|
|
859
|
+
if (!response.ok) {
|
|
860
|
+
throw new Error(`Failed to get Stacks network info: ${response.statusText}`);
|
|
861
|
+
}
|
|
862
|
+
const data = await response.json();
|
|
863
|
+
return {
|
|
864
|
+
networkId: data.network_id,
|
|
865
|
+
stacksBlockHeight: data.stacks_tip_height,
|
|
866
|
+
burnBlockHeight: data.burn_block_height,
|
|
867
|
+
serverVersion: data.server_version
|
|
868
|
+
};
|
|
869
|
+
}
|
|
870
|
+
/**
|
|
871
|
+
* Get the current Stacks block height.
|
|
872
|
+
* Used for session expiry calculations.
|
|
873
|
+
*/
|
|
874
|
+
async getCurrentBlockHeight() {
|
|
875
|
+
const info = await this.getNetworkInfo();
|
|
876
|
+
return info.stacksBlockHeight;
|
|
877
|
+
}
|
|
878
|
+
// ========================================================================
|
|
879
|
+
// Internal: Read-Only Contract Calls via Hiro API
|
|
880
|
+
// ========================================================================
|
|
881
|
+
/**
|
|
882
|
+
* Call a read-only Clarity function via the Hiro API.
|
|
883
|
+
* Uses the /v2/contracts/call-read endpoint.
|
|
884
|
+
*/
|
|
885
|
+
async callReadOnly(contractAddress, contractName, functionName, args) {
|
|
886
|
+
const url = `${this.rpcUrl}/v2/contracts/call-read/${contractAddress}/${contractName}/${functionName}`;
|
|
887
|
+
const response = await fetch(url, {
|
|
888
|
+
method: "POST",
|
|
889
|
+
headers: { "Content-Type": "application/json" },
|
|
890
|
+
body: JSON.stringify({
|
|
891
|
+
sender: contractAddress,
|
|
892
|
+
arguments: args
|
|
893
|
+
})
|
|
894
|
+
});
|
|
895
|
+
if (!response.ok) {
|
|
896
|
+
const errorText = await response.text().catch(() => "Unknown error");
|
|
897
|
+
throw new Error(
|
|
898
|
+
`Read-only call failed: ${contractAddress}.${contractName}::${functionName} - ${response.status}: ${errorText}`
|
|
899
|
+
);
|
|
900
|
+
}
|
|
901
|
+
const data = await response.json();
|
|
902
|
+
if (!data.okay) {
|
|
903
|
+
throw new Error(
|
|
904
|
+
`Read-only call returned error: ${contractAddress}.${contractName}::${functionName} - ${data.cause || "Unknown cause"}`
|
|
905
|
+
);
|
|
906
|
+
}
|
|
907
|
+
return this.parseClarityValue(data.result);
|
|
908
|
+
}
|
|
909
|
+
/**
|
|
910
|
+
* Parse a hex-encoded Clarity value from the API response.
|
|
911
|
+
* This is a simplified parser for common Clarity types.
|
|
912
|
+
*/
|
|
913
|
+
parseClarityValue(hex) {
|
|
914
|
+
if (!hex || hex === "0x") {
|
|
915
|
+
return null;
|
|
916
|
+
}
|
|
917
|
+
const bytes = hexToBytes(hex);
|
|
918
|
+
if (bytes.length === 0) {
|
|
919
|
+
return null;
|
|
920
|
+
}
|
|
921
|
+
const typeId = bytes[0];
|
|
922
|
+
switch (typeId) {
|
|
923
|
+
// int (0x00)
|
|
924
|
+
case 0: {
|
|
925
|
+
let value = 0n;
|
|
926
|
+
for (let i = 1; i < 17 && i < bytes.length; i++) {
|
|
927
|
+
value = value << 8n | BigInt(bytes[i]);
|
|
928
|
+
}
|
|
929
|
+
return { value };
|
|
930
|
+
}
|
|
931
|
+
// uint (0x01)
|
|
932
|
+
case 1: {
|
|
933
|
+
let value = 0n;
|
|
934
|
+
for (let i = 1; i < 17 && i < bytes.length; i++) {
|
|
935
|
+
value = value << 8n | BigInt(bytes[i]);
|
|
936
|
+
}
|
|
937
|
+
return { value };
|
|
938
|
+
}
|
|
939
|
+
// buffer (0x02)
|
|
940
|
+
case 2: {
|
|
941
|
+
const len = bytes[1] << 24 | bytes[2] << 16 | bytes[3] << 8 | bytes[4];
|
|
942
|
+
const bufValue = bytes.slice(5, 5 + len);
|
|
943
|
+
return { value: "0x" + bytesToHex(bufValue) };
|
|
944
|
+
}
|
|
945
|
+
// bool true (0x03)
|
|
946
|
+
case 3:
|
|
947
|
+
return true;
|
|
948
|
+
// bool false (0x04)
|
|
949
|
+
case 4:
|
|
950
|
+
return false;
|
|
951
|
+
// optional none (0x09)
|
|
952
|
+
case 9:
|
|
953
|
+
return null;
|
|
954
|
+
// optional some (0x0a)
|
|
955
|
+
case 10:
|
|
956
|
+
return this.parseClarityValue("0x" + bytesToHex(bytes.slice(1)));
|
|
957
|
+
// response ok (0x07)
|
|
958
|
+
case 7:
|
|
959
|
+
return this.parseClarityValue("0x" + bytesToHex(bytes.slice(1)));
|
|
960
|
+
// response err (0x08)
|
|
961
|
+
case 8: {
|
|
962
|
+
const errVal = this.parseClarityValue("0x" + bytesToHex(bytes.slice(1)));
|
|
963
|
+
throw new Error(`Clarity error: ${JSON.stringify(errVal)}`);
|
|
964
|
+
}
|
|
965
|
+
// tuple (0x0c)
|
|
966
|
+
case 12: {
|
|
967
|
+
return { value: hex };
|
|
968
|
+
}
|
|
969
|
+
default:
|
|
970
|
+
return { value: hex };
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
};
|
|
974
|
+
|
|
975
|
+
// src/chains/stacks/StacksPostConditions.ts
|
|
976
|
+
function buildStxWithdrawalPostConditions(contractPrincipal, amount) {
|
|
977
|
+
return [
|
|
978
|
+
{
|
|
979
|
+
type: "stx",
|
|
980
|
+
principal: contractPrincipal,
|
|
981
|
+
comparison: "eq",
|
|
982
|
+
amount
|
|
983
|
+
}
|
|
984
|
+
];
|
|
985
|
+
}
|
|
986
|
+
function buildStxDepositPostConditions(senderPrincipal, amount) {
|
|
987
|
+
return [
|
|
988
|
+
{
|
|
989
|
+
type: "stx",
|
|
990
|
+
principal: senderPrincipal,
|
|
991
|
+
comparison: "eq",
|
|
992
|
+
amount
|
|
993
|
+
}
|
|
994
|
+
];
|
|
995
|
+
}
|
|
996
|
+
function buildSbtcWithdrawalPostConditions(contractPrincipal, amount, sbtcContractAddress = "SM3VDXK3WZZSA84XXFKAFAF15NNZX32CTSG82JFQ4", sbtcContractName = "sbtc-token") {
|
|
997
|
+
return [
|
|
998
|
+
{
|
|
999
|
+
type: "ft",
|
|
1000
|
+
principal: contractPrincipal,
|
|
1001
|
+
comparison: "eq",
|
|
1002
|
+
amount,
|
|
1003
|
+
contractAddress: sbtcContractAddress,
|
|
1004
|
+
contractName: sbtcContractName,
|
|
1005
|
+
tokenName: "sbtc-token"
|
|
1006
|
+
}
|
|
1007
|
+
];
|
|
1008
|
+
}
|
|
1009
|
+
function buildExecutePostConditions(actionType, contractPrincipal, amount) {
|
|
1010
|
+
switch (actionType) {
|
|
1011
|
+
case 1:
|
|
1012
|
+
return buildStxWithdrawalPostConditions(contractPrincipal, amount);
|
|
1013
|
+
case 2:
|
|
1014
|
+
return buildSbtcWithdrawalPostConditions(contractPrincipal, amount);
|
|
1015
|
+
default:
|
|
1016
|
+
return [];
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
1019
|
+
function validatePostConditions(postConditions, expectedAmount) {
|
|
1020
|
+
if (postConditions.length === 0) {
|
|
1021
|
+
return {
|
|
1022
|
+
valid: false,
|
|
1023
|
+
error: "No post-conditions attached. Asset transfers require post-conditions for safety."
|
|
1024
|
+
};
|
|
1025
|
+
}
|
|
1026
|
+
const hasMatchingAmount = postConditions.some((pc) => {
|
|
1027
|
+
if (pc.type === "stx" || pc.type === "ft") {
|
|
1028
|
+
return pc.amount === expectedAmount && pc.comparison === "eq";
|
|
1029
|
+
}
|
|
1030
|
+
return false;
|
|
1031
|
+
});
|
|
1032
|
+
if (!hasMatchingAmount) {
|
|
1033
|
+
return {
|
|
1034
|
+
valid: false,
|
|
1035
|
+
error: `No post-condition matches expected amount ${expectedAmount}. Ensure exact-match post-conditions are attached.`
|
|
1036
|
+
};
|
|
1037
|
+
}
|
|
1038
|
+
return { valid: true };
|
|
1039
|
+
}
|
|
1040
|
+
function principalForPostCondition(principal) {
|
|
1041
|
+
if (isContractPrincipal(principal)) {
|
|
1042
|
+
const parsed = parseContractPrincipal(principal);
|
|
1043
|
+
return { address: parsed.address, contractName: parsed.contractName };
|
|
1044
|
+
}
|
|
1045
|
+
return { address: principal };
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
export {
|
|
1049
|
+
compressPublicKey,
|
|
1050
|
+
rsToCompactSignature,
|
|
1051
|
+
parseDERSignature,
|
|
1052
|
+
derToCompactSignature,
|
|
1053
|
+
computeKeyHash,
|
|
1054
|
+
computeKeyHashFromCoords,
|
|
1055
|
+
buildRegistrationHash,
|
|
1056
|
+
buildSessionRegistrationHash,
|
|
1057
|
+
buildRevocationHash,
|
|
1058
|
+
buildExecuteHash,
|
|
1059
|
+
buildWithdrawalHash,
|
|
1060
|
+
bytesToHex,
|
|
1061
|
+
hexToBytes,
|
|
1062
|
+
STACKS_MAINNET_PREFIX,
|
|
1063
|
+
STACKS_TESTNET_PREFIX,
|
|
1064
|
+
isValidStacksPrincipal,
|
|
1065
|
+
isValidStandardPrincipal,
|
|
1066
|
+
isValidContractName,
|
|
1067
|
+
getNetworkFromAddress,
|
|
1068
|
+
getContractPrincipal,
|
|
1069
|
+
parseContractPrincipal,
|
|
1070
|
+
isContractPrincipal,
|
|
1071
|
+
getStacksExplorerTxUrl,
|
|
1072
|
+
getStacksExplorerAddressUrl,
|
|
1073
|
+
STACKS_ACTION_TYPES,
|
|
1074
|
+
StacksClient,
|
|
1075
|
+
buildStxWithdrawalPostConditions,
|
|
1076
|
+
buildStxDepositPostConditions,
|
|
1077
|
+
buildSbtcWithdrawalPostConditions,
|
|
1078
|
+
buildExecutePostConditions,
|
|
1079
|
+
validatePostConditions,
|
|
1080
|
+
principalForPostCondition
|
|
1081
|
+
};
|
|
1082
|
+
//# sourceMappingURL=chunk-5T6KPH7A.mjs.map
|