-
Notifications
You must be signed in to change notification settings - Fork 442
Expand file tree
/
Copy pathweb3.ts
More file actions
89 lines (71 loc) · 2.91 KB
/
web3.ts
File metadata and controls
89 lines (71 loc) · 2.91 KB
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
import type { Web3Provider } from '@clerk/types';
import { toHex } from './hex';
import { getInjectedWeb3Providers } from './injectedWeb3Providers';
type GetWeb3IdentifierParams = {
provider: Web3Provider;
};
export async function getWeb3Identifier(params: GetWeb3IdentifierParams): Promise<string> {
const { provider } = params;
const ethereum = await getEthereumProvider(provider);
// TODO - core-3: Improve error handling for the case when the provider is not found
if (!ethereum) {
// If a plugin for the requested provider is not found,
// the flow will fail as it has been the expected behavior so far.
return '';
}
const identifiers = await ethereum.request({ method: 'eth_requestAccounts' });
// @ts-ignore
return (identifiers && identifiers[0]) || '';
}
type GenerateWeb3SignatureParams = GenerateSignatureParams & {
provider: Web3Provider;
};
export async function generateWeb3Signature(params: GenerateWeb3SignatureParams): Promise<string> {
const { identifier, nonce, provider } = params;
const ethereum = await getEthereumProvider(provider);
// TODO - core-3: Improve error handling for the case when the provider is not found
if (!ethereum) {
// If a plugin for the requested provider is not found,
// the flow will fail as it has been the expected behavior so far.
return '';
}
return await ethereum.request({
method: 'personal_sign',
params: [`0x${toHex(nonce)}`, identifier],
});
}
export async function getMetamaskIdentifier(): Promise<string> {
return await getWeb3Identifier({ provider: 'metamask' });
}
export async function getCoinbaseWalletIdentifier(): Promise<string> {
return await getWeb3Identifier({ provider: 'coinbase_wallet' });
}
export async function getOKXWalletIdentifier(): Promise<string> {
return await getWeb3Identifier({ provider: 'okx_wallet' });
}
type GenerateSignatureParams = {
identifier: string;
nonce: string;
};
export async function generateSignatureWithMetamask(params: GenerateSignatureParams): Promise<string> {
return await generateWeb3Signature({ ...params, provider: 'metamask' });
}
export async function generateSignatureWithCoinbaseWallet(params: GenerateSignatureParams): Promise<string> {
return await generateWeb3Signature({ ...params, provider: 'coinbase_wallet' });
}
export async function generateSignatureWithOKXWallet(params: GenerateSignatureParams): Promise<string> {
return await generateWeb3Signature({ ...params, provider: 'okx_wallet' });
}
async function getEthereumProvider(provider: Web3Provider) {
if (provider === 'coinbase_wallet') {
const createCoinbaseWalletSDK = await import('@coinbase/wallet-sdk').then(mod => mod.createCoinbaseWalletSDK);
const sdk = createCoinbaseWalletSDK({
preference: {
options: 'all',
},
});
return sdk.getProvider();
}
const injectedWeb3Providers = getInjectedWeb3Providers();
return injectedWeb3Providers.get(provider);
}