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