-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.ts
110 lines (88 loc) · 3.02 KB
/
index.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// chain
export enum SupportedProviderImplementations {
LedgerConnect = "LedgerConnect",
WalletConnect = "WalletConnect",
}
// logging
export type EnableDebugLogsFunction = () => void;
// support
export type CheckSupportOptions = {
walletConnectVersion?: number;
providerType: SupportedProviders;
// WalletConnect v2 init parameters
projectId?: string; // REQUIRED WC v2 project id, throws if v2 and not set
chains?: number[]; // REQUIRED ethereum chains, has default
optionalChains?: number[]; // OPTIONAL ethereum chains
methods?: string[]; // REQUIRED ethereum methods, has default
optionalMethods?: string[]; // OPTIONAL ethereum methods
events?: string[]; // REQUIRED ethereum events, has default
optionalEvents?: string[]; // OPTIONAL ethereum events
rpcMap?: { [chainId: string]: string }; // OPTIONAL rpc urls for each chain
// WalletConnect v1 init parameters
chainId?: number;
bridge?: string;
infuraId?: string;
rpc?: { [chainId: number]: string };
};
export type CheckSupportResult = {
isLedgerConnectSupported: boolean;
isLedgerConnectEnabled: boolean;
isChainIdSupported?: boolean;
providerImplementation: SupportedProviderImplementations;
};
export type CheckSupportFunction = (
options: CheckSupportOptions
) => CheckSupportResult;
// ethereum
export type EthereumRequestPayload = {
method: string;
params?: unknown[] | object;
};
export interface EthereumProvider {
providers?: EthereumProvider[];
connector?: unknown;
session?: unknown;
chainId: string | number;
request<T = unknown>(args: EthereumRequestPayload): Promise<T>;
disconnect?: { (): Promise<void> };
on(event: any, listener: any): void;
removeListener(event: string, listener: any): void;
}
// getProvider
export enum SupportedProviders {
Ethereum = 'Ethereum',
}
export type ProviderResult = EthereumProvider;
export type GetProviderFunction = () => Promise<ProviderResult>;
// script loader
export interface LedgerConnectKit {
enableDebugLogs: EnableDebugLogsFunction;
checkSupport: CheckSupportFunction;
getProvider: GetProviderFunction;
}
export async function loadConnectKit(): Promise<LedgerConnectKit> {
const src = "https://cdn.jsdelivr.net/npm/@ledgerhq/connect-kit@1.1.8";
const globalName = "ledgerConnectKit";
return new Promise((resolve, reject) => {
const scriptId = `ledger-ck-script-${globalName}`;
// we don't support server side rendering, reject with no stack trace for now
if (typeof document === "undefined") {
reject("Connect Kit does not support server side");
return;
}
if (document.getElementById(scriptId)) {
resolve((window as { [key: string]: any })[globalName]);
} else {
const script = document.createElement("script");
script.src = src;
script.id = scriptId;
script.addEventListener("load", () => {
resolve((window as { [key: string]: any })[globalName]);
});
script.addEventListener("error", (e) => {
reject(e.error);
});
document.head.appendChild(script);
}
});
}