@reown/appkit-common-react-native 2.0.0-alpha.0 → 2.0.0
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/lib/commonjs/adapters/BlockchainAdapter.js +79 -0
- package/lib/commonjs/adapters/BlockchainAdapter.js.map +1 -0
- package/lib/commonjs/adapters/EvmAdapter.js +83 -0
- package/lib/commonjs/adapters/EvmAdapter.js.map +1 -0
- package/lib/commonjs/adapters/SolanaBaseAdapter.js +12 -0
- package/lib/commonjs/adapters/SolanaBaseAdapter.js.map +1 -0
- package/lib/commonjs/index.js +62 -1
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/networks/bitcoin.js +40 -0
- package/lib/commonjs/networks/bitcoin.js.map +1 -0
- package/lib/commonjs/networks/solana.js +78 -0
- package/lib/commonjs/networks/solana.js.map +1 -0
- package/lib/commonjs/utils/ConstantsUtil.js +1 -1
- package/lib/commonjs/utils/ConstantsUtil.js.map +1 -1
- package/lib/commonjs/utils/NumberUtil.js +52 -11
- package/lib/commonjs/utils/NumberUtil.js.map +1 -1
- package/lib/commonjs/utils/TypeUtil.js +9 -79
- package/lib/commonjs/utils/TypeUtil.js.map +1 -1
- package/lib/module/adapters/BlockchainAdapter.js +72 -0
- package/lib/module/adapters/BlockchainAdapter.js.map +1 -0
- package/lib/module/adapters/EvmAdapter.js +76 -0
- package/lib/module/adapters/EvmAdapter.js.map +1 -0
- package/lib/module/adapters/SolanaBaseAdapter.js +5 -0
- package/lib/module/adapters/SolanaBaseAdapter.js.map +1 -0
- package/lib/module/index.js +5 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/networks/bitcoin.js +34 -0
- package/lib/module/networks/bitcoin.js.map +1 -0
- package/lib/module/networks/solana.js +72 -0
- package/lib/module/networks/solana.js.map +1 -0
- package/lib/module/utils/ConstantsUtil.js +1 -1
- package/lib/module/utils/ConstantsUtil.js.map +1 -1
- package/lib/module/utils/NumberUtil.js +52 -11
- package/lib/module/utils/NumberUtil.js.map +1 -1
- package/lib/module/utils/TypeUtil.js +7 -75
- package/lib/module/utils/TypeUtil.js.map +1 -1
- package/lib/typescript/adapters/BlockchainAdapter.d.ts +27 -0
- package/lib/typescript/adapters/BlockchainAdapter.d.ts.map +1 -0
- package/lib/typescript/adapters/EvmAdapter.d.ts +6 -0
- package/lib/typescript/adapters/EvmAdapter.d.ts.map +1 -0
- package/lib/typescript/adapters/SolanaBaseAdapter.d.ts +4 -0
- package/lib/typescript/adapters/SolanaBaseAdapter.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +5 -0
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/networks/bitcoin.d.ts +4 -0
- package/lib/typescript/networks/bitcoin.d.ts.map +1 -0
- package/lib/typescript/networks/solana.d.ts +5 -0
- package/lib/typescript/networks/solana.d.ts.map +1 -0
- package/lib/typescript/utils/NumberUtil.d.ts +39 -11
- package/lib/typescript/utils/NumberUtil.d.ts.map +1 -1
- package/lib/typescript/utils/TypeUtil.d.ts +59 -42
- package/lib/typescript/utils/TypeUtil.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/adapters/BlockchainAdapter.ts +109 -0
- package/src/adapters/EvmAdapter.ts +79 -0
- package/src/adapters/SolanaBaseAdapter.ts +5 -0
- package/src/index.ts +5 -0
- package/src/networks/bitcoin.ts +32 -0
- package/src/networks/solana.ts +44 -0
- package/src/utils/ConstantsUtil.ts +1 -1
- package/src/utils/NumberUtil.ts +54 -11
- package/src/utils/TypeUtil.ts +72 -113
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { BlockchainAdapter } from './BlockchainAdapter';
|
|
2
|
+
import { NumberUtil } from '../utils/NumberUtil';
|
|
3
|
+
|
|
4
|
+
export abstract class EVMAdapter extends BlockchainAdapter {
|
|
5
|
+
async estimateGas({ address, to, data, chainNamespace }: any): Promise<bigint> {
|
|
6
|
+
const provider = this.getProvider();
|
|
7
|
+
|
|
8
|
+
if (!provider) {
|
|
9
|
+
throw new Error('EVMAdapter:estimateGas - provider is undefined');
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (!address) {
|
|
13
|
+
throw new Error('EVMAdapter:estimateGas - from address is undefined');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (chainNamespace && chainNamespace !== 'eip155') {
|
|
17
|
+
throw new Error('EVMAdapter:estimateGas - chainNamespace is not eip155');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
try {
|
|
21
|
+
const txParams = {
|
|
22
|
+
from: address,
|
|
23
|
+
to,
|
|
24
|
+
data,
|
|
25
|
+
type: '0x0' // optional, legacy type
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const estimatedGasHex = await provider.request({
|
|
29
|
+
method: 'eth_estimateGas',
|
|
30
|
+
params: [txParams]
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
return BigInt(estimatedGasHex as string);
|
|
34
|
+
} catch (error) {
|
|
35
|
+
throw new Error('EVMAdapter:estimateGas - eth_estimateGas RPC failed');
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async sendTransaction(data: any) {
|
|
40
|
+
const { address } = data || {};
|
|
41
|
+
|
|
42
|
+
if (!this.getProvider()) {
|
|
43
|
+
throw new Error('EVMAdapter:sendTransaction - provider is undefined');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (!address) {
|
|
47
|
+
throw new Error('EVMAdapter:sendTransaction - address is undefined');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const txParams = {
|
|
51
|
+
from: address,
|
|
52
|
+
to: data.to,
|
|
53
|
+
value: NumberUtil.convertNumericToHexString(data.value),
|
|
54
|
+
gas: NumberUtil.convertNumericToHexString(data.gas),
|
|
55
|
+
gasPrice: NumberUtil.convertNumericToHexString(data.gasPrice),
|
|
56
|
+
data: data.data, // hex-encoded bytecode
|
|
57
|
+
type: '0x0' // optional: legacy transaction type
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const txHash = await this.getProvider().request({
|
|
61
|
+
method: 'eth_sendTransaction',
|
|
62
|
+
params: [txParams]
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
let receipt = null;
|
|
66
|
+
while (!receipt) {
|
|
67
|
+
receipt = (await this.getProvider().request({
|
|
68
|
+
method: 'eth_getTransactionReceipt',
|
|
69
|
+
params: [txHash]
|
|
70
|
+
})) as { blockHash?: `0x${string}` };
|
|
71
|
+
|
|
72
|
+
if (!receipt) {
|
|
73
|
+
await new Promise(r => setTimeout(r, 1000)); // wait 1s
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return receipt?.blockHash || null;
|
|
78
|
+
}
|
|
79
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -8,4 +8,9 @@ export { PresetsUtil } from './utils/PresetsUtil';
|
|
|
8
8
|
export { StringUtil } from './utils/StringUtil';
|
|
9
9
|
export { ErrorUtil } from './utils/ErrorUtil';
|
|
10
10
|
export { erc20ABI } from './contracts/erc20';
|
|
11
|
+
export { solana, solanaDevnet, solanaTestnet } from './networks/solana';
|
|
12
|
+
export { bitcoin, bitcoinTestnet } from './networks/bitcoin';
|
|
13
|
+
export { BlockchainAdapter } from './adapters/BlockchainAdapter';
|
|
14
|
+
export { EVMAdapter } from './adapters/EvmAdapter';
|
|
15
|
+
export { SolanaBaseAdapter } from './adapters/SolanaBaseAdapter';
|
|
11
16
|
export * from './utils/TypeUtil';
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { AppKitNetwork } from '../utils/TypeUtil';
|
|
2
|
+
|
|
3
|
+
export const bitcoin: AppKitNetwork = {
|
|
4
|
+
id: '000000000019d6689c085ae165831e93',
|
|
5
|
+
caipNetworkId: 'bip122:000000000019d6689c085ae165831e93',
|
|
6
|
+
chainNamespace: 'bip122',
|
|
7
|
+
name: 'Bitcoin',
|
|
8
|
+
nativeCurrency: {
|
|
9
|
+
name: 'Bitcoin',
|
|
10
|
+
symbol: 'BTC',
|
|
11
|
+
decimals: 8
|
|
12
|
+
},
|
|
13
|
+
rpcUrls: {
|
|
14
|
+
default: { http: ['https://rpc.walletconnect.org/v1'] }
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const bitcoinTestnet: AppKitNetwork = {
|
|
19
|
+
id: '000000000933ea01ad0ee984209779ba',
|
|
20
|
+
caipNetworkId: 'bip122:000000000933ea01ad0ee984209779ba',
|
|
21
|
+
chainNamespace: 'bip122',
|
|
22
|
+
name: 'Bitcoin Testnet',
|
|
23
|
+
nativeCurrency: {
|
|
24
|
+
name: 'Bitcoin',
|
|
25
|
+
symbol: 'BTC',
|
|
26
|
+
decimals: 8
|
|
27
|
+
},
|
|
28
|
+
rpcUrls: {
|
|
29
|
+
default: { http: ['https://rpc.walletconnect.org/v1'] }
|
|
30
|
+
},
|
|
31
|
+
testnet: true
|
|
32
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { AppKitNetwork } from '../utils/TypeUtil';
|
|
2
|
+
|
|
3
|
+
export const solana: AppKitNetwork = {
|
|
4
|
+
id: '5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
|
|
5
|
+
name: 'Solana',
|
|
6
|
+
nativeCurrency: { name: 'Solana', symbol: 'SOL', decimals: 9 },
|
|
7
|
+
rpcUrls: {
|
|
8
|
+
default: {
|
|
9
|
+
http: ['https://rpc.walletconnect.org/v1']
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
blockExplorers: { default: { name: 'Solscan', url: 'https://solscan.io' } },
|
|
13
|
+
chainNamespace: 'solana',
|
|
14
|
+
caipNetworkId: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
|
|
15
|
+
deprecatedCaipNetworkId: 'solana:4sGjMW1sUnHzSxGspuhpqLDx6wiyjNtZ',
|
|
16
|
+
testnet: false
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export const solanaDevnet: AppKitNetwork = {
|
|
20
|
+
id: 'EtWTRABZaYq6iMfeYKouRu166VU2xqa1',
|
|
21
|
+
name: 'Solana Devnet',
|
|
22
|
+
nativeCurrency: { name: 'Solana', symbol: 'SOL', decimals: 9 },
|
|
23
|
+
rpcUrls: {
|
|
24
|
+
default: { http: ['https://rpc.walletconnect.org/v1'] }
|
|
25
|
+
},
|
|
26
|
+
blockExplorers: { default: { name: 'Solscan', url: 'https://solscan.io' } },
|
|
27
|
+
chainNamespace: 'solana',
|
|
28
|
+
caipNetworkId: 'solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1',
|
|
29
|
+
deprecatedCaipNetworkId: 'solana:8E9rvCKLFQia2Y35HXjjpWzj8weVo44K',
|
|
30
|
+
testnet: true
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export const solanaTestnet: AppKitNetwork = {
|
|
34
|
+
id: '4uhcVJyU9pJkvQyS88uRDiswHXSCkY3z',
|
|
35
|
+
name: 'Solana Testnet',
|
|
36
|
+
nativeCurrency: { name: 'Solana', symbol: 'SOL', decimals: 9 },
|
|
37
|
+
rpcUrls: {
|
|
38
|
+
default: { http: ['https://rpc.walletconnect.org/v1'] }
|
|
39
|
+
},
|
|
40
|
+
blockExplorers: { default: { name: 'Solscan', url: 'https://solscan.io' } },
|
|
41
|
+
chainNamespace: 'solana',
|
|
42
|
+
caipNetworkId: 'solana:4uhcVJyU9pJkvQyS88uRDiswHXSCkY3z',
|
|
43
|
+
testnet: true
|
|
44
|
+
};
|
package/src/utils/NumberUtil.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import * as BigNumber from 'bignumber.js';
|
|
2
2
|
|
|
3
3
|
export const NumberUtil = {
|
|
4
|
+
/**
|
|
5
|
+
* Creates a BigNumber instance from a given value.
|
|
6
|
+
* If the value is a string, commas are removed before conversion.
|
|
7
|
+
* @param value - The input value (string, number, or BigNumber) to convert to a BigNumber.
|
|
8
|
+
* @returns A BigNumber instance.
|
|
9
|
+
*/
|
|
4
10
|
bigNumber(value: BigNumber.BigNumber.Value) {
|
|
5
11
|
if (typeof value === 'string') {
|
|
6
12
|
return new BigNumber.BigNumber(value.replace(/,/g, ''));
|
|
@@ -10,10 +16,11 @@ export const NumberUtil = {
|
|
|
10
16
|
},
|
|
11
17
|
|
|
12
18
|
/**
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
* @param
|
|
16
|
-
* @
|
|
19
|
+
* Multiplies two numbers using BigNumber for precision, especially with decimals.
|
|
20
|
+
* Handles undefined inputs by returning BigNumber(0).
|
|
21
|
+
* @param a - The first multiplicand (string, number, or BigNumber). Commas are removed if it's a string.
|
|
22
|
+
* @param b - The second multiplicand (string, number, or BigNumber). Commas are removed if it's a string.
|
|
23
|
+
* @returns The product as a BigNumber instance, or BigNumber(0) if either input is undefined.
|
|
17
24
|
*/
|
|
18
25
|
multiply(a: BigNumber.BigNumber.Value | undefined, b: BigNumber.BigNumber.Value | undefined) {
|
|
19
26
|
if (a === undefined || b === undefined) {
|
|
@@ -26,6 +33,13 @@ export const NumberUtil = {
|
|
|
26
33
|
return aBigNumber.multipliedBy(bBigNumber);
|
|
27
34
|
},
|
|
28
35
|
|
|
36
|
+
/**
|
|
37
|
+
* Rounds a number to a specified number of decimal places if its string representation meets a certain length threshold.
|
|
38
|
+
* @param number - The number to potentially round.
|
|
39
|
+
* @param threshold - The minimum string length of the number to trigger rounding.
|
|
40
|
+
* @param fixed - The number of decimal places to round to.
|
|
41
|
+
* @returns The rounded number (as a string if rounded, otherwise the original number) or the original number.
|
|
42
|
+
*/
|
|
29
43
|
roundNumber(number: number, threshold: number, fixed: number) {
|
|
30
44
|
const roundedNumber =
|
|
31
45
|
number.toString().length >= threshold ? Number(number).toFixed(fixed) : number;
|
|
@@ -33,6 +47,12 @@ export const NumberUtil = {
|
|
|
33
47
|
return roundedNumber;
|
|
34
48
|
},
|
|
35
49
|
|
|
50
|
+
/**
|
|
51
|
+
* Calculates the next multiple of ten greater than or equal to the given amount.
|
|
52
|
+
* Defaults to 10 if no amount is provided or if the calculated multiple is less than 10.
|
|
53
|
+
* @param amount - The number for which to find the next multiple of ten. Optional.
|
|
54
|
+
* @returns The next multiple of ten, at least 10.
|
|
55
|
+
*/
|
|
36
56
|
nextMultipleOfTen(amount?: number) {
|
|
37
57
|
if (!amount) return 10;
|
|
38
58
|
|
|
@@ -40,10 +60,10 @@ export const NumberUtil = {
|
|
|
40
60
|
},
|
|
41
61
|
|
|
42
62
|
/**
|
|
43
|
-
*
|
|
44
|
-
* @param value - The value to format
|
|
45
|
-
* @param decimals - number of
|
|
46
|
-
* @returns
|
|
63
|
+
* Formats a number or string to a human-readable string with a specified number of decimal places, using US locale formatting.
|
|
64
|
+
* @param value - The value to format (string, number, or undefined). If undefined, returns '0.00'.
|
|
65
|
+
* @param decimals - The number of decimal places to display. Defaults to 2.
|
|
66
|
+
* @returns A locale-formatted string representation of the number.
|
|
47
67
|
*/
|
|
48
68
|
formatNumberToLocalString(value: string | number | undefined, decimals = 2) {
|
|
49
69
|
if (value === undefined) {
|
|
@@ -62,10 +82,11 @@ export const NumberUtil = {
|
|
|
62
82
|
minimumFractionDigits: decimals
|
|
63
83
|
});
|
|
64
84
|
},
|
|
85
|
+
|
|
65
86
|
/**
|
|
66
|
-
*
|
|
67
|
-
* @param value - The formatted string to parse
|
|
68
|
-
* @returns
|
|
87
|
+
* Parses a locale-formatted numeric string (e.g., with commas) back into a number.
|
|
88
|
+
* @param value - The formatted string to parse. If undefined, returns 0.
|
|
89
|
+
* @returns The parsed number, or 0 if the input is undefined.
|
|
69
90
|
*/
|
|
70
91
|
parseLocalStringToNumber(value: string | undefined) {
|
|
71
92
|
if (value === undefined) {
|
|
@@ -74,5 +95,27 @@ export const NumberUtil = {
|
|
|
74
95
|
|
|
75
96
|
// Remove any commas used as thousand separators and parse the float
|
|
76
97
|
return parseFloat(value.replace(/,/gu, ''));
|
|
98
|
+
},
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Converts a numeric value (BigInt, number, or string representation of a number) to a 0x-prefixed hexadecimal string.
|
|
102
|
+
* This is often required for Ethereum RPC parameters like value, gas, gasPrice.
|
|
103
|
+
* @param value - The value to convert. Can be BigInt, number, or a string (decimal or hex).
|
|
104
|
+
* @returns A 0x-prefixed hexadecimal string, or undefined if the input is undefined or null.
|
|
105
|
+
* @throws Error if the value cannot be converted to BigInt.
|
|
106
|
+
*/
|
|
107
|
+
convertNumericToHexString: (value: any): string | undefined => {
|
|
108
|
+
if (value === undefined || value === null) {
|
|
109
|
+
return undefined;
|
|
110
|
+
}
|
|
111
|
+
try {
|
|
112
|
+
// This handles BigInt, number, or string representation of a number (decimal or hex)
|
|
113
|
+
const bigIntValue = BigInt(value);
|
|
114
|
+
// Ethereum RPC spec requires "0x0" for zero, and other values to be 0x-prefixed hex.
|
|
115
|
+
|
|
116
|
+
return '0x' + bigIntValue.toString(16);
|
|
117
|
+
} catch (error) {
|
|
118
|
+
throw new Error(`NumberUtil: Invalid parameter, cannot convert to hex: ${value}`);
|
|
119
|
+
}
|
|
77
120
|
}
|
|
78
121
|
};
|
package/src/utils/TypeUtil.ts
CHANGED
|
@@ -45,7 +45,7 @@ export interface Balance {
|
|
|
45
45
|
name: string;
|
|
46
46
|
symbol: string;
|
|
47
47
|
chainId: string;
|
|
48
|
-
address?:
|
|
48
|
+
address?: CaipAddress;
|
|
49
49
|
value?: number;
|
|
50
50
|
price: number;
|
|
51
51
|
quantity: BalanceQuantity;
|
|
@@ -146,23 +146,30 @@ export type Tokens = Record<CaipNetworkId, Token>;
|
|
|
146
146
|
|
|
147
147
|
export type ConnectorType = 'WALLET_CONNECT' | 'COINBASE' | 'AUTH' | 'EXTERNAL';
|
|
148
148
|
|
|
149
|
+
export type Metadata = {
|
|
150
|
+
name: string;
|
|
151
|
+
description: string;
|
|
152
|
+
url: string;
|
|
153
|
+
icons: string[];
|
|
154
|
+
redirect?: {
|
|
155
|
+
native?: string;
|
|
156
|
+
universal?: string;
|
|
157
|
+
linkMode?: boolean;
|
|
158
|
+
};
|
|
159
|
+
};
|
|
160
|
+
|
|
149
161
|
//********** Adapter Event Payloads **********//
|
|
150
162
|
export type AccountsChangedEvent = {
|
|
151
163
|
accounts: string[];
|
|
152
|
-
namespace: ChainNamespace;
|
|
153
164
|
};
|
|
154
165
|
|
|
155
166
|
export type ChainChangedEvent = {
|
|
156
167
|
chainId: string;
|
|
157
|
-
namespace: ChainNamespace;
|
|
158
168
|
};
|
|
159
169
|
|
|
160
|
-
export type DisconnectEvent = {
|
|
161
|
-
namespace: ChainNamespace;
|
|
162
|
-
};
|
|
170
|
+
export type DisconnectEvent = {};
|
|
163
171
|
|
|
164
172
|
export type BalanceChangedEvent = {
|
|
165
|
-
namespace: ChainNamespace;
|
|
166
173
|
address: CaipAddress;
|
|
167
174
|
balance: {
|
|
168
175
|
amount: string;
|
|
@@ -179,104 +186,6 @@ export interface AdapterEvents {
|
|
|
179
186
|
balanceChanged: (event: BalanceChangedEvent) => void;
|
|
180
187
|
}
|
|
181
188
|
|
|
182
|
-
//********** Adapter Types **********//
|
|
183
|
-
export abstract class BlockchainAdapter extends EventEmitter {
|
|
184
|
-
public projectId: string;
|
|
185
|
-
public connector?: WalletConnector;
|
|
186
|
-
public supportedNamespace: ChainNamespace;
|
|
187
|
-
|
|
188
|
-
// Typed emit method
|
|
189
|
-
override emit<K extends keyof AdapterEvents>(
|
|
190
|
-
event: K,
|
|
191
|
-
payload: Parameters<AdapterEvents[K]>[0]
|
|
192
|
-
): boolean {
|
|
193
|
-
return super.emit(event, payload);
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
constructor({
|
|
197
|
-
projectId,
|
|
198
|
-
supportedNamespace
|
|
199
|
-
}: {
|
|
200
|
-
projectId: string;
|
|
201
|
-
supportedNamespace: ChainNamespace;
|
|
202
|
-
}) {
|
|
203
|
-
super();
|
|
204
|
-
this.projectId = projectId;
|
|
205
|
-
this.supportedNamespace = supportedNamespace;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
setConnector(connector: WalletConnector) {
|
|
209
|
-
this.connector = connector;
|
|
210
|
-
this.subscribeToEvents();
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
removeConnector() {
|
|
214
|
-
this.connector = undefined;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
getProvider(): Provider {
|
|
218
|
-
if (!this.connector) throw new Error('No active connector');
|
|
219
|
-
|
|
220
|
-
return this.connector.getProvider();
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
subscribeToEvents(): void {
|
|
224
|
-
const provider = this.connector?.getProvider();
|
|
225
|
-
if (!provider) return;
|
|
226
|
-
|
|
227
|
-
provider.on('chainChanged', this.onChainChanged.bind(this));
|
|
228
|
-
provider.on('accountsChanged', this.onAccountsChanged.bind(this));
|
|
229
|
-
provider.on('disconnect', this.onDisconnect.bind(this));
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
onChainChanged(chainId: string): void {
|
|
233
|
-
const _chains = this.getAccounts()?.map(account => account.split(':')[1]);
|
|
234
|
-
const shouldEmit = _chains?.some(chain => chain === chainId);
|
|
235
|
-
|
|
236
|
-
if (shouldEmit) {
|
|
237
|
-
this.emit('chainChanged', { chainId, namespace: this.getSupportedNamespace() });
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
onAccountsChanged(accounts: string[]): void {
|
|
242
|
-
const _accounts = this.getAccounts();
|
|
243
|
-
const shouldEmit = _accounts?.some(account => {
|
|
244
|
-
const accountAddress = account.split(':')[2];
|
|
245
|
-
|
|
246
|
-
return accountAddress !== undefined && accounts.includes(accountAddress);
|
|
247
|
-
});
|
|
248
|
-
|
|
249
|
-
if (shouldEmit) {
|
|
250
|
-
this.emit('accountsChanged', { accounts, namespace: this.getSupportedNamespace() });
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
onDisconnect(): void {
|
|
255
|
-
this.emit('disconnect', { namespace: this.getSupportedNamespace() });
|
|
256
|
-
|
|
257
|
-
const provider = this.connector?.getProvider();
|
|
258
|
-
if (provider) {
|
|
259
|
-
provider.off('chainChanged', this.onChainChanged.bind(this));
|
|
260
|
-
provider.off('accountsChanged', this.onAccountsChanged.bind(this));
|
|
261
|
-
provider.off('disconnect', this.onDisconnect.bind(this));
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
this.connector = undefined;
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
abstract disconnect(): Promise<void>;
|
|
268
|
-
abstract getSupportedNamespace(): ChainNamespace;
|
|
269
|
-
abstract getBalance(params: GetBalanceParams): Promise<GetBalanceResponse>;
|
|
270
|
-
abstract getAccounts(): CaipAddress[] | undefined;
|
|
271
|
-
abstract switchNetwork(network: AppKitNetwork): Promise<void>;
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
export abstract class EVMAdapter extends BlockchainAdapter {
|
|
275
|
-
// ens logic
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
export abstract class SolanaBaseAdapter extends BlockchainAdapter {}
|
|
279
|
-
|
|
280
189
|
export interface GetBalanceParams {
|
|
281
190
|
address?: CaipAddress;
|
|
282
191
|
network?: AppKitNetwork;
|
|
@@ -305,25 +214,44 @@ export type Namespaces = Record<string, Namespace>;
|
|
|
305
214
|
|
|
306
215
|
export type ProposalNamespaces = Record<
|
|
307
216
|
string,
|
|
308
|
-
Omit<Namespace, 'accounts'> &
|
|
217
|
+
Omit<Namespace, 'accounts'> &
|
|
218
|
+
Required<Pick<Namespace, 'chains'>> & { rpcMap: Record<string, string> }
|
|
309
219
|
>;
|
|
310
220
|
|
|
221
|
+
export type ConnectOptions = {
|
|
222
|
+
namespaces?: ProposalNamespaces;
|
|
223
|
+
defaultChain?: CaipNetworkId;
|
|
224
|
+
universalLink?: string;
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
export type ConnectorInitOptions = {
|
|
228
|
+
storage: Storage;
|
|
229
|
+
metadata: Metadata;
|
|
230
|
+
};
|
|
231
|
+
|
|
311
232
|
export abstract class WalletConnector extends EventEmitter {
|
|
312
233
|
public type: New_ConnectorType;
|
|
313
|
-
protected provider
|
|
234
|
+
protected provider?: Provider;
|
|
314
235
|
protected namespaces?: Namespaces;
|
|
315
236
|
protected wallet?: WalletInfo;
|
|
237
|
+
protected storage?: Storage;
|
|
238
|
+
protected metadata?: Metadata;
|
|
316
239
|
|
|
317
|
-
constructor({ type
|
|
240
|
+
constructor({ type }: { type: New_ConnectorType }) {
|
|
318
241
|
super();
|
|
319
242
|
this.type = type;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
public async init(ops: ConnectorInitOptions) {
|
|
246
|
+
this.storage = ops.storage;
|
|
247
|
+
this.metadata = ops.metadata;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
public setProvider(provider: Provider) {
|
|
320
251
|
this.provider = provider;
|
|
321
252
|
}
|
|
322
253
|
|
|
323
|
-
abstract connect(opts:
|
|
324
|
-
namespaces?: ProposalNamespaces;
|
|
325
|
-
defaultChain?: CaipNetworkId;
|
|
326
|
-
}): Promise<Namespaces | undefined>;
|
|
254
|
+
abstract connect(opts: ConnectOptions): Promise<Namespaces | undefined>;
|
|
327
255
|
abstract disconnect(): Promise<void>;
|
|
328
256
|
abstract getProvider(): Provider;
|
|
329
257
|
abstract getNamespaces(): Namespaces;
|
|
@@ -352,7 +280,7 @@ export interface RequestArguments {
|
|
|
352
280
|
}
|
|
353
281
|
|
|
354
282
|
//TODO: rename this and remove the old one ConnectorType
|
|
355
|
-
export type New_ConnectorType = 'walletconnect' | 'coinbase' | 'auth';
|
|
283
|
+
export type New_ConnectorType = 'walletconnect' | 'coinbase' | 'auth' | 'phantom';
|
|
356
284
|
|
|
357
285
|
//********** Others **********//
|
|
358
286
|
|
|
@@ -375,3 +303,34 @@ export interface WalletInfo {
|
|
|
375
303
|
};
|
|
376
304
|
[key: string]: unknown;
|
|
377
305
|
}
|
|
306
|
+
|
|
307
|
+
export interface Storage {
|
|
308
|
+
/**
|
|
309
|
+
* Returns all keys in storage.
|
|
310
|
+
*/
|
|
311
|
+
getKeys(): Promise<string[]>;
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Returns all key-value entries in storage.
|
|
315
|
+
*/
|
|
316
|
+
getEntries<T = any>(): Promise<[string, T][]>;
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Get an item from storage for a given key.
|
|
320
|
+
* @param key The key to retrieve.
|
|
321
|
+
*/
|
|
322
|
+
getItem<T = any>(key: string): Promise<T | undefined>;
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Set an item in storage for a given key.
|
|
326
|
+
* @param key The key to set.
|
|
327
|
+
* @param value The value to set.
|
|
328
|
+
*/
|
|
329
|
+
setItem<T = any>(key: string, value: T): Promise<void>;
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Remove an item from storage for a given key.
|
|
333
|
+
* @param key The key to remove.
|
|
334
|
+
*/
|
|
335
|
+
removeItem(key: string): Promise<void>;
|
|
336
|
+
}
|