@turnkey/core 1.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.MD +5 -0
- package/dist/__clients__/core.d.ts +1010 -0
- package/dist/__clients__/core.d.ts.map +1 -0
- package/dist/__clients__/core.js +2358 -0
- package/dist/__clients__/core.js.map +1 -0
- package/dist/__clients__/core.mjs +2356 -0
- package/dist/__clients__/core.mjs.map +1 -0
- package/dist/__generated__/sdk-client-base.d.ts +211 -0
- package/dist/__generated__/sdk-client-base.d.ts.map +1 -0
- package/dist/__generated__/sdk-client-base.js +3219 -0
- package/dist/__generated__/sdk-client-base.js.map +1 -0
- package/dist/__generated__/sdk-client-base.mjs +3217 -0
- package/dist/__generated__/sdk-client-base.mjs.map +1 -0
- package/dist/__generated__/version.d.ts +2 -0
- package/dist/__generated__/version.d.ts.map +1 -0
- package/dist/__generated__/version.js +6 -0
- package/dist/__generated__/version.js.map +1 -0
- package/dist/__generated__/version.mjs +4 -0
- package/dist/__generated__/version.mjs.map +1 -0
- package/dist/__inputs__/public_api.types.d.ts +5170 -0
- package/dist/__inputs__/public_api.types.d.ts.map +1 -0
- package/dist/__polyfills__/window.d.ts +15 -0
- package/dist/__polyfills__/window.d.ts.map +1 -0
- package/dist/__polyfills__/window.js +30 -0
- package/dist/__polyfills__/window.js.map +1 -0
- package/dist/__polyfills__/window.mjs +28 -0
- package/dist/__polyfills__/window.mjs.map +1 -0
- package/dist/__stampers__/api/base.d.ts +20 -0
- package/dist/__stampers__/api/base.d.ts.map +1 -0
- package/dist/__stampers__/api/base.js +67 -0
- package/dist/__stampers__/api/base.js.map +1 -0
- package/dist/__stampers__/api/base.mjs +65 -0
- package/dist/__stampers__/api/base.mjs.map +1 -0
- package/dist/__stampers__/api/mobile/stamper.d.ts +13 -0
- package/dist/__stampers__/api/mobile/stamper.d.ts.map +1 -0
- package/dist/__stampers__/api/mobile/stamper.js +69 -0
- package/dist/__stampers__/api/mobile/stamper.js.map +1 -0
- package/dist/__stampers__/api/mobile/stamper.mjs +67 -0
- package/dist/__stampers__/api/mobile/stamper.mjs.map +1 -0
- package/dist/__stampers__/api/web/stamper.d.ts +14 -0
- package/dist/__stampers__/api/web/stamper.d.ts.map +1 -0
- package/dist/__stampers__/api/web/stamper.js +212 -0
- package/dist/__stampers__/api/web/stamper.js.map +1 -0
- package/dist/__stampers__/api/web/stamper.mjs +210 -0
- package/dist/__stampers__/api/web/stamper.mjs.map +1 -0
- package/dist/__stampers__/passkey/base.d.ts +18 -0
- package/dist/__stampers__/passkey/base.d.ts.map +1 -0
- package/dist/__stampers__/passkey/base.js +138 -0
- package/dist/__stampers__/passkey/base.js.map +1 -0
- package/dist/__stampers__/passkey/base.mjs +134 -0
- package/dist/__stampers__/passkey/base.mjs.map +1 -0
- package/dist/__storage__/base.d.ts +3 -0
- package/dist/__storage__/base.d.ts.map +1 -0
- package/dist/__storage__/base.js +27 -0
- package/dist/__storage__/base.js.map +1 -0
- package/dist/__storage__/base.mjs +25 -0
- package/dist/__storage__/base.mjs.map +1 -0
- package/dist/__storage__/mobile/storage.d.ts +18 -0
- package/dist/__storage__/mobile/storage.d.ts.map +1 -0
- package/dist/__storage__/mobile/storage.js +74 -0
- package/dist/__storage__/mobile/storage.js.map +1 -0
- package/dist/__storage__/mobile/storage.mjs +72 -0
- package/dist/__storage__/mobile/storage.mjs.map +1 -0
- package/dist/__storage__/web/storage.d.ts +19 -0
- package/dist/__storage__/web/storage.d.ts.map +1 -0
- package/dist/__storage__/web/storage.js +79 -0
- package/dist/__storage__/web/storage.js.map +1 -0
- package/dist/__storage__/web/storage.mjs +77 -0
- package/dist/__storage__/web/storage.mjs.map +1 -0
- package/dist/__types__/base.d.ts +407 -0
- package/dist/__types__/base.d.ts.map +1 -0
- package/dist/__types__/base.js +88 -0
- package/dist/__types__/base.js.map +1 -0
- package/dist/__types__/base.mjs +84 -0
- package/dist/__types__/base.mjs.map +1 -0
- package/dist/__wallet__/base.d.ts +3 -0
- package/dist/__wallet__/base.d.ts.map +1 -0
- package/dist/__wallet__/base.js +24 -0
- package/dist/__wallet__/base.js.map +1 -0
- package/dist/__wallet__/base.mjs +22 -0
- package/dist/__wallet__/base.mjs.map +1 -0
- package/dist/__wallet__/connector.d.ts +33 -0
- package/dist/__wallet__/connector.d.ts.map +1 -0
- package/dist/__wallet__/connector.js +63 -0
- package/dist/__wallet__/connector.js.map +1 -0
- package/dist/__wallet__/connector.mjs +61 -0
- package/dist/__wallet__/connector.mjs.map +1 -0
- package/dist/__wallet__/mobile/manager.d.ts +35 -0
- package/dist/__wallet__/mobile/manager.d.ts.map +1 -0
- package/dist/__wallet__/mobile/manager.js +95 -0
- package/dist/__wallet__/mobile/manager.js.map +1 -0
- package/dist/__wallet__/mobile/manager.mjs +93 -0
- package/dist/__wallet__/mobile/manager.mjs.map +1 -0
- package/dist/__wallet__/stamper.d.ts +19 -0
- package/dist/__wallet__/stamper.d.ts.map +1 -0
- package/dist/__wallet__/stamper.js +116 -0
- package/dist/__wallet__/stamper.js.map +1 -0
- package/dist/__wallet__/stamper.mjs +113 -0
- package/dist/__wallet__/stamper.mjs.map +1 -0
- package/dist/__wallet__/wallet-connect/base.d.ts +83 -0
- package/dist/__wallet__/wallet-connect/base.d.ts.map +1 -0
- package/dist/__wallet__/wallet-connect/base.js +362 -0
- package/dist/__wallet__/wallet-connect/base.js.map +1 -0
- package/dist/__wallet__/wallet-connect/base.mjs +360 -0
- package/dist/__wallet__/wallet-connect/base.mjs.map +1 -0
- package/dist/__wallet__/wallet-connect/client.d.ts +78 -0
- package/dist/__wallet__/wallet-connect/client.d.ts.map +1 -0
- package/dist/__wallet__/wallet-connect/client.js +139 -0
- package/dist/__wallet__/wallet-connect/client.js.map +1 -0
- package/dist/__wallet__/wallet-connect/client.mjs +137 -0
- package/dist/__wallet__/wallet-connect/client.mjs.map +1 -0
- package/dist/__wallet__/web/manager.d.ts +41 -0
- package/dist/__wallet__/web/manager.d.ts.map +1 -0
- package/dist/__wallet__/web/manager.js +115 -0
- package/dist/__wallet__/web/manager.js.map +1 -0
- package/dist/__wallet__/web/manager.mjs +113 -0
- package/dist/__wallet__/web/manager.mjs.map +1 -0
- package/dist/__wallet__/web/native/ethereum.d.ts +67 -0
- package/dist/__wallet__/web/native/ethereum.d.ts.map +1 -0
- package/dist/__wallet__/web/native/ethereum.js +248 -0
- package/dist/__wallet__/web/native/ethereum.js.map +1 -0
- package/dist/__wallet__/web/native/ethereum.mjs +245 -0
- package/dist/__wallet__/web/native/ethereum.mjs.map +1 -0
- package/dist/__wallet__/web/native/solana.d.ts +19 -0
- package/dist/__wallet__/web/native/solana.d.ts.map +1 -0
- package/dist/__wallet__/web/native/solana.js +149 -0
- package/dist/__wallet__/web/native/solana.js.map +1 -0
- package/dist/__wallet__/web/native/solana.mjs +146 -0
- package/dist/__wallet__/web/native/solana.mjs.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +78 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +8 -0
- package/dist/index.mjs.map +1 -0
- package/dist/turnkey-helpers.d.ts +68 -0
- package/dist/turnkey-helpers.d.ts.map +1 -0
- package/dist/turnkey-helpers.js +466 -0
- package/dist/turnkey-helpers.js.map +1 -0
- package/dist/turnkey-helpers.mjs +399 -0
- package/dist/turnkey-helpers.mjs.map +1 -0
- package/dist/utils.d.ts +54 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +596 -0
- package/dist/utils.js.map +1 -0
- package/dist/utils.mjs +574 -0
- package/dist/utils.mjs.map +1 -0
- package/package.json +67 -0
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
import { uint8ArrayFromHexString, stringToBase64urlString, uint8ArrayToHexString } from '@turnkey/encoding';
|
|
2
|
+
import bs58 from 'bs58';
|
|
3
|
+
import { toHex, recoverPublicKey, hashMessage } from 'viem';
|
|
4
|
+
import { compressRawPublicKey } from '@turnkey/crypto';
|
|
5
|
+
import { WalletInterfaceType, Chain, SignIntent } from '../../__types__/base.mjs';
|
|
6
|
+
import { Transaction } from 'ethers';
|
|
7
|
+
|
|
8
|
+
class WalletConnectWallet {
|
|
9
|
+
constructor(client) {
|
|
10
|
+
this.client = client;
|
|
11
|
+
this.interfaceType = WalletInterfaceType.WalletConnect;
|
|
12
|
+
this.ethereumNamespaces = [];
|
|
13
|
+
this.solanaNamespaces = [];
|
|
14
|
+
this.client.onSessionDelete(() => { });
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Initializes WalletConnect pairing flow with the specified chains.
|
|
18
|
+
* If an active session already exists, pairing is skipped.
|
|
19
|
+
*
|
|
20
|
+
* @param opts.ethereum - Whether to enable Ethereum pairing
|
|
21
|
+
* @param opts.solana - Whether to enable Solana pairing
|
|
22
|
+
* @throws {Error} If no chains are enabled
|
|
23
|
+
*/
|
|
24
|
+
async init(opts) {
|
|
25
|
+
this.ethereumNamespaces = opts.ethereumNamespaces;
|
|
26
|
+
if (this.ethereumNamespaces.length > 0) {
|
|
27
|
+
this.ethChain = this.ethereumNamespaces[0];
|
|
28
|
+
}
|
|
29
|
+
this.solanaNamespaces = opts.solanaNamespaces;
|
|
30
|
+
if (this.solanaNamespaces.length > 0) {
|
|
31
|
+
this.solChain = this.solanaNamespaces[0];
|
|
32
|
+
}
|
|
33
|
+
if (this.ethereumNamespaces.length === 0 &&
|
|
34
|
+
this.solanaNamespaces.length === 0) {
|
|
35
|
+
throw new Error("At least one namespace must be enabled for WalletConnect");
|
|
36
|
+
}
|
|
37
|
+
const session = this.client.getSession();
|
|
38
|
+
if (hasConnectedAccounts(session)) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const namespaces = {};
|
|
42
|
+
if (this.ethereumNamespaces.length > 0) {
|
|
43
|
+
namespaces.eip155 = {
|
|
44
|
+
methods: [
|
|
45
|
+
"personal_sign",
|
|
46
|
+
"eth_sendTransaction",
|
|
47
|
+
"eth_chainId",
|
|
48
|
+
"wallet_switchEthereumChain",
|
|
49
|
+
"wallet_addEthereumChain",
|
|
50
|
+
],
|
|
51
|
+
chains: this.ethereumNamespaces,
|
|
52
|
+
events: ["accountsChanged", "chainChanged"],
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
if (this.solanaNamespaces.length > 0) {
|
|
56
|
+
namespaces.solana = {
|
|
57
|
+
methods: [
|
|
58
|
+
"solana_signMessage",
|
|
59
|
+
"solana_signTransaction",
|
|
60
|
+
"solana_sendTransaction",
|
|
61
|
+
],
|
|
62
|
+
chains: this.solanaNamespaces,
|
|
63
|
+
events: ["accountsChanged", "chainChanged"],
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
this.uri = await this.client.pair(namespaces);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Returns WalletConnect providers with associated chain/account metadata.
|
|
70
|
+
*/
|
|
71
|
+
async getProviders() {
|
|
72
|
+
const session = this.client.getSession();
|
|
73
|
+
const info = {
|
|
74
|
+
name: "WalletConnect",
|
|
75
|
+
icon: "https://raw.githubusercontent.com/WalletConnect/walletconnect-assets/refs/heads/master/Icon/Blue%20(Default)/Icon.svg",
|
|
76
|
+
};
|
|
77
|
+
const providers = [];
|
|
78
|
+
if (this.ethereumNamespaces.length > 0) {
|
|
79
|
+
providers.push(await this.buildEthProvider(session, info));
|
|
80
|
+
}
|
|
81
|
+
if (this.solanaNamespaces.length > 0) {
|
|
82
|
+
providers.push(this.buildSolProvider(session, info));
|
|
83
|
+
}
|
|
84
|
+
return providers;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Approves the session if needed and ensures at least one account is available.
|
|
88
|
+
*/
|
|
89
|
+
async connectWalletAccount(_provider) {
|
|
90
|
+
const session = await this.client.approve();
|
|
91
|
+
if (!hasConnectedAccounts(session))
|
|
92
|
+
throw new Error("No account found in session");
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Switch the user’s WalletConnect session to a new EVM chain.
|
|
96
|
+
*
|
|
97
|
+
* @param provider – the WalletProvider returned by getProviders()
|
|
98
|
+
* @param chainOrId – either:
|
|
99
|
+
* • a CAIP string ("eip155:1", "eip155:137", …)
|
|
100
|
+
* • a SwitchableChain object carrying full metadata
|
|
101
|
+
*/
|
|
102
|
+
async switchChain(provider, chainOrId) {
|
|
103
|
+
if (provider.chainInfo.namespace !== Chain.Ethereum) {
|
|
104
|
+
throw new Error("Only EVM wallets support chain switching");
|
|
105
|
+
}
|
|
106
|
+
const session = this.client.getSession();
|
|
107
|
+
if (!session) {
|
|
108
|
+
throw new Error("No active WalletConnect session");
|
|
109
|
+
}
|
|
110
|
+
const hexChainId = typeof chainOrId === "string" ? chainOrId : chainOrId.id;
|
|
111
|
+
const caip = `eip155:${Number.parseInt(hexChainId, 16)}`;
|
|
112
|
+
if (!this.ethereumNamespaces.includes(caip)) {
|
|
113
|
+
throw new Error(`Unsupported chain ${caip}. Supported chains: ${this.ethereumNamespaces.join(", ")}. If you’d like to support ${caip}, add it to the \`ethereumNamespaces\` in your WalletConnect config.`);
|
|
114
|
+
}
|
|
115
|
+
try {
|
|
116
|
+
// first we just try switching
|
|
117
|
+
await this.client.request(this.ethChain, "wallet_switchEthereumChain", [
|
|
118
|
+
{ chainId: hexChainId },
|
|
119
|
+
]);
|
|
120
|
+
this.ethChain = caip;
|
|
121
|
+
}
|
|
122
|
+
catch (err) {
|
|
123
|
+
throw new Error(`Failed to switch chain: ${err.message || err.toString()}`);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Signs a message or transaction using the specified provider and intent.
|
|
128
|
+
*
|
|
129
|
+
* @param message - The message or serialized transaction to sign.
|
|
130
|
+
* @param provider - The WalletProvider to use.
|
|
131
|
+
* @param intent - The type of signing intent (message, tx, send).
|
|
132
|
+
*/
|
|
133
|
+
async sign(message, provider, intent) {
|
|
134
|
+
const session = await this.ensureSession();
|
|
135
|
+
if (!hasConnectedAccounts(session)) {
|
|
136
|
+
await this.connectWalletAccount(provider);
|
|
137
|
+
}
|
|
138
|
+
if (provider.chainInfo.namespace === Chain.Ethereum) {
|
|
139
|
+
const address = getConnectedEthereum(session);
|
|
140
|
+
if (!address) {
|
|
141
|
+
throw new Error("no Ethereum account to sign with");
|
|
142
|
+
}
|
|
143
|
+
switch (intent) {
|
|
144
|
+
case SignIntent.SignMessage:
|
|
145
|
+
return (await this.client.request(this.ethChain, "personal_sign", [
|
|
146
|
+
message,
|
|
147
|
+
address,
|
|
148
|
+
]));
|
|
149
|
+
case SignIntent.SignAndSendTransaction:
|
|
150
|
+
const account = provider.connectedAddresses[0];
|
|
151
|
+
const tx = Transaction.from(message);
|
|
152
|
+
const txParams = {
|
|
153
|
+
from: account,
|
|
154
|
+
to: tx.to?.toString(),
|
|
155
|
+
value: toHex(tx.value),
|
|
156
|
+
gas: toHex(tx.gasLimit),
|
|
157
|
+
maxFeePerGas: toHex(tx.maxFeePerGas ?? 0n),
|
|
158
|
+
maxPriorityFeePerGas: toHex(tx.maxPriorityFeePerGas ?? 0n),
|
|
159
|
+
nonce: toHex(tx.nonce),
|
|
160
|
+
chainId: toHex(tx.chainId),
|
|
161
|
+
data: tx.data?.toString() ?? "0x",
|
|
162
|
+
};
|
|
163
|
+
return (await this.client.request(this.ethChain, "eth_sendTransaction", [txParams]));
|
|
164
|
+
default:
|
|
165
|
+
throw new Error(`Unsupported Ethereum intent: ${intent}`);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
if (provider.chainInfo.namespace === Chain.Solana) {
|
|
169
|
+
const address = getConnectedSolana(session);
|
|
170
|
+
if (!address) {
|
|
171
|
+
throw new Error("no Solana account to sign with");
|
|
172
|
+
}
|
|
173
|
+
switch (intent) {
|
|
174
|
+
case SignIntent.SignMessage: {
|
|
175
|
+
const msgBytes = new TextEncoder().encode(message);
|
|
176
|
+
const msgB58 = bs58.encode(msgBytes);
|
|
177
|
+
const { signature: sigB58 } = await this.client.request(this.solChain, "solana_signMessage", {
|
|
178
|
+
pubkey: address,
|
|
179
|
+
message: msgB58,
|
|
180
|
+
});
|
|
181
|
+
return uint8ArrayToHexString(bs58.decode(sigB58));
|
|
182
|
+
}
|
|
183
|
+
case SignIntent.SignTransaction: {
|
|
184
|
+
const txBytes = uint8ArrayFromHexString(message);
|
|
185
|
+
const txBase64 = stringToBase64urlString(String.fromCharCode(...txBytes));
|
|
186
|
+
const { signature: sigB58 } = await this.client.request(this.solChain, "solana_signTransaction", {
|
|
187
|
+
feePayer: address,
|
|
188
|
+
transaction: txBase64,
|
|
189
|
+
});
|
|
190
|
+
return uint8ArrayToHexString(bs58.decode(sigB58));
|
|
191
|
+
}
|
|
192
|
+
case SignIntent.SignAndSendTransaction: {
|
|
193
|
+
const txBytes = uint8ArrayFromHexString(message);
|
|
194
|
+
const txBase64 = stringToBase64urlString(String.fromCharCode(...txBytes));
|
|
195
|
+
const sigB58 = await this.client.request(this.solChain, "solana_sendTransaction", {
|
|
196
|
+
feePayer: address,
|
|
197
|
+
transaction: txBase64,
|
|
198
|
+
options: { skipPreflight: false },
|
|
199
|
+
});
|
|
200
|
+
return uint8ArrayToHexString(bs58.decode(sigB58));
|
|
201
|
+
}
|
|
202
|
+
default:
|
|
203
|
+
throw new Error(`Unsupported Solana intent: ${intent}`);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
throw new Error("No supported namespace available for signing");
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Retrieves the public key of the connected wallet.
|
|
210
|
+
*
|
|
211
|
+
* @param provider - The WalletProvider to fetch the key from.
|
|
212
|
+
* @returns Compressed public key as a hex string.
|
|
213
|
+
*/
|
|
214
|
+
async getPublicKey(provider) {
|
|
215
|
+
const session = this.client.getSession();
|
|
216
|
+
if (provider.chainInfo.namespace === Chain.Ethereum) {
|
|
217
|
+
const address = getConnectedEthereum(session);
|
|
218
|
+
if (!address) {
|
|
219
|
+
throw new Error("No Ethereum account to retrieve public key");
|
|
220
|
+
}
|
|
221
|
+
const sig = await this.client.request(this.ethChain, "personal_sign", [
|
|
222
|
+
"GET_PUBLIC_KEY",
|
|
223
|
+
address,
|
|
224
|
+
]);
|
|
225
|
+
const rawPublicKey = await recoverPublicKey({
|
|
226
|
+
hash: hashMessage("GET_PUBLIC_KEY"),
|
|
227
|
+
signature: sig,
|
|
228
|
+
});
|
|
229
|
+
const publicKeyHex = rawPublicKey.startsWith("0x")
|
|
230
|
+
? rawPublicKey.slice(2)
|
|
231
|
+
: rawPublicKey;
|
|
232
|
+
const publicKeyBytes = uint8ArrayFromHexString(publicKeyHex);
|
|
233
|
+
const publicKeyBytesCompressed = compressRawPublicKey(publicKeyBytes);
|
|
234
|
+
return uint8ArrayToHexString(publicKeyBytesCompressed);
|
|
235
|
+
}
|
|
236
|
+
if (provider.chainInfo.namespace === Chain.Solana) {
|
|
237
|
+
const address = getConnectedSolana(session);
|
|
238
|
+
if (!address) {
|
|
239
|
+
throw new Error("No Solana account to retrieve public key");
|
|
240
|
+
}
|
|
241
|
+
const publicKeyBytes = bs58.decode(address);
|
|
242
|
+
return uint8ArrayToHexString(publicKeyBytes);
|
|
243
|
+
}
|
|
244
|
+
throw new Error("No supported namespace for public key retrieval");
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Disconnects the current session and re-initiates a fresh pairing URI.
|
|
248
|
+
*/
|
|
249
|
+
async disconnectWalletAccount(_provider) {
|
|
250
|
+
await this.client.disconnect();
|
|
251
|
+
const namespaces = {};
|
|
252
|
+
if (this.ethereumNamespaces.length > 0) {
|
|
253
|
+
namespaces.eip155 = {
|
|
254
|
+
methods: [
|
|
255
|
+
"personal_sign",
|
|
256
|
+
"eth_sendTransaction",
|
|
257
|
+
"eth_chainId",
|
|
258
|
+
"wallet_switchEthereumChain",
|
|
259
|
+
"wallet_addEthereumChain",
|
|
260
|
+
],
|
|
261
|
+
chains: this.ethereumNamespaces,
|
|
262
|
+
events: ["accountsChanged", "chainChanged"],
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
if (this.solanaNamespaces.length > 0) {
|
|
266
|
+
namespaces.solana = {
|
|
267
|
+
methods: [
|
|
268
|
+
"solana_signMessage",
|
|
269
|
+
"solana_signTransaction",
|
|
270
|
+
"solana_sendTransaction",
|
|
271
|
+
],
|
|
272
|
+
chains: this.solanaNamespaces,
|
|
273
|
+
events: ["accountsChanged", "chainChanged"],
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
await this.client.pair(namespaces).then((newUri) => {
|
|
277
|
+
this.uri = newUri;
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Builds a lightweight provider interface for the given chain.
|
|
282
|
+
*
|
|
283
|
+
* @param chainId - The namespace chain ID (e.g., eip155:1)
|
|
284
|
+
* @returns A WalletConnect-compatible provider implementation
|
|
285
|
+
*/
|
|
286
|
+
makeProvider(chainId) {
|
|
287
|
+
return {
|
|
288
|
+
request: ({ method, params }) => {
|
|
289
|
+
return this.client.request(chainId, method, params);
|
|
290
|
+
},
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Ensures there is an active WalletConnect session, initiating approval if necessary.
|
|
295
|
+
*
|
|
296
|
+
* @returns The current WalletConnect session
|
|
297
|
+
* @throws {Error} If approval fails or session is not established
|
|
298
|
+
*/
|
|
299
|
+
async ensureSession() {
|
|
300
|
+
let session = this.client.getSession();
|
|
301
|
+
if (!session) {
|
|
302
|
+
await this.client.approve();
|
|
303
|
+
session = this.client.getSession();
|
|
304
|
+
if (!session)
|
|
305
|
+
throw new Error("WalletConnect: approval failed");
|
|
306
|
+
}
|
|
307
|
+
return session;
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Builds a WalletProvider descriptor for an EVM chain.
|
|
311
|
+
*/
|
|
312
|
+
async buildEthProvider(session, info) {
|
|
313
|
+
const raw = session?.namespaces.eip155?.accounts?.[0] ?? "";
|
|
314
|
+
const address = raw.split(":")[2];
|
|
315
|
+
const chainIdString = this.ethChain.split(":")[1] ?? "1";
|
|
316
|
+
const chainIdDecimal = Number(chainIdString);
|
|
317
|
+
const chainidHex = `0x${chainIdDecimal.toString(16)}`;
|
|
318
|
+
return {
|
|
319
|
+
interfaceType: WalletInterfaceType.WalletConnect,
|
|
320
|
+
chainInfo: {
|
|
321
|
+
namespace: Chain.Ethereum,
|
|
322
|
+
chainId: chainidHex,
|
|
323
|
+
},
|
|
324
|
+
info,
|
|
325
|
+
provider: this.makeProvider(this.ethChain),
|
|
326
|
+
connectedAddresses: address ? [address] : [],
|
|
327
|
+
...(this.uri && { uri: this.uri }),
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Builds a WalletProvider descriptor for Solana.
|
|
332
|
+
*/
|
|
333
|
+
buildSolProvider(session, info) {
|
|
334
|
+
const raw = session?.namespaces.solana?.accounts?.[0] ?? "";
|
|
335
|
+
const address = raw.split(":")[2];
|
|
336
|
+
return {
|
|
337
|
+
interfaceType: WalletInterfaceType.WalletConnect,
|
|
338
|
+
chainInfo: { namespace: Chain.Solana },
|
|
339
|
+
info,
|
|
340
|
+
provider: this.makeProvider(this.solChain),
|
|
341
|
+
connectedAddresses: address ? [address] : [],
|
|
342
|
+
...(this.uri && { uri: this.uri }),
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
function hasConnectedAccounts(session) {
|
|
347
|
+
return (!!session &&
|
|
348
|
+
Object.values(session.namespaces).some((ns) => ns.accounts?.length > 0));
|
|
349
|
+
}
|
|
350
|
+
function getConnectedEthereum(session) {
|
|
351
|
+
const acc = session?.namespaces.eip155?.accounts?.[0];
|
|
352
|
+
return acc ? acc.split(":")[2] : undefined;
|
|
353
|
+
}
|
|
354
|
+
function getConnectedSolana(session) {
|
|
355
|
+
const acc = session?.namespaces.solana?.accounts?.[0];
|
|
356
|
+
return acc ? acc.split(":")[2] : undefined;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
export { WalletConnectWallet };
|
|
360
|
+
//# sourceMappingURL=base.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base.mjs","sources":["../../../src/__wallet__/wallet-connect/base.ts"],"sourcesContent":[null],"names":[],"mappings":";;;;;;;MAsBa,mBAAmB,CAAA;AAW9B,IAAA,WAAA,CAAoB,MAA2B,EAAA;QAA3B,IAAM,CAAA,MAAA,GAAN,MAAM;AAVjB,QAAA,IAAA,CAAA,aAAa,GAAG,mBAAmB,CAAC,aAAa;QAElD,IAAkB,CAAA,kBAAA,GAAa,EAAE;QACjC,IAAgB,CAAA,gBAAA,GAAa,EAAE;QAQrC,IAAI,CAAC,MAAM,CAAC,eAAe,CAAC,MAAK,GAAG,CAAC;;AAGvC;;;;;;;AAOG;IACH,MAAM,IAAI,CAAC,IAGV,EAAA;AACC,QAAA,IAAI,CAAC,kBAAkB,GAAG,IAAI,CAAC,kBAAkB;QACjD,IAAI,IAAI,CAAC,kBAAkB,CAAC,MAAM,GAAG,CAAC,EAAE;YACtC,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,kBAAkB,CAAC,CAAC,CAAE;;AAG7C,QAAA,IAAI,CAAC,gBAAgB,GAAG,IAAI,CAAC,gBAAgB;QAC7C,IAAI,IAAI,CAAC,gBAAgB,CAAC,MAAM,GAAG,CAAC,EAAE;YACpC,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,gBAAgB,CAAC,CAAC,CAAE;;AAG3C,QAAA,IACE,IAAI,CAAC,kBAAkB,CAAC,MAAM,KAAK,CAAC;AACpC,YAAA,IAAI,CAAC,gBAAgB,CAAC,MAAM,KAAK,CAAC,EAClC;AACA,YAAA,MAAM,IAAI,KAAK,CACb,0DAA0D,CAC3D;;QAGH,MAAM,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,UAAU,EAAE;AACxC,QAAA,IAAI,oBAAoB,CAAC,OAAO,CAAC,EAAE;YACjC;;QAGF,MAAM,UAAU,GAAwB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,GAAG,SAAS;AAC5C;;;;"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { CoreTypes, ProposalTypes, SessionTypes } from "@walletconnect/types";
|
|
2
|
+
export declare class WalletConnectClient {
|
|
3
|
+
private client;
|
|
4
|
+
private pendingApproval;
|
|
5
|
+
private sessionDeleteHandlers;
|
|
6
|
+
/**
|
|
7
|
+
* Registers a callback that runs when the WalletConnect session is deleted.
|
|
8
|
+
*
|
|
9
|
+
* - Useful for clearing UI state or internal session data.
|
|
10
|
+
*
|
|
11
|
+
* @param fn - A callback function to invoke when the session is deleted.
|
|
12
|
+
*/
|
|
13
|
+
onSessionDelete(fn: () => void): void;
|
|
14
|
+
/**
|
|
15
|
+
* Initializes the WalletConnect SignClient with your project credentials.
|
|
16
|
+
*
|
|
17
|
+
* - Must be called before calling `pair()` or `request()`.
|
|
18
|
+
* - Configures metadata and optionally a custom relay server.
|
|
19
|
+
*
|
|
20
|
+
* @param opts.projectId - WalletConnect project ID.
|
|
21
|
+
* @param opts.metadata - Metadata about your app (name, URL, icons).
|
|
22
|
+
* @param opts.relayUrl - (Optional) Custom relay server URL.
|
|
23
|
+
* @returns A promise that resolves once the client is initialized.
|
|
24
|
+
*/
|
|
25
|
+
init(opts: {
|
|
26
|
+
projectId: string;
|
|
27
|
+
appMetadata: CoreTypes.Metadata;
|
|
28
|
+
relayUrl?: string;
|
|
29
|
+
}): Promise<void>;
|
|
30
|
+
/**
|
|
31
|
+
* Initiates a pairing request and returns a URI to be scanned or deep-linked.
|
|
32
|
+
*
|
|
33
|
+
* - Must be followed by a call to `approve()` to complete the pairing.
|
|
34
|
+
* - Throws if a pairing is already in progress.
|
|
35
|
+
*
|
|
36
|
+
* @param namespaces - Namespaces to request for optional capabilities.
|
|
37
|
+
* @returns A WalletConnect URI for the wallet to connect with.
|
|
38
|
+
* @throws {Error} If a pairing is already in progress or URI is not returned.
|
|
39
|
+
*/
|
|
40
|
+
pair(namespaces: ProposalTypes.OptionalNamespaces): Promise<string>;
|
|
41
|
+
/**
|
|
42
|
+
* Completes the pairing approval process after the wallet approves the request.
|
|
43
|
+
*
|
|
44
|
+
* - Must be called after `pair()` and after the wallet has scanned and approved the URI.
|
|
45
|
+
*
|
|
46
|
+
* @returns A promise that resolves to the established session.
|
|
47
|
+
* @throws {Error} If called before `pair()` or approval fails.
|
|
48
|
+
*/
|
|
49
|
+
approve(): Promise<SessionTypes.Struct>;
|
|
50
|
+
/**
|
|
51
|
+
* Retrieves the most recent active WalletConnect session.
|
|
52
|
+
*
|
|
53
|
+
* @returns The most recent session, or `null` if none are active.
|
|
54
|
+
*/
|
|
55
|
+
getSession(): SessionTypes.Struct | null;
|
|
56
|
+
/**
|
|
57
|
+
* Sends a JSON-RPC request over the active WalletConnect session.
|
|
58
|
+
*
|
|
59
|
+
* - The session must already be connected.
|
|
60
|
+
*
|
|
61
|
+
* @param chainId - Chain ID of the target blockchain (e.g. `eip155:1`).
|
|
62
|
+
* @param method - The RPC method name to call.
|
|
63
|
+
* @param params - The parameters to pass into the RPC method.
|
|
64
|
+
* @returns A promise that resolves with the RPC response.
|
|
65
|
+
* @throws {Error} If no active session is found.
|
|
66
|
+
*/
|
|
67
|
+
request(chainId: string, method: string, params: any[] | Record<string, any>): Promise<any>;
|
|
68
|
+
/**
|
|
69
|
+
* Disconnects the active session, if one exists.
|
|
70
|
+
*
|
|
71
|
+
* - Sends a disconnect signal to the wallet.
|
|
72
|
+
* - Does nothing if no session is currently active.
|
|
73
|
+
*
|
|
74
|
+
* @returns A promise that resolves once disconnection is complete.
|
|
75
|
+
*/
|
|
76
|
+
disconnect(): Promise<void>;
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../../src/__wallet__/wallet-connect/client.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,SAAS,EACT,aAAa,EACb,YAAY,EACb,MAAM,sBAAsB,CAAC;AAE9B,qBAAa,mBAAmB;IAC9B,OAAO,CAAC,MAAM,CAAc;IAG5B,OAAO,CAAC,eAAe,CAAqD;IAG5E,OAAO,CAAC,qBAAqB,CAAyB;IAEtD;;;;;;OAMG;IACI,eAAe,CAAC,EAAE,EAAE,MAAM,IAAI;IAIrC;;;;;;;;;;OAUG;IACG,IAAI,CAAC,IAAI,EAAE;QACf,SAAS,EAAE,MAAM,CAAC;QAClB,WAAW,EAAE,SAAS,CAAC,QAAQ,CAAC;QAChC,QAAQ,CAAC,EAAE,MAAM,CAAC;KACnB,GAAG,OAAO,CAAC,IAAI,CAAC;IAajB;;;;;;;;;OASG;IACG,IAAI,CAAC,UAAU,EAAE,aAAa,CAAC,kBAAkB,GAAG,OAAO,CAAC,MAAM,CAAC;IAiBzE;;;;;;;OAOG;IACG,OAAO,IAAI,OAAO,CAAC,YAAY,CAAC,MAAM,CAAC;IAc7C;;;;OAIG;IACH,UAAU,IAAI,YAAY,CAAC,MAAM,GAAG,IAAI;IAKxC;;;;;;;;;;OAUG;IACG,OAAO,CACX,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,GAAG,EAAE,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAClC,OAAO,CAAC,GAAG,CAAC;IAaf;;;;;;;OAOG;IACG,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;CASlC"}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var SignClient = require('@walletconnect/sign-client');
|
|
4
|
+
|
|
5
|
+
class WalletConnectClient {
|
|
6
|
+
constructor() {
|
|
7
|
+
// tracks the pending approval callback returned from `connect()`
|
|
8
|
+
this.pendingApproval = null;
|
|
9
|
+
// callbacks to run when the session is deleted
|
|
10
|
+
this.sessionDeleteHandlers = [];
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Registers a callback that runs when the WalletConnect session is deleted.
|
|
14
|
+
*
|
|
15
|
+
* - Useful for clearing UI state or internal session data.
|
|
16
|
+
*
|
|
17
|
+
* @param fn - A callback function to invoke when the session is deleted.
|
|
18
|
+
*/
|
|
19
|
+
onSessionDelete(fn) {
|
|
20
|
+
this.sessionDeleteHandlers.push(fn);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Initializes the WalletConnect SignClient with your project credentials.
|
|
24
|
+
*
|
|
25
|
+
* - Must be called before calling `pair()` or `request()`.
|
|
26
|
+
* - Configures metadata and optionally a custom relay server.
|
|
27
|
+
*
|
|
28
|
+
* @param opts.projectId - WalletConnect project ID.
|
|
29
|
+
* @param opts.metadata - Metadata about your app (name, URL, icons).
|
|
30
|
+
* @param opts.relayUrl - (Optional) Custom relay server URL.
|
|
31
|
+
* @returns A promise that resolves once the client is initialized.
|
|
32
|
+
*/
|
|
33
|
+
async init(opts) {
|
|
34
|
+
this.client = await SignClient.init({
|
|
35
|
+
projectId: opts.projectId,
|
|
36
|
+
metadata: opts.appMetadata,
|
|
37
|
+
...(opts.relayUrl ? { relayUrl: opts.relayUrl } : {}),
|
|
38
|
+
});
|
|
39
|
+
// we listen for session deletion events and notify subscribers
|
|
40
|
+
this.client.on("session_delete", () => {
|
|
41
|
+
this.sessionDeleteHandlers.forEach((h) => h());
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Initiates a pairing request and returns a URI to be scanned or deep-linked.
|
|
46
|
+
*
|
|
47
|
+
* - Must be followed by a call to `approve()` to complete the pairing.
|
|
48
|
+
* - Throws if a pairing is already in progress.
|
|
49
|
+
*
|
|
50
|
+
* @param namespaces - Namespaces to request for optional capabilities.
|
|
51
|
+
* @returns A WalletConnect URI for the wallet to connect with.
|
|
52
|
+
* @throws {Error} If a pairing is already in progress or URI is not returned.
|
|
53
|
+
*/
|
|
54
|
+
async pair(namespaces) {
|
|
55
|
+
if (this.pendingApproval) {
|
|
56
|
+
throw new Error("WalletConnect: Pairing already in progress");
|
|
57
|
+
}
|
|
58
|
+
const { uri, approval } = await this.client.connect({
|
|
59
|
+
optionalNamespaces: namespaces,
|
|
60
|
+
});
|
|
61
|
+
if (!uri) {
|
|
62
|
+
throw new Error("WalletConnect: no URI returned");
|
|
63
|
+
}
|
|
64
|
+
this.pendingApproval = approval;
|
|
65
|
+
return uri;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Completes the pairing approval process after the wallet approves the request.
|
|
69
|
+
*
|
|
70
|
+
* - Must be called after `pair()` and after the wallet has scanned and approved the URI.
|
|
71
|
+
*
|
|
72
|
+
* @returns A promise that resolves to the established session.
|
|
73
|
+
* @throws {Error} If called before `pair()` or approval fails.
|
|
74
|
+
*/
|
|
75
|
+
async approve() {
|
|
76
|
+
if (!this.pendingApproval) {
|
|
77
|
+
throw new Error("WalletConnect: call pair() before approve()");
|
|
78
|
+
}
|
|
79
|
+
try {
|
|
80
|
+
const session = await this.pendingApproval();
|
|
81
|
+
return session;
|
|
82
|
+
}
|
|
83
|
+
finally {
|
|
84
|
+
// we clear the pending state regardless of outcome
|
|
85
|
+
this.pendingApproval = null;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Retrieves the most recent active WalletConnect session.
|
|
90
|
+
*
|
|
91
|
+
* @returns The most recent session, or `null` if none are active.
|
|
92
|
+
*/
|
|
93
|
+
getSession() {
|
|
94
|
+
const sessions = this.client.session.getAll();
|
|
95
|
+
return sessions.length ? sessions[sessions.length - 1] : null;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Sends a JSON-RPC request over the active WalletConnect session.
|
|
99
|
+
*
|
|
100
|
+
* - The session must already be connected.
|
|
101
|
+
*
|
|
102
|
+
* @param chainId - Chain ID of the target blockchain (e.g. `eip155:1`).
|
|
103
|
+
* @param method - The RPC method name to call.
|
|
104
|
+
* @param params - The parameters to pass into the RPC method.
|
|
105
|
+
* @returns A promise that resolves with the RPC response.
|
|
106
|
+
* @throws {Error} If no active session is found.
|
|
107
|
+
*/
|
|
108
|
+
async request(chainId, method, params) {
|
|
109
|
+
const session = this.getSession();
|
|
110
|
+
if (!session) {
|
|
111
|
+
throw new Error("WalletConnect: no active session");
|
|
112
|
+
}
|
|
113
|
+
return this.client.request({
|
|
114
|
+
topic: session.topic,
|
|
115
|
+
chainId,
|
|
116
|
+
request: { method, params },
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Disconnects the active session, if one exists.
|
|
121
|
+
*
|
|
122
|
+
* - Sends a disconnect signal to the wallet.
|
|
123
|
+
* - Does nothing if no session is currently active.
|
|
124
|
+
*
|
|
125
|
+
* @returns A promise that resolves once disconnection is complete.
|
|
126
|
+
*/
|
|
127
|
+
async disconnect() {
|
|
128
|
+
const session = this.getSession();
|
|
129
|
+
if (!session)
|
|
130
|
+
return;
|
|
131
|
+
await this.client.disconnect({
|
|
132
|
+
topic: session.topic,
|
|
133
|
+
reason: { code: 6000, message: "User disconnected" },
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
exports.WalletConnectClient = WalletConnectClient;
|
|
139
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sources":["../../../src/__wallet__/wallet-connect/client.ts"],"sourcesContent":[null],"names":[],"mappings":";;;;MAOa,mBAAmB,CAAA;AAAhC,IAAA,WAAA,GAAA;;QAIU,IAAe,CAAA,eAAA,GAAgD,IAAI;;QAGnE,IAAqB,CAAA,qBAAA,GAAsB,EAAE;;AAErD;;;;;;AAMG;AACI,IAAA,eAAe,CAAC,EAAc,EAAA;AACnC,QAAA,IAAI,CAAC,qBAAqB,CAAC,IAAI,CAAC,EAAE,CAAC;;AAGrC;;;;;;;;;;AAUG;IACH,MAAM,IAAI,CAAC,IAIV,EAAA;AACC,QAAA,IAAI,CAAC,MAAM,GAAG,MAAM,UAAU,CAAC,IA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