@solana-mobile/mobile-wallet-adapter-protocol 2.0.0 → 2.0.1
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/.gitignore +2 -0
- package/README.md +69 -62
- package/android/.gitignore +14 -0
- package/android/.gradle/4.4.1/fileChanges/last-build.bin +0 -0
- package/android/.gradle/4.4.1/fileHashes/fileHashes.bin +0 -0
- package/android/.gradle/4.4.1/fileHashes/fileHashes.lock +0 -0
- package/android/.gradle/7.1/dependencies-accessors/dependencies-accessors.lock +0 -0
- package/android/.gradle/7.1/dependencies-accessors/gc.properties +0 -0
- package/android/.gradle/7.1/fileChanges/last-build.bin +0 -0
- package/android/.gradle/7.1/fileHashes/fileHashes.lock +0 -0
- package/android/.gradle/7.1/gc.properties +0 -0
- package/android/.gradle/7.5/checksums/checksums.lock +0 -0
- package/android/.gradle/7.5/dependencies-accessors/dependencies-accessors.lock +0 -0
- package/android/.gradle/7.5/dependencies-accessors/gc.properties +0 -0
- package/android/.gradle/7.5/fileChanges/last-build.bin +0 -0
- package/android/.gradle/7.5/fileHashes/fileHashes.lock +0 -0
- package/android/.gradle/7.5/gc.properties +0 -0
- package/android/.gradle/7.5.1/checksums/checksums.lock +0 -0
- package/android/.gradle/7.5.1/checksums/sha1-checksums.bin +0 -0
- package/android/.gradle/7.5.1/dependencies-accessors/dependencies-accessors.lock +0 -0
- package/android/.gradle/7.5.1/dependencies-accessors/gc.properties +0 -0
- package/android/.gradle/7.5.1/fileChanges/last-build.bin +0 -0
- package/android/.gradle/7.5.1/fileHashes/fileHashes.lock +0 -0
- package/android/.gradle/7.5.1/gc.properties +0 -0
- package/android/.gradle/buildOutputCleanup/buildOutputCleanup.lock +0 -0
- package/android/.gradle/buildOutputCleanup/cache.properties +2 -0
- package/android/.gradle/checksums/checksums.lock +0 -0
- package/android/.gradle/checksums/md5-checksums.bin +0 -0
- package/android/.gradle/checksums/sha1-checksums.bin +0 -0
- package/android/.gradle/vcs-1/gc.properties +0 -0
- package/android/build.gradle +146 -146
- package/android/gradle/wrapper/gradle-wrapper.properties +5 -5
- package/android/src/main/java/com/solanamobile/mobilewalletadapter/reactnative/SolanaMobileWalletAdapterModule.kt +176 -139
- package/package.json +50 -51
- package/src/__forks__/react-native/transact.ts +106 -0
- package/src/arrayBufferToBase64String.ts +10 -0
- package/src/associationPort.ts +19 -0
- package/src/createHelloReq.ts +12 -0
- package/src/createSequenceNumberVector.ts +11 -0
- package/src/errors.ts +93 -0
- package/src/generateAssociationKeypair.ts +10 -0
- package/src/generateECDHKeypair.ts +10 -0
- package/src/getAssociateAndroidIntentURL.ts +52 -0
- package/src/getJWS.ts +19 -0
- package/src/getStringWithURLUnsafeBase64CharactersReplaced.ts +11 -0
- package/src/index.ts +3 -0
- package/src/jsonRpcMessage.ts +81 -0
- package/src/parseHelloRsp.ts +44 -0
- package/src/startSession.ts +94 -0
- package/src/transact.ts +268 -0
- package/src/types.ts +111 -0
- package/tsconfig.cjs.json +7 -0
- package/tsconfig.json +8 -0
package/src/errors.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
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
|
+
} as const;
|
|
10
|
+
type SolanaMobileWalletAdapterErrorCodeEnum =
|
|
11
|
+
typeof SolanaMobileWalletAdapterErrorCode[keyof typeof SolanaMobileWalletAdapterErrorCode];
|
|
12
|
+
|
|
13
|
+
type ErrorDataTypeMap = {
|
|
14
|
+
[SolanaMobileWalletAdapterErrorCode.ERROR_ASSOCIATION_PORT_OUT_OF_RANGE]: {
|
|
15
|
+
port: number;
|
|
16
|
+
};
|
|
17
|
+
[SolanaMobileWalletAdapterErrorCode.ERROR_FORBIDDEN_WALLET_BASE_URL]: undefined;
|
|
18
|
+
[SolanaMobileWalletAdapterErrorCode.ERROR_SECURE_CONTEXT_REQUIRED]: undefined;
|
|
19
|
+
[SolanaMobileWalletAdapterErrorCode.ERROR_SESSION_CLOSED]: {
|
|
20
|
+
closeEvent: CloseEvent;
|
|
21
|
+
};
|
|
22
|
+
[SolanaMobileWalletAdapterErrorCode.ERROR_SESSION_TIMEOUT]: undefined;
|
|
23
|
+
[SolanaMobileWalletAdapterErrorCode.ERROR_WALLET_NOT_FOUND]: undefined;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export class SolanaMobileWalletAdapterError<TErrorCode extends SolanaMobileWalletAdapterErrorCodeEnum> extends Error {
|
|
27
|
+
data: ErrorDataTypeMap[TErrorCode] | undefined;
|
|
28
|
+
code: TErrorCode;
|
|
29
|
+
constructor(
|
|
30
|
+
...args: ErrorDataTypeMap[TErrorCode] extends Record<string, unknown>
|
|
31
|
+
? [code: TErrorCode, message: string, data: ErrorDataTypeMap[TErrorCode]]
|
|
32
|
+
: [code: TErrorCode, message: string]
|
|
33
|
+
) {
|
|
34
|
+
const [code, message, data] = args;
|
|
35
|
+
super(message);
|
|
36
|
+
this.code = code;
|
|
37
|
+
this.data = data;
|
|
38
|
+
this.name = 'SolanaMobileWalletAdapterError';
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
type JSONRPCErrorCode = number;
|
|
43
|
+
|
|
44
|
+
// Typescript `enums` thwart tree-shaking. See https://bargsten.org/jsts/enums/
|
|
45
|
+
export const SolanaMobileWalletAdapterProtocolErrorCode = {
|
|
46
|
+
// Keep these in sync with `mobilewalletadapter/common/ProtocolContract.java`.
|
|
47
|
+
ERROR_AUTHORIZATION_FAILED: -1,
|
|
48
|
+
ERROR_INVALID_PAYLOADS: -2,
|
|
49
|
+
ERROR_NOT_SIGNED: -3,
|
|
50
|
+
ERROR_NOT_SUBMITTED: -4,
|
|
51
|
+
ERROR_TOO_MANY_PAYLOADS: -5,
|
|
52
|
+
ERROR_ATTEST_ORIGIN_ANDROID: -100,
|
|
53
|
+
} as const;
|
|
54
|
+
type SolanaMobileWalletAdapterProtocolErrorCodeEnum =
|
|
55
|
+
typeof SolanaMobileWalletAdapterProtocolErrorCode[keyof typeof SolanaMobileWalletAdapterProtocolErrorCode];
|
|
56
|
+
|
|
57
|
+
type ProtocolErrorDataTypeMap = {
|
|
58
|
+
[SolanaMobileWalletAdapterProtocolErrorCode.ERROR_AUTHORIZATION_FAILED]: undefined;
|
|
59
|
+
[SolanaMobileWalletAdapterProtocolErrorCode.ERROR_INVALID_PAYLOADS]: undefined;
|
|
60
|
+
[SolanaMobileWalletAdapterProtocolErrorCode.ERROR_NOT_SIGNED]: undefined;
|
|
61
|
+
[SolanaMobileWalletAdapterProtocolErrorCode.ERROR_NOT_SUBMITTED]: undefined;
|
|
62
|
+
[SolanaMobileWalletAdapterProtocolErrorCode.ERROR_TOO_MANY_PAYLOADS]: undefined;
|
|
63
|
+
[SolanaMobileWalletAdapterProtocolErrorCode.ERROR_ATTEST_ORIGIN_ANDROID]: {
|
|
64
|
+
attest_origin_uri: string;
|
|
65
|
+
challenge: string;
|
|
66
|
+
context: string;
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
export class SolanaMobileWalletAdapterProtocolError<
|
|
71
|
+
TErrorCode extends SolanaMobileWalletAdapterProtocolErrorCodeEnum,
|
|
72
|
+
> extends Error {
|
|
73
|
+
data: ProtocolErrorDataTypeMap[TErrorCode] | undefined;
|
|
74
|
+
code: TErrorCode | JSONRPCErrorCode;
|
|
75
|
+
jsonRpcMessageId: number;
|
|
76
|
+
constructor(
|
|
77
|
+
...args: ProtocolErrorDataTypeMap[TErrorCode] extends Record<string, unknown>
|
|
78
|
+
? [
|
|
79
|
+
jsonRpcMessageId: number,
|
|
80
|
+
code: TErrorCode | JSONRPCErrorCode,
|
|
81
|
+
message: string,
|
|
82
|
+
data: ProtocolErrorDataTypeMap[TErrorCode],
|
|
83
|
+
]
|
|
84
|
+
: [jsonRpcMessageId: number, code: TErrorCode | JSONRPCErrorCode, message: string]
|
|
85
|
+
) {
|
|
86
|
+
const [jsonRpcMessageId, code, message, data] = args;
|
|
87
|
+
super(message);
|
|
88
|
+
this.code = code;
|
|
89
|
+
this.data = data;
|
|
90
|
+
this.jsonRpcMessageId = jsonRpcMessageId;
|
|
91
|
+
this.name = 'SolanaMobileWalletAdapterProtocolError';
|
|
92
|
+
}
|
|
93
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import arrayBufferToBase64String from './arrayBufferToBase64String.js';
|
|
2
|
+
import { assertAssociationPort } from './associationPort.js';
|
|
3
|
+
import { SolanaMobileWalletAdapterError, SolanaMobileWalletAdapterErrorCode } from './errors.js';
|
|
4
|
+
import getStringWithURLUnsafeBase64CharactersReplaced from './getStringWithURLUnsafeBase64CharactersReplaced.js';
|
|
5
|
+
|
|
6
|
+
const INTENT_NAME = 'solana-wallet';
|
|
7
|
+
|
|
8
|
+
function getPathParts(pathString: string) {
|
|
9
|
+
return (
|
|
10
|
+
pathString
|
|
11
|
+
// Strip leading and trailing slashes
|
|
12
|
+
.replace(/(^\/+|\/+$)/g, '')
|
|
13
|
+
// Return an array of directories
|
|
14
|
+
.split('/')
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function getIntentURL(methodPathname: string, intentUrlBase?: string) {
|
|
19
|
+
let baseUrl: URL | null = null;
|
|
20
|
+
if (intentUrlBase) {
|
|
21
|
+
try {
|
|
22
|
+
baseUrl = new URL(intentUrlBase);
|
|
23
|
+
} catch {} // eslint-disable-line no-empty
|
|
24
|
+
if (baseUrl?.protocol !== 'https:') {
|
|
25
|
+
throw new SolanaMobileWalletAdapterError(
|
|
26
|
+
SolanaMobileWalletAdapterErrorCode.ERROR_FORBIDDEN_WALLET_BASE_URL,
|
|
27
|
+
'Base URLs supplied by wallets must be valid `https` URLs',
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
baseUrl ||= new URL(`${INTENT_NAME}:/`);
|
|
32
|
+
const pathname = methodPathname.startsWith('/')
|
|
33
|
+
? // Method is an absolute path. Replace it wholesale.
|
|
34
|
+
methodPathname
|
|
35
|
+
: // Method is a relative path. Merge it with the existing one.
|
|
36
|
+
[...getPathParts(baseUrl.pathname), ...getPathParts(methodPathname)].join('/');
|
|
37
|
+
return new URL(pathname, baseUrl);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export default async function getAssociateAndroidIntentURL(
|
|
41
|
+
associationPublicKey: CryptoKey,
|
|
42
|
+
putativePort: number,
|
|
43
|
+
associationURLBase?: string,
|
|
44
|
+
): Promise<URL> {
|
|
45
|
+
const associationPort = assertAssociationPort(putativePort);
|
|
46
|
+
const exportedKey = await crypto.subtle.exportKey('raw', associationPublicKey);
|
|
47
|
+
const encodedKey = arrayBufferToBase64String(exportedKey);
|
|
48
|
+
const url = getIntentURL('v1/associate/local', associationURLBase);
|
|
49
|
+
url.searchParams.set('association', getStringWithURLUnsafeBase64CharactersReplaced(encodedKey));
|
|
50
|
+
url.searchParams.set('port', `${associationPort}`);
|
|
51
|
+
return url;
|
|
52
|
+
}
|
package/src/getJWS.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import arrayBufferToBase64String from './arrayBufferToBase64String.js';
|
|
2
|
+
|
|
3
|
+
export default async function getJWS(payload: string, privateKey: CryptoKey) {
|
|
4
|
+
const header = { alg: 'ES256' };
|
|
5
|
+
const headerEncoded = window.btoa(JSON.stringify(header));
|
|
6
|
+
const payloadEncoded = window.btoa(payload);
|
|
7
|
+
const message = `${headerEncoded}.${payloadEncoded}`;
|
|
8
|
+
const signatureBuffer = await crypto.subtle.sign(
|
|
9
|
+
{
|
|
10
|
+
name: 'ECDSA',
|
|
11
|
+
hash: 'SHA-256',
|
|
12
|
+
},
|
|
13
|
+
privateKey,
|
|
14
|
+
new TextEncoder().encode(message),
|
|
15
|
+
);
|
|
16
|
+
const signature = arrayBufferToBase64String(signatureBuffer);
|
|
17
|
+
const jws = `${message}.${signature}`;
|
|
18
|
+
return jws;
|
|
19
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import createSequenceNumberVector, { SEQUENCE_NUMBER_BYTES } from './createSequenceNumberVector.js';
|
|
2
|
+
import { SolanaMobileWalletAdapterProtocolError } from './errors.js';
|
|
3
|
+
import { SharedSecret } from './parseHelloRsp.js';
|
|
4
|
+
|
|
5
|
+
const INITIALIZATION_VECTOR_BYTES = 12;
|
|
6
|
+
|
|
7
|
+
interface JSONRPCRequest<TParams> {
|
|
8
|
+
id: number;
|
|
9
|
+
jsonrpc: '2.0';
|
|
10
|
+
method: string;
|
|
11
|
+
params: TParams;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
type JSONRPCResponse<TMessage> = {
|
|
15
|
+
id: number;
|
|
16
|
+
jsonrpc: '2.0';
|
|
17
|
+
result: TMessage;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export async function encryptJsonRpcMessage<TParams>(
|
|
21
|
+
jsonRpcMessage: JSONRPCRequest<TParams>,
|
|
22
|
+
sharedSecret: SharedSecret,
|
|
23
|
+
) {
|
|
24
|
+
const plaintext = JSON.stringify(jsonRpcMessage);
|
|
25
|
+
const sequenceNumberVector = createSequenceNumberVector(jsonRpcMessage.id);
|
|
26
|
+
const initializationVector = new Uint8Array(INITIALIZATION_VECTOR_BYTES);
|
|
27
|
+
crypto.getRandomValues(initializationVector);
|
|
28
|
+
const ciphertext = await crypto.subtle.encrypt(
|
|
29
|
+
getAlgorithmParams(sequenceNumberVector, initializationVector),
|
|
30
|
+
sharedSecret,
|
|
31
|
+
new TextEncoder().encode(plaintext),
|
|
32
|
+
);
|
|
33
|
+
const response = new Uint8Array(
|
|
34
|
+
sequenceNumberVector.byteLength + initializationVector.byteLength + ciphertext.byteLength,
|
|
35
|
+
);
|
|
36
|
+
response.set(new Uint8Array(sequenceNumberVector), 0);
|
|
37
|
+
response.set(new Uint8Array(initializationVector), sequenceNumberVector.byteLength);
|
|
38
|
+
response.set(new Uint8Array(ciphertext), sequenceNumberVector.byteLength + initializationVector.byteLength);
|
|
39
|
+
return response;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export async function decryptJsonRpcMessage<TMessage>(message: ArrayBuffer, sharedSecret: SharedSecret) {
|
|
43
|
+
const sequenceNumberVector = message.slice(0, SEQUENCE_NUMBER_BYTES);
|
|
44
|
+
const initializationVector = message.slice(
|
|
45
|
+
SEQUENCE_NUMBER_BYTES,
|
|
46
|
+
SEQUENCE_NUMBER_BYTES + INITIALIZATION_VECTOR_BYTES,
|
|
47
|
+
);
|
|
48
|
+
const ciphertext = message.slice(SEQUENCE_NUMBER_BYTES + INITIALIZATION_VECTOR_BYTES);
|
|
49
|
+
const plaintextBuffer = await crypto.subtle.decrypt(
|
|
50
|
+
getAlgorithmParams(sequenceNumberVector, initializationVector),
|
|
51
|
+
sharedSecret,
|
|
52
|
+
ciphertext,
|
|
53
|
+
);
|
|
54
|
+
const plaintext = getUtf8Decoder().decode(plaintextBuffer);
|
|
55
|
+
const jsonRpcMessage = JSON.parse(plaintext);
|
|
56
|
+
if (Object.hasOwnProperty.call(jsonRpcMessage, 'error')) {
|
|
57
|
+
throw new SolanaMobileWalletAdapterProtocolError<typeof jsonRpcMessage.error.code>(
|
|
58
|
+
jsonRpcMessage.id,
|
|
59
|
+
jsonRpcMessage.error.code,
|
|
60
|
+
jsonRpcMessage.error.message,
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
return jsonRpcMessage as JSONRPCResponse<TMessage>;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function getAlgorithmParams(sequenceNumber: ArrayBuffer, initializationVector: ArrayBuffer) {
|
|
67
|
+
return {
|
|
68
|
+
additionalData: sequenceNumber,
|
|
69
|
+
iv: initializationVector,
|
|
70
|
+
name: 'AES-GCM',
|
|
71
|
+
tagLength: 128, // 16 byte tag => 128 bits
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
let _utf8Decoder: TextDecoder | undefined;
|
|
76
|
+
function getUtf8Decoder() {
|
|
77
|
+
if (_utf8Decoder === undefined) {
|
|
78
|
+
_utf8Decoder = new TextDecoder('utf-8');
|
|
79
|
+
}
|
|
80
|
+
return _utf8Decoder;
|
|
81
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A secret agreed upon by the app and the wallet. Used as
|
|
3
|
+
* a symmetric key to encrypt and decrypt messages over an
|
|
4
|
+
* unsecured channel.
|
|
5
|
+
*/
|
|
6
|
+
export type SharedSecret = CryptoKey;
|
|
7
|
+
|
|
8
|
+
export default async function parseHelloRsp(
|
|
9
|
+
payloadBuffer: ArrayBuffer, // The X9.62-encoded wallet endpoint ephemeral ECDH public keypoint.
|
|
10
|
+
associationPublicKey: CryptoKey,
|
|
11
|
+
ecdhPrivateKey: CryptoKey,
|
|
12
|
+
): Promise<SharedSecret> {
|
|
13
|
+
const [associationPublicKeyBuffer, walletPublicKey] = await Promise.all([
|
|
14
|
+
crypto.subtle.exportKey('raw', associationPublicKey),
|
|
15
|
+
crypto.subtle.importKey(
|
|
16
|
+
'raw',
|
|
17
|
+
payloadBuffer,
|
|
18
|
+
{ name: 'ECDH', namedCurve: 'P-256' },
|
|
19
|
+
false /* extractable */,
|
|
20
|
+
[] /* keyUsages */,
|
|
21
|
+
),
|
|
22
|
+
]);
|
|
23
|
+
const sharedSecret = await crypto.subtle.deriveBits({ name: 'ECDH', public: walletPublicKey }, ecdhPrivateKey, 256);
|
|
24
|
+
const ecdhSecretKey = await crypto.subtle.importKey(
|
|
25
|
+
'raw',
|
|
26
|
+
sharedSecret,
|
|
27
|
+
'HKDF',
|
|
28
|
+
false /* extractable */,
|
|
29
|
+
['deriveKey'] /* keyUsages */,
|
|
30
|
+
);
|
|
31
|
+
const aesKeyMaterialVal = await crypto.subtle.deriveKey(
|
|
32
|
+
{
|
|
33
|
+
name: 'HKDF',
|
|
34
|
+
hash: 'SHA-256',
|
|
35
|
+
salt: new Uint8Array(associationPublicKeyBuffer),
|
|
36
|
+
info: new Uint8Array(),
|
|
37
|
+
},
|
|
38
|
+
ecdhSecretKey,
|
|
39
|
+
{ name: 'AES-GCM', length: 128 },
|
|
40
|
+
false /* extractable */,
|
|
41
|
+
['encrypt', 'decrypt'],
|
|
42
|
+
);
|
|
43
|
+
return aesKeyMaterialVal as SharedSecret;
|
|
44
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { AssociationPort, getRandomAssociationPort } from './associationPort.js';
|
|
2
|
+
import { SolanaMobileWalletAdapterError, SolanaMobileWalletAdapterErrorCode } from './errors.js';
|
|
3
|
+
import getAssociateAndroidIntentURL from './getAssociateAndroidIntentURL.js';
|
|
4
|
+
|
|
5
|
+
// Typescript `enums` thwart tree-shaking. See https://bargsten.org/jsts/enums/
|
|
6
|
+
const Browser = {
|
|
7
|
+
Firefox: 0,
|
|
8
|
+
Other: 1,
|
|
9
|
+
} as const;
|
|
10
|
+
type BrowserEnum = typeof Browser[keyof typeof Browser];
|
|
11
|
+
|
|
12
|
+
function assertUnreachable(x: never): never {
|
|
13
|
+
return x;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function getBrowser(): BrowserEnum {
|
|
17
|
+
return navigator.userAgent.indexOf('Firefox/') !== -1 ? Browser.Firefox : Browser.Other;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function getDetectionPromise() {
|
|
21
|
+
// Chrome and others silently fail if a custom protocol is not supported.
|
|
22
|
+
// For these, we wait to see if the browser is navigated away from in
|
|
23
|
+
// a reasonable amount of time (ie. the native wallet opened).
|
|
24
|
+
return new Promise<void>((resolve, reject) => {
|
|
25
|
+
function cleanup() {
|
|
26
|
+
clearTimeout(timeoutId);
|
|
27
|
+
window.removeEventListener('blur', handleBlur);
|
|
28
|
+
}
|
|
29
|
+
function handleBlur() {
|
|
30
|
+
cleanup();
|
|
31
|
+
resolve();
|
|
32
|
+
}
|
|
33
|
+
window.addEventListener('blur', handleBlur);
|
|
34
|
+
const timeoutId = setTimeout(() => {
|
|
35
|
+
cleanup();
|
|
36
|
+
reject();
|
|
37
|
+
}, 2000);
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
let _frame: HTMLIFrameElement | null = null;
|
|
42
|
+
function launchUrlThroughHiddenFrame(url: URL) {
|
|
43
|
+
if (_frame == null) {
|
|
44
|
+
_frame = document.createElement('iframe');
|
|
45
|
+
_frame.style.display = 'none';
|
|
46
|
+
document.body.appendChild(_frame);
|
|
47
|
+
}
|
|
48
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
49
|
+
_frame.contentWindow!.location.href = url.toString();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export async function startSession(
|
|
53
|
+
associationPublicKey: CryptoKey,
|
|
54
|
+
associationURLBase?: string,
|
|
55
|
+
): Promise<AssociationPort> {
|
|
56
|
+
const randomAssociationPort = getRandomAssociationPort();
|
|
57
|
+
const associationUrl = await getAssociateAndroidIntentURL(
|
|
58
|
+
associationPublicKey,
|
|
59
|
+
randomAssociationPort,
|
|
60
|
+
associationURLBase,
|
|
61
|
+
);
|
|
62
|
+
if (associationUrl.protocol === 'https:') {
|
|
63
|
+
// The association URL is an Android 'App Link' or iOS 'Universal Link'.
|
|
64
|
+
// These are regular web URLs that are designed to launch an app if it
|
|
65
|
+
// is installed or load the actual target webpage if not.
|
|
66
|
+
window.location.assign(associationUrl);
|
|
67
|
+
} else {
|
|
68
|
+
// The association URL has a custom protocol (eg. `solana-wallet:`)
|
|
69
|
+
try {
|
|
70
|
+
const browser = getBrowser();
|
|
71
|
+
switch (browser) {
|
|
72
|
+
case Browser.Firefox:
|
|
73
|
+
// If a custom protocol is not supported in Firefox, it throws.
|
|
74
|
+
launchUrlThroughHiddenFrame(associationUrl);
|
|
75
|
+
// If we reached this line, it's supported.
|
|
76
|
+
break;
|
|
77
|
+
case Browser.Other: {
|
|
78
|
+
const detectionPromise = getDetectionPromise();
|
|
79
|
+
window.location.assign(associationUrl);
|
|
80
|
+
await detectionPromise;
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
default:
|
|
84
|
+
assertUnreachable(browser);
|
|
85
|
+
}
|
|
86
|
+
} catch (e) {
|
|
87
|
+
throw new SolanaMobileWalletAdapterError(
|
|
88
|
+
SolanaMobileWalletAdapterErrorCode.ERROR_WALLET_NOT_FOUND,
|
|
89
|
+
'Found no installed wallet that supports the mobile wallet protocol.',
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return randomAssociationPort;
|
|
94
|
+
}
|