@solana-mobile/mobile-wallet-adapter-protocol 2.1.3 → 2.1.5
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 +158 -146
- package/android/gradle/wrapper/gradle-wrapper.properties +5 -5
- package/android/gradle.properties +5 -5
- package/android/src/main/java/com/solanamobile/mobilewalletadapter/reactnative/JSONSerializationUtils.kt +11 -9
- package/android/src/main/java/com/solanamobile/mobilewalletadapter/reactnative/SolanaMobileWalletAdapterModule.kt +201 -178
- package/android/src/main/java/com/solanamobile/mobilewalletadapter/reactnative/SolanaMobileWalletAdapterPackage.kt +26 -7
- package/android/src/newarch/SolanaMobileWalletAdapter.kt +7 -0
- package/android/src/oldarch/SolanaMobileWalletAdapter.kt +16 -0
- package/lib/cjs/index.browser.js +266 -5
- package/lib/cjs/index.js +266 -5
- package/lib/cjs/index.native.js +5 -2
- package/lib/esm/index.browser.js +266 -6
- package/lib/esm/index.js +266 -6
- package/lib/types/index.browser.d.ts +17 -1
- package/lib/types/index.browser.d.ts.map +1 -1
- package/lib/types/index.d.ts +17 -1
- package/lib/types/index.d.ts.map +1 -1
- package/lib/types/index.native.d.ts +17 -1
- package/lib/types/index.native.d.ts.map +1 -1
- package/package.json +70 -58
- package/.gitignore +0 -2
- package/android/.gitignore +0 -14
- package/src/__forks__/react-native/base64Utils.ts +0 -1
- package/src/__forks__/react-native/transact.ts +0 -92
- package/src/arrayBufferToBase64String.ts +0 -10
- package/src/associationPort.ts +0 -19
- package/src/base64Utils.ts +0 -3
- package/src/createHelloReq.ts +0 -12
- package/src/createMobileWalletProxy.ts +0 -175
- package/src/createSIWSMessage.ts +0 -14
- package/src/createSequenceNumberVector.ts +0 -11
- package/src/encryptedMessage.ts +0 -60
- package/src/errors.ts +0 -95
- package/src/generateAssociationKeypair.ts +0 -10
- package/src/generateECDHKeypair.ts +0 -10
- package/src/getAssociateAndroidIntentURL.ts +0 -57
- package/src/getJWS.ts +0 -19
- package/src/getStringWithURLUnsafeBase64CharactersReplaced.ts +0 -11
- package/src/index.ts +0 -3
- package/src/jsonRpcMessage.ts +0 -38
- package/src/parseHelloRsp.ts +0 -46
- package/src/parseSessionProps.ts +0 -33
- package/src/startSession.ts +0 -94
- package/src/transact.ts +0 -266
- package/src/types.ts +0 -181
- package/tsconfig.cjs.json +0 -7
- package/tsconfig.json +0 -8
package/package.json
CHANGED
|
@@ -1,58 +1,70 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@solana-mobile/mobile-wallet-adapter-protocol",
|
|
3
|
-
"description": "An implementation of the Solana Mobile Mobile Wallet Adapter protocol. Use this to open a session with a mobile wallet app, and to issue API calls to it.",
|
|
4
|
-
"version": "2.1.
|
|
5
|
-
"author": "Steven Luscher <steven.luscher@solanamobile.com>",
|
|
6
|
-
"repository":
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
"./
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
"
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
"
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
"
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
"@
|
|
49
|
-
"
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
"
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
}
|
|
58
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@solana-mobile/mobile-wallet-adapter-protocol",
|
|
3
|
+
"description": "An implementation of the Solana Mobile Mobile Wallet Adapter protocol. Use this to open a session with a mobile wallet app, and to issue API calls to it.",
|
|
4
|
+
"version": "2.1.5",
|
|
5
|
+
"author": "Steven Luscher <steven.luscher@solanamobile.com>",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/solana-mobile/mobile-wallet-adapter.git"
|
|
9
|
+
},
|
|
10
|
+
"license": "Apache-2.0",
|
|
11
|
+
"type": "module",
|
|
12
|
+
"sideEffects": false,
|
|
13
|
+
"main": "lib/cjs/index.js",
|
|
14
|
+
"react-native": "lib/cjs/index.native.js",
|
|
15
|
+
"module": "lib/esm/index.js",
|
|
16
|
+
"types": "lib/types/index.d.ts",
|
|
17
|
+
"browser": {
|
|
18
|
+
"./lib/cjs/index.js": "./lib/cjs/index.browser.js",
|
|
19
|
+
"./lib/esm/index.js": "./lib/esm/index.browser.js"
|
|
20
|
+
},
|
|
21
|
+
"exports": {
|
|
22
|
+
"./package.json": "./package.json",
|
|
23
|
+
".": {
|
|
24
|
+
"import": "./lib/esm/index.js",
|
|
25
|
+
"require": "./lib/cjs/index.js",
|
|
26
|
+
"types": "./lib/types/index.d.ts"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"android",
|
|
31
|
+
"!android/build",
|
|
32
|
+
"lib",
|
|
33
|
+
"LICENSE"
|
|
34
|
+
],
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public"
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"clean": "shx rm -rf lib/*",
|
|
40
|
+
"build": "yarn clean && rollup --config ../../rollup.config.ts --configPlugin rollup-plugin-ts",
|
|
41
|
+
"build:watch": "yarn clean && rollup --config ../../rollup.config.ts --configPlugin rollup-plugin-ts --watch",
|
|
42
|
+
"postbuild": "cross-env echo {\\\"type\\\":\\\"commonjs\\\"} | npx json > lib/cjs/package.json && echo {\\\"type\\\":\\\"module\\\"} | npx json > lib/esm/package.json",
|
|
43
|
+
"prepublishOnly": "agadoo"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@solana/wallet-standard": "^1.1.2",
|
|
47
|
+
"@solana/wallet-standard-util": "^1.1.1",
|
|
48
|
+
"@wallet-standard/core": "^1.0.3",
|
|
49
|
+
"js-base64": "^3.7.5"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@solana/web3.js": "^1.91.7",
|
|
53
|
+
"@types/react-native": "^0.69.3",
|
|
54
|
+
"agadoo": "^3.0.0",
|
|
55
|
+
"cross-env": "^7.0.3",
|
|
56
|
+
"shx": "^0.3.4"
|
|
57
|
+
},
|
|
58
|
+
"peerDependencies": {
|
|
59
|
+
"@solana/web3.js": "^1.58.0",
|
|
60
|
+
"react-native": ">0.69"
|
|
61
|
+
},
|
|
62
|
+
"codegenConfig": {
|
|
63
|
+
"name": "SolanaMobileWalletAdapter",
|
|
64
|
+
"type": "all",
|
|
65
|
+
"jsSrcsDir": "./src/codegenSpec",
|
|
66
|
+
"android": {
|
|
67
|
+
"javaPackageName": "com.solanamobile.mobilewalletadapter.reactnative"
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
package/.gitignore
DELETED
package/android/.gitignore
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { encode } from 'js-base64';
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import { NativeModules, Platform } from 'react-native';
|
|
2
|
-
|
|
3
|
-
import { SolanaMobileWalletAdapterError, SolanaMobileWalletAdapterProtocolError } from '../../errors.js';
|
|
4
|
-
import { MobileWallet, SessionProperties,WalletAssociationConfig } from '../../types.js';
|
|
5
|
-
import createMobileWalletProxy from '../../createMobileWalletProxy.js';
|
|
6
|
-
|
|
7
|
-
type ReactNativeError = Error & { code?: string; userInfo?: Record<string, unknown> };
|
|
8
|
-
|
|
9
|
-
const LINKING_ERROR =
|
|
10
|
-
`The package 'solana-mobile-wallet-adapter-protocol' doesn't seem to be linked. Make sure: \n\n` +
|
|
11
|
-
'- You rebuilt the app after installing the package\n' +
|
|
12
|
-
'- If you are using Lerna workspaces\n' +
|
|
13
|
-
' - You have added `@solana-mobile/mobile-wallet-adapter-protocol` as an explicit dependency, and\n' +
|
|
14
|
-
' - You have added `@solana-mobile/mobile-wallet-adapter-protocol` to the `nohoist` section of your package.json\n' +
|
|
15
|
-
'- You are not using Expo managed workflow\n';
|
|
16
|
-
|
|
17
|
-
const SolanaMobileWalletAdapter =
|
|
18
|
-
Platform.OS === 'android' && NativeModules.SolanaMobileWalletAdapter
|
|
19
|
-
? NativeModules.SolanaMobileWalletAdapter
|
|
20
|
-
: new Proxy(
|
|
21
|
-
{},
|
|
22
|
-
{
|
|
23
|
-
get() {
|
|
24
|
-
throw new Error(
|
|
25
|
-
Platform.OS !== 'android'
|
|
26
|
-
? 'The package `solana-mobile-wallet-adapter-protocol` is only compatible with React Native Android'
|
|
27
|
-
: LINKING_ERROR,
|
|
28
|
-
);
|
|
29
|
-
},
|
|
30
|
-
},
|
|
31
|
-
);
|
|
32
|
-
|
|
33
|
-
function getErrorMessage(e: ReactNativeError): string {
|
|
34
|
-
switch (e.code) {
|
|
35
|
-
case 'ERROR_WALLET_NOT_FOUND':
|
|
36
|
-
return 'Found no installed wallet that supports the mobile wallet protocol.';
|
|
37
|
-
default:
|
|
38
|
-
return e.message;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function handleError(e: any): never {
|
|
43
|
-
if (e instanceof Error) {
|
|
44
|
-
const reactNativeError: ReactNativeError = e;
|
|
45
|
-
switch (reactNativeError.code) {
|
|
46
|
-
case undefined:
|
|
47
|
-
throw e;
|
|
48
|
-
case 'JSON_RPC_ERROR': {
|
|
49
|
-
const details = reactNativeError.userInfo as Readonly<{ jsonRpcErrorCode: number }>;
|
|
50
|
-
throw new SolanaMobileWalletAdapterProtocolError(
|
|
51
|
-
0 /* jsonRpcMessageId */,
|
|
52
|
-
details.jsonRpcErrorCode,
|
|
53
|
-
e.message,
|
|
54
|
-
);
|
|
55
|
-
}
|
|
56
|
-
default:
|
|
57
|
-
throw new SolanaMobileWalletAdapterError<any>(
|
|
58
|
-
reactNativeError.code,
|
|
59
|
-
getErrorMessage(reactNativeError),
|
|
60
|
-
reactNativeError.userInfo,
|
|
61
|
-
);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
throw e;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export async function transact<TReturn>(
|
|
68
|
-
callback: (wallet: MobileWallet) => TReturn,
|
|
69
|
-
config?: WalletAssociationConfig,
|
|
70
|
-
): Promise<TReturn> {
|
|
71
|
-
let didSuccessfullyConnect = false;
|
|
72
|
-
try {
|
|
73
|
-
const sessionProperties: SessionProperties = await SolanaMobileWalletAdapter.startSession(config);
|
|
74
|
-
didSuccessfullyConnect = true;
|
|
75
|
-
const wallet = createMobileWalletProxy(sessionProperties.protocol_version,
|
|
76
|
-
async (method, params) => {
|
|
77
|
-
try {
|
|
78
|
-
return SolanaMobileWalletAdapter.invoke(method, params);
|
|
79
|
-
} catch (e) {
|
|
80
|
-
return handleError(e);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
);
|
|
84
|
-
return await callback(wallet);
|
|
85
|
-
} catch (e) {
|
|
86
|
-
return handleError(e);
|
|
87
|
-
} finally {
|
|
88
|
-
if (didSuccessfullyConnect) {
|
|
89
|
-
await SolanaMobileWalletAdapter.endSession();
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
// https://stackoverflow.com/a/9458996/802047
|
|
2
|
-
export default function arrayBufferToBase64String(buffer: ArrayBuffer) {
|
|
3
|
-
let binary = '';
|
|
4
|
-
const bytes = new Uint8Array(buffer);
|
|
5
|
-
const len = bytes.byteLength;
|
|
6
|
-
for (let ii = 0; ii < len; ii++) {
|
|
7
|
-
binary += String.fromCharCode(bytes[ii]);
|
|
8
|
-
}
|
|
9
|
-
return window.btoa(binary);
|
|
10
|
-
}
|
package/src/associationPort.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { SolanaMobileWalletAdapterError, SolanaMobileWalletAdapterErrorCode } from './errors.js';
|
|
2
|
-
|
|
3
|
-
declare const tag: unique symbol;
|
|
4
|
-
export type AssociationPort = number & { readonly [tag]: 'AssociationPort' };
|
|
5
|
-
|
|
6
|
-
export function getRandomAssociationPort(): AssociationPort {
|
|
7
|
-
return assertAssociationPort(49152 + Math.floor(Math.random() * (65535 - 49152 + 1)));
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export function assertAssociationPort(port: number): AssociationPort {
|
|
11
|
-
if (port < 49152 || port > 65535) {
|
|
12
|
-
throw new SolanaMobileWalletAdapterError(
|
|
13
|
-
SolanaMobileWalletAdapterErrorCode.ERROR_ASSOCIATION_PORT_OUT_OF_RANGE,
|
|
14
|
-
`Association port number must be between 49152 and 65535. ${port} given.`,
|
|
15
|
-
{ port },
|
|
16
|
-
);
|
|
17
|
-
}
|
|
18
|
-
return port as AssociationPort;
|
|
19
|
-
}
|
package/src/base64Utils.ts
DELETED
package/src/createHelloReq.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export default async function createHelloReq(ecdhPublicKey: CryptoKey, associationKeypairPrivateKey: CryptoKey) {
|
|
2
|
-
const publicKeyBuffer = await crypto.subtle.exportKey('raw', ecdhPublicKey);
|
|
3
|
-
const signatureBuffer = await crypto.subtle.sign(
|
|
4
|
-
{ hash: 'SHA-256', name: 'ECDSA' },
|
|
5
|
-
associationKeypairPrivateKey,
|
|
6
|
-
publicKeyBuffer,
|
|
7
|
-
);
|
|
8
|
-
const response = new Uint8Array(publicKeyBuffer.byteLength + signatureBuffer.byteLength);
|
|
9
|
-
response.set(new Uint8Array(publicKeyBuffer), 0);
|
|
10
|
-
response.set(new Uint8Array(signatureBuffer), publicKeyBuffer.byteLength);
|
|
11
|
-
return response;
|
|
12
|
-
}
|
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
import { createSIWSMessageBase64 } from "./createSIWSMessage";
|
|
2
|
-
import {
|
|
3
|
-
AuthorizationResult,
|
|
4
|
-
MobileWallet,
|
|
5
|
-
ProtocolVersion,
|
|
6
|
-
SignInPayload,
|
|
7
|
-
SignInResult,
|
|
8
|
-
SolanaCloneAuthorization,
|
|
9
|
-
SolanaSignTransactions
|
|
10
|
-
} from "./types";
|
|
11
|
-
import type { IdentifierArray } from "@wallet-standard/core";
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Creates a {@link MobileWallet} proxy that handles backwards compatibility and API to RPC conversion.
|
|
15
|
-
*
|
|
16
|
-
* @param protocolVersion the protocol version in use for this session/request
|
|
17
|
-
* @param protocolRequestHandler callback function that handles sending the RPC request to the wallet endpoint.
|
|
18
|
-
* @returns a {@link MobileWallet} proxy
|
|
19
|
-
*/
|
|
20
|
-
export default function createMobileWalletProxy<
|
|
21
|
-
TMethodName extends keyof MobileWallet,
|
|
22
|
-
TReturn extends Awaited<ReturnType<MobileWallet[TMethodName]>>
|
|
23
|
-
>(
|
|
24
|
-
protocolVersion: ProtocolVersion,
|
|
25
|
-
protocolRequestHandler: (method: string, params: Parameters<MobileWallet[TMethodName]>[0]) => Promise<any>
|
|
26
|
-
): MobileWallet {
|
|
27
|
-
return new Proxy<MobileWallet>({} as MobileWallet, {
|
|
28
|
-
get<TMethodName extends keyof MobileWallet>(target: MobileWallet, p: TMethodName) {
|
|
29
|
-
if (target[p] == null) {
|
|
30
|
-
target[p] = async function (inputParams: Parameters<MobileWallet[TMethodName]>[0]) {
|
|
31
|
-
const { method, params } = handleMobileWalletRequest(p, inputParams, protocolVersion);
|
|
32
|
-
const result = await protocolRequestHandler(method, params) as Awaited<ReturnType<MobileWallet[TMethodName]>>;
|
|
33
|
-
// if the request tried to sign in but the wallet did not return a sign in result, fallback on message signing
|
|
34
|
-
if (method === 'authorize' && (params as any).sign_in_payload && !(result as any).sign_in_result) {
|
|
35
|
-
(result as any)['sign_in_result'] = await signInFallback(
|
|
36
|
-
(params as Parameters<MobileWallet['authorize']>[0]).sign_in_payload as SignInPayload,
|
|
37
|
-
result as Awaited<ReturnType<MobileWallet['authorize']>>,
|
|
38
|
-
protocolRequestHandler
|
|
39
|
-
);
|
|
40
|
-
}
|
|
41
|
-
return handleMobileWalletResponse(p, result, protocolVersion) as TReturn;
|
|
42
|
-
} as MobileWallet[TMethodName];
|
|
43
|
-
}
|
|
44
|
-
return target[p];
|
|
45
|
-
},
|
|
46
|
-
defineProperty() {
|
|
47
|
-
return false;
|
|
48
|
-
},
|
|
49
|
-
deleteProperty() {
|
|
50
|
-
return false;
|
|
51
|
-
},
|
|
52
|
-
});
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Handles all {@link MobileWallet} API requests and determines the correct MWA RPC method and params to call.
|
|
57
|
-
* This handles backwards compatibility, based on the provided @protocolVersion.
|
|
58
|
-
*
|
|
59
|
-
* @param methodName the name of {@link MobileWallet} method that was called
|
|
60
|
-
* @param methodParams the parameters that were passed to the method
|
|
61
|
-
* @param protocolVersion the protocol version in use for this session/request
|
|
62
|
-
* @returns the RPC request method and params that should be sent to the wallet endpoint
|
|
63
|
-
*/
|
|
64
|
-
function handleMobileWalletRequest<TMethodName extends keyof MobileWallet>(
|
|
65
|
-
methodName: TMethodName,
|
|
66
|
-
methodParams: Parameters<MobileWallet[TMethodName]>[0],
|
|
67
|
-
protocolVersion: ProtocolVersion
|
|
68
|
-
) {
|
|
69
|
-
let params = methodParams;
|
|
70
|
-
let method: string = methodName
|
|
71
|
-
.toString()
|
|
72
|
-
.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`)
|
|
73
|
-
.toLowerCase();
|
|
74
|
-
switch (methodName) {
|
|
75
|
-
case 'authorize': {
|
|
76
|
-
let { chain } = params as Parameters<MobileWallet['authorize']>[0];
|
|
77
|
-
if (protocolVersion === 'legacy') {
|
|
78
|
-
switch (chain) {
|
|
79
|
-
case 'solana:testnet': { chain = 'testnet'; break; }
|
|
80
|
-
case 'solana:devnet': { chain = 'devnet'; break; }
|
|
81
|
-
case 'solana:mainnet': { chain = 'mainnet-beta'; break; }
|
|
82
|
-
default: { chain = (params as any).cluster; }
|
|
83
|
-
}
|
|
84
|
-
(params as any).cluster = chain;
|
|
85
|
-
} else {
|
|
86
|
-
switch (chain) {
|
|
87
|
-
case 'testnet':
|
|
88
|
-
case 'devnet': { chain = `solana:${chain}`; break; }
|
|
89
|
-
case 'mainnet-beta': { chain = 'solana:mainnet'; break; }
|
|
90
|
-
}
|
|
91
|
-
(params as Parameters<MobileWallet['authorize']>[0]).chain = chain;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
case 'reauthorize': {
|
|
95
|
-
const { auth_token, identity } = params as Parameters<MobileWallet['authorize' | 'reauthorize']>[0];
|
|
96
|
-
if (auth_token) {
|
|
97
|
-
switch (protocolVersion) {
|
|
98
|
-
case 'legacy': {
|
|
99
|
-
method = 'reauthorize';
|
|
100
|
-
params = { auth_token: auth_token, identity: identity };
|
|
101
|
-
break;
|
|
102
|
-
}
|
|
103
|
-
default: {
|
|
104
|
-
method = 'authorize';
|
|
105
|
-
break;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
break;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
return { method, params }
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Handles all {@link MobileWallet} API responses and modifies the response for backwards compatibility, if needed
|
|
117
|
-
*
|
|
118
|
-
* @param method the {@link MobileWallet} method that was called
|
|
119
|
-
* @param response the original response that was returned by the method call
|
|
120
|
-
* @param protocolVersion the protocol version in use for this session/request
|
|
121
|
-
* @returns the possibly modified response
|
|
122
|
-
*/
|
|
123
|
-
function handleMobileWalletResponse<TMethodName extends keyof MobileWallet>(
|
|
124
|
-
method: TMethodName,
|
|
125
|
-
response: Awaited<ReturnType<MobileWallet[TMethodName]>>,
|
|
126
|
-
protocolVersion: ProtocolVersion
|
|
127
|
-
): Awaited<ReturnType<MobileWallet[TMethodName]>> {
|
|
128
|
-
switch (method) {
|
|
129
|
-
case 'getCapabilities': {
|
|
130
|
-
const capabilities = response as Awaited<ReturnType<MobileWallet['getCapabilities']>>
|
|
131
|
-
switch (protocolVersion) {
|
|
132
|
-
case 'legacy': {
|
|
133
|
-
const features: `${string}:${string}`[] = [ SolanaSignTransactions ];
|
|
134
|
-
if (capabilities.supports_clone_authorization === true) {
|
|
135
|
-
features.push(SolanaCloneAuthorization);
|
|
136
|
-
}
|
|
137
|
-
return {
|
|
138
|
-
...capabilities,
|
|
139
|
-
features: features as IdentifierArray,
|
|
140
|
-
} as Awaited<ReturnType<MobileWallet[TMethodName]>>;
|
|
141
|
-
}
|
|
142
|
-
case 'v1': {
|
|
143
|
-
return {
|
|
144
|
-
...capabilities,
|
|
145
|
-
supports_sign_and_send_transactions: true,
|
|
146
|
-
supports_clone_authorization: capabilities.features.includes(SolanaCloneAuthorization)
|
|
147
|
-
} as Awaited<ReturnType<MobileWallet[TMethodName]>>;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
return response;
|
|
153
|
-
};
|
|
154
|
-
|
|
155
|
-
async function signInFallback(
|
|
156
|
-
signInPayload: SignInPayload,
|
|
157
|
-
authorizationResult: Awaited<ReturnType<MobileWallet['authorize']>>,
|
|
158
|
-
protocolRequestHandler: (method: string, params: Parameters<MobileWallet['signMessages']>[0]) => Promise<unknown>
|
|
159
|
-
) {
|
|
160
|
-
const domain = signInPayload.domain ?? window.location.host;
|
|
161
|
-
const address = (authorizationResult as AuthorizationResult).accounts[0].address;
|
|
162
|
-
const siwsMessage = createSIWSMessageBase64({ ...signInPayload, domain, address })
|
|
163
|
-
const signMessageResult = await (protocolRequestHandler('sign_messages',
|
|
164
|
-
{
|
|
165
|
-
addresses: [ address ],
|
|
166
|
-
payloads: [ siwsMessage ]
|
|
167
|
-
}
|
|
168
|
-
) as ReturnType<MobileWallet['signMessages']>);
|
|
169
|
-
const signInResult: SignInResult = {
|
|
170
|
-
address: address,
|
|
171
|
-
signed_message: siwsMessage,
|
|
172
|
-
signature: signMessageResult.signed_payloads[0].slice(siwsMessage.length)
|
|
173
|
-
};
|
|
174
|
-
return signInResult;
|
|
175
|
-
}
|
package/src/createSIWSMessage.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
SolanaSignInInputWithRequiredFields,
|
|
3
|
-
createSignInMessageText,
|
|
4
|
-
} from '@solana/wallet-standard-util';
|
|
5
|
-
import { SignInPayload } from './types';
|
|
6
|
-
import { encode } from './base64Utils';
|
|
7
|
-
|
|
8
|
-
export function createSIWSMessage(payload: SolanaSignInInputWithRequiredFields & SignInPayload): string {
|
|
9
|
-
return createSignInMessageText(payload);
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export function createSIWSMessageBase64(payload: SolanaSignInInputWithRequiredFields & SignInPayload): string {
|
|
13
|
-
return encode(createSIWSMessage(payload));
|
|
14
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export const SEQUENCE_NUMBER_BYTES = 4;
|
|
2
|
-
|
|
3
|
-
export default function createSequenceNumberVector(sequenceNumber: number): Uint8Array {
|
|
4
|
-
if (sequenceNumber >= 4294967296) {
|
|
5
|
-
throw new Error('Outbound sequence number overflow. The maximum sequence number is 32-bytes.');
|
|
6
|
-
}
|
|
7
|
-
const byteArray = new ArrayBuffer(SEQUENCE_NUMBER_BYTES);
|
|
8
|
-
const view = new DataView(byteArray);
|
|
9
|
-
view.setUint32(0, sequenceNumber, /* littleEndian */ false);
|
|
10
|
-
return new Uint8Array(byteArray);
|
|
11
|
-
}
|
package/src/encryptedMessage.ts
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import createSequenceNumberVector, { SEQUENCE_NUMBER_BYTES } from './createSequenceNumberVector.js';
|
|
2
|
-
import { SharedSecret } from './parseHelloRsp.js';
|
|
3
|
-
|
|
4
|
-
const INITIALIZATION_VECTOR_BYTES = 12;
|
|
5
|
-
export const ENCODED_PUBLIC_KEY_LENGTH_BYTES = 65;
|
|
6
|
-
|
|
7
|
-
export async function encryptMessage(
|
|
8
|
-
plaintext: string,
|
|
9
|
-
sequenceNumber: number,
|
|
10
|
-
sharedSecret: SharedSecret,
|
|
11
|
-
) {
|
|
12
|
-
const sequenceNumberVector = createSequenceNumberVector(sequenceNumber);
|
|
13
|
-
const initializationVector = new Uint8Array(INITIALIZATION_VECTOR_BYTES);
|
|
14
|
-
crypto.getRandomValues(initializationVector);
|
|
15
|
-
const ciphertext = await crypto.subtle.encrypt(
|
|
16
|
-
getAlgorithmParams(sequenceNumberVector, initializationVector),
|
|
17
|
-
sharedSecret,
|
|
18
|
-
new TextEncoder().encode(plaintext),
|
|
19
|
-
);
|
|
20
|
-
const response = new Uint8Array(
|
|
21
|
-
sequenceNumberVector.byteLength + initializationVector.byteLength + ciphertext.byteLength,
|
|
22
|
-
);
|
|
23
|
-
response.set(new Uint8Array(sequenceNumberVector), 0);
|
|
24
|
-
response.set(new Uint8Array(initializationVector), sequenceNumberVector.byteLength);
|
|
25
|
-
response.set(new Uint8Array(ciphertext), sequenceNumberVector.byteLength + initializationVector.byteLength);
|
|
26
|
-
return response;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export async function decryptMessage(message: ArrayBuffer, sharedSecret: SharedSecret) {
|
|
30
|
-
const sequenceNumberVector = message.slice(0, SEQUENCE_NUMBER_BYTES);
|
|
31
|
-
const initializationVector = message.slice(
|
|
32
|
-
SEQUENCE_NUMBER_BYTES,
|
|
33
|
-
SEQUENCE_NUMBER_BYTES + INITIALIZATION_VECTOR_BYTES,
|
|
34
|
-
);
|
|
35
|
-
const ciphertext = message.slice(SEQUENCE_NUMBER_BYTES + INITIALIZATION_VECTOR_BYTES);
|
|
36
|
-
const plaintextBuffer = await crypto.subtle.decrypt(
|
|
37
|
-
getAlgorithmParams(sequenceNumberVector, initializationVector),
|
|
38
|
-
sharedSecret,
|
|
39
|
-
ciphertext,
|
|
40
|
-
);
|
|
41
|
-
const plaintext = getUtf8Decoder().decode(plaintextBuffer);
|
|
42
|
-
return plaintext
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function getAlgorithmParams(sequenceNumber: ArrayBuffer, initializationVector: ArrayBuffer) {
|
|
46
|
-
return {
|
|
47
|
-
additionalData: sequenceNumber,
|
|
48
|
-
iv: initializationVector,
|
|
49
|
-
name: 'AES-GCM',
|
|
50
|
-
tagLength: 128, // 16 byte tag => 128 bits
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
let _utf8Decoder: TextDecoder | undefined;
|
|
55
|
-
function getUtf8Decoder() {
|
|
56
|
-
if (_utf8Decoder === undefined) {
|
|
57
|
-
_utf8Decoder = new TextDecoder('utf-8');
|
|
58
|
-
}
|
|
59
|
-
return _utf8Decoder;
|
|
60
|
-
}
|
package/src/errors.ts
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
// Typescript `enums` thwart tree-shaking. See https://bargsten.org/jsts/enums/
|
|
2
|
-
export const SolanaMobileWalletAdapterErrorCode = {
|
|
3
|
-
ERROR_ASSOCIATION_PORT_OUT_OF_RANGE: 'ERROR_ASSOCIATION_PORT_OUT_OF_RANGE',
|
|
4
|
-
ERROR_FORBIDDEN_WALLET_BASE_URL: 'ERROR_FORBIDDEN_WALLET_BASE_URL',
|
|
5
|
-
ERROR_SECURE_CONTEXT_REQUIRED: 'ERROR_SECURE_CONTEXT_REQUIRED',
|
|
6
|
-
ERROR_SESSION_CLOSED: 'ERROR_SESSION_CLOSED',
|
|
7
|
-
ERROR_SESSION_TIMEOUT: 'ERROR_SESSION_TIMEOUT',
|
|
8
|
-
ERROR_WALLET_NOT_FOUND: 'ERROR_WALLET_NOT_FOUND',
|
|
9
|
-
ERROR_INVALID_PROTOCOL_VERSION: 'ERROR_INVALID_PROTOCOL_VERSION',
|
|
10
|
-
} as const;
|
|
11
|
-
type SolanaMobileWalletAdapterErrorCodeEnum =
|
|
12
|
-
typeof SolanaMobileWalletAdapterErrorCode[keyof typeof SolanaMobileWalletAdapterErrorCode];
|
|
13
|
-
|
|
14
|
-
type ErrorDataTypeMap = {
|
|
15
|
-
[SolanaMobileWalletAdapterErrorCode.ERROR_ASSOCIATION_PORT_OUT_OF_RANGE]: {
|
|
16
|
-
port: number;
|
|
17
|
-
};
|
|
18
|
-
[SolanaMobileWalletAdapterErrorCode.ERROR_FORBIDDEN_WALLET_BASE_URL]: undefined;
|
|
19
|
-
[SolanaMobileWalletAdapterErrorCode.ERROR_SECURE_CONTEXT_REQUIRED]: undefined;
|
|
20
|
-
[SolanaMobileWalletAdapterErrorCode.ERROR_SESSION_CLOSED]: {
|
|
21
|
-
closeEvent: CloseEvent;
|
|
22
|
-
};
|
|
23
|
-
[SolanaMobileWalletAdapterErrorCode.ERROR_SESSION_TIMEOUT]: undefined;
|
|
24
|
-
[SolanaMobileWalletAdapterErrorCode.ERROR_WALLET_NOT_FOUND]: undefined;
|
|
25
|
-
[SolanaMobileWalletAdapterErrorCode.ERROR_INVALID_PROTOCOL_VERSION]: undefined;
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
export class SolanaMobileWalletAdapterError<TErrorCode extends SolanaMobileWalletAdapterErrorCodeEnum> extends Error {
|
|
29
|
-
data: ErrorDataTypeMap[TErrorCode] | undefined;
|
|
30
|
-
code: TErrorCode;
|
|
31
|
-
constructor(
|
|
32
|
-
...args: ErrorDataTypeMap[TErrorCode] extends Record<string, unknown>
|
|
33
|
-
? [code: TErrorCode, message: string, data: ErrorDataTypeMap[TErrorCode]]
|
|
34
|
-
: [code: TErrorCode, message: string]
|
|
35
|
-
) {
|
|
36
|
-
const [code, message, data] = args;
|
|
37
|
-
super(message);
|
|
38
|
-
this.code = code;
|
|
39
|
-
this.data = data;
|
|
40
|
-
this.name = 'SolanaMobileWalletAdapterError';
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
type JSONRPCErrorCode = number;
|
|
45
|
-
|
|
46
|
-
// Typescript `enums` thwart tree-shaking. See https://bargsten.org/jsts/enums/
|
|
47
|
-
export const SolanaMobileWalletAdapterProtocolErrorCode = {
|
|
48
|
-
// Keep these in sync with `mobilewalletadapter/common/ProtocolContract.java`.
|
|
49
|
-
ERROR_AUTHORIZATION_FAILED: -1,
|
|
50
|
-
ERROR_INVALID_PAYLOADS: -2,
|
|
51
|
-
ERROR_NOT_SIGNED: -3,
|
|
52
|
-
ERROR_NOT_SUBMITTED: -4,
|
|
53
|
-
ERROR_TOO_MANY_PAYLOADS: -5,
|
|
54
|
-
ERROR_ATTEST_ORIGIN_ANDROID: -100,
|
|
55
|
-
} as const;
|
|
56
|
-
type SolanaMobileWalletAdapterProtocolErrorCodeEnum =
|
|
57
|
-
typeof SolanaMobileWalletAdapterProtocolErrorCode[keyof typeof SolanaMobileWalletAdapterProtocolErrorCode];
|
|
58
|
-
|
|
59
|
-
type ProtocolErrorDataTypeMap = {
|
|
60
|
-
[SolanaMobileWalletAdapterProtocolErrorCode.ERROR_AUTHORIZATION_FAILED]: undefined;
|
|
61
|
-
[SolanaMobileWalletAdapterProtocolErrorCode.ERROR_INVALID_PAYLOADS]: undefined;
|
|
62
|
-
[SolanaMobileWalletAdapterProtocolErrorCode.ERROR_NOT_SIGNED]: undefined;
|
|
63
|
-
[SolanaMobileWalletAdapterProtocolErrorCode.ERROR_NOT_SUBMITTED]: undefined;
|
|
64
|
-
[SolanaMobileWalletAdapterProtocolErrorCode.ERROR_TOO_MANY_PAYLOADS]: undefined;
|
|
65
|
-
[SolanaMobileWalletAdapterProtocolErrorCode.ERROR_ATTEST_ORIGIN_ANDROID]: {
|
|
66
|
-
attest_origin_uri: string;
|
|
67
|
-
challenge: string;
|
|
68
|
-
context: string;
|
|
69
|
-
};
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
export class SolanaMobileWalletAdapterProtocolError<
|
|
73
|
-
TErrorCode extends SolanaMobileWalletAdapterProtocolErrorCodeEnum,
|
|
74
|
-
> extends Error {
|
|
75
|
-
data: ProtocolErrorDataTypeMap[TErrorCode] | undefined;
|
|
76
|
-
code: TErrorCode | JSONRPCErrorCode;
|
|
77
|
-
jsonRpcMessageId: number;
|
|
78
|
-
constructor(
|
|
79
|
-
...args: ProtocolErrorDataTypeMap[TErrorCode] extends Record<string, unknown>
|
|
80
|
-
? [
|
|
81
|
-
jsonRpcMessageId: number,
|
|
82
|
-
code: TErrorCode | JSONRPCErrorCode,
|
|
83
|
-
message: string,
|
|
84
|
-
data: ProtocolErrorDataTypeMap[TErrorCode],
|
|
85
|
-
]
|
|
86
|
-
: [jsonRpcMessageId: number, code: TErrorCode | JSONRPCErrorCode, message: string]
|
|
87
|
-
) {
|
|
88
|
-
const [jsonRpcMessageId, code, message, data] = args;
|
|
89
|
-
super(message);
|
|
90
|
-
this.code = code;
|
|
91
|
-
this.data = data;
|
|
92
|
-
this.jsonRpcMessageId = jsonRpcMessageId;
|
|
93
|
-
this.name = 'SolanaMobileWalletAdapterProtocolError';
|
|
94
|
-
}
|
|
95
|
-
}
|