@solana-mobile/mobile-wallet-adapter-protocol 2.0.1 → 2.1.0-alpha1
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/android/build.gradle +3 -3
- package/android/src/main/java/com/solanamobile/mobilewalletadapter/reactnative/SolanaMobileWalletAdapterModule.kt +17 -15
- package/lib/cjs/index.browser.js +290 -81
- package/lib/cjs/index.js +290 -81
- package/lib/cjs/index.native.js +180 -28
- package/lib/esm/index.browser.js +288 -82
- package/lib/esm/index.js +288 -82
- package/lib/types/index.browser.d.ts +68 -5
- package/lib/types/index.browser.d.ts.map +1 -1
- package/lib/types/index.d.ts +68 -5
- package/lib/types/index.d.ts.map +1 -1
- package/lib/types/index.native.d.ts +68 -5
- package/lib/types/index.native.d.ts.map +1 -1
- package/package.json +10 -2
- package/src/__forks__/react-native/base64Utils.ts +1 -0
- package/src/__forks__/react-native/transact.ts +92 -106
- package/src/base64Utils.ts +3 -0
- package/src/createMobileWalletProxy.ts +175 -0
- package/src/createSIWSMessage.ts +14 -0
- package/src/encryptedMessage.ts +60 -0
- package/src/errors.ts +95 -93
- package/src/getAssociateAndroidIntentURL.ts +57 -52
- package/src/jsonRpcMessage.ts +38 -81
- package/src/parseHelloRsp.ts +46 -44
- package/src/parseSessionProps.ts +33 -0
- package/src/transact.ts +266 -268
- package/src/types.ts +74 -4
package/lib/cjs/index.native.js
CHANGED
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
var reactNative = require('react-native');
|
|
6
|
+
var walletStandardUtil = require('@solana/wallet-standard-util');
|
|
7
|
+
var jsBase64 = require('js-base64');
|
|
6
8
|
|
|
7
9
|
// Typescript `enums` thwart tree-shaking. See https://bargsten.org/jsts/enums/
|
|
8
10
|
const SolanaMobileWalletAdapterErrorCode = {
|
|
@@ -12,6 +14,7 @@ const SolanaMobileWalletAdapterErrorCode = {
|
|
|
12
14
|
ERROR_SESSION_CLOSED: 'ERROR_SESSION_CLOSED',
|
|
13
15
|
ERROR_SESSION_TIMEOUT: 'ERROR_SESSION_TIMEOUT',
|
|
14
16
|
ERROR_WALLET_NOT_FOUND: 'ERROR_WALLET_NOT_FOUND',
|
|
17
|
+
ERROR_INVALID_PROTOCOL_VERSION: 'ERROR_INVALID_PROTOCOL_VERSION',
|
|
15
18
|
};
|
|
16
19
|
class SolanaMobileWalletAdapterError extends Error {
|
|
17
20
|
constructor(...args) {
|
|
@@ -68,6 +71,171 @@ function __awaiter(thisArg, _arguments, P, generator) {
|
|
|
68
71
|
});
|
|
69
72
|
}
|
|
70
73
|
|
|
74
|
+
function createSIWSMessage(payload) {
|
|
75
|
+
return walletStandardUtil.createSignInMessageText(payload);
|
|
76
|
+
}
|
|
77
|
+
function createSIWSMessageBase64(payload) {
|
|
78
|
+
return jsBase64.encode(createSIWSMessage(payload));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// optional features
|
|
82
|
+
const SolanaSignTransactions = 'solana:signTransactions';
|
|
83
|
+
const SolanaCloneAuthorization = 'solana:cloneAuthorization';
|
|
84
|
+
const SolanaSignInWithSolana = 'solana:signInWithSolana';
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Creates a {@link MobileWallet} proxy that handles backwards compatibility and API to RPC conversion.
|
|
88
|
+
*
|
|
89
|
+
* @param protocolVersion the protocol version in use for this session/request
|
|
90
|
+
* @param protocolRequestHandler callback function that handles sending the RPC request to the wallet endpoint.
|
|
91
|
+
* @returns a {@link MobileWallet} proxy
|
|
92
|
+
*/
|
|
93
|
+
function createMobileWalletProxy(protocolVersion, protocolRequestHandler) {
|
|
94
|
+
return new Proxy({}, {
|
|
95
|
+
get(target, p) {
|
|
96
|
+
if (target[p] == null) {
|
|
97
|
+
target[p] = function (inputParams) {
|
|
98
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
99
|
+
const { method, params } = handleMobileWalletRequest(p, inputParams, protocolVersion);
|
|
100
|
+
const result = yield protocolRequestHandler(method, params);
|
|
101
|
+
// if the request tried to sign in but the wallet did not return a sign in result, fallback on message signing
|
|
102
|
+
if (method === 'authorize' && params.sign_in_payload && !result.sign_in_result) {
|
|
103
|
+
result['sign_in_result'] = yield signInFallback(params.sign_in_payload, result, protocolRequestHandler);
|
|
104
|
+
}
|
|
105
|
+
return handleMobileWalletResponse(p, result, protocolVersion);
|
|
106
|
+
});
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
return target[p];
|
|
110
|
+
},
|
|
111
|
+
defineProperty() {
|
|
112
|
+
return false;
|
|
113
|
+
},
|
|
114
|
+
deleteProperty() {
|
|
115
|
+
return false;
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Handles all {@link MobileWallet} API requests and determines the correct MWA RPC method and params to call.
|
|
121
|
+
* This handles backwards compatibility, based on the provided @protocolVersion.
|
|
122
|
+
*
|
|
123
|
+
* @param methodName the name of {@link MobileWallet} method that was called
|
|
124
|
+
* @param methodParams the parameters that were passed to the method
|
|
125
|
+
* @param protocolVersion the protocol version in use for this session/request
|
|
126
|
+
* @returns the RPC request method and params that should be sent to the wallet endpoint
|
|
127
|
+
*/
|
|
128
|
+
function handleMobileWalletRequest(methodName, methodParams, protocolVersion) {
|
|
129
|
+
let params = methodParams;
|
|
130
|
+
let method = methodName
|
|
131
|
+
.toString()
|
|
132
|
+
.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`)
|
|
133
|
+
.toLowerCase();
|
|
134
|
+
switch (methodName) {
|
|
135
|
+
case 'authorize': {
|
|
136
|
+
let { chain } = params;
|
|
137
|
+
if (protocolVersion === 'legacy') {
|
|
138
|
+
switch (chain) {
|
|
139
|
+
case 'solana:testnet': {
|
|
140
|
+
chain = 'testnet';
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
case 'solana:devnet': {
|
|
144
|
+
chain = 'devnet';
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
case 'solana:mainnet': {
|
|
148
|
+
chain = 'mainnet-beta';
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
default: {
|
|
152
|
+
chain = params.cluster;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
params.cluster = chain;
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
switch (chain) {
|
|
159
|
+
case 'testnet':
|
|
160
|
+
case 'devnet': {
|
|
161
|
+
chain = `solana:${chain}`;
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
case 'mainnet-beta': {
|
|
165
|
+
chain = 'solana:mainnet';
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
params.chain = chain;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
case 'reauthorize': {
|
|
173
|
+
const { auth_token, identity } = params;
|
|
174
|
+
if (auth_token) {
|
|
175
|
+
switch (protocolVersion) {
|
|
176
|
+
case 'legacy': {
|
|
177
|
+
method = 'reauthorize';
|
|
178
|
+
params = { auth_token: auth_token, identity: identity };
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
181
|
+
default: {
|
|
182
|
+
method = 'authorize';
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
break;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return { method, params };
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
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
|
+
*/
|
|
200
|
+
function handleMobileWalletResponse(method, response, protocolVersion) {
|
|
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) {
|
|
208
|
+
features.push(SolanaCloneAuthorization);
|
|
209
|
+
}
|
|
210
|
+
return Object.assign(Object.assign({}, capabilities), { features: features });
|
|
211
|
+
}
|
|
212
|
+
case 'v1': {
|
|
213
|
+
return Object.assign(Object.assign({}, capabilities), { supports_sign_and_send_transactions: true, supports_clone_authorization: capabilities.features.includes(SolanaCloneAuthorization) });
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return response;
|
|
219
|
+
}
|
|
220
|
+
function signInFallback(signInPayload, authorizationResult, protocolRequestHandler) {
|
|
221
|
+
var _a;
|
|
222
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
223
|
+
const domain = (_a = signInPayload.domain) !== null && _a !== void 0 ? _a : window.location.host;
|
|
224
|
+
const address = authorizationResult.accounts[0].address;
|
|
225
|
+
const siwsMessage = createSIWSMessageBase64(Object.assign(Object.assign({}, signInPayload), { domain, address }));
|
|
226
|
+
const signMessageResult = yield protocolRequestHandler('sign_messages', {
|
|
227
|
+
addresses: [address],
|
|
228
|
+
payloads: [siwsMessage]
|
|
229
|
+
});
|
|
230
|
+
const signInResult = {
|
|
231
|
+
address: address,
|
|
232
|
+
signed_message: siwsMessage,
|
|
233
|
+
signature: signMessageResult.signed_payloads[0].slice(siwsMessage.length)
|
|
234
|
+
};
|
|
235
|
+
return signInResult;
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
|
|
71
239
|
const LINKING_ERROR = `The package 'solana-mobile-wallet-adapter-protocol' doesn't seem to be linked. Make sure: \n\n` +
|
|
72
240
|
'- You rebuilt the app after installing the package\n' +
|
|
73
241
|
'- If you are using Lerna workspaces\n' +
|
|
@@ -111,35 +279,16 @@ function transact(callback, config) {
|
|
|
111
279
|
return __awaiter(this, void 0, void 0, function* () {
|
|
112
280
|
let didSuccessfullyConnect = false;
|
|
113
281
|
try {
|
|
114
|
-
yield SolanaMobileWalletAdapter.startSession(config);
|
|
282
|
+
const sessionProperties = yield SolanaMobileWalletAdapter.startSession(config);
|
|
115
283
|
didSuccessfullyConnect = true;
|
|
116
|
-
const wallet =
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
125
|
-
try {
|
|
126
|
-
return yield SolanaMobileWalletAdapter.invoke(method, params);
|
|
127
|
-
}
|
|
128
|
-
catch (e) {
|
|
129
|
-
return handleError(e);
|
|
130
|
-
}
|
|
131
|
-
});
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
return target[p];
|
|
135
|
-
},
|
|
136
|
-
defineProperty() {
|
|
137
|
-
return false;
|
|
138
|
-
},
|
|
139
|
-
deleteProperty() {
|
|
140
|
-
return false;
|
|
141
|
-
},
|
|
142
|
-
});
|
|
284
|
+
const wallet = createMobileWalletProxy(sessionProperties.protocol_version, (method, params) => __awaiter(this, void 0, void 0, function* () {
|
|
285
|
+
try {
|
|
286
|
+
return SolanaMobileWalletAdapter.invoke(method, params);
|
|
287
|
+
}
|
|
288
|
+
catch (e) {
|
|
289
|
+
return handleError(e);
|
|
290
|
+
}
|
|
291
|
+
}));
|
|
143
292
|
return yield callback(wallet);
|
|
144
293
|
}
|
|
145
294
|
catch (e) {
|
|
@@ -153,8 +302,11 @@ function transact(callback, config) {
|
|
|
153
302
|
});
|
|
154
303
|
}
|
|
155
304
|
|
|
305
|
+
exports.SolanaCloneAuthorization = SolanaCloneAuthorization;
|
|
156
306
|
exports.SolanaMobileWalletAdapterError = SolanaMobileWalletAdapterError;
|
|
157
307
|
exports.SolanaMobileWalletAdapterErrorCode = SolanaMobileWalletAdapterErrorCode;
|
|
158
308
|
exports.SolanaMobileWalletAdapterProtocolError = SolanaMobileWalletAdapterProtocolError;
|
|
159
309
|
exports.SolanaMobileWalletAdapterProtocolErrorCode = SolanaMobileWalletAdapterProtocolErrorCode;
|
|
310
|
+
exports.SolanaSignInWithSolana = SolanaSignInWithSolana;
|
|
311
|
+
exports.SolanaSignTransactions = SolanaSignTransactions;
|
|
160
312
|
exports.transact = transact;
|