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