@solana-mobile/mobile-wallet-adapter-protocol 2.2.6 → 2.2.7
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 +9 -6
- package/android/gradle/wrapper/gradle-wrapper.jar +0 -0
- package/android/gradle/wrapper/gradle-wrapper.properties +1 -1
- package/android/gradlew +1 -1
- package/lib/cjs/index.browser.js +816 -963
- package/lib/cjs/index.browser.js.map +1 -0
- package/lib/cjs/index.js +816 -963
- package/lib/cjs/index.js.map +1 -0
- package/lib/cjs/index.native.js +239 -290
- package/lib/cjs/index.native.js.map +1 -0
- package/lib/cjs/package.json +1 -3
- package/lib/esm/index.browser.js +815 -961
- package/lib/esm/index.browser.js.map +1 -0
- package/lib/esm/index.js +815 -961
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/package.json +1 -3
- package/lib/types/index.d.ts +172 -181
- package/lib/types/index.d.ts.map +1 -1
- package/package.json +7 -6
- package/lib/types/index.browser.d.ts +0 -270
- package/lib/types/index.browser.d.ts.map +0 -1
- package/lib/types/index.native.d.ts +0 -270
- package/lib/types/index.native.d.ts.map +0 -1
package/lib/esm/index.browser.js
CHANGED
|
@@ -1,1052 +1,906 @@
|
|
|
1
|
-
import { createSignInMessageText } from
|
|
2
|
-
import { getBase58Decoder } from
|
|
3
|
-
|
|
4
|
-
// Typescript `enums` thwart tree-shaking. See https://bargsten.org/jsts/enums/
|
|
1
|
+
import { createSignInMessageText } from "@solana/wallet-standard-util";
|
|
2
|
+
import { getBase58Decoder } from "@solana/codecs-strings";
|
|
3
|
+
//#region src/errors.ts
|
|
5
4
|
const SolanaMobileWalletAdapterErrorCode = {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
5
|
+
ERROR_ASSOCIATION_PORT_OUT_OF_RANGE: "ERROR_ASSOCIATION_PORT_OUT_OF_RANGE",
|
|
6
|
+
ERROR_REFLECTOR_ID_OUT_OF_RANGE: "ERROR_REFLECTOR_ID_OUT_OF_RANGE",
|
|
7
|
+
ERROR_FORBIDDEN_WALLET_BASE_URL: "ERROR_FORBIDDEN_WALLET_BASE_URL",
|
|
8
|
+
ERROR_SECURE_CONTEXT_REQUIRED: "ERROR_SECURE_CONTEXT_REQUIRED",
|
|
9
|
+
ERROR_SESSION_CLOSED: "ERROR_SESSION_CLOSED",
|
|
10
|
+
ERROR_SESSION_TIMEOUT: "ERROR_SESSION_TIMEOUT",
|
|
11
|
+
ERROR_WALLET_NOT_FOUND: "ERROR_WALLET_NOT_FOUND",
|
|
12
|
+
ERROR_INVALID_PROTOCOL_VERSION: "ERROR_INVALID_PROTOCOL_VERSION",
|
|
13
|
+
ERROR_BROWSER_NOT_SUPPORTED: "ERROR_BROWSER_NOT_SUPPORTED",
|
|
14
|
+
ERROR_LOOPBACK_ACCESS_BLOCKED: "ERROR_LOOPBACK_ACCESS_BLOCKED",
|
|
15
|
+
ERROR_ASSOCIATION_CANCELLED: "ERROR_ASSOCIATION_CANCELLED"
|
|
16
|
+
};
|
|
17
|
+
var SolanaMobileWalletAdapterError = class extends Error {
|
|
18
|
+
data;
|
|
19
|
+
code;
|
|
20
|
+
constructor(...args) {
|
|
21
|
+
const [code, message, data] = args;
|
|
22
|
+
super(message);
|
|
23
|
+
this.code = code;
|
|
24
|
+
this.data = data;
|
|
25
|
+
this.name = "SolanaMobileWalletAdapterError";
|
|
26
|
+
}
|
|
17
27
|
};
|
|
18
|
-
class SolanaMobileWalletAdapterError extends Error {
|
|
19
|
-
data;
|
|
20
|
-
code;
|
|
21
|
-
constructor(...args) {
|
|
22
|
-
const [code, message, data] = args;
|
|
23
|
-
super(message);
|
|
24
|
-
this.code = code;
|
|
25
|
-
this.data = data;
|
|
26
|
-
this.name = 'SolanaMobileWalletAdapterError';
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
// Typescript `enums` thwart tree-shaking. See https://bargsten.org/jsts/enums/
|
|
30
28
|
const SolanaMobileWalletAdapterProtocolErrorCode = {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
ERROR_ATTEST_ORIGIN_ANDROID: -100,
|
|
29
|
+
ERROR_AUTHORIZATION_FAILED: -1,
|
|
30
|
+
ERROR_INVALID_PAYLOADS: -2,
|
|
31
|
+
ERROR_NOT_SIGNED: -3,
|
|
32
|
+
ERROR_NOT_SUBMITTED: -4,
|
|
33
|
+
ERROR_TOO_MANY_PAYLOADS: -5,
|
|
34
|
+
ERROR_ATTEST_ORIGIN_ANDROID: -100
|
|
38
35
|
};
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
52
|
-
|
|
36
|
+
var SolanaMobileWalletAdapterProtocolError = class extends Error {
|
|
37
|
+
data;
|
|
38
|
+
code;
|
|
39
|
+
jsonRpcMessageId;
|
|
40
|
+
constructor(...args) {
|
|
41
|
+
const [jsonRpcMessageId, code, message, data] = args;
|
|
42
|
+
super(message);
|
|
43
|
+
this.code = code;
|
|
44
|
+
this.data = data;
|
|
45
|
+
this.jsonRpcMessageId = jsonRpcMessageId;
|
|
46
|
+
this.name = "SolanaMobileWalletAdapterProtocolError";
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
//#endregion
|
|
50
|
+
//#region src/base64Utils.ts
|
|
53
51
|
function encode(input) {
|
|
54
|
-
|
|
52
|
+
return window.btoa(input);
|
|
55
53
|
}
|
|
56
54
|
function fromUint8Array$1(byteArray, urlsafe) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
.replace(/\+/g, '-')
|
|
61
|
-
.replace(/\//g, '_')
|
|
62
|
-
.replace(/=+$/, '');
|
|
63
|
-
}
|
|
64
|
-
else
|
|
65
|
-
return base64;
|
|
55
|
+
const base64 = window.btoa(String.fromCharCode.call(null, ...byteArray));
|
|
56
|
+
if (urlsafe) return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
57
|
+
else return base64;
|
|
66
58
|
}
|
|
67
59
|
function toUint8Array(base64EncodedByteArray) {
|
|
68
|
-
|
|
69
|
-
.atob(base64EncodedByteArray)
|
|
70
|
-
.split('')
|
|
71
|
-
.map((c) => c.charCodeAt(0)));
|
|
60
|
+
return new Uint8Array(window.atob(base64EncodedByteArray).split("").map((c) => c.charCodeAt(0)));
|
|
72
61
|
}
|
|
73
|
-
|
|
62
|
+
//#endregion
|
|
63
|
+
//#region src/createHelloReq.ts
|
|
74
64
|
async function createHelloReq(ecdhPublicKey, associationKeypairPrivateKey) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
65
|
+
const publicKeyBuffer = await crypto.subtle.exportKey("raw", ecdhPublicKey);
|
|
66
|
+
const signatureBuffer = await crypto.subtle.sign({
|
|
67
|
+
hash: "SHA-256",
|
|
68
|
+
name: "ECDSA"
|
|
69
|
+
}, associationKeypairPrivateKey, publicKeyBuffer);
|
|
70
|
+
const response = new Uint8Array(publicKeyBuffer.byteLength + signatureBuffer.byteLength);
|
|
71
|
+
response.set(new Uint8Array(publicKeyBuffer), 0);
|
|
72
|
+
response.set(new Uint8Array(signatureBuffer), publicKeyBuffer.byteLength);
|
|
73
|
+
return response;
|
|
74
|
+
}
|
|
75
|
+
//#endregion
|
|
76
|
+
//#region src/createSIWSMessage.ts
|
|
83
77
|
function createSIWSMessage(payload) {
|
|
84
|
-
|
|
78
|
+
return createSignInMessageText(payload);
|
|
85
79
|
}
|
|
86
80
|
function createSIWSMessageBase64Url(payload) {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
const SolanaSignInWithSolana = 'solana:signInWithSolana';
|
|
97
|
-
|
|
81
|
+
return encode(createSIWSMessage(payload)).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
82
|
+
}
|
|
83
|
+
//#endregion
|
|
84
|
+
//#region src/types.ts
|
|
85
|
+
const SolanaSignTransactions = "solana:signTransactions";
|
|
86
|
+
const SolanaCloneAuthorization = "solana:cloneAuthorization";
|
|
87
|
+
const SolanaSignInWithSolana = "solana:signInWithSolana";
|
|
88
|
+
//#endregion
|
|
89
|
+
//#region src/base58Utils.ts
|
|
98
90
|
function fromUint8Array(byteArray) {
|
|
99
|
-
|
|
91
|
+
return getBase58Decoder().decode(byteArray);
|
|
100
92
|
}
|
|
101
93
|
function base64ToBase58(base64EncodedString) {
|
|
102
|
-
|
|
94
|
+
return fromUint8Array(toUint8Array(base64EncodedString));
|
|
103
95
|
}
|
|
104
|
-
|
|
96
|
+
//#endregion
|
|
97
|
+
//#region src/createMobileWalletProxy.ts
|
|
105
98
|
/**
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
99
|
+
* Creates a {@link MobileWallet} proxy that handles backwards compatibility and API to RPC conversion.
|
|
100
|
+
*
|
|
101
|
+
* @param protocolVersion the protocol version in use for this session/request
|
|
102
|
+
* @param protocolRequestHandler callback function that handles sending the RPC request to the wallet endpoint.
|
|
103
|
+
* @returns a {@link MobileWallet} proxy
|
|
104
|
+
*/
|
|
112
105
|
function createMobileWalletProxy(protocolVersion, protocolRequestHandler) {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
return target[p];
|
|
134
|
-
},
|
|
135
|
-
defineProperty() {
|
|
136
|
-
return false;
|
|
137
|
-
},
|
|
138
|
-
deleteProperty() {
|
|
139
|
-
return false;
|
|
140
|
-
},
|
|
141
|
-
});
|
|
106
|
+
return new Proxy({}, {
|
|
107
|
+
get(target, p) {
|
|
108
|
+
if (p === "then") return null;
|
|
109
|
+
if (target[p] == null) target[p] = async function(inputParams) {
|
|
110
|
+
const { method, params } = handleMobileWalletRequest(p, inputParams, protocolVersion);
|
|
111
|
+
const result = await protocolRequestHandler(method, params);
|
|
112
|
+
if (method === "authorize" && params.sign_in_payload && !result.sign_in_result) result["sign_in_result"] = await signInFallback(params.sign_in_payload, result, protocolRequestHandler);
|
|
113
|
+
return handleMobileWalletResponse(p, result, protocolVersion);
|
|
114
|
+
};
|
|
115
|
+
return target[p];
|
|
116
|
+
},
|
|
117
|
+
defineProperty() {
|
|
118
|
+
return false;
|
|
119
|
+
},
|
|
120
|
+
deleteProperty() {
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
});
|
|
142
124
|
}
|
|
143
125
|
/**
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
126
|
+
* Handles all {@link MobileWallet} API requests and determines the correct MWA RPC method and params to call.
|
|
127
|
+
* This handles backwards compatibility, based on the provided @protocolVersion.
|
|
128
|
+
*
|
|
129
|
+
* @param methodName the name of {@link MobileWallet} method that was called
|
|
130
|
+
* @param methodParams the parameters that were passed to the method
|
|
131
|
+
* @param protocolVersion the protocol version in use for this session/request
|
|
132
|
+
* @returns the RPC request method and params that should be sent to the wallet endpoint
|
|
133
|
+
*/
|
|
152
134
|
function handleMobileWalletRequest(methodName, methodParams, protocolVersion) {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
method = 'authorize';
|
|
207
|
-
break;
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
break;
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
return { method, params };
|
|
135
|
+
let params = methodParams;
|
|
136
|
+
let method = methodName.toString().replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`).toLowerCase();
|
|
137
|
+
switch (methodName) {
|
|
138
|
+
case "authorize": {
|
|
139
|
+
let { chain } = params;
|
|
140
|
+
if (protocolVersion === "legacy") {
|
|
141
|
+
switch (chain) {
|
|
142
|
+
case "solana:testnet":
|
|
143
|
+
chain = "testnet";
|
|
144
|
+
break;
|
|
145
|
+
case "solana:devnet":
|
|
146
|
+
chain = "devnet";
|
|
147
|
+
break;
|
|
148
|
+
case "solana:mainnet":
|
|
149
|
+
chain = "mainnet-beta";
|
|
150
|
+
break;
|
|
151
|
+
default: chain = params.cluster;
|
|
152
|
+
}
|
|
153
|
+
params.cluster = chain;
|
|
154
|
+
} else {
|
|
155
|
+
switch (chain) {
|
|
156
|
+
case "testnet":
|
|
157
|
+
case "devnet":
|
|
158
|
+
chain = `solana:${chain}`;
|
|
159
|
+
break;
|
|
160
|
+
case "mainnet-beta":
|
|
161
|
+
chain = "solana:mainnet";
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
params.chain = chain;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
case "reauthorize": {
|
|
168
|
+
const { auth_token, identity } = params;
|
|
169
|
+
if (auth_token) switch (protocolVersion) {
|
|
170
|
+
case "legacy":
|
|
171
|
+
method = "reauthorize";
|
|
172
|
+
params = {
|
|
173
|
+
auth_token,
|
|
174
|
+
identity
|
|
175
|
+
};
|
|
176
|
+
break;
|
|
177
|
+
default:
|
|
178
|
+
method = "authorize";
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
181
|
+
break;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return {
|
|
185
|
+
method,
|
|
186
|
+
params
|
|
187
|
+
};
|
|
215
188
|
}
|
|
216
189
|
/**
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
190
|
+
* Handles all {@link MobileWallet} API responses and modifies the response for backwards compatibility, if needed
|
|
191
|
+
*
|
|
192
|
+
* @param method the {@link MobileWallet} method that was called
|
|
193
|
+
* @param response the original response that was returned by the method call
|
|
194
|
+
* @param protocolVersion the protocol version in use for this session/request
|
|
195
|
+
* @returns the possibly modified response
|
|
196
|
+
*/
|
|
224
197
|
function handleMobileWalletResponse(method, response, protocolVersion) {
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
return response;
|
|
198
|
+
switch (method) {
|
|
199
|
+
case "getCapabilities": {
|
|
200
|
+
const capabilities = response;
|
|
201
|
+
switch (protocolVersion) {
|
|
202
|
+
case "legacy": {
|
|
203
|
+
const features = [SolanaSignTransactions];
|
|
204
|
+
if (capabilities.supports_clone_authorization === true) features.push(SolanaCloneAuthorization);
|
|
205
|
+
return {
|
|
206
|
+
...capabilities,
|
|
207
|
+
features
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
case "v1": return {
|
|
211
|
+
...capabilities,
|
|
212
|
+
supports_sign_and_send_transactions: true,
|
|
213
|
+
supports_clone_authorization: capabilities.features.includes(SolanaCloneAuthorization)
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return response;
|
|
250
219
|
}
|
|
251
220
|
async function signInFallback(signInPayload, authorizationResult, protocolRequestHandler) {
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
return signInResult;
|
|
221
|
+
const domain = signInPayload.domain ?? window.location.host;
|
|
222
|
+
const address = authorizationResult.accounts[0].address;
|
|
223
|
+
const siwsMessage = createSIWSMessageBase64Url({
|
|
224
|
+
...signInPayload,
|
|
225
|
+
domain,
|
|
226
|
+
address: base64ToBase58(address)
|
|
227
|
+
});
|
|
228
|
+
const signedPayload = toUint8Array((await protocolRequestHandler("sign_messages", {
|
|
229
|
+
addresses: [address],
|
|
230
|
+
payloads: [siwsMessage]
|
|
231
|
+
})).signed_payloads[0]);
|
|
232
|
+
const signedMessage = fromUint8Array$1(signedPayload.slice(0, signedPayload.length - 64));
|
|
233
|
+
const signature = fromUint8Array$1(signedPayload.slice(signedPayload.length - 64));
|
|
234
|
+
return {
|
|
235
|
+
address,
|
|
236
|
+
signed_message: signedMessage.length == 0 ? siwsMessage : signedMessage,
|
|
237
|
+
signature
|
|
238
|
+
};
|
|
271
239
|
}
|
|
272
|
-
|
|
273
|
-
const SEQUENCE_NUMBER_BYTES = 4;
|
|
274
240
|
function createSequenceNumberVector(sequenceNumber) {
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
const view = new DataView(byteArray);
|
|
280
|
-
view.setUint32(0, sequenceNumber, /* littleEndian */ false);
|
|
281
|
-
return new Uint8Array(byteArray);
|
|
241
|
+
if (sequenceNumber >= 4294967296) throw new Error("Outbound sequence number overflow. The maximum sequence number is 32-bytes.");
|
|
242
|
+
const byteArray = /* @__PURE__ */ new ArrayBuffer(4);
|
|
243
|
+
new DataView(byteArray).setUint32(0, sequenceNumber, false);
|
|
244
|
+
return new Uint8Array(byteArray);
|
|
282
245
|
}
|
|
283
|
-
|
|
246
|
+
//#endregion
|
|
247
|
+
//#region src/encryptedMessage.ts
|
|
284
248
|
const INITIALIZATION_VECTOR_BYTES = 12;
|
|
285
|
-
const ENCODED_PUBLIC_KEY_LENGTH_BYTES = 65;
|
|
286
249
|
async function encryptMessage(plaintext, sequenceNumber, sharedSecret) {
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
250
|
+
const sequenceNumberVector = createSequenceNumberVector(sequenceNumber);
|
|
251
|
+
const initializationVector = new Uint8Array(INITIALIZATION_VECTOR_BYTES);
|
|
252
|
+
crypto.getRandomValues(initializationVector);
|
|
253
|
+
const ciphertext = await crypto.subtle.encrypt(getAlgorithmParams(sequenceNumberVector, initializationVector), sharedSecret, new TextEncoder().encode(plaintext));
|
|
254
|
+
const response = new Uint8Array(sequenceNumberVector.byteLength + initializationVector.byteLength + ciphertext.byteLength);
|
|
255
|
+
response.set(new Uint8Array(sequenceNumberVector), 0);
|
|
256
|
+
response.set(new Uint8Array(initializationVector), sequenceNumberVector.byteLength);
|
|
257
|
+
response.set(new Uint8Array(ciphertext), sequenceNumberVector.byteLength + initializationVector.byteLength);
|
|
258
|
+
return response;
|
|
296
259
|
}
|
|
297
260
|
async function decryptMessage(message, sharedSecret) {
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
return plaintext;
|
|
261
|
+
const sequenceNumberVector = message.slice(0, 4);
|
|
262
|
+
const initializationVector = message.slice(4, 4 + INITIALIZATION_VECTOR_BYTES);
|
|
263
|
+
const ciphertext = message.slice(4 + INITIALIZATION_VECTOR_BYTES);
|
|
264
|
+
const plaintextBuffer = await crypto.subtle.decrypt(getAlgorithmParams(sequenceNumberVector, initializationVector), sharedSecret, ciphertext);
|
|
265
|
+
return getUtf8Decoder().decode(plaintextBuffer);
|
|
304
266
|
}
|
|
305
267
|
function getAlgorithmParams(sequenceNumber, initializationVector) {
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
268
|
+
return {
|
|
269
|
+
additionalData: sequenceNumber,
|
|
270
|
+
iv: initializationVector,
|
|
271
|
+
name: "AES-GCM",
|
|
272
|
+
tagLength: 128
|
|
273
|
+
};
|
|
312
274
|
}
|
|
313
275
|
let _utf8Decoder;
|
|
314
276
|
function getUtf8Decoder() {
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
}
|
|
318
|
-
return _utf8Decoder;
|
|
277
|
+
if (_utf8Decoder === void 0) _utf8Decoder = new TextDecoder("utf-8");
|
|
278
|
+
return _utf8Decoder;
|
|
319
279
|
}
|
|
320
|
-
|
|
280
|
+
//#endregion
|
|
281
|
+
//#region src/generateAssociationKeypair.ts
|
|
321
282
|
async function generateAssociationKeypair() {
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
283
|
+
return await crypto.subtle.generateKey({
|
|
284
|
+
name: "ECDSA",
|
|
285
|
+
namedCurve: "P-256"
|
|
286
|
+
}, false, ["sign"]);
|
|
326
287
|
}
|
|
327
|
-
|
|
288
|
+
//#endregion
|
|
289
|
+
//#region src/generateECDHKeypair.ts
|
|
328
290
|
async function generateECDHKeypair() {
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
291
|
+
return await crypto.subtle.generateKey({
|
|
292
|
+
name: "ECDH",
|
|
293
|
+
namedCurve: "P-256"
|
|
294
|
+
}, false, ["deriveKey", "deriveBits"]);
|
|
333
295
|
}
|
|
334
|
-
|
|
335
|
-
|
|
296
|
+
//#endregion
|
|
297
|
+
//#region src/arrayBufferToBase64String.ts
|
|
336
298
|
function arrayBufferToBase64String(buffer) {
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
299
|
+
let binary = "";
|
|
300
|
+
const bytes = new Uint8Array(buffer);
|
|
301
|
+
const len = bytes.byteLength;
|
|
302
|
+
for (let ii = 0; ii < len; ii++) binary += String.fromCharCode(bytes[ii]);
|
|
303
|
+
return window.btoa(binary);
|
|
304
|
+
}
|
|
305
|
+
//#endregion
|
|
306
|
+
//#region src/associationPort.ts
|
|
346
307
|
function getRandomAssociationPort() {
|
|
347
|
-
|
|
308
|
+
return assertAssociationPort(49152 + Math.floor(Math.random() * 16384));
|
|
348
309
|
}
|
|
349
310
|
function assertAssociationPort(port) {
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
}
|
|
353
|
-
return port;
|
|
311
|
+
if (port < 49152 || port > 65535) throw new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_ASSOCIATION_PORT_OUT_OF_RANGE, `Association port number must be between 49152 and 65535. ${port} given.`, { port });
|
|
312
|
+
return port;
|
|
354
313
|
}
|
|
355
|
-
|
|
314
|
+
//#endregion
|
|
315
|
+
//#region src/getStringWithURLUnsafeBase64CharactersReplaced.ts
|
|
356
316
|
function getStringWithURLUnsafeCharactersReplaced(unsafeBase64EncodedString) {
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
|
|
317
|
+
return unsafeBase64EncodedString.replace(/[/+=]/g, (m) => ({
|
|
318
|
+
"/": "_",
|
|
319
|
+
"+": "-",
|
|
320
|
+
"=": "."
|
|
321
|
+
})[m]);
|
|
322
|
+
}
|
|
323
|
+
//#endregion
|
|
324
|
+
//#region src/getAssociateAndroidIntentURL.ts
|
|
325
|
+
const INTENT_NAME = "solana-wallet";
|
|
365
326
|
function getPathParts(pathString) {
|
|
366
|
-
|
|
367
|
-
// Strip leading and trailing slashes
|
|
368
|
-
.replace(/(^\/+|\/+$)/g, '')
|
|
369
|
-
// Return an array of directories
|
|
370
|
-
.split('/'));
|
|
327
|
+
return pathString.replace(/(^\/+|\/+$)/g, "").split("/");
|
|
371
328
|
}
|
|
372
329
|
function getIntentURL(methodPathname, intentUrlBase) {
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
url.searchParams.set('reflector', `${hostAuthority}`);
|
|
409
|
-
url.searchParams.set('id', `${fromUint8Array$1(reflectorId, true)}`);
|
|
410
|
-
protocolVersions.forEach((version) => {
|
|
411
|
-
url.searchParams.set('v', version);
|
|
412
|
-
});
|
|
413
|
-
return url;
|
|
414
|
-
}
|
|
415
|
-
|
|
330
|
+
let baseUrl = null;
|
|
331
|
+
if (intentUrlBase) {
|
|
332
|
+
try {
|
|
333
|
+
baseUrl = new URL(intentUrlBase);
|
|
334
|
+
} catch {}
|
|
335
|
+
if (baseUrl?.protocol !== "https:") throw new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_FORBIDDEN_WALLET_BASE_URL, "Base URLs supplied by wallets must be valid `https` URLs");
|
|
336
|
+
}
|
|
337
|
+
baseUrl ||= new URL(`${INTENT_NAME}:/`);
|
|
338
|
+
const pathname = methodPathname.startsWith("/") ? methodPathname : [...getPathParts(baseUrl.pathname), ...getPathParts(methodPathname)].join("/");
|
|
339
|
+
return new URL(pathname, baseUrl);
|
|
340
|
+
}
|
|
341
|
+
async function getAssociateAndroidIntentURL(associationPublicKey, putativePort, associationURLBase, protocolVersions = ["v1"]) {
|
|
342
|
+
const associationPort = assertAssociationPort(putativePort);
|
|
343
|
+
const encodedKey = arrayBufferToBase64String(await crypto.subtle.exportKey("raw", associationPublicKey));
|
|
344
|
+
const url = getIntentURL("v1/associate/local", associationURLBase);
|
|
345
|
+
url.searchParams.set("association", getStringWithURLUnsafeCharactersReplaced(encodedKey));
|
|
346
|
+
url.searchParams.set("port", `${associationPort}`);
|
|
347
|
+
protocolVersions.forEach((version) => {
|
|
348
|
+
url.searchParams.set("v", version);
|
|
349
|
+
});
|
|
350
|
+
return url;
|
|
351
|
+
}
|
|
352
|
+
async function getRemoteAssociateAndroidIntentURL(associationPublicKey, hostAuthority, reflectorId, associationURLBase, protocolVersions = ["v1"]) {
|
|
353
|
+
const encodedKey = arrayBufferToBase64String(await crypto.subtle.exportKey("raw", associationPublicKey));
|
|
354
|
+
const url = getIntentURL("v1/associate/remote", associationURLBase);
|
|
355
|
+
url.searchParams.set("association", getStringWithURLUnsafeCharactersReplaced(encodedKey));
|
|
356
|
+
url.searchParams.set("reflector", `${hostAuthority}`);
|
|
357
|
+
url.searchParams.set("id", `${fromUint8Array$1(reflectorId, true)}`);
|
|
358
|
+
protocolVersions.forEach((version) => {
|
|
359
|
+
url.searchParams.set("v", version);
|
|
360
|
+
});
|
|
361
|
+
return url;
|
|
362
|
+
}
|
|
363
|
+
//#endregion
|
|
364
|
+
//#region src/jsonRpcMessage.ts
|
|
416
365
|
async function encryptJsonRpcMessage(jsonRpcMessage, sharedSecret) {
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
366
|
+
const plaintext = JSON.stringify(jsonRpcMessage);
|
|
367
|
+
const sequenceNumber = jsonRpcMessage.id;
|
|
368
|
+
return encryptMessage(plaintext, sequenceNumber, sharedSecret);
|
|
420
369
|
}
|
|
421
370
|
async function decryptJsonRpcMessage(message, sharedSecret) {
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
371
|
+
const plaintext = await decryptMessage(message, sharedSecret);
|
|
372
|
+
const jsonRpcMessage = JSON.parse(plaintext);
|
|
373
|
+
if (Object.hasOwnProperty.call(jsonRpcMessage, "error")) throw new SolanaMobileWalletAdapterProtocolError(jsonRpcMessage.id, jsonRpcMessage.error.code, jsonRpcMessage.error.message);
|
|
374
|
+
return jsonRpcMessage;
|
|
375
|
+
}
|
|
376
|
+
//#endregion
|
|
377
|
+
//#region src/parseHelloRsp.ts
|
|
378
|
+
async function parseHelloRsp(payloadBuffer, associationPublicKey, ecdhPrivateKey) {
|
|
379
|
+
const [associationPublicKeyBuffer, walletPublicKey] = await Promise.all([crypto.subtle.exportKey("raw", associationPublicKey), crypto.subtle.importKey("raw", payloadBuffer.slice(0, 65), {
|
|
380
|
+
name: "ECDH",
|
|
381
|
+
namedCurve: "P-256"
|
|
382
|
+
}, false, [])]);
|
|
383
|
+
const sharedSecret = await crypto.subtle.deriveBits({
|
|
384
|
+
name: "ECDH",
|
|
385
|
+
public: walletPublicKey
|
|
386
|
+
}, ecdhPrivateKey, 256);
|
|
387
|
+
const ecdhSecretKey = await crypto.subtle.importKey("raw", sharedSecret, "HKDF", false, ["deriveKey"]);
|
|
388
|
+
return await crypto.subtle.deriveKey({
|
|
389
|
+
name: "HKDF",
|
|
390
|
+
hash: "SHA-256",
|
|
391
|
+
salt: new Uint8Array(associationPublicKeyBuffer),
|
|
392
|
+
info: new Uint8Array()
|
|
393
|
+
}, ecdhSecretKey, {
|
|
394
|
+
name: "AES-GCM",
|
|
395
|
+
length: 128
|
|
396
|
+
}, false, ["encrypt", "decrypt"]);
|
|
397
|
+
}
|
|
398
|
+
//#endregion
|
|
399
|
+
//#region src/parseSessionProps.ts
|
|
447
400
|
async function parseSessionProps(message, sharedSecret) {
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
protocol_version: protocolVersion
|
|
467
|
-
});
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
// Typescript `enums` thwart tree-shaking. See https://bargsten.org/jsts/enums/
|
|
401
|
+
const plaintext = await decryptMessage(message, sharedSecret);
|
|
402
|
+
const jsonProperties = JSON.parse(plaintext);
|
|
403
|
+
let protocolVersion = "legacy";
|
|
404
|
+
if (Object.hasOwnProperty.call(jsonProperties, "v")) switch (jsonProperties.v) {
|
|
405
|
+
case 1:
|
|
406
|
+
case "1":
|
|
407
|
+
case "v1":
|
|
408
|
+
protocolVersion = "v1";
|
|
409
|
+
break;
|
|
410
|
+
case "legacy":
|
|
411
|
+
protocolVersion = "legacy";
|
|
412
|
+
break;
|
|
413
|
+
default: throw new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_INVALID_PROTOCOL_VERSION, `Unknown/unsupported protocol version: ${jsonProperties.v}`);
|
|
414
|
+
}
|
|
415
|
+
return { protocol_version: protocolVersion };
|
|
416
|
+
}
|
|
417
|
+
//#endregion
|
|
418
|
+
//#region src/startSession.ts
|
|
471
419
|
const Browser = {
|
|
472
|
-
|
|
473
|
-
|
|
420
|
+
Firefox: 0,
|
|
421
|
+
Other: 1
|
|
474
422
|
};
|
|
475
423
|
function assertUnreachable(x) {
|
|
476
|
-
|
|
424
|
+
return x;
|
|
477
425
|
}
|
|
478
426
|
function getBrowser() {
|
|
479
|
-
|
|
427
|
+
return navigator.userAgent.indexOf("Firefox/") !== -1 ? Browser.Firefox : Browser.Other;
|
|
480
428
|
}
|
|
481
429
|
function getDetectionPromise() {
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
reject();
|
|
498
|
-
}, 3000);
|
|
499
|
-
});
|
|
430
|
+
return new Promise((resolve, reject) => {
|
|
431
|
+
function cleanup() {
|
|
432
|
+
clearTimeout(timeoutId);
|
|
433
|
+
window.removeEventListener("blur", handleBlur);
|
|
434
|
+
}
|
|
435
|
+
function handleBlur() {
|
|
436
|
+
cleanup();
|
|
437
|
+
resolve();
|
|
438
|
+
}
|
|
439
|
+
window.addEventListener("blur", handleBlur);
|
|
440
|
+
const timeoutId = setTimeout(() => {
|
|
441
|
+
cleanup();
|
|
442
|
+
reject();
|
|
443
|
+
}, 3e3);
|
|
444
|
+
});
|
|
500
445
|
}
|
|
501
446
|
let _frame = null;
|
|
502
447
|
function launchUrlThroughHiddenFrame(url) {
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
_frame.contentWindow.location.href = url.toString();
|
|
448
|
+
if (_frame == null) {
|
|
449
|
+
_frame = document.createElement("iframe");
|
|
450
|
+
_frame.style.display = "none";
|
|
451
|
+
document.body.appendChild(_frame);
|
|
452
|
+
}
|
|
453
|
+
_frame.contentWindow.location.href = url.toString();
|
|
510
454
|
}
|
|
511
455
|
async function launchAssociation(associationUrl) {
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
window.location.assign(associationUrl);
|
|
531
|
-
await detectionPromise;
|
|
532
|
-
break;
|
|
533
|
-
}
|
|
534
|
-
default:
|
|
535
|
-
assertUnreachable(browser);
|
|
536
|
-
}
|
|
537
|
-
}
|
|
538
|
-
catch (e) {
|
|
539
|
-
throw new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_WALLET_NOT_FOUND, 'Found no installed wallet that supports the mobile wallet protocol.');
|
|
540
|
-
}
|
|
541
|
-
}
|
|
456
|
+
if (associationUrl.protocol === "https:") window.location.assign(associationUrl);
|
|
457
|
+
else try {
|
|
458
|
+
const browser = getBrowser();
|
|
459
|
+
switch (browser) {
|
|
460
|
+
case Browser.Firefox:
|
|
461
|
+
launchUrlThroughHiddenFrame(associationUrl);
|
|
462
|
+
break;
|
|
463
|
+
case Browser.Other: {
|
|
464
|
+
const detectionPromise = getDetectionPromise();
|
|
465
|
+
window.location.assign(associationUrl);
|
|
466
|
+
await detectionPromise;
|
|
467
|
+
break;
|
|
468
|
+
}
|
|
469
|
+
default: assertUnreachable(browser);
|
|
470
|
+
}
|
|
471
|
+
} catch (e) {
|
|
472
|
+
throw new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_WALLET_NOT_FOUND, "Found no installed wallet that supports the mobile wallet protocol.");
|
|
473
|
+
}
|
|
542
474
|
}
|
|
543
475
|
async function startSession(associationPublicKey, associationURLBase) {
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
return randomAssociationPort;
|
|
476
|
+
const randomAssociationPort = getRandomAssociationPort();
|
|
477
|
+
await launchAssociation(await getAssociateAndroidIntentURL(associationPublicKey, randomAssociationPort, associationURLBase));
|
|
478
|
+
return randomAssociationPort;
|
|
548
479
|
}
|
|
549
|
-
|
|
480
|
+
//#endregion
|
|
481
|
+
//#region src/transact.ts
|
|
550
482
|
const WEBSOCKET_CONNECTION_CONFIG = {
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
timeoutMs: 30000,
|
|
483
|
+
retryDelayScheduleMs: [
|
|
484
|
+
150,
|
|
485
|
+
150,
|
|
486
|
+
200,
|
|
487
|
+
500,
|
|
488
|
+
500,
|
|
489
|
+
750,
|
|
490
|
+
750,
|
|
491
|
+
1e3
|
|
492
|
+
],
|
|
493
|
+
timeoutMs: 3e4
|
|
563
494
|
};
|
|
564
|
-
const WEBSOCKET_PROTOCOL_BINARY =
|
|
565
|
-
const WEBSOCKET_PROTOCOL_BASE64 =
|
|
495
|
+
const WEBSOCKET_PROTOCOL_BINARY = "com.solana.mobilewalletadapter.v1";
|
|
496
|
+
const WEBSOCKET_PROTOCOL_BASE64 = "com.solana.mobilewalletadapter.v1.base64";
|
|
566
497
|
function assertSecureContext() {
|
|
567
|
-
|
|
568
|
-
throw new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_SECURE_CONTEXT_REQUIRED, 'The mobile wallet adapter protocol must be used in a secure context (`https`).');
|
|
569
|
-
}
|
|
498
|
+
if (typeof window === "undefined" || window.isSecureContext !== true) throw new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_SECURE_CONTEXT_REQUIRED, "The mobile wallet adapter protocol must be used in a secure context (`https`).");
|
|
570
499
|
}
|
|
571
500
|
function assertSecureEndpointSpecificURI(walletUriBase) {
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
if (url.protocol !== 'https:') {
|
|
580
|
-
throw new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_FORBIDDEN_WALLET_BASE_URL, 'Base URLs supplied by wallets must be valid `https` URLs');
|
|
581
|
-
}
|
|
501
|
+
let url;
|
|
502
|
+
try {
|
|
503
|
+
url = new URL(walletUriBase);
|
|
504
|
+
} catch {
|
|
505
|
+
throw new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_FORBIDDEN_WALLET_BASE_URL, "Invalid base URL supplied by wallet");
|
|
506
|
+
}
|
|
507
|
+
if (url.protocol !== "https:") throw new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_FORBIDDEN_WALLET_BASE_URL, "Base URLs supplied by wallets must be valid `https` URLs");
|
|
582
508
|
}
|
|
583
509
|
function getSequenceNumberFromByteArray(byteArray) {
|
|
584
|
-
|
|
585
|
-
return view.getUint32(0, /* littleEndian */ false);
|
|
510
|
+
return new DataView(byteArray).getUint32(0, false);
|
|
586
511
|
}
|
|
587
512
|
function decodeVarLong(byteArray) {
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
513
|
+
var bytes = new Uint8Array(byteArray), l = byteArray.byteLength, limit = 10, value = 0, offset = 0, b;
|
|
514
|
+
do {
|
|
515
|
+
if (offset >= l || offset > limit) throw new RangeError("Failed to decode varint");
|
|
516
|
+
b = bytes[offset++];
|
|
517
|
+
value |= (b & 127) << 7 * offset;
|
|
518
|
+
} while (b >= 128);
|
|
519
|
+
return {
|
|
520
|
+
value,
|
|
521
|
+
offset
|
|
522
|
+
};
|
|
596
523
|
}
|
|
597
524
|
function getReflectorIdFromByteArray(byteArray) {
|
|
598
|
-
|
|
599
|
-
|
|
525
|
+
let { value: length, offset } = decodeVarLong(byteArray);
|
|
526
|
+
return new Uint8Array(byteArray.slice(offset, offset + length));
|
|
600
527
|
}
|
|
601
528
|
async function transact(callback, config) {
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
}
|
|
529
|
+
const { wallet, close } = await startScenario(config);
|
|
530
|
+
try {
|
|
531
|
+
return await callback(await wallet);
|
|
532
|
+
} finally {
|
|
533
|
+
close();
|
|
534
|
+
}
|
|
609
535
|
}
|
|
610
536
|
async function startScenario(config) {
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
if (!sessionEstablished) {
|
|
788
|
-
reject(new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_SESSION_CLOSED, `The wallet session was closed before connection.`, { closeEvent: new CloseEvent('socket was closed before connection') }));
|
|
789
|
-
}
|
|
790
|
-
};
|
|
791
|
-
let disposeSocket;
|
|
792
|
-
let retryWaitTimeoutId;
|
|
793
|
-
const attemptSocketConnection = () => {
|
|
794
|
-
if (disposeSocket) {
|
|
795
|
-
disposeSocket();
|
|
796
|
-
}
|
|
797
|
-
state = { __type: 'connecting', associationKeypair };
|
|
798
|
-
if (connectionStartTime === undefined) {
|
|
799
|
-
connectionStartTime = Date.now();
|
|
800
|
-
}
|
|
801
|
-
socket = new WebSocket(websocketURL, [WEBSOCKET_PROTOCOL_BINARY]);
|
|
802
|
-
socket.addEventListener('open', handleOpen);
|
|
803
|
-
socket.addEventListener('close', handleClose);
|
|
804
|
-
socket.addEventListener('error', handleError);
|
|
805
|
-
socket.addEventListener('message', handleMessage);
|
|
806
|
-
disposeSocket = () => {
|
|
807
|
-
window.clearTimeout(retryWaitTimeoutId);
|
|
808
|
-
socket.removeEventListener('open', handleOpen);
|
|
809
|
-
socket.removeEventListener('close', handleClose);
|
|
810
|
-
socket.removeEventListener('error', handleError);
|
|
811
|
-
socket.removeEventListener('message', handleMessage);
|
|
812
|
-
};
|
|
813
|
-
};
|
|
814
|
-
attemptSocketConnection();
|
|
815
|
-
}) };
|
|
537
|
+
assertSecureContext();
|
|
538
|
+
const associationKeypair = await generateAssociationKeypair();
|
|
539
|
+
const websocketURL = `ws://localhost:${await startSession(associationKeypair.publicKey, config?.baseUri)}/solana-wallet`;
|
|
540
|
+
let connectionStartTime;
|
|
541
|
+
const getNextRetryDelayMs = (() => {
|
|
542
|
+
const schedule = [...WEBSOCKET_CONNECTION_CONFIG.retryDelayScheduleMs];
|
|
543
|
+
return () => schedule.length > 1 ? schedule.shift() : schedule[0];
|
|
544
|
+
})();
|
|
545
|
+
let nextJsonRpcMessageId = 1;
|
|
546
|
+
let lastKnownInboundSequenceNumber = 0;
|
|
547
|
+
let state = { __type: "disconnected" };
|
|
548
|
+
let socket;
|
|
549
|
+
let sessionEstablished = false;
|
|
550
|
+
let handleForceClose;
|
|
551
|
+
return {
|
|
552
|
+
close: () => {
|
|
553
|
+
socket.close();
|
|
554
|
+
handleForceClose();
|
|
555
|
+
},
|
|
556
|
+
wallet: new Promise((resolve, reject) => {
|
|
557
|
+
const jsonRpcResponsePromises = {};
|
|
558
|
+
const handleOpen = async () => {
|
|
559
|
+
if (state.__type !== "connecting") {
|
|
560
|
+
console.warn(`Expected adapter state to be \`connecting\` at the moment the websocket opens. Got \`${state.__type}\`.`);
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
socket.removeEventListener("open", handleOpen);
|
|
564
|
+
const { associationKeypair } = state;
|
|
565
|
+
const ecdhKeypair = await generateECDHKeypair();
|
|
566
|
+
socket.send(await createHelloReq(ecdhKeypair.publicKey, associationKeypair.privateKey));
|
|
567
|
+
state = {
|
|
568
|
+
__type: "hello_req_sent",
|
|
569
|
+
associationPublicKey: associationKeypair.publicKey,
|
|
570
|
+
ecdhPrivateKey: ecdhKeypair.privateKey
|
|
571
|
+
};
|
|
572
|
+
};
|
|
573
|
+
const handleClose = (evt) => {
|
|
574
|
+
if (evt.wasClean) state = { __type: "disconnected" };
|
|
575
|
+
else reject(new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_SESSION_CLOSED, `The wallet session dropped unexpectedly (${evt.code}: ${evt.reason}).`, { closeEvent: evt }));
|
|
576
|
+
disposeSocket();
|
|
577
|
+
};
|
|
578
|
+
const handleError = async (_evt) => {
|
|
579
|
+
disposeSocket();
|
|
580
|
+
if (Date.now() - connectionStartTime >= WEBSOCKET_CONNECTION_CONFIG.timeoutMs) reject(new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_SESSION_TIMEOUT, `Failed to connect to the wallet websocket at ${websocketURL}.`));
|
|
581
|
+
else {
|
|
582
|
+
await new Promise((resolve) => {
|
|
583
|
+
const retryDelayMs = getNextRetryDelayMs();
|
|
584
|
+
retryWaitTimeoutId = window.setTimeout(resolve, retryDelayMs);
|
|
585
|
+
});
|
|
586
|
+
attemptSocketConnection();
|
|
587
|
+
}
|
|
588
|
+
};
|
|
589
|
+
const handleMessage = async (evt) => {
|
|
590
|
+
const responseBuffer = await evt.data.arrayBuffer();
|
|
591
|
+
switch (state.__type) {
|
|
592
|
+
case "connecting":
|
|
593
|
+
if (responseBuffer.byteLength !== 0) throw new Error("Encountered unexpected message while connecting");
|
|
594
|
+
const ecdhKeypair = await generateECDHKeypair();
|
|
595
|
+
socket.send(await createHelloReq(ecdhKeypair.publicKey, associationKeypair.privateKey));
|
|
596
|
+
state = {
|
|
597
|
+
__type: "hello_req_sent",
|
|
598
|
+
associationPublicKey: associationKeypair.publicKey,
|
|
599
|
+
ecdhPrivateKey: ecdhKeypair.privateKey
|
|
600
|
+
};
|
|
601
|
+
break;
|
|
602
|
+
case "connected":
|
|
603
|
+
try {
|
|
604
|
+
const sequenceNumber = getSequenceNumberFromByteArray(responseBuffer.slice(0, 4));
|
|
605
|
+
if (sequenceNumber !== lastKnownInboundSequenceNumber + 1) throw new Error("Encrypted message has invalid sequence number");
|
|
606
|
+
lastKnownInboundSequenceNumber = sequenceNumber;
|
|
607
|
+
const jsonRpcMessage = await decryptJsonRpcMessage(responseBuffer, state.sharedSecret);
|
|
608
|
+
const responsePromise = jsonRpcResponsePromises[jsonRpcMessage.id];
|
|
609
|
+
delete jsonRpcResponsePromises[jsonRpcMessage.id];
|
|
610
|
+
responsePromise.resolve(jsonRpcMessage.result);
|
|
611
|
+
} catch (e) {
|
|
612
|
+
if (e instanceof SolanaMobileWalletAdapterProtocolError) {
|
|
613
|
+
const responsePromise = jsonRpcResponsePromises[e.jsonRpcMessageId];
|
|
614
|
+
delete jsonRpcResponsePromises[e.jsonRpcMessageId];
|
|
615
|
+
responsePromise.reject(e);
|
|
616
|
+
} else throw e;
|
|
617
|
+
}
|
|
618
|
+
break;
|
|
619
|
+
case "hello_req_sent": {
|
|
620
|
+
if (responseBuffer.byteLength === 0) {
|
|
621
|
+
const ecdhKeypair = await generateECDHKeypair();
|
|
622
|
+
socket.send(await createHelloReq(ecdhKeypair.publicKey, associationKeypair.privateKey));
|
|
623
|
+
state = {
|
|
624
|
+
__type: "hello_req_sent",
|
|
625
|
+
associationPublicKey: associationKeypair.publicKey,
|
|
626
|
+
ecdhPrivateKey: ecdhKeypair.privateKey
|
|
627
|
+
};
|
|
628
|
+
break;
|
|
629
|
+
}
|
|
630
|
+
const sharedSecret = await parseHelloRsp(responseBuffer, state.associationPublicKey, state.ecdhPrivateKey);
|
|
631
|
+
const sessionPropertiesBuffer = responseBuffer.slice(65);
|
|
632
|
+
const sessionProperties = sessionPropertiesBuffer.byteLength !== 0 ? await (async () => {
|
|
633
|
+
const sequenceNumber = getSequenceNumberFromByteArray(sessionPropertiesBuffer.slice(0, 4));
|
|
634
|
+
if (sequenceNumber !== lastKnownInboundSequenceNumber + 1) throw new Error("Encrypted message has invalid sequence number");
|
|
635
|
+
lastKnownInboundSequenceNumber = sequenceNumber;
|
|
636
|
+
return parseSessionProps(sessionPropertiesBuffer, sharedSecret);
|
|
637
|
+
})() : { protocol_version: "legacy" };
|
|
638
|
+
state = {
|
|
639
|
+
__type: "connected",
|
|
640
|
+
sharedSecret,
|
|
641
|
+
sessionProperties
|
|
642
|
+
};
|
|
643
|
+
const wallet = createMobileWalletProxy(sessionProperties.protocol_version, async (method, params) => {
|
|
644
|
+
const id = nextJsonRpcMessageId++;
|
|
645
|
+
socket.send(await encryptJsonRpcMessage({
|
|
646
|
+
id,
|
|
647
|
+
jsonrpc: "2.0",
|
|
648
|
+
method,
|
|
649
|
+
params: params ?? {}
|
|
650
|
+
}, sharedSecret));
|
|
651
|
+
return new Promise((resolve, reject) => {
|
|
652
|
+
jsonRpcResponsePromises[id] = {
|
|
653
|
+
resolve(result) {
|
|
654
|
+
switch (method) {
|
|
655
|
+
case "authorize":
|
|
656
|
+
case "reauthorize": {
|
|
657
|
+
const { wallet_uri_base } = result;
|
|
658
|
+
if (wallet_uri_base != null) try {
|
|
659
|
+
assertSecureEndpointSpecificURI(wallet_uri_base);
|
|
660
|
+
} catch (e) {
|
|
661
|
+
reject(e);
|
|
662
|
+
return;
|
|
663
|
+
}
|
|
664
|
+
break;
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
resolve(result);
|
|
668
|
+
},
|
|
669
|
+
reject
|
|
670
|
+
};
|
|
671
|
+
});
|
|
672
|
+
});
|
|
673
|
+
sessionEstablished = true;
|
|
674
|
+
try {
|
|
675
|
+
resolve(wallet);
|
|
676
|
+
} catch (e) {
|
|
677
|
+
reject(e);
|
|
678
|
+
}
|
|
679
|
+
break;
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
};
|
|
683
|
+
handleForceClose = () => {
|
|
684
|
+
socket.removeEventListener("message", handleMessage);
|
|
685
|
+
disposeSocket();
|
|
686
|
+
if (!sessionEstablished) reject(new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_SESSION_CLOSED, `The wallet session was closed before connection.`, { closeEvent: new CloseEvent("socket was closed before connection") }));
|
|
687
|
+
};
|
|
688
|
+
let disposeSocket;
|
|
689
|
+
let retryWaitTimeoutId;
|
|
690
|
+
const attemptSocketConnection = () => {
|
|
691
|
+
if (disposeSocket) disposeSocket();
|
|
692
|
+
state = {
|
|
693
|
+
__type: "connecting",
|
|
694
|
+
associationKeypair
|
|
695
|
+
};
|
|
696
|
+
if (connectionStartTime === void 0) connectionStartTime = Date.now();
|
|
697
|
+
socket = new WebSocket(websocketURL, [WEBSOCKET_PROTOCOL_BINARY]);
|
|
698
|
+
socket.addEventListener("open", handleOpen);
|
|
699
|
+
socket.addEventListener("close", handleClose);
|
|
700
|
+
socket.addEventListener("error", handleError);
|
|
701
|
+
socket.addEventListener("message", handleMessage);
|
|
702
|
+
disposeSocket = () => {
|
|
703
|
+
window.clearTimeout(retryWaitTimeoutId);
|
|
704
|
+
socket.removeEventListener("open", handleOpen);
|
|
705
|
+
socket.removeEventListener("close", handleClose);
|
|
706
|
+
socket.removeEventListener("error", handleError);
|
|
707
|
+
socket.removeEventListener("message", handleMessage);
|
|
708
|
+
};
|
|
709
|
+
};
|
|
710
|
+
attemptSocketConnection();
|
|
711
|
+
})
|
|
712
|
+
};
|
|
816
713
|
}
|
|
817
714
|
async function startRemoteScenario(config) {
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
resolve(result) {
|
|
1008
|
-
switch (method) {
|
|
1009
|
-
case 'authorize':
|
|
1010
|
-
case 'reauthorize': {
|
|
1011
|
-
const { wallet_uri_base } = result;
|
|
1012
|
-
if (wallet_uri_base != null) {
|
|
1013
|
-
try {
|
|
1014
|
-
assertSecureEndpointSpecificURI(wallet_uri_base);
|
|
1015
|
-
}
|
|
1016
|
-
catch (e) {
|
|
1017
|
-
reject(e);
|
|
1018
|
-
return;
|
|
1019
|
-
}
|
|
1020
|
-
}
|
|
1021
|
-
break;
|
|
1022
|
-
}
|
|
1023
|
-
}
|
|
1024
|
-
resolve(result);
|
|
1025
|
-
},
|
|
1026
|
-
reject,
|
|
1027
|
-
};
|
|
1028
|
-
});
|
|
1029
|
-
});
|
|
1030
|
-
sessionEstablished = true;
|
|
1031
|
-
try {
|
|
1032
|
-
resolve(wallet);
|
|
1033
|
-
}
|
|
1034
|
-
catch (e) {
|
|
1035
|
-
reject(e);
|
|
1036
|
-
}
|
|
1037
|
-
break;
|
|
1038
|
-
}
|
|
1039
|
-
}
|
|
1040
|
-
};
|
|
1041
|
-
socket.addEventListener('message', handleMessage);
|
|
1042
|
-
handleClose = () => {
|
|
1043
|
-
socket.removeEventListener('message', handleMessage);
|
|
1044
|
-
disposeSocket();
|
|
1045
|
-
if (!sessionEstablished) {
|
|
1046
|
-
reject(new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_SESSION_CLOSED, `The wallet session was closed before connection.`, { closeEvent: new CloseEvent('socket was closed before connection') }));
|
|
1047
|
-
}
|
|
1048
|
-
};
|
|
1049
|
-
}) };
|
|
1050
|
-
}
|
|
1051
|
-
|
|
715
|
+
assertSecureContext();
|
|
716
|
+
const associationKeypair = await generateAssociationKeypair();
|
|
717
|
+
const websocketURL = `wss://${config?.remoteHostAuthority}/reflect`;
|
|
718
|
+
let connectionStartTime;
|
|
719
|
+
const getNextRetryDelayMs = (() => {
|
|
720
|
+
const schedule = [...WEBSOCKET_CONNECTION_CONFIG.retryDelayScheduleMs];
|
|
721
|
+
return () => schedule.length > 1 ? schedule.shift() : schedule[0];
|
|
722
|
+
})();
|
|
723
|
+
let nextJsonRpcMessageId = 1;
|
|
724
|
+
let lastKnownInboundSequenceNumber = 0;
|
|
725
|
+
let encoding;
|
|
726
|
+
let state = { __type: "disconnected" };
|
|
727
|
+
let socket;
|
|
728
|
+
let disposeSocket;
|
|
729
|
+
let decodeBytes = async (evt) => {
|
|
730
|
+
if (encoding == "base64") return toUint8Array(await evt.data).buffer;
|
|
731
|
+
else return await evt.data.arrayBuffer();
|
|
732
|
+
};
|
|
733
|
+
const associationUrl = await new Promise((resolve, reject) => {
|
|
734
|
+
const handleOpen = async () => {
|
|
735
|
+
if (state.__type !== "connecting") {
|
|
736
|
+
console.warn(`Expected adapter state to be \`connecting\` at the moment the websocket opens. Got \`${state.__type}\`.`);
|
|
737
|
+
return;
|
|
738
|
+
}
|
|
739
|
+
if (socket.protocol.includes(WEBSOCKET_PROTOCOL_BASE64)) encoding = "base64";
|
|
740
|
+
else encoding = "binary";
|
|
741
|
+
socket.removeEventListener("open", handleOpen);
|
|
742
|
+
};
|
|
743
|
+
const handleClose = (evt) => {
|
|
744
|
+
if (evt.wasClean) state = { __type: "disconnected" };
|
|
745
|
+
else reject(new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_SESSION_CLOSED, `The wallet session dropped unexpectedly (${evt.code}: ${evt.reason}).`, { closeEvent: evt }));
|
|
746
|
+
disposeSocket();
|
|
747
|
+
};
|
|
748
|
+
const handleError = async (_evt) => {
|
|
749
|
+
disposeSocket();
|
|
750
|
+
if (Date.now() - connectionStartTime >= WEBSOCKET_CONNECTION_CONFIG.timeoutMs) reject(new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_SESSION_TIMEOUT, `Failed to connect to the wallet websocket at ${websocketURL}.`));
|
|
751
|
+
else {
|
|
752
|
+
await new Promise((resolve) => {
|
|
753
|
+
const retryDelayMs = getNextRetryDelayMs();
|
|
754
|
+
retryWaitTimeoutId = window.setTimeout(resolve, retryDelayMs);
|
|
755
|
+
});
|
|
756
|
+
attemptSocketConnection();
|
|
757
|
+
}
|
|
758
|
+
};
|
|
759
|
+
const handleReflectorIdMessage = async (evt) => {
|
|
760
|
+
const responseBuffer = await decodeBytes(evt);
|
|
761
|
+
if (state.__type === "connecting") {
|
|
762
|
+
if (responseBuffer.byteLength == 0) throw new Error("Encountered unexpected message while connecting");
|
|
763
|
+
const reflectorId = getReflectorIdFromByteArray(responseBuffer);
|
|
764
|
+
state = {
|
|
765
|
+
__type: "reflector_id_received",
|
|
766
|
+
reflectorId
|
|
767
|
+
};
|
|
768
|
+
const associationUrl = await getRemoteAssociateAndroidIntentURL(associationKeypair.publicKey, config.remoteHostAuthority, reflectorId, config?.baseUri);
|
|
769
|
+
socket.removeEventListener("message", handleReflectorIdMessage);
|
|
770
|
+
resolve(associationUrl);
|
|
771
|
+
}
|
|
772
|
+
};
|
|
773
|
+
let retryWaitTimeoutId;
|
|
774
|
+
const attemptSocketConnection = () => {
|
|
775
|
+
if (disposeSocket) disposeSocket();
|
|
776
|
+
state = {
|
|
777
|
+
__type: "connecting",
|
|
778
|
+
associationKeypair
|
|
779
|
+
};
|
|
780
|
+
if (connectionStartTime === void 0) connectionStartTime = Date.now();
|
|
781
|
+
socket = new WebSocket(websocketURL, [WEBSOCKET_PROTOCOL_BINARY, WEBSOCKET_PROTOCOL_BASE64]);
|
|
782
|
+
socket.addEventListener("open", handleOpen);
|
|
783
|
+
socket.addEventListener("close", handleClose);
|
|
784
|
+
socket.addEventListener("error", handleError);
|
|
785
|
+
socket.addEventListener("message", handleReflectorIdMessage);
|
|
786
|
+
disposeSocket = () => {
|
|
787
|
+
window.clearTimeout(retryWaitTimeoutId);
|
|
788
|
+
socket.removeEventListener("open", handleOpen);
|
|
789
|
+
socket.removeEventListener("close", handleClose);
|
|
790
|
+
socket.removeEventListener("error", handleError);
|
|
791
|
+
socket.removeEventListener("message", handleReflectorIdMessage);
|
|
792
|
+
};
|
|
793
|
+
};
|
|
794
|
+
attemptSocketConnection();
|
|
795
|
+
});
|
|
796
|
+
let sessionEstablished = false;
|
|
797
|
+
let handleClose;
|
|
798
|
+
return {
|
|
799
|
+
associationUrl,
|
|
800
|
+
close: () => {
|
|
801
|
+
socket.close();
|
|
802
|
+
handleClose();
|
|
803
|
+
},
|
|
804
|
+
wallet: new Promise((resolve, reject) => {
|
|
805
|
+
const jsonRpcResponsePromises = {};
|
|
806
|
+
const handleMessage = async (evt) => {
|
|
807
|
+
const responseBuffer = await decodeBytes(evt);
|
|
808
|
+
switch (state.__type) {
|
|
809
|
+
case "reflector_id_received":
|
|
810
|
+
if (responseBuffer.byteLength !== 0) throw new Error("Encountered unexpected message while awaiting reflection");
|
|
811
|
+
const ecdhKeypair = await generateECDHKeypair();
|
|
812
|
+
const binaryMsg = await createHelloReq(ecdhKeypair.publicKey, associationKeypair.privateKey);
|
|
813
|
+
if (encoding == "base64") socket.send(fromUint8Array$1(binaryMsg));
|
|
814
|
+
else socket.send(binaryMsg);
|
|
815
|
+
state = {
|
|
816
|
+
__type: "hello_req_sent",
|
|
817
|
+
associationPublicKey: associationKeypair.publicKey,
|
|
818
|
+
ecdhPrivateKey: ecdhKeypair.privateKey
|
|
819
|
+
};
|
|
820
|
+
break;
|
|
821
|
+
case "connected":
|
|
822
|
+
try {
|
|
823
|
+
const sequenceNumber = getSequenceNumberFromByteArray(responseBuffer.slice(0, 4));
|
|
824
|
+
if (sequenceNumber !== lastKnownInboundSequenceNumber + 1) throw new Error("Encrypted message has invalid sequence number");
|
|
825
|
+
lastKnownInboundSequenceNumber = sequenceNumber;
|
|
826
|
+
const jsonRpcMessage = await decryptJsonRpcMessage(responseBuffer, state.sharedSecret);
|
|
827
|
+
const responsePromise = jsonRpcResponsePromises[jsonRpcMessage.id];
|
|
828
|
+
delete jsonRpcResponsePromises[jsonRpcMessage.id];
|
|
829
|
+
responsePromise.resolve(jsonRpcMessage.result);
|
|
830
|
+
} catch (e) {
|
|
831
|
+
if (e instanceof SolanaMobileWalletAdapterProtocolError) {
|
|
832
|
+
const responsePromise = jsonRpcResponsePromises[e.jsonRpcMessageId];
|
|
833
|
+
delete jsonRpcResponsePromises[e.jsonRpcMessageId];
|
|
834
|
+
responsePromise.reject(e);
|
|
835
|
+
} else throw e;
|
|
836
|
+
}
|
|
837
|
+
break;
|
|
838
|
+
case "hello_req_sent": {
|
|
839
|
+
const sharedSecret = await parseHelloRsp(responseBuffer, state.associationPublicKey, state.ecdhPrivateKey);
|
|
840
|
+
const sessionPropertiesBuffer = responseBuffer.slice(65);
|
|
841
|
+
const sessionProperties = sessionPropertiesBuffer.byteLength !== 0 ? await (async () => {
|
|
842
|
+
const sequenceNumber = getSequenceNumberFromByteArray(sessionPropertiesBuffer.slice(0, 4));
|
|
843
|
+
if (sequenceNumber !== lastKnownInboundSequenceNumber + 1) throw new Error("Encrypted message has invalid sequence number");
|
|
844
|
+
lastKnownInboundSequenceNumber = sequenceNumber;
|
|
845
|
+
return parseSessionProps(sessionPropertiesBuffer, sharedSecret);
|
|
846
|
+
})() : { protocol_version: "legacy" };
|
|
847
|
+
state = {
|
|
848
|
+
__type: "connected",
|
|
849
|
+
sharedSecret,
|
|
850
|
+
sessionProperties
|
|
851
|
+
};
|
|
852
|
+
const wallet = createMobileWalletProxy(sessionProperties.protocol_version, async (method, params) => {
|
|
853
|
+
const id = nextJsonRpcMessageId++;
|
|
854
|
+
const binaryMsg = await encryptJsonRpcMessage({
|
|
855
|
+
id,
|
|
856
|
+
jsonrpc: "2.0",
|
|
857
|
+
method,
|
|
858
|
+
params: params ?? {}
|
|
859
|
+
}, sharedSecret);
|
|
860
|
+
if (encoding == "base64") socket.send(fromUint8Array$1(binaryMsg));
|
|
861
|
+
else socket.send(binaryMsg);
|
|
862
|
+
return new Promise((resolve, reject) => {
|
|
863
|
+
jsonRpcResponsePromises[id] = {
|
|
864
|
+
resolve(result) {
|
|
865
|
+
switch (method) {
|
|
866
|
+
case "authorize":
|
|
867
|
+
case "reauthorize": {
|
|
868
|
+
const { wallet_uri_base } = result;
|
|
869
|
+
if (wallet_uri_base != null) try {
|
|
870
|
+
assertSecureEndpointSpecificURI(wallet_uri_base);
|
|
871
|
+
} catch (e) {
|
|
872
|
+
reject(e);
|
|
873
|
+
return;
|
|
874
|
+
}
|
|
875
|
+
break;
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
resolve(result);
|
|
879
|
+
},
|
|
880
|
+
reject
|
|
881
|
+
};
|
|
882
|
+
});
|
|
883
|
+
});
|
|
884
|
+
sessionEstablished = true;
|
|
885
|
+
try {
|
|
886
|
+
resolve(wallet);
|
|
887
|
+
} catch (e) {
|
|
888
|
+
reject(e);
|
|
889
|
+
}
|
|
890
|
+
break;
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
};
|
|
894
|
+
socket.addEventListener("message", handleMessage);
|
|
895
|
+
handleClose = () => {
|
|
896
|
+
socket.removeEventListener("message", handleMessage);
|
|
897
|
+
disposeSocket();
|
|
898
|
+
if (!sessionEstablished) reject(new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_SESSION_CLOSED, `The wallet session was closed before connection.`, { closeEvent: new CloseEvent("socket was closed before connection") }));
|
|
899
|
+
};
|
|
900
|
+
})
|
|
901
|
+
};
|
|
902
|
+
}
|
|
903
|
+
//#endregion
|
|
1052
904
|
export { SolanaCloneAuthorization, SolanaMobileWalletAdapterError, SolanaMobileWalletAdapterErrorCode, SolanaMobileWalletAdapterProtocolError, SolanaMobileWalletAdapterProtocolErrorCode, SolanaSignInWithSolana, SolanaSignTransactions, startRemoteScenario, startScenario, transact };
|
|
905
|
+
|
|
906
|
+
//# sourceMappingURL=index.browser.js.map
|