@solana-mobile/mobile-wallet-adapter-protocol 2.2.4 → 2.2.6

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.
@@ -1,10 +1,9 @@
1
1
  'use strict';
2
2
 
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
3
  var reactNative = require('react-native');
6
- var walletStandardUtil = require('@solana/wallet-standard-util');
7
4
  var jsBase64 = require('js-base64');
5
+ var walletStandardUtil = require('@solana/wallet-standard-util');
6
+ var codecsStrings = require('@solana/codecs-strings');
8
7
 
9
8
  // Typescript `enums` thwart tree-shaking. See https://bargsten.org/jsts/enums/
10
9
  const SolanaMobileWalletAdapterErrorCode = {
@@ -17,8 +16,12 @@ const SolanaMobileWalletAdapterErrorCode = {
17
16
  ERROR_WALLET_NOT_FOUND: 'ERROR_WALLET_NOT_FOUND',
18
17
  ERROR_INVALID_PROTOCOL_VERSION: 'ERROR_INVALID_PROTOCOL_VERSION',
19
18
  ERROR_BROWSER_NOT_SUPPORTED: 'ERROR_BROWSER_NOT_SUPPORTED',
19
+ ERROR_LOOPBACK_ACCESS_BLOCKED: 'ERROR_LOOPBACK_ACCESS_BLOCKED',
20
+ ERROR_ASSOCIATION_CANCELLED: 'ERROR_ASSOCIATION_CANCELLED',
20
21
  };
21
22
  class SolanaMobileWalletAdapterError extends Error {
23
+ data;
24
+ code;
22
25
  constructor(...args) {
23
26
  const [code, message, data] = args;
24
27
  super(message);
@@ -38,6 +41,9 @@ const SolanaMobileWalletAdapterProtocolErrorCode = {
38
41
  ERROR_ATTEST_ORIGIN_ANDROID: -100,
39
42
  };
40
43
  class SolanaMobileWalletAdapterProtocolError extends Error {
44
+ data;
45
+ code;
46
+ jsonRpcMessageId;
41
47
  constructor(...args) {
42
48
  const [jsonRpcMessageId, code, message, data] = args;
43
49
  super(message);
@@ -48,38 +54,16 @@ class SolanaMobileWalletAdapterProtocolError extends Error {
48
54
  }
49
55
  }
50
56
 
51
- /******************************************************************************
52
- Copyright (c) Microsoft Corporation.
53
-
54
- Permission to use, copy, modify, and/or distribute this software for any
55
- purpose with or without fee is hereby granted.
56
-
57
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
58
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
59
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
60
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
61
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
62
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
63
- PERFORMANCE OF THIS SOFTWARE.
64
- ***************************************************************************** */
65
-
66
- function __awaiter(thisArg, _arguments, P, generator) {
67
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
68
- return new (P || (P = Promise))(function (resolve, reject) {
69
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
70
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
71
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
72
- step((generator = generator.apply(thisArg, _arguments || [])).next());
73
- });
74
- }
75
-
76
57
  var NativeSolanaMobileWalletAdapter = reactNative.TurboModuleRegistry.getEnforcing('SolanaMobileWalletAdapter');
77
58
 
78
59
  function createSIWSMessage(payload) {
79
60
  return walletStandardUtil.createSignInMessageText(payload);
80
61
  }
81
- function createSIWSMessageBase64(payload) {
82
- return jsBase64.encode(createSIWSMessage(payload));
62
+ function createSIWSMessageBase64Url(payload) {
63
+ return jsBase64.encode(createSIWSMessage(payload))
64
+ .replace(/\+/g, '-')
65
+ .replace(/\//g, '_')
66
+ .replace(/=+$/, ''); // convert to base64url encoding;
83
67
  }
84
68
 
85
69
  // optional features
@@ -87,6 +71,13 @@ const SolanaSignTransactions = 'solana:signTransactions';
87
71
  const SolanaCloneAuthorization = 'solana:cloneAuthorization';
88
72
  const SolanaSignInWithSolana = 'solana:signInWithSolana';
89
73
 
74
+ function fromUint8Array(byteArray) {
75
+ return codecsStrings.getBase58Decoder().decode(byteArray);
76
+ }
77
+ function base64ToBase58(base64EncodedString) {
78
+ return fromUint8Array(jsBase64.toUint8Array(base64EncodedString));
79
+ }
80
+
90
81
  /**
91
82
  * Creates a {@link MobileWallet} proxy that handles backwards compatibility and API to RPC conversion.
92
83
  *
@@ -105,16 +96,14 @@ function createMobileWalletProxy(protocolVersion, protocolRequestHandler) {
105
96
  return null;
106
97
  }
107
98
  if (target[p] == null) {
108
- target[p] = function (inputParams) {
109
- return __awaiter(this, void 0, void 0, function* () {
110
- const { method, params } = handleMobileWalletRequest(p, inputParams, protocolVersion);
111
- const result = yield protocolRequestHandler(method, params);
112
- // if the request tried to sign in but the wallet did not return a sign in result, fallback on message signing
113
- if (method === 'authorize' && params.sign_in_payload && !result.sign_in_result) {
114
- result['sign_in_result'] = yield signInFallback(params.sign_in_payload, result, protocolRequestHandler);
115
- }
116
- return handleMobileWalletResponse(p, result, protocolVersion);
117
- });
99
+ target[p] = async function (inputParams) {
100
+ const { method, params } = handleMobileWalletRequest(p, inputParams, protocolVersion);
101
+ const result = await protocolRequestHandler(method, params);
102
+ // if the request tried to sign in but the wallet did not return a sign in result, fallback on message signing
103
+ if (method === 'authorize' && params.sign_in_payload && !result.sign_in_result) {
104
+ result['sign_in_result'] = await signInFallback(params.sign_in_payload, result, protocolRequestHandler);
105
+ }
106
+ return handleMobileWalletResponse(p, result, protocolVersion);
118
107
  };
119
108
  }
120
109
  return target[p];
@@ -218,42 +207,52 @@ function handleMobileWalletResponse(method, response, protocolVersion) {
218
207
  if (capabilities.supports_clone_authorization === true) {
219
208
  features.push(SolanaCloneAuthorization);
220
209
  }
221
- return Object.assign(Object.assign({}, capabilities), { features: features });
210
+ return {
211
+ ...capabilities,
212
+ features: features,
213
+ };
222
214
  }
223
215
  case 'v1': {
224
- return Object.assign(Object.assign({}, capabilities), { supports_sign_and_send_transactions: true, supports_clone_authorization: capabilities.features.includes(SolanaCloneAuthorization) });
216
+ return {
217
+ ...capabilities,
218
+ supports_sign_and_send_transactions: true,
219
+ supports_clone_authorization: capabilities.features.includes(SolanaCloneAuthorization)
220
+ };
225
221
  }
226
222
  }
227
223
  }
228
224
  }
229
225
  return response;
230
226
  }
231
- function signInFallback(signInPayload, authorizationResult, protocolRequestHandler) {
232
- var _a;
233
- return __awaiter(this, void 0, void 0, function* () {
234
- const domain = (_a = signInPayload.domain) !== null && _a !== void 0 ? _a : window.location.host;
235
- const address = authorizationResult.accounts[0].address;
236
- const siwsMessage = createSIWSMessageBase64(Object.assign(Object.assign({}, signInPayload), { domain, address }));
237
- const signMessageResult = yield protocolRequestHandler('sign_messages', {
238
- addresses: [address],
239
- payloads: [siwsMessage]
240
- });
241
- const signInResult = {
242
- address: address,
243
- signed_message: siwsMessage,
244
- signature: signMessageResult.signed_payloads[0].slice(siwsMessage.length)
245
- };
246
- return signInResult;
227
+ async function signInFallback(signInPayload, authorizationResult, protocolRequestHandler) {
228
+ const domain = signInPayload.domain ?? window.location.host;
229
+ const address = authorizationResult.accounts[0].address;
230
+ const siwsMessage = createSIWSMessageBase64Url({ ...signInPayload, domain, address: base64ToBase58(address) });
231
+ const signMessageResult = await protocolRequestHandler('sign_messages', {
232
+ addresses: [address],
233
+ payloads: [siwsMessage]
247
234
  });
235
+ const signedPayload = jsBase64.toUint8Array(signMessageResult.signed_payloads[0]);
236
+ const signedMessage = jsBase64.fromUint8Array(signedPayload.slice(0, signedPayload.length - 64));
237
+ const signature = jsBase64.fromUint8Array(signedPayload.slice(signedPayload.length - 64));
238
+ const signInResult = {
239
+ address: address,
240
+ // Workaround: some wallets have been observed to only reply with the message signature.
241
+ // This is non-compliant with the spec, but in the interest of maximizing compatibility,
242
+ // detect this case and reuse the original message.
243
+ signed_message: signedMessage.length == 0 ? siwsMessage : signedMessage,
244
+ signature
245
+ };
246
+ return signInResult;
248
247
  }
249
248
 
250
249
  reactNative.AppRegistry.registerHeadlessTask('SolanaMobileWalletAdapterSessionBackgroundTask', () => {
251
- return () => __awaiter(void 0, void 0, void 0, function* () {
250
+ return async () => {
252
251
  // This is a no-op task that is used to keep the app alive while the session is active.
253
252
  // The actual session management is handled in the native module.
254
253
  // This is necessary for the React Native Android implementation to work correctly.
255
254
  // The task is started before startActivityResult and stopped when the activity result callback is triggered
256
- });
255
+ };
257
256
  });
258
257
  const LINKING_ERROR = `The package 'solana-mobile-wallet-adapter-protocol' doesn't seem to be linked. Make sure: \n\n` +
259
258
  '- You rebuilt the app after installing the package\n' +
@@ -294,31 +293,29 @@ function handleError(e) {
294
293
  }
295
294
  throw e;
296
295
  }
297
- function transact(callback, config) {
298
- return __awaiter(this, void 0, void 0, function* () {
299
- let didSuccessfullyConnect = false;
300
- try {
301
- const sessionProperties = yield SolanaMobileWalletAdapter.startSession(config);
302
- didSuccessfullyConnect = true;
303
- const wallet = createMobileWalletProxy(sessionProperties.protocol_version, (method, params) => __awaiter(this, void 0, void 0, function* () {
304
- try {
305
- return SolanaMobileWalletAdapter.invoke(method, params);
306
- }
307
- catch (e) {
308
- return handleError(e);
309
- }
310
- }));
311
- return yield callback(wallet);
312
- }
313
- catch (e) {
314
- return handleError(e);
315
- }
316
- finally {
317
- if (didSuccessfullyConnect) {
318
- yield SolanaMobileWalletAdapter.endSession();
296
+ async function transact(callback, config) {
297
+ let didSuccessfullyConnect = false;
298
+ try {
299
+ const sessionProperties = await SolanaMobileWalletAdapter.startSession(config);
300
+ didSuccessfullyConnect = true;
301
+ const wallet = createMobileWalletProxy(sessionProperties.protocol_version, async (method, params) => {
302
+ try {
303
+ return SolanaMobileWalletAdapter.invoke(method, params);
304
+ }
305
+ catch (e) {
306
+ return handleError(e);
319
307
  }
308
+ });
309
+ return await callback(wallet);
310
+ }
311
+ catch (e) {
312
+ return handleError(e);
313
+ }
314
+ finally {
315
+ if (didSuccessfullyConnect) {
316
+ await SolanaMobileWalletAdapter.endSession();
320
317
  }
321
- });
318
+ }
322
319
  }
323
320
 
324
321
  exports.SolanaCloneAuthorization = SolanaCloneAuthorization;