-
Notifications
You must be signed in to change notification settings - Fork 59
/
wallet-detector.ts
54 lines (49 loc) · 1.8 KB
/
wallet-detector.ts
1
2
3
4
5
6
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
import BrowserConnection from './connection/Browser';
import BrowserWindowMessageConnection from './connection/BrowserWindowMessage';
import { MESSAGE_DIRECTION, METHODS } from './schema';
import { WalletInfo } from './rpc/types';
import { UnsupportedPlatformError } from '../utils/errors';
interface Wallet {
info: WalletInfo;
getConnection: () => BrowserWindowMessageConnection;
}
interface Wallets { [key: string]: Wallet }
/**
* A function to detect available wallets
* @category aepp wallet communication
* @param connection - connection to use to detect wallets
* @param onDetected - call-back function which trigger on new wallet
* @returns a function to stop scanning
*/
export default (
connection: BrowserConnection,
onDetected: ({ wallets, newWallet }: { wallets: Wallets; newWallet: Wallet }) => void,
): () => void => {
if (window == null) throw new UnsupportedPlatformError('Window object not found, you can run wallet detector only in browser');
const wallets: Wallets = {};
connection.connect((
{ method, params }: { method: string; params: WalletInfo },
origin: string,
source: Window,
) => {
if (method !== METHODS.readyToConnect || wallets[params.id] != null) return;
const wallet = {
info: params,
getConnection() {
return new BrowserWindowMessageConnection({
target: source,
...params.type === 'extension' ? {
sendDirection: MESSAGE_DIRECTION.to_waellet,
receiveDirection: MESSAGE_DIRECTION.to_aepp,
...window.origin !== 'null' && { origin: window.origin },
} : {
origin: params.origin,
},
});
},
};
wallets[wallet.info.id] = wallet;
onDetected({ wallets, newWallet: wallet });
}, () => {});
return () => connection.disconnect();
};