@vue-solana/core 0.4.1 → 0.5.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/README.md +12 -2
- package/dist/index.cjs +19 -17
- package/dist/index.d.cts +4 -4
- package/dist/index.d.mts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.mjs +4 -4
- package/dist/ios-wallet.cjs +12 -538
- package/dist/ios-wallet.d.cts +27 -8
- package/dist/ios-wallet.d.mts +27 -8
- package/dist/ios-wallet.d.ts +27 -8
- package/dist/ios-wallet.mjs +4 -517
- package/dist/rpc.cjs +1 -4
- package/dist/rpc.mjs +1 -4
- package/dist/shared/core.8V8PLE-S.cjs +237 -0
- package/dist/shared/core.D7olTUN_.mjs +225 -0
- package/dist/shared/core.DJZldB_x.cjs +612 -0
- package/dist/shared/core.DSI7vIfT.mjs +586 -0
- package/dist/transaction.cjs +35 -0
- package/dist/transaction.d.cts +3 -2
- package/dist/transaction.d.mts +3 -2
- package/dist/transaction.d.ts +3 -2
- package/dist/transaction.mjs +35 -1
- package/dist/types.d.cts +11 -2
- package/dist/types.d.mts +11 -2
- package/dist/types.d.ts +11 -2
- package/dist/wallet-standard.cjs +13 -219
- package/dist/wallet-standard.d.cts +6 -3
- package/dist/wallet-standard.d.mts +6 -3
- package/dist/wallet-standard.d.ts +6 -3
- package/dist/wallet-standard.mjs +6 -213
- package/package.json +1 -1
|
@@ -0,0 +1,612 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const web3Compat = require('@solana/web3-compat');
|
|
4
|
+
const bs58 = require('bs58');
|
|
5
|
+
const naclModule = require('tweetnacl');
|
|
6
|
+
|
|
7
|
+
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
8
|
+
|
|
9
|
+
function _interopNamespaceCompat(e) {
|
|
10
|
+
if (e && typeof e === 'object' && 'default' in e) return e;
|
|
11
|
+
const n = Object.create(null);
|
|
12
|
+
if (e) {
|
|
13
|
+
for (const k in e) {
|
|
14
|
+
n[k] = e[k];
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
n.default = e;
|
|
18
|
+
return n;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const bs58__default = /*#__PURE__*/_interopDefaultCompat(bs58);
|
|
22
|
+
const naclModule__namespace = /*#__PURE__*/_interopNamespaceCompat(naclModule);
|
|
23
|
+
|
|
24
|
+
const DEFAULT_IOS_WALLET_CHAINS = [
|
|
25
|
+
"solana:mainnet",
|
|
26
|
+
"solana:devnet"
|
|
27
|
+
];
|
|
28
|
+
const CALLBACK_PARAMS = /* @__PURE__ */ new Set([
|
|
29
|
+
"data",
|
|
30
|
+
"nonce",
|
|
31
|
+
"errorCode",
|
|
32
|
+
"errorMessage",
|
|
33
|
+
"phantom_encryption_public_key",
|
|
34
|
+
"solflare_encryption_public_key",
|
|
35
|
+
"wallet_encryption_public_key"
|
|
36
|
+
]);
|
|
37
|
+
function isSolanaIosBrowserWalletSupported() {
|
|
38
|
+
return isIosBrowser();
|
|
39
|
+
}
|
|
40
|
+
function isIosBrowser() {
|
|
41
|
+
if (typeof window === "undefined" || typeof navigator === "undefined") {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
const userAgentDataPlatform = getNavigatorUserAgentDataPlatform();
|
|
45
|
+
if (isIosPlatform(userAgentDataPlatform)) {
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
const userAgent = navigator.userAgent;
|
|
49
|
+
const platform = getNavigatorPlatform();
|
|
50
|
+
const maxTouchPoints = navigator.maxTouchPoints ?? 0;
|
|
51
|
+
return /iPad|iPhone|iPod/i.test(userAgent) || platform === "MacIntel" && maxTouchPoints > 1;
|
|
52
|
+
}
|
|
53
|
+
function getDefaultIosWalletAppIdentity() {
|
|
54
|
+
if (typeof window === "undefined") {
|
|
55
|
+
return {
|
|
56
|
+
name: "Vue Solana App",
|
|
57
|
+
uri: "https://localhost"
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
name: document.title || "Vue Solana App",
|
|
62
|
+
uri: window.location.origin,
|
|
63
|
+
icon: getDocumentIconUrl()
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
function getDefaultIosWalletRedirectUrl() {
|
|
67
|
+
if (typeof window === "undefined") {
|
|
68
|
+
return "https://localhost";
|
|
69
|
+
}
|
|
70
|
+
const url = new URL(window.location.href);
|
|
71
|
+
for (const param of CALLBACK_PARAMS) {
|
|
72
|
+
url.searchParams.delete(param);
|
|
73
|
+
}
|
|
74
|
+
return url.toString();
|
|
75
|
+
}
|
|
76
|
+
function openIosWalletUrl(url) {
|
|
77
|
+
if (typeof window === "undefined") {
|
|
78
|
+
throw new Error("iOS wallet links are only available in browser runtimes");
|
|
79
|
+
}
|
|
80
|
+
window.location.assign(url);
|
|
81
|
+
}
|
|
82
|
+
function waitForRedirect() {
|
|
83
|
+
return new Promise(() => {
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
function hasIosWalletCallbackParams(url) {
|
|
87
|
+
for (const param of CALLBACK_PARAMS) {
|
|
88
|
+
if (url.searchParams.has(param)) {
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
function cleanCallbackUrl(url, clearUrl) {
|
|
95
|
+
if (!clearUrl || typeof window === "undefined") {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
for (const param of CALLBACK_PARAMS) {
|
|
99
|
+
url.searchParams.delete(param);
|
|
100
|
+
}
|
|
101
|
+
window.history.replaceState(window.history.state, document.title, url.toString());
|
|
102
|
+
}
|
|
103
|
+
function getDocumentIconUrl() {
|
|
104
|
+
const icon = document.querySelector(
|
|
105
|
+
'link[rel="icon"], link[rel="shortcut icon"], link[rel="apple-touch-icon"]'
|
|
106
|
+
);
|
|
107
|
+
return icon?.href;
|
|
108
|
+
}
|
|
109
|
+
function getNavigatorUserAgentDataPlatform() {
|
|
110
|
+
const userAgentData = Reflect.get(navigator, "userAgentData");
|
|
111
|
+
if (!isObjectRecord(userAgentData)) {
|
|
112
|
+
return void 0;
|
|
113
|
+
}
|
|
114
|
+
const platform = Reflect.get(userAgentData, "platform");
|
|
115
|
+
return typeof platform === "string" ? platform : void 0;
|
|
116
|
+
}
|
|
117
|
+
function getNavigatorPlatform() {
|
|
118
|
+
const platform = Reflect.get(navigator, "platform");
|
|
119
|
+
return typeof platform === "string" ? platform : "";
|
|
120
|
+
}
|
|
121
|
+
function isIosPlatform(platform) {
|
|
122
|
+
return platform?.toLowerCase() === "ios";
|
|
123
|
+
}
|
|
124
|
+
function isObjectRecord(value) {
|
|
125
|
+
return typeof value === "object" && value !== null;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const nacl = resolveTweetNaCl();
|
|
129
|
+
function createRequestId() {
|
|
130
|
+
return bs58__default.encode(nacl.randomBytes(16));
|
|
131
|
+
}
|
|
132
|
+
function getSharedSecret(walletEncryptionPublicKey, dappSecretKey) {
|
|
133
|
+
if (!walletEncryptionPublicKey) {
|
|
134
|
+
throw new Error("Missing iOS wallet encryption public key");
|
|
135
|
+
}
|
|
136
|
+
return nacl.box.before(bs58__default.decode(walletEncryptionPublicKey), bs58__default.decode(dappSecretKey));
|
|
137
|
+
}
|
|
138
|
+
function encryptPayload(payload, nonce, sharedSecret) {
|
|
139
|
+
return bs58__default.encode(nacl.box.after(encodeJson(payload), nonce, sharedSecret));
|
|
140
|
+
}
|
|
141
|
+
function decryptPayload(data, nonce, sharedSecret) {
|
|
142
|
+
const decrypted = nacl.box.open.after(bs58__default.decode(data), bs58__default.decode(nonce), sharedSecret);
|
|
143
|
+
if (!decrypted) {
|
|
144
|
+
throw new Error("Unable to decrypt iOS wallet callback");
|
|
145
|
+
}
|
|
146
|
+
return JSON.parse(new TextDecoder().decode(decrypted));
|
|
147
|
+
}
|
|
148
|
+
function resolveTweetNaCl() {
|
|
149
|
+
const moduleDefault = naclModule__namespace.default;
|
|
150
|
+
const globalNacl = globalThis.nacl;
|
|
151
|
+
return "box" in naclModule__namespace ? naclModule__namespace : moduleDefault ?? globalNacl;
|
|
152
|
+
}
|
|
153
|
+
function encodeJson(value) {
|
|
154
|
+
return new TextEncoder().encode(JSON.stringify(value));
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const IOS_WALLETS = [
|
|
158
|
+
{
|
|
159
|
+
id: "phantom",
|
|
160
|
+
name: "Phantom",
|
|
161
|
+
icon: "https://phantom.app/img/phantom-logo.svg",
|
|
162
|
+
appUrl: "https://phantom.app",
|
|
163
|
+
installUrl: "https://phantom.app/download",
|
|
164
|
+
encryptionPublicKeyParam: "phantom_encryption_public_key",
|
|
165
|
+
connectUrl: "https://phantom.app/ul/v1/connect",
|
|
166
|
+
signTransactionUrl: "https://phantom.app/ul/v1/signTransaction",
|
|
167
|
+
signAllTransactionsUrl: "https://phantom.app/ul/v1/signAllTransactions"
|
|
168
|
+
},
|
|
169
|
+
{
|
|
170
|
+
id: "solflare",
|
|
171
|
+
name: "Solflare",
|
|
172
|
+
icon: "https://solflare.com/favicon.ico",
|
|
173
|
+
appUrl: "https://solflare.com",
|
|
174
|
+
installUrl: "https://solflare.com/download",
|
|
175
|
+
encryptionPublicKeyParam: "solflare_encryption_public_key",
|
|
176
|
+
connectUrl: "https://solflare.com/ul/v1/connect",
|
|
177
|
+
signTransactionUrl: "https://solflare.com/ul/v1/signTransaction",
|
|
178
|
+
signAllTransactionsUrl: "https://solflare.com/ul/v1/signAllTransactions",
|
|
179
|
+
signAndSendTransactionUrl: "https://solflare.com/ul/v1/signAndSendTransaction"
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
id: "backpack",
|
|
183
|
+
name: "Backpack",
|
|
184
|
+
icon: "https://backpack.app/favicon.ico",
|
|
185
|
+
appUrl: "https://backpack.app",
|
|
186
|
+
installUrl: "https://backpack.app/download",
|
|
187
|
+
encryptionPublicKeyParam: "wallet_encryption_public_key",
|
|
188
|
+
connectUrl: "https://backpack.app/ul/v1/connect",
|
|
189
|
+
signTransactionUrl: "https://backpack.app/ul/v1/signTransaction",
|
|
190
|
+
signAllTransactionsUrl: "https://backpack.app/ul/v1/signAllTransactions",
|
|
191
|
+
signAndSendTransactionUrl: "https://backpack.app/ul/v1/signAndSendTransaction"
|
|
192
|
+
}
|
|
193
|
+
];
|
|
194
|
+
function getIosWalletDefinitionById(walletId) {
|
|
195
|
+
const definition = IOS_WALLETS.find((wallet) => wallet.id === walletId);
|
|
196
|
+
if (!definition) {
|
|
197
|
+
throw new Error(`Unknown iOS wallet: ${walletId}`);
|
|
198
|
+
}
|
|
199
|
+
return definition;
|
|
200
|
+
}
|
|
201
|
+
function isIosWalletDefinition(value) {
|
|
202
|
+
return typeof value === "object" && value !== null && "id" in value && typeof value.id === "string" && IOS_WALLETS.some((wallet) => wallet.id === value.id);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const PENDING_REQUEST_KEY = "vue-solana:ios-wallet:pending";
|
|
206
|
+
const PENDING_REQUEST_TTL_MS = 10 * 60 * 1e3;
|
|
207
|
+
const SESSION_PREFIX = "vue-solana:ios-wallet:session:";
|
|
208
|
+
function createPendingRequest(walletId, method, keyPair, redirectUrl, requestedTransactionCount) {
|
|
209
|
+
return {
|
|
210
|
+
id: createRequestId(),
|
|
211
|
+
walletId,
|
|
212
|
+
method,
|
|
213
|
+
dappEncryptionPublicKey: bs58__default.encode(keyPair.publicKey),
|
|
214
|
+
dappEncryptionSecretKey: bs58__default.encode(keyPair.secretKey),
|
|
215
|
+
redirectUrl: redirectUrl ?? getDefaultIosWalletRedirectUrl(),
|
|
216
|
+
createdAt: Date.now(),
|
|
217
|
+
requestedTransactionCount
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
function getStoredIosWalletAccount(walletId, chains) {
|
|
221
|
+
const session = getStoredSession(walletId);
|
|
222
|
+
if (!session) {
|
|
223
|
+
return [];
|
|
224
|
+
}
|
|
225
|
+
return [
|
|
226
|
+
{
|
|
227
|
+
address: session.publicKey,
|
|
228
|
+
publicKey: new web3Compat.PublicKey(session.publicKey).toBytes(),
|
|
229
|
+
chains
|
|
230
|
+
}
|
|
231
|
+
];
|
|
232
|
+
}
|
|
233
|
+
function getStoredSession(walletId) {
|
|
234
|
+
const value = getStorage()?.getItem(`${SESSION_PREFIX}${walletId}`);
|
|
235
|
+
if (!value) {
|
|
236
|
+
return null;
|
|
237
|
+
}
|
|
238
|
+
try {
|
|
239
|
+
const session = JSON.parse(value);
|
|
240
|
+
new web3Compat.PublicKey(session.publicKey);
|
|
241
|
+
return session;
|
|
242
|
+
} catch {
|
|
243
|
+
removeStoredSession(walletId);
|
|
244
|
+
return null;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
function storeSession(session) {
|
|
248
|
+
getStorage()?.setItem(`${SESSION_PREFIX}${session.walletId}`, JSON.stringify(session));
|
|
249
|
+
}
|
|
250
|
+
function removeStoredSession(walletId) {
|
|
251
|
+
getStorage()?.removeItem(`${SESSION_PREFIX}${walletId}`);
|
|
252
|
+
}
|
|
253
|
+
function getPendingRequest() {
|
|
254
|
+
const value = getStorage()?.getItem(PENDING_REQUEST_KEY);
|
|
255
|
+
if (!value) {
|
|
256
|
+
return null;
|
|
257
|
+
}
|
|
258
|
+
try {
|
|
259
|
+
return JSON.parse(value);
|
|
260
|
+
} catch {
|
|
261
|
+
clearPendingRequest();
|
|
262
|
+
return null;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
function storePendingRequest(request) {
|
|
266
|
+
getStorage()?.setItem(PENDING_REQUEST_KEY, JSON.stringify(request));
|
|
267
|
+
}
|
|
268
|
+
function clearPendingRequest() {
|
|
269
|
+
getStorage()?.removeItem(PENDING_REQUEST_KEY);
|
|
270
|
+
}
|
|
271
|
+
function isPendingRequestExpired(request) {
|
|
272
|
+
return Date.now() - request.createdAt > PENDING_REQUEST_TTL_MS;
|
|
273
|
+
}
|
|
274
|
+
function getStorage() {
|
|
275
|
+
if (typeof window === "undefined") {
|
|
276
|
+
return null;
|
|
277
|
+
}
|
|
278
|
+
try {
|
|
279
|
+
return window.sessionStorage;
|
|
280
|
+
} catch {
|
|
281
|
+
return null;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function handleSolanaIosWalletCallback(options = {}) {
|
|
286
|
+
if (typeof window === "undefined") {
|
|
287
|
+
return null;
|
|
288
|
+
}
|
|
289
|
+
const url = new URL(window.location.href);
|
|
290
|
+
if (!hasIosWalletCallbackParams(url)) {
|
|
291
|
+
return null;
|
|
292
|
+
}
|
|
293
|
+
const errorCode = url.searchParams.get("errorCode");
|
|
294
|
+
const errorMessage = url.searchParams.get("errorMessage");
|
|
295
|
+
if (errorCode || errorMessage) {
|
|
296
|
+
clearPendingRequest();
|
|
297
|
+
cleanCallbackUrl(url, options.clearUrl);
|
|
298
|
+
throw new Error(errorMessage || `iOS wallet request failed: ${errorCode}`);
|
|
299
|
+
}
|
|
300
|
+
const pending = getPendingRequest();
|
|
301
|
+
if (!pending) {
|
|
302
|
+
cleanCallbackUrl(url, options.clearUrl);
|
|
303
|
+
throw new Error("Received an iOS wallet callback without a pending request");
|
|
304
|
+
}
|
|
305
|
+
if (isPendingRequestExpired(pending)) {
|
|
306
|
+
clearPendingRequest();
|
|
307
|
+
cleanCallbackUrl(url, options.clearUrl);
|
|
308
|
+
throw new Error("Received an expired iOS wallet callback");
|
|
309
|
+
}
|
|
310
|
+
const definition = getIosWalletDefinitionById(pending.walletId);
|
|
311
|
+
const walletEncryptionPublicKey = url.searchParams.get(definition.encryptionPublicKeyParam);
|
|
312
|
+
const nonce = url.searchParams.get("nonce");
|
|
313
|
+
const data = url.searchParams.get("data");
|
|
314
|
+
if (!nonce || !data) {
|
|
315
|
+
clearPendingRequest();
|
|
316
|
+
cleanCallbackUrl(url, options.clearUrl);
|
|
317
|
+
throw new Error("Received an incomplete iOS wallet callback");
|
|
318
|
+
}
|
|
319
|
+
try {
|
|
320
|
+
const sharedSecret = getSharedSecret(
|
|
321
|
+
walletEncryptionPublicKey ?? getStoredSession(pending.walletId)?.walletEncryptionPublicKey,
|
|
322
|
+
pending.dappEncryptionSecretKey
|
|
323
|
+
);
|
|
324
|
+
const payload = decryptPayload(data, nonce, sharedSecret);
|
|
325
|
+
const result = createCallbackResult(pending, payload);
|
|
326
|
+
if (pending.method === "connect") {
|
|
327
|
+
if (!walletEncryptionPublicKey) {
|
|
328
|
+
throw new Error("iOS wallet connect callback did not include an encryption public key");
|
|
329
|
+
}
|
|
330
|
+
const publicKey = getPublicKeyField(payload, "public_key");
|
|
331
|
+
storeSession({
|
|
332
|
+
walletId: pending.walletId,
|
|
333
|
+
publicKey,
|
|
334
|
+
session: getStringField(payload, "session"),
|
|
335
|
+
dappEncryptionPublicKey: pending.dappEncryptionPublicKey,
|
|
336
|
+
dappEncryptionSecretKey: pending.dappEncryptionSecretKey,
|
|
337
|
+
walletEncryptionPublicKey,
|
|
338
|
+
sharedSecret: bs58__default.encode(sharedSecret)
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
return result;
|
|
342
|
+
} finally {
|
|
343
|
+
clearPendingRequest();
|
|
344
|
+
cleanCallbackUrl(url, options.clearUrl);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
function createCallbackResult(pending, payload) {
|
|
348
|
+
if (pending.method === "connect") {
|
|
349
|
+
return {
|
|
350
|
+
walletId: pending.walletId,
|
|
351
|
+
method: pending.method,
|
|
352
|
+
publicKey: getPublicKeyField(payload, "public_key")
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
if (pending.method === "signTransaction") {
|
|
356
|
+
return {
|
|
357
|
+
walletId: pending.walletId,
|
|
358
|
+
method: pending.method,
|
|
359
|
+
transaction: bs58__default.decode(getStringField(payload, "transaction"))
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
if (pending.method === "signAllTransactions") {
|
|
363
|
+
const transactions = payload.transactions;
|
|
364
|
+
if (!Array.isArray(transactions) || transactions.some((transaction) => typeof transaction !== "string")) {
|
|
365
|
+
throw new Error("iOS wallet returned invalid signed transactions");
|
|
366
|
+
}
|
|
367
|
+
if (typeof pending.requestedTransactionCount === "number" && transactions.length !== pending.requestedTransactionCount) {
|
|
368
|
+
throw new Error(
|
|
369
|
+
`iOS wallet returned ${transactions.length} signed transactions for ${pending.requestedTransactionCount} requested transactions`
|
|
370
|
+
);
|
|
371
|
+
}
|
|
372
|
+
return {
|
|
373
|
+
walletId: pending.walletId,
|
|
374
|
+
method: pending.method,
|
|
375
|
+
transactions: transactions.map((transaction) => bs58__default.decode(transaction))
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
return {
|
|
379
|
+
walletId: pending.walletId,
|
|
380
|
+
method: pending.method,
|
|
381
|
+
signature: getStringField(payload, "signature")
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
function getStringField(payload, field) {
|
|
385
|
+
const value = payload[field];
|
|
386
|
+
if (typeof value !== "string" || !value) {
|
|
387
|
+
throw new Error(`iOS wallet callback is missing ${field}`);
|
|
388
|
+
}
|
|
389
|
+
return value;
|
|
390
|
+
}
|
|
391
|
+
function getPublicKeyField(payload, field) {
|
|
392
|
+
const value = getStringField(payload, field);
|
|
393
|
+
try {
|
|
394
|
+
new web3Compat.PublicKey(value);
|
|
395
|
+
} catch {
|
|
396
|
+
throw new Error(`iOS wallet callback returned an invalid ${field}`);
|
|
397
|
+
}
|
|
398
|
+
return value;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
function serializeTransaction(transaction) {
|
|
402
|
+
if (transaction instanceof web3Compat.Transaction) {
|
|
403
|
+
return transaction.serialize({ requireAllSignatures: false, verifySignatures: false });
|
|
404
|
+
}
|
|
405
|
+
return transaction.serialize();
|
|
406
|
+
}
|
|
407
|
+
function deserializeTransaction(source, bytes) {
|
|
408
|
+
if (source instanceof web3Compat.Transaction) {
|
|
409
|
+
return web3Compat.Transaction.from(bytes);
|
|
410
|
+
}
|
|
411
|
+
return web3Compat.VersionedTransaction.deserialize(bytes);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
function getSolanaIosWallets(options = {}) {
|
|
415
|
+
if (!isSolanaIosBrowserWalletSupported()) {
|
|
416
|
+
return [];
|
|
417
|
+
}
|
|
418
|
+
const chains = options.chains?.length ? options.chains : DEFAULT_IOS_WALLET_CHAINS;
|
|
419
|
+
const callbackUrl = options.redirectUrl ?? getDefaultIosWalletRedirectUrl();
|
|
420
|
+
return IOS_WALLETS.map((wallet) => ({
|
|
421
|
+
name: wallet.name,
|
|
422
|
+
icon: wallet.icon,
|
|
423
|
+
chains,
|
|
424
|
+
platform: "mobile",
|
|
425
|
+
source: "deep-link",
|
|
426
|
+
appUrl: wallet.appUrl,
|
|
427
|
+
installUrl: wallet.installUrl,
|
|
428
|
+
callbackUrl,
|
|
429
|
+
capabilities: {
|
|
430
|
+
connect: true,
|
|
431
|
+
signTransaction: Boolean(wallet.signTransactionUrl),
|
|
432
|
+
signAllTransactions: Boolean(wallet.signAllTransactionsUrl),
|
|
433
|
+
signAndSendTransaction: Boolean(wallet.signAndSendTransactionUrl)
|
|
434
|
+
},
|
|
435
|
+
accounts: getStoredIosWalletAccount(wallet.id, chains),
|
|
436
|
+
wallet
|
|
437
|
+
}));
|
|
438
|
+
}
|
|
439
|
+
function adaptSolanaIosWallet(walletInfo, options = {}) {
|
|
440
|
+
const definition = getIosWalletDefinition(walletInfo);
|
|
441
|
+
let connecting = false;
|
|
442
|
+
let disconnecting = false;
|
|
443
|
+
let session = getStoredSession(definition.id);
|
|
444
|
+
function refreshSession() {
|
|
445
|
+
session = getStoredSession(definition.id);
|
|
446
|
+
options.onChange?.();
|
|
447
|
+
}
|
|
448
|
+
return {
|
|
449
|
+
get publicKey() {
|
|
450
|
+
return session?.publicKey ? new web3Compat.PublicKey(session.publicKey) : null;
|
|
451
|
+
},
|
|
452
|
+
get connected() {
|
|
453
|
+
return Boolean(session?.publicKey && session.session);
|
|
454
|
+
},
|
|
455
|
+
get connecting() {
|
|
456
|
+
return connecting;
|
|
457
|
+
},
|
|
458
|
+
get disconnecting() {
|
|
459
|
+
return disconnecting;
|
|
460
|
+
},
|
|
461
|
+
async connect() {
|
|
462
|
+
const callback = handleSolanaIosWalletCallback({ clearUrl: true });
|
|
463
|
+
if (callback?.walletId === definition.id) {
|
|
464
|
+
refreshSession();
|
|
465
|
+
return;
|
|
466
|
+
}
|
|
467
|
+
connecting = true;
|
|
468
|
+
options.onChange?.();
|
|
469
|
+
try {
|
|
470
|
+
launchConnect(definition, options);
|
|
471
|
+
await waitForRedirect();
|
|
472
|
+
} finally {
|
|
473
|
+
connecting = false;
|
|
474
|
+
options.onChange?.();
|
|
475
|
+
}
|
|
476
|
+
},
|
|
477
|
+
async disconnect() {
|
|
478
|
+
disconnecting = true;
|
|
479
|
+
options.onChange?.();
|
|
480
|
+
try {
|
|
481
|
+
removeStoredSession(definition.id);
|
|
482
|
+
refreshSession();
|
|
483
|
+
} finally {
|
|
484
|
+
disconnecting = false;
|
|
485
|
+
options.onChange?.();
|
|
486
|
+
}
|
|
487
|
+
},
|
|
488
|
+
signTransaction: definition.signTransactionUrl ? async (transaction) => {
|
|
489
|
+
const signedTransaction = await launchSignTransaction(definition, transaction, options);
|
|
490
|
+
return deserializeTransaction(transaction, signedTransaction);
|
|
491
|
+
} : void 0,
|
|
492
|
+
signAllTransactions: definition.signAllTransactionsUrl ? async (transactions) => {
|
|
493
|
+
const signedTransactions = await launchSignAllTransactions(
|
|
494
|
+
definition,
|
|
495
|
+
transactions,
|
|
496
|
+
options
|
|
497
|
+
);
|
|
498
|
+
if (signedTransactions.length !== transactions.length) {
|
|
499
|
+
throw new Error(
|
|
500
|
+
`iOS wallet returned ${signedTransactions.length} signed transactions for ${transactions.length} requested transactions`
|
|
501
|
+
);
|
|
502
|
+
}
|
|
503
|
+
return signedTransactions.map(
|
|
504
|
+
(transaction, index) => deserializeTransaction(transactions[index], transaction)
|
|
505
|
+
);
|
|
506
|
+
} : void 0,
|
|
507
|
+
signAndSendTransaction: definition.signAndSendTransactionUrl ? async (transaction, sendOptions) => launchSignAndSendTransaction(definition, transaction, sendOptions, options) : void 0
|
|
508
|
+
};
|
|
509
|
+
}
|
|
510
|
+
function isSolanaIosWalletInfo(walletInfo) {
|
|
511
|
+
return walletInfo.source === "deep-link" && isIosWalletDefinition(walletInfo.wallet);
|
|
512
|
+
}
|
|
513
|
+
function launchConnect(definition, options) {
|
|
514
|
+
const keyPair = nacl.box.keyPair();
|
|
515
|
+
const pending = createPendingRequest(definition.id, "connect", keyPair, options.redirectUrl);
|
|
516
|
+
const appIdentity = options.appIdentity ?? getDefaultIosWalletAppIdentity();
|
|
517
|
+
const url = new URL(definition.connectUrl);
|
|
518
|
+
url.searchParams.set("app_url", appIdentity.uri);
|
|
519
|
+
url.searchParams.set("dapp_encryption_public_key", pending.dappEncryptionPublicKey);
|
|
520
|
+
url.searchParams.set("redirect_link", pending.redirectUrl);
|
|
521
|
+
if (options.cluster && options.cluster !== "localnet") {
|
|
522
|
+
url.searchParams.set("cluster", options.cluster);
|
|
523
|
+
}
|
|
524
|
+
storePendingRequest(pending);
|
|
525
|
+
openIosWalletUrl(url.toString());
|
|
526
|
+
}
|
|
527
|
+
async function launchSignTransaction(definition, transaction, options) {
|
|
528
|
+
const response = await launchEncryptedWalletRequest(definition, "signTransaction", options, {
|
|
529
|
+
transaction: bs58__default.encode(serializeTransaction(transaction))
|
|
530
|
+
});
|
|
531
|
+
if (!response.transaction) {
|
|
532
|
+
throw new Error("iOS wallet did not return a signed transaction");
|
|
533
|
+
}
|
|
534
|
+
return response.transaction;
|
|
535
|
+
}
|
|
536
|
+
async function launchSignAllTransactions(definition, transactions, options) {
|
|
537
|
+
const response = await launchEncryptedWalletRequest(definition, "signAllTransactions", options, {
|
|
538
|
+
transactions: transactions.map((transaction) => bs58__default.encode(serializeTransaction(transaction)))
|
|
539
|
+
});
|
|
540
|
+
if (!response.transactions) {
|
|
541
|
+
throw new Error("iOS wallet did not return signed transactions");
|
|
542
|
+
}
|
|
543
|
+
return response.transactions;
|
|
544
|
+
}
|
|
545
|
+
async function launchSignAndSendTransaction(definition, transaction, sendOptions, options) {
|
|
546
|
+
const response = await launchEncryptedWalletRequest(
|
|
547
|
+
definition,
|
|
548
|
+
"signAndSendTransaction",
|
|
549
|
+
options,
|
|
550
|
+
{
|
|
551
|
+
transaction: bs58__default.encode(serializeTransaction(transaction)),
|
|
552
|
+
sendOptions
|
|
553
|
+
}
|
|
554
|
+
);
|
|
555
|
+
if (!response.signature) {
|
|
556
|
+
throw new Error("iOS wallet did not return a transaction signature");
|
|
557
|
+
}
|
|
558
|
+
return { signature: response.signature };
|
|
559
|
+
}
|
|
560
|
+
async function launchEncryptedWalletRequest(definition, method, options, payload) {
|
|
561
|
+
const session = getStoredSession(definition.id);
|
|
562
|
+
if (!session) {
|
|
563
|
+
throw new Error("Connect the iOS wallet before signing");
|
|
564
|
+
}
|
|
565
|
+
const methodUrl = getIosWalletMethodUrl(definition, method);
|
|
566
|
+
const pending = createPendingRequest(
|
|
567
|
+
definition.id,
|
|
568
|
+
method,
|
|
569
|
+
{
|
|
570
|
+
publicKey: bs58__default.decode(session.dappEncryptionPublicKey),
|
|
571
|
+
secretKey: bs58__default.decode(session.dappEncryptionSecretKey)
|
|
572
|
+
},
|
|
573
|
+
options.redirectUrl,
|
|
574
|
+
method === "signAllTransactions" && Array.isArray(payload.transactions) ? payload.transactions.length : void 0
|
|
575
|
+
);
|
|
576
|
+
const nonce = nacl.randomBytes(nacl.box.nonceLength);
|
|
577
|
+
const encryptedPayload = encryptPayload(
|
|
578
|
+
{ ...payload, session: session.session },
|
|
579
|
+
nonce,
|
|
580
|
+
bs58__default.decode(session.sharedSecret)
|
|
581
|
+
);
|
|
582
|
+
const url = new URL(methodUrl);
|
|
583
|
+
url.searchParams.set("dapp_encryption_public_key", session.dappEncryptionPublicKey);
|
|
584
|
+
url.searchParams.set("nonce", bs58__default.encode(nonce));
|
|
585
|
+
url.searchParams.set("redirect_link", pending.redirectUrl);
|
|
586
|
+
url.searchParams.set("payload", encryptedPayload);
|
|
587
|
+
storePendingRequest(pending);
|
|
588
|
+
openIosWalletUrl(url.toString());
|
|
589
|
+
return waitForRedirect();
|
|
590
|
+
}
|
|
591
|
+
function getIosWalletMethodUrl(definition, method) {
|
|
592
|
+
const url = definition[`${method}Url`];
|
|
593
|
+
if (!url) {
|
|
594
|
+
throw new Error(`${definition.name} does not support ${method} through iOS deeplinks`);
|
|
595
|
+
}
|
|
596
|
+
return url;
|
|
597
|
+
}
|
|
598
|
+
function getIosWalletDefinition(walletInfo) {
|
|
599
|
+
if (!isIosWalletDefinition(walletInfo.wallet)) {
|
|
600
|
+
throw new Error("Solana wallet info is not an iOS wallet");
|
|
601
|
+
}
|
|
602
|
+
return walletInfo.wallet;
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
exports.adaptSolanaIosWallet = adaptSolanaIosWallet;
|
|
606
|
+
exports.getDefaultIosWalletAppIdentity = getDefaultIosWalletAppIdentity;
|
|
607
|
+
exports.getDefaultIosWalletRedirectUrl = getDefaultIosWalletRedirectUrl;
|
|
608
|
+
exports.getSolanaIosWallets = getSolanaIosWallets;
|
|
609
|
+
exports.handleSolanaIosWalletCallback = handleSolanaIosWalletCallback;
|
|
610
|
+
exports.isIosBrowser = isIosBrowser;
|
|
611
|
+
exports.isSolanaIosBrowserWalletSupported = isSolanaIosBrowserWalletSupported;
|
|
612
|
+
exports.isSolanaIosWalletInfo = isSolanaIosWalletInfo;
|