@reown/appkit-common-react-native 2.0.0-alpha.0 → 2.0.0-alpha.2
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 +81 -0
- package/lib/commonjs/adapters/BlockchainAdapter.js.map +1 -0
- package/lib/commonjs/adapters/EvmAdapter.js +204 -0
- package/lib/commonjs/adapters/EvmAdapter.js.map +1 -0
- package/lib/commonjs/adapters/SolanaBaseAdapter.js +10 -0
- package/lib/commonjs/adapters/SolanaBaseAdapter.js.map +1 -0
- package/lib/commonjs/contracts/erc20.js.map +1 -1
- package/lib/commonjs/contracts/usdt.js.map +1 -1
- 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/package.json +1 -0
- package/lib/commonjs/utils/ConstantsUtil.js +31 -5
- package/lib/commonjs/utils/ConstantsUtil.js.map +1 -1
- package/lib/commonjs/utils/ContractUtil.js.map +1 -1
- package/lib/commonjs/utils/DateUtil.js.map +1 -1
- package/lib/commonjs/utils/ErrorUtil.js +2 -2
- package/lib/commonjs/utils/ErrorUtil.js.map +1 -1
- package/lib/commonjs/utils/NamesUtil.js.map +1 -1
- package/lib/commonjs/utils/NetworkUtil.js.map +1 -1
- package/lib/commonjs/utils/NumberUtil.js +53 -13
- package/lib/commonjs/utils/NumberUtil.js.map +1 -1
- package/lib/commonjs/utils/PresetsUtil.js +0 -18
- package/lib/commonjs/utils/PresetsUtil.js.map +1 -1
- package/lib/commonjs/utils/StringUtil.js +7 -0
- package/lib/commonjs/utils/StringUtil.js.map +1 -1
- package/lib/commonjs/utils/TypeUtil.js +19 -81
- package/lib/commonjs/utils/TypeUtil.js.map +1 -1
- package/lib/module/adapters/BlockchainAdapter.js +76 -0
- package/lib/module/adapters/BlockchainAdapter.js.map +1 -0
- package/lib/module/adapters/EvmAdapter.js +200 -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/contracts/erc20.js +2 -0
- package/lib/module/contracts/erc20.js.map +1 -1
- package/lib/module/contracts/usdt.js +2 -0
- package/lib/module/contracts/usdt.js.map +1 -1
- package/lib/module/index.js +7 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/networks/bitcoin.js +36 -0
- package/lib/module/networks/bitcoin.js.map +1 -0
- package/lib/module/networks/solana.js +74 -0
- package/lib/module/networks/solana.js.map +1 -0
- package/lib/module/utils/ConstantsUtil.js +33 -5
- package/lib/module/utils/ConstantsUtil.js.map +1 -1
- package/lib/module/utils/ContractUtil.js +2 -0
- package/lib/module/utils/ContractUtil.js.map +1 -1
- package/lib/module/utils/DateUtil.js +2 -0
- package/lib/module/utils/DateUtil.js.map +1 -1
- package/lib/module/utils/ErrorUtil.js +4 -2
- package/lib/module/utils/ErrorUtil.js.map +1 -1
- package/lib/module/utils/NamesUtil.js +2 -0
- package/lib/module/utils/NamesUtil.js.map +1 -1
- package/lib/module/utils/NetworkUtil.js +2 -0
- package/lib/module/utils/NetworkUtil.js.map +1 -1
- package/lib/module/utils/NumberUtil.js +54 -11
- package/lib/module/utils/NumberUtil.js.map +1 -1
- package/lib/module/utils/PresetsUtil.js +2 -18
- package/lib/module/utils/PresetsUtil.js.map +1 -1
- package/lib/module/utils/StringUtil.js +9 -0
- package/lib/module/utils/StringUtil.js.map +1 -1
- package/lib/module/utils/TypeUtil.js +19 -77
- package/lib/module/utils/TypeUtil.js.map +1 -1
- package/lib/typescript/adapters/BlockchainAdapter.d.ts +25 -0
- package/lib/typescript/adapters/BlockchainAdapter.d.ts.map +1 -0
- package/lib/typescript/adapters/EvmAdapter.d.ts +27 -0
- package/lib/typescript/adapters/EvmAdapter.d.ts.map +1 -0
- package/lib/typescript/adapters/SolanaBaseAdapter.d.ts +6 -0
- package/lib/typescript/adapters/SolanaBaseAdapter.d.ts.map +1 -0
- package/lib/typescript/adapters/__tests__/EvmAdapter.test.d.ts +2 -0
- package/lib/typescript/adapters/__tests__/EvmAdapter.test.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/ConstantsUtil.d.ts +27 -3
- package/lib/typescript/utils/ConstantsUtil.d.ts.map +1 -1
- package/lib/typescript/utils/NumberUtil.d.ts +39 -11
- package/lib/typescript/utils/NumberUtil.d.ts.map +1 -1
- package/lib/typescript/utils/PresetsUtil.d.ts +0 -5
- package/lib/typescript/utils/PresetsUtil.d.ts.map +1 -1
- package/lib/typescript/utils/StringUtil.d.ts +1 -0
- package/lib/typescript/utils/StringUtil.d.ts.map +1 -1
- package/lib/typescript/utils/TypeUtil.d.ts +203 -68
- package/lib/typescript/utils/TypeUtil.d.ts.map +1 -1
- package/package.json +7 -17
- package/src/adapters/BlockchainAdapter.ts +107 -0
- package/src/adapters/EvmAdapter.ts +259 -0
- package/src/adapters/SolanaBaseAdapter.ts +6 -0
- package/src/adapters/__tests__/EvmAdapter.test.ts +126 -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 +33 -6
- package/src/utils/ErrorUtil.ts +2 -2
- package/src/utils/NumberUtil.ts +54 -11
- package/src/utils/PresetsUtil.ts +0 -25
- package/src/utils/StringUtil.ts +7 -0
- package/src/utils/TypeUtil.ts +259 -143
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
import { BlockchainAdapter } from './BlockchainAdapter';
|
|
2
|
+
import { NumberUtil } from '../utils/NumberUtil';
|
|
3
|
+
import type { AppKitNetwork } from '../utils/TypeUtil';
|
|
4
|
+
|
|
5
|
+
// Type definitions for writeContract
|
|
6
|
+
export interface WriteContractData {
|
|
7
|
+
tokenAddress: `0x${string}`;
|
|
8
|
+
receiverAddress: `0x${string}`;
|
|
9
|
+
tokenAmount: bigint;
|
|
10
|
+
fromAddress: `0x${string}`;
|
|
11
|
+
method: 'transfer' | 'transferFrom' | 'approve';
|
|
12
|
+
abi?: any; // Optional ABI for future extensibility
|
|
13
|
+
spenderAddress?: `0x${string}`; // Required for transferFrom and approve
|
|
14
|
+
network: AppKitNetwork;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface SendTransactionData {
|
|
18
|
+
address: `0x${string}`;
|
|
19
|
+
network: AppKitNetwork;
|
|
20
|
+
to: `0x${string}`;
|
|
21
|
+
value: string;
|
|
22
|
+
data: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Simple ABI encoder for ERC20 functions
|
|
26
|
+
function encodeERC20Function(method: string, params: any[]): string {
|
|
27
|
+
const functionSelectors = {
|
|
28
|
+
transfer: '0xa9059cbb', // transfer(address,uint256)
|
|
29
|
+
transferFrom: '0x23b872dd', // transferFrom(address,address,uint256)
|
|
30
|
+
approve: '0x095ea7b3' // approve(address,uint256)
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const selector = functionSelectors[method as keyof typeof functionSelectors];
|
|
34
|
+
if (!selector) {
|
|
35
|
+
throw new Error(`EVMAdapter:encodeERC20Function - unsupported method: ${method}`);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
let encodedParams = '';
|
|
39
|
+
|
|
40
|
+
switch (method) {
|
|
41
|
+
case 'transfer':
|
|
42
|
+
if (params.length !== 2) throw new Error('transfer requires 2 parameters: to, amount');
|
|
43
|
+
const [to, amount] = params;
|
|
44
|
+
encodedParams =
|
|
45
|
+
to.toLowerCase().slice(2).padStart(64, '0') + amount.toString(16).padStart(64, '0');
|
|
46
|
+
break;
|
|
47
|
+
|
|
48
|
+
case 'transferFrom':
|
|
49
|
+
if (params.length !== 3)
|
|
50
|
+
throw new Error('transferFrom requires 3 parameters: from, to, amount');
|
|
51
|
+
const [from, toTransferFrom, amountTransferFrom] = params;
|
|
52
|
+
encodedParams =
|
|
53
|
+
from.toLowerCase().slice(2).padStart(64, '0') +
|
|
54
|
+
toTransferFrom.toLowerCase().slice(2).padStart(64, '0') +
|
|
55
|
+
amountTransferFrom.toString(16).padStart(64, '0');
|
|
56
|
+
break;
|
|
57
|
+
|
|
58
|
+
case 'approve':
|
|
59
|
+
if (params.length !== 2) throw new Error('approve requires 2 parameters: spender, amount');
|
|
60
|
+
const [spender, amountApprove] = params;
|
|
61
|
+
encodedParams =
|
|
62
|
+
spender.toLowerCase().slice(2).padStart(64, '0') +
|
|
63
|
+
amountApprove.toString(16).padStart(64, '0');
|
|
64
|
+
break;
|
|
65
|
+
|
|
66
|
+
default:
|
|
67
|
+
throw new Error(`EVMAdapter:encodeERC20Function - unsupported method: ${method}`);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return selector + encodedParams;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export abstract class EVMAdapter extends BlockchainAdapter {
|
|
74
|
+
override subscribeToEvents(): void {
|
|
75
|
+
super.subscribeToEvents();
|
|
76
|
+
const provider = this.getProvider();
|
|
77
|
+
if (!provider) return;
|
|
78
|
+
|
|
79
|
+
// provider.on('reown_updateEmail', (info: any) => {
|
|
80
|
+
// // this.emit('updateEmail', email);
|
|
81
|
+
// //TODO: check this
|
|
82
|
+
// // console.log('reown_updateEmail', info);
|
|
83
|
+
// });
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async signMessage(address: string, message: string, chain?: string): Promise<string> {
|
|
87
|
+
const provider = this.getProvider();
|
|
88
|
+
|
|
89
|
+
if (!provider) {
|
|
90
|
+
throw new Error('EVMAdapter:signMessage - provider is undefined');
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const signature = await provider.request(
|
|
94
|
+
{
|
|
95
|
+
method: 'personal_sign',
|
|
96
|
+
params: [message, address]
|
|
97
|
+
},
|
|
98
|
+
`eip155:${chain}`
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
return signature as string;
|
|
102
|
+
}
|
|
103
|
+
async estimateGas({ address, to, data, chainNamespace }: any): Promise<bigint> {
|
|
104
|
+
const provider = this.getProvider();
|
|
105
|
+
|
|
106
|
+
if (!provider) {
|
|
107
|
+
throw new Error('EVMAdapter:estimateGas - provider is undefined');
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (!address) {
|
|
111
|
+
throw new Error('EVMAdapter:estimateGas - from address is undefined');
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (chainNamespace && chainNamespace !== 'eip155') {
|
|
115
|
+
throw new Error('EVMAdapter:estimateGas - chainNamespace is not eip155');
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
try {
|
|
119
|
+
const txParams = {
|
|
120
|
+
from: address,
|
|
121
|
+
to,
|
|
122
|
+
data,
|
|
123
|
+
type: '0x0' // optional, legacy type
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
const estimatedGasHex = await provider.request({
|
|
127
|
+
method: 'eth_estimateGas',
|
|
128
|
+
params: [txParams]
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
return BigInt(estimatedGasHex as string);
|
|
132
|
+
} catch (error) {
|
|
133
|
+
throw new Error('EVMAdapter:estimateGas - eth_estimateGas RPC failed');
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
async sendTransaction(data: SendTransactionData): Promise<`0x${string}` | null> {
|
|
138
|
+
const { address, network } = data || {};
|
|
139
|
+
|
|
140
|
+
if (!this.getProvider()) {
|
|
141
|
+
throw new Error('EVMAdapter:sendTransaction - provider is undefined');
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (!address) {
|
|
145
|
+
throw new Error('EVMAdapter:sendTransaction - address is undefined');
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (!network) {
|
|
149
|
+
throw new Error('EVMAdapter:sendTransaction - network is undefined');
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const txParams = {
|
|
153
|
+
from: address,
|
|
154
|
+
to: data.to,
|
|
155
|
+
value: NumberUtil.convertNumericToHexString(data.value),
|
|
156
|
+
data: data.data, // hex-encoded bytecode
|
|
157
|
+
type: '0x0' // optional: legacy transaction type
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
const txHash = await this.getProvider().request(
|
|
161
|
+
{
|
|
162
|
+
method: 'eth_sendTransaction',
|
|
163
|
+
params: [txParams]
|
|
164
|
+
},
|
|
165
|
+
network.caipNetworkId
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
return txHash as `0x${string}` | null;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
async writeContract(data: WriteContractData): Promise<`0x${string}` | null> {
|
|
172
|
+
const {
|
|
173
|
+
tokenAddress,
|
|
174
|
+
receiverAddress,
|
|
175
|
+
tokenAmount,
|
|
176
|
+
method,
|
|
177
|
+
fromAddress,
|
|
178
|
+
spenderAddress,
|
|
179
|
+
network
|
|
180
|
+
} = data;
|
|
181
|
+
|
|
182
|
+
if (!this.getProvider()) {
|
|
183
|
+
throw new Error('EVMAdapter:writeContract - provider is undefined');
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (!fromAddress) {
|
|
187
|
+
throw new Error('EVMAdapter:writeContract - fromAddress is undefined');
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (!tokenAddress) {
|
|
191
|
+
throw new Error('EVMAdapter:writeContract - tokenAddress is undefined');
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (!tokenAmount) {
|
|
195
|
+
throw new Error('EVMAdapter:writeContract - tokenAmount is undefined');
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (!method) {
|
|
199
|
+
throw new Error('EVMAdapter:writeContract - method is undefined');
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// Validate method-specific parameters
|
|
203
|
+
if (method === 'transfer' && !receiverAddress) {
|
|
204
|
+
throw new Error('EVMAdapter:writeContract - receiverAddress is required for transfer method');
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if ((method === 'transferFrom' || method === 'approve') && !spenderAddress) {
|
|
208
|
+
throw new Error(`EVMAdapter:writeContract - spenderAddress is required for ${method} method`);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// Encode the function call data based on method
|
|
212
|
+
let encodedData: string;
|
|
213
|
+
|
|
214
|
+
switch (method) {
|
|
215
|
+
case 'transfer':
|
|
216
|
+
encodedData = encodeERC20Function('transfer', [receiverAddress, tokenAmount]);
|
|
217
|
+
break;
|
|
218
|
+
case 'transferFrom':
|
|
219
|
+
encodedData = encodeERC20Function('transferFrom', [
|
|
220
|
+
fromAddress,
|
|
221
|
+
receiverAddress,
|
|
222
|
+
tokenAmount
|
|
223
|
+
]);
|
|
224
|
+
break;
|
|
225
|
+
case 'approve':
|
|
226
|
+
encodedData = encodeERC20Function('approve', [spenderAddress, tokenAmount]);
|
|
227
|
+
break;
|
|
228
|
+
default:
|
|
229
|
+
throw new Error(`EVMAdapter:writeContract - method '${method}' is not supported`);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// Create transaction parameters
|
|
233
|
+
const txParams = {
|
|
234
|
+
from: fromAddress,
|
|
235
|
+
to: tokenAddress,
|
|
236
|
+
data: encodedData,
|
|
237
|
+
value: '0x0', // No ETH value for token operations
|
|
238
|
+
type: '0x0' // legacy transaction type
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
try {
|
|
242
|
+
// Send the transaction
|
|
243
|
+
const txHash = await this.getProvider().request(
|
|
244
|
+
{
|
|
245
|
+
method: 'eth_sendTransaction',
|
|
246
|
+
params: [txParams]
|
|
247
|
+
},
|
|
248
|
+
network.caipNetworkId
|
|
249
|
+
);
|
|
250
|
+
|
|
251
|
+
return txHash as `0x${string}` | null;
|
|
252
|
+
} catch (error) {
|
|
253
|
+
if (error instanceof Error) {
|
|
254
|
+
throw new Error(`EVMAdapter:writeContract - ${error.message}`);
|
|
255
|
+
}
|
|
256
|
+
throw new Error('EVMAdapter:writeContract - unknown error occurred');
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { EVMAdapter } from '../EvmAdapter';
|
|
2
|
+
import type { AppKitNetwork, CaipAddress } from '../../utils/TypeUtil';
|
|
3
|
+
|
|
4
|
+
const network: AppKitNetwork = {
|
|
5
|
+
id: 1,
|
|
6
|
+
name: 'Ethereum',
|
|
7
|
+
nativeCurrency: { name: 'Ether', symbol: 'ETH', decimals: 18 },
|
|
8
|
+
rpcUrls: {
|
|
9
|
+
default: {
|
|
10
|
+
http: ['https://eth.merkle.io']
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
blockExplorers: {
|
|
14
|
+
default: {
|
|
15
|
+
name: 'Etherscan',
|
|
16
|
+
url: 'https://etherscan.io'
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
chainNamespace: 'eip155',
|
|
20
|
+
caipNetworkId: 'eip155:1'
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
// Mock implementation for testing
|
|
24
|
+
class MockEVMAdapter extends EVMAdapter {
|
|
25
|
+
private mockProvider: any;
|
|
26
|
+
|
|
27
|
+
constructor() {
|
|
28
|
+
super({ projectId: 'test', supportedNamespace: 'eip155', adapterType: 'ethers' });
|
|
29
|
+
this.mockProvider = {
|
|
30
|
+
request: jest.fn(),
|
|
31
|
+
on: jest.fn(),
|
|
32
|
+
off: jest.fn()
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
setMockProvider(provider: any) {
|
|
37
|
+
this.mockProvider = provider;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
override getProvider() {
|
|
41
|
+
return this.mockProvider;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async disconnect(): Promise<void> {}
|
|
45
|
+
getSupportedNamespace() {
|
|
46
|
+
return 'eip155' as const;
|
|
47
|
+
}
|
|
48
|
+
async getBalance() {
|
|
49
|
+
return { amount: '0', symbol: 'ETH' };
|
|
50
|
+
}
|
|
51
|
+
getAccounts(): CaipAddress[] | undefined {
|
|
52
|
+
return ['eip155:1:0x1234567890123456789012345678901234567890'];
|
|
53
|
+
}
|
|
54
|
+
async switchNetwork() {}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
describe('EVMAdapter', () => {
|
|
58
|
+
let adapter: MockEVMAdapter;
|
|
59
|
+
|
|
60
|
+
beforeEach(() => {
|
|
61
|
+
adapter = new MockEVMAdapter();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
describe('writeContract', () => {
|
|
65
|
+
it('should encode transfer function correctly', async () => {
|
|
66
|
+
const mockProvider = {
|
|
67
|
+
request: jest.fn().mockResolvedValueOnce('0x1234567890abcdef') // eth_sendTransaction
|
|
68
|
+
};
|
|
69
|
+
adapter.setMockProvider(mockProvider);
|
|
70
|
+
|
|
71
|
+
const result = await adapter.writeContract({
|
|
72
|
+
tokenAddress: '0x1234567890123456789012345678901234567890',
|
|
73
|
+
receiverAddress: '0xabcdefabcdefabcdefabcdefabcdefabcdefabcd',
|
|
74
|
+
tokenAmount: BigInt(1000000000000000000), // 1 token with 18 decimals
|
|
75
|
+
fromAddress: '0x1234567890123456789012345678901234567890',
|
|
76
|
+
method: 'transfer',
|
|
77
|
+
network
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
expect(result).toBe('0x1234567890abcdef');
|
|
81
|
+
expect(mockProvider.request).toHaveBeenCalledWith(
|
|
82
|
+
{
|
|
83
|
+
method: 'eth_sendTransaction',
|
|
84
|
+
params: [
|
|
85
|
+
{
|
|
86
|
+
from: '0x1234567890123456789012345678901234567890',
|
|
87
|
+
to: '0x1234567890123456789012345678901234567890',
|
|
88
|
+
data: '0xa9059cbb000000000000000000000000abcdefabcdefabcdefabcdefabcdefabcdefabcd0000000000000000000000000000000000000000000000000de0b6b3a7640000',
|
|
89
|
+
value: '0x0',
|
|
90
|
+
type: '0x0'
|
|
91
|
+
}
|
|
92
|
+
]
|
|
93
|
+
},
|
|
94
|
+
'eip155:1'
|
|
95
|
+
);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('should throw error for unsupported method', async () => {
|
|
99
|
+
await expect(
|
|
100
|
+
adapter.writeContract({
|
|
101
|
+
tokenAddress: '0x1234567890123456789012345678901234567890',
|
|
102
|
+
receiverAddress: '0xabcdefabcdefabcdefabcdefabcdefabcdefabcd',
|
|
103
|
+
tokenAmount: BigInt(1000000000000000000),
|
|
104
|
+
fromAddress: '0x1234567890123456789012345678901234567890',
|
|
105
|
+
method: 'unsupported' as any,
|
|
106
|
+
network
|
|
107
|
+
})
|
|
108
|
+
).rejects.toThrow("method 'unsupported' is not supported");
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('should throw error when provider is undefined', async () => {
|
|
112
|
+
adapter.setMockProvider(undefined);
|
|
113
|
+
|
|
114
|
+
await expect(
|
|
115
|
+
adapter.writeContract({
|
|
116
|
+
tokenAddress: '0x1234567890123456789012345678901234567890',
|
|
117
|
+
receiverAddress: '0xabcdefabcdefabcdefabcdefabcdefabcdefabcd',
|
|
118
|
+
tokenAmount: BigInt(1000000000000000000),
|
|
119
|
+
fromAddress: '0x1234567890123456789012345678901234567890',
|
|
120
|
+
method: 'transfer',
|
|
121
|
+
network
|
|
122
|
+
})
|
|
123
|
+
).rejects.toThrow('provider is undefined');
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
});
|
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
|
+
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export const ConstantsUtil = {
|
|
2
|
-
VERSION: '2.0.0-alpha.
|
|
2
|
+
VERSION: '2.0.0-alpha.2',
|
|
3
3
|
|
|
4
4
|
EIP155: 'eip155',
|
|
5
5
|
ADD_CHAIN_METHOD: 'wallet_addEthereumChain',
|
|
@@ -11,12 +11,14 @@ export const ConstantsUtil = {
|
|
|
11
11
|
BLOCKCHAIN_API_RPC_URL_STAGING: 'https://staging.rpc.walletconnect.org',
|
|
12
12
|
PULSE_API_URL: 'https://pulse.walletconnect.org',
|
|
13
13
|
API_URL: 'https://api.web3modal.org',
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
AUTH_CONNECTOR_ID: 'appKitAuth',
|
|
14
|
+
WEB_WALLET_URL: 'https://web-wallet.walletconnect.org',
|
|
15
|
+
SECURE_SITE_DASHBOARD: `https://secure.reown.com/dashboard`,
|
|
16
|
+
SECURE_SITE_ICON: `https://secure.reown.com/images/favicon.png`,
|
|
18
17
|
|
|
19
18
|
COINBASE_EXPLORER_ID: 'fd20dc426fb37566d803205b19bbc1d4096b248ac04548e3cfb6b3a38bd033aa',
|
|
19
|
+
PHANTOM_EXPLORER_ID: 'a797aa35c0fadbfc1a53e7f675162ed5226968b44a19ee3d24385c64d1d3c393',
|
|
20
|
+
|
|
21
|
+
WALLET_CONNECT_IMAGE_ID: 'ef1a1fcf-7fe8-4d69-bd6d-fda1345b4400',
|
|
20
22
|
|
|
21
23
|
USDT_CONTRACT_ADDRESSES: [
|
|
22
24
|
// Mainnet
|
|
@@ -33,5 +35,30 @@ export const ConstantsUtil = {
|
|
|
33
35
|
'0x55d398326f99059fF775485246999027B3197955',
|
|
34
36
|
// Arbitrum
|
|
35
37
|
'0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9'
|
|
36
|
-
]
|
|
38
|
+
],
|
|
39
|
+
|
|
40
|
+
PHANTOM_CUSTOM_WALLET: {
|
|
41
|
+
id: 'a797aa35c0fadbfc1a53e7f675162ed5226968b44a19ee3d24385c64d1d3c393',
|
|
42
|
+
name: 'Phantom Wallet',
|
|
43
|
+
image_id: 'b6ec7b81-bb4f-427d-e290-7631e6e50d00',
|
|
44
|
+
mobile_link: 'phantom://'
|
|
45
|
+
},
|
|
46
|
+
|
|
47
|
+
// Storage Keys
|
|
48
|
+
STORAGE_KEYS: {
|
|
49
|
+
WC_DEEPLINK: 'WALLETCONNECT_DEEPLINK_CHOICE', //dont change this one
|
|
50
|
+
RECENT_WALLET: '@appkit/recent_wallet',
|
|
51
|
+
CONNECTED_WALLET_IMAGE_URL: '@appkit/connected_wallet_image_url',
|
|
52
|
+
CONNECTED_CONNECTORS: '@appkit/connected_connectors',
|
|
53
|
+
CONNECTED_SOCIAL: '@appkit/connected_social',
|
|
54
|
+
ONRAMP_PREFERRED_COUNTRY: '@appkit/onramp_preferred_country',
|
|
55
|
+
ONRAMP_COUNTRIES: '@appkit/onramp_countries',
|
|
56
|
+
ONRAMP_SERVICE_PROVIDERS: '@appkit/onramp_service_providers',
|
|
57
|
+
ONRAMP_FIAT_LIMITS: '@appkit/onramp_fiat_limits',
|
|
58
|
+
ONRAMP_FIAT_CURRENCIES: '@appkit/onramp_fiat_currencies',
|
|
59
|
+
ONRAMP_PREFERRED_FIAT_CURRENCY: '@appkit/onramp_preferred_fiat_currency',
|
|
60
|
+
ONRAMP_COUNTRIES_DEFAULTS: '@appkit/onramp_countries_defaults',
|
|
61
|
+
ACTIVE_NAMESPACE: '@appkit/active_namespace',
|
|
62
|
+
COINBASE_CONNECTOR_SESSION: '@appkit/coinbase_connector/session'
|
|
63
|
+
}
|
|
37
64
|
};
|
package/src/utils/ErrorUtil.ts
CHANGED
|
@@ -16,12 +16,12 @@ export const ErrorUtil = {
|
|
|
16
16
|
ALERT_ERRORS: {
|
|
17
17
|
INVALID_APP_CONFIGURATION: {
|
|
18
18
|
shortMessage: 'Invalid App Configuration',
|
|
19
|
-
longMessage: `Bundle ID not found on Allowlist - Please verify that your bundle ID is allowed at https://
|
|
19
|
+
longMessage: `Bundle ID not found on Allowlist - Please verify that your bundle ID is allowed at https://dashboard.reown.com`
|
|
20
20
|
},
|
|
21
21
|
SOCIALS_TIMEOUT: {
|
|
22
22
|
shortMessage: 'Invalid App Configuration',
|
|
23
23
|
longMessage:
|
|
24
|
-
'There was an issue loading the embedded wallet. Please verify that your bundle ID is allowed at https://
|
|
24
|
+
'There was an issue loading the embedded wallet. Please verify that your bundle ID is allowed at https://dashboard.reown.com'
|
|
25
25
|
},
|
|
26
26
|
JWT_TOKEN_NOT_VALID: {
|
|
27
27
|
shortMessage: 'Session Expired',
|
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
|
};
|