@meshconnect/uwc-core 0.2.13 → 0.2.14

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.
@@ -0,0 +1,329 @@
1
+ import { describe, it, expect } from 'vitest'
2
+ import { createNetworkRpcMap, getRpcUrlForNetwork } from './network-rpc-utils'
3
+ import type { Network, NetworkId } from '@meshconnect/uwc-types'
4
+
5
+ describe('network-rpc-utils', () => {
6
+ describe('createNetworkRpcMap', () => {
7
+ it('should create RPC map from networks with RPC URLs', () => {
8
+ const networks: Network[] = [
9
+ {
10
+ internalId: 1,
11
+ id: 'eip155:1',
12
+ namespace: 'eip155',
13
+ name: 'Ethereum Mainnet',
14
+ logoUrl: 'https://example.com/ethereum.png',
15
+ nativeCurrency: {
16
+ name: 'Ether',
17
+ symbol: 'ETH',
18
+ decimals: 18
19
+ },
20
+ rpcUrls: {
21
+ default: {
22
+ http: [
23
+ 'https://mainnet.infura.io/v3/123',
24
+ 'https://eth-mainnet.alchemyapi.io/v2/456'
25
+ ]
26
+ }
27
+ },
28
+ networkType: 'evm'
29
+ },
30
+ {
31
+ internalId: 2,
32
+ id: 'eip155:11155111',
33
+ namespace: 'eip155',
34
+ name: 'Sepolia Testnet',
35
+ logoUrl: 'https://example.com/sepolia.png',
36
+ nativeCurrency: {
37
+ name: 'Sepolia Ether',
38
+ symbol: 'SEP',
39
+ decimals: 18
40
+ },
41
+ rpcUrls: {
42
+ default: {
43
+ http: ['https://sepolia.infura.io/v3/123']
44
+ }
45
+ },
46
+ networkType: 'evm'
47
+ }
48
+ ]
49
+
50
+ const result = createNetworkRpcMap(networks)
51
+
52
+ expect(result).toEqual({
53
+ 'eip155:1': 'https://mainnet.infura.io/v3/123',
54
+ 'eip155:11155111': 'https://sepolia.infura.io/v3/123'
55
+ })
56
+ })
57
+
58
+ it('should ignore networks without RPC URLs', () => {
59
+ const networks: Network[] = [
60
+ {
61
+ internalId: 1,
62
+ id: 'eip155:1',
63
+ namespace: 'eip155',
64
+ name: 'Ethereum Mainnet',
65
+ logoUrl: 'https://example.com/ethereum.png',
66
+ nativeCurrency: {
67
+ name: 'Ether',
68
+ symbol: 'ETH',
69
+ decimals: 18
70
+ },
71
+ networkType: 'evm'
72
+ },
73
+ {
74
+ internalId: 2,
75
+ id: 'eip155:11155111',
76
+ namespace: 'eip155',
77
+ name: 'Sepolia Testnet',
78
+ logoUrl: 'https://example.com/sepolia.png',
79
+ nativeCurrency: {
80
+ name: 'Sepolia Ether',
81
+ symbol: 'SEP',
82
+ decimals: 18
83
+ },
84
+ rpcUrls: {
85
+ default: {
86
+ http: ['https://sepolia.infura.io/v3/123']
87
+ }
88
+ },
89
+ networkType: 'evm'
90
+ }
91
+ ]
92
+
93
+ const result = createNetworkRpcMap(networks)
94
+
95
+ expect(result).toEqual({
96
+ 'eip155:11155111': 'https://sepolia.infura.io/v3/123'
97
+ })
98
+ })
99
+
100
+ it('should handle networks with empty RPC URLs array', () => {
101
+ const networks: Network[] = [
102
+ {
103
+ internalId: 1,
104
+ id: 'eip155:1',
105
+ namespace: 'eip155',
106
+ name: 'Ethereum Mainnet',
107
+ logoUrl: 'https://example.com/ethereum.png',
108
+ nativeCurrency: {
109
+ name: 'Ether',
110
+ symbol: 'ETH',
111
+ decimals: 18
112
+ },
113
+ rpcUrls: {
114
+ default: {
115
+ http: []
116
+ }
117
+ },
118
+ networkType: 'evm'
119
+ }
120
+ ]
121
+
122
+ const result = createNetworkRpcMap(networks)
123
+
124
+ expect(result).toEqual({})
125
+ })
126
+
127
+ it('should handle mixed network types', () => {
128
+ const networks: Network[] = [
129
+ {
130
+ internalId: 1,
131
+ id: 'eip155:1',
132
+ namespace: 'eip155',
133
+ name: 'Ethereum Mainnet',
134
+ logoUrl: 'https://example.com/ethereum.png',
135
+ nativeCurrency: {
136
+ name: 'Ether',
137
+ symbol: 'ETH',
138
+ decimals: 18
139
+ },
140
+ rpcUrls: {
141
+ default: {
142
+ http: ['https://mainnet.infura.io/v3/123']
143
+ }
144
+ },
145
+ networkType: 'evm'
146
+ },
147
+ {
148
+ internalId: 2,
149
+ id: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
150
+ namespace: 'solana',
151
+ name: 'Solana Mainnet',
152
+ logoUrl: 'https://example.com/solana.png',
153
+ nativeCurrency: {
154
+ name: 'Solana',
155
+ symbol: 'SOL',
156
+ decimals: 9
157
+ },
158
+ rpcUrls: {
159
+ default: {
160
+ http: ['https://api.mainnet-beta.solana.com']
161
+ }
162
+ },
163
+ networkType: 'solana'
164
+ },
165
+ {
166
+ internalId: 3,
167
+ id: 'bip122:000000000019d6689c085ae165831e93',
168
+ namespace: 'bip122',
169
+ name: 'Bitcoin Mainnet',
170
+ logoUrl: 'https://example.com/bitcoin.png',
171
+ nativeCurrency: {
172
+ name: 'Bitcoin',
173
+ symbol: 'BTC',
174
+ decimals: 8
175
+ },
176
+ networkType: 'bitcoin'
177
+ }
178
+ ]
179
+
180
+ const result = createNetworkRpcMap(networks)
181
+
182
+ expect(result).toEqual({
183
+ 'eip155:1': 'https://mainnet.infura.io/v3/123',
184
+ 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp':
185
+ 'https://api.mainnet-beta.solana.com'
186
+ })
187
+ })
188
+ })
189
+
190
+ describe('getRpcUrlForNetwork', () => {
191
+ const mockRpcMap = {
192
+ 'eip155:1': 'https://mainnet.infura.io/v3/123',
193
+ 'eip155:11155111': 'https://sepolia.infura.io/v3/123',
194
+ 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp':
195
+ 'https://api.mainnet-beta.solana.com'
196
+ }
197
+
198
+ it('should return RPC URL for existing network ID', () => {
199
+ const result = getRpcUrlForNetwork(mockRpcMap, 'eip155:1')
200
+
201
+ expect(result).toBe('https://mainnet.infura.io/v3/123')
202
+ })
203
+
204
+ it('should return RPC URL for different network types', () => {
205
+ const ethereumResult = getRpcUrlForNetwork(mockRpcMap, 'eip155:11155111')
206
+ const solanaResult = getRpcUrlForNetwork(
207
+ mockRpcMap,
208
+ 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp'
209
+ )
210
+
211
+ expect(ethereumResult).toBe('https://sepolia.infura.io/v3/123')
212
+ expect(solanaResult).toBe('https://api.mainnet-beta.solana.com')
213
+ })
214
+
215
+ it('should return undefined for empty RPC map', () => {
216
+ const result = getRpcUrlForNetwork({}, 'eip155:1')
217
+
218
+ expect(result).toBeUndefined()
219
+ })
220
+
221
+ it('should handle all supported network IDs', () => {
222
+ const comprehensiveRpcMap = {
223
+ 'eip155:1': 'https://mainnet.infura.io/v3/123',
224
+ 'eip155:11155111': 'https://sepolia.infura.io/v3/123',
225
+ 'eip155:56': 'https://bsc-dataseed.binance.org',
226
+ 'eip155:137': 'https://polygon-rpc.com',
227
+ 'eip155:43114': 'https://api.avax.network/ext/bc/C/rpc',
228
+ 'eip155:42161': 'https://arb1.arbitrum.io/rpc',
229
+ 'eip155:10': 'https://mainnet.optimism.io',
230
+ 'eip155:8453': 'https://mainnet.base.org',
231
+ 'bip122:000000000019d6689c085ae165831e93':
232
+ 'https://bitcoin-mainnet.g.alchemy.com/v2/123',
233
+ 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp':
234
+ 'https://api.mainnet-beta.solana.com',
235
+ 'tron:0x2b6653dc': 'https://api.trongrid.io',
236
+ 'eip155:81457': 'https://rpc.blast.io',
237
+ 'bip122:1a91e3dace36e2be3bf030a65679fe82':
238
+ 'https://dogecoin-mainnet.g.alchemy.com/v2/123',
239
+ 'xrpl:0': 'https://xrplcluster.com',
240
+ 'bip122:12a765e31ffd4059bada1e25190f6e98':
241
+ 'https://litecoin-mainnet.g.alchemy.com/v2/123'
242
+ }
243
+
244
+ // Test a few key network IDs
245
+ expect(getRpcUrlForNetwork(comprehensiveRpcMap, 'eip155:1')).toBe(
246
+ 'https://mainnet.infura.io/v3/123'
247
+ )
248
+ expect(
249
+ getRpcUrlForNetwork(
250
+ comprehensiveRpcMap,
251
+ 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp'
252
+ )
253
+ ).toBe('https://api.mainnet-beta.solana.com')
254
+ expect(
255
+ getRpcUrlForNetwork(
256
+ comprehensiveRpcMap,
257
+ 'bip122:000000000019d6689c085ae165831e93'
258
+ )
259
+ ).toBe('https://bitcoin-mainnet.g.alchemy.com/v2/123')
260
+ expect(getRpcUrlForNetwork(comprehensiveRpcMap, 'tron:0x2b6653dc')).toBe(
261
+ 'https://api.trongrid.io'
262
+ )
263
+ expect(getRpcUrlForNetwork(comprehensiveRpcMap, 'xrpl:0')).toBe(
264
+ 'https://xrplcluster.com'
265
+ )
266
+ })
267
+ })
268
+
269
+ describe('integration tests', () => {
270
+ it('should work together to create and retrieve RPC URLs', () => {
271
+ const networks: Network[] = [
272
+ {
273
+ internalId: 1,
274
+ id: 'eip155:1',
275
+ namespace: 'eip155',
276
+ name: 'Ethereum Mainnet',
277
+ logoUrl: 'https://example.com/ethereum.png',
278
+ nativeCurrency: {
279
+ name: 'Ether',
280
+ symbol: 'ETH',
281
+ decimals: 18
282
+ },
283
+ rpcUrls: {
284
+ default: {
285
+ http: [
286
+ 'https://mainnet.infura.io/v3/123',
287
+ 'https://eth-mainnet.alchemyapi.io/v2/456'
288
+ ]
289
+ }
290
+ },
291
+ networkType: 'evm'
292
+ },
293
+ {
294
+ internalId: 2,
295
+ id: 'eip155:11155111',
296
+ namespace: 'eip155',
297
+ name: 'Sepolia Testnet',
298
+ logoUrl: 'https://example.com/sepolia.png',
299
+ nativeCurrency: {
300
+ name: 'Sepolia Ether',
301
+ symbol: 'SEP',
302
+ decimals: 18
303
+ },
304
+ rpcUrls: {
305
+ default: {
306
+ http: ['https://sepolia.infura.io/v3/123']
307
+ }
308
+ },
309
+ networkType: 'evm'
310
+ }
311
+ ]
312
+
313
+ // Create RPC map
314
+ const rpcMap = createNetworkRpcMap(networks)
315
+
316
+ // Retrieve RPC URLs
317
+ const ethereumRpc = getRpcUrlForNetwork(rpcMap, 'eip155:1')
318
+ const sepoliaRpc = getRpcUrlForNetwork(rpcMap, 'eip155:11155111')
319
+ const nonExistentRpc = getRpcUrlForNetwork(
320
+ rpcMap,
321
+ 'eip155:999999' as NetworkId
322
+ )
323
+
324
+ expect(ethereumRpc).toBe('https://mainnet.infura.io/v3/123')
325
+ expect(sepoliaRpc).toBe('https://sepolia.infura.io/v3/123')
326
+ expect(nonExistentRpc).toBeUndefined()
327
+ })
328
+ })
329
+ })
@@ -1,18 +0,0 @@
1
- import type { ConnectionMode, ConnectionModeState, Session, Chain } from '@meshconnect/uwc-types';
2
- export declare class ConnectionManager {
3
- private connectionMode;
4
- private session;
5
- private listeners;
6
- getConnectionMode(): ConnectionMode;
7
- getSession(): Session;
8
- setConnectionMode(mode: ConnectionMode): void;
9
- updateSession(updates: Partial<Session>): void;
10
- setActiveNetwork(network: Chain | null): void;
11
- setActiveAddress(address: string | null): void;
12
- setAvailableNetworks(networks: Chain[]): void;
13
- subscribe(listener: () => void): () => void;
14
- private notifyListeners;
15
- getState(): ConnectionModeState;
16
- }
17
- export declare const connectionManager: ConnectionManager;
18
- //# sourceMappingURL=connection-manager.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"connection-manager.d.ts","sourceRoot":"","sources":["../src/connection-manager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,cAAc,EACd,mBAAmB,EACnB,OAAO,EACP,KAAK,EACN,MAAM,wBAAwB,CAAA;AAE/B,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,cAAc,CAA6B;IACnD,OAAO,CAAC,OAAO,CAId;IACD,OAAO,CAAC,SAAS,CAA6B;IAE9C,iBAAiB,IAAI,cAAc;IAInC,UAAU,IAAI,OAAO;IAIrB,iBAAiB,CAAC,IAAI,EAAE,cAAc,GAAG,IAAI;IAK7C,aAAa,CAAC,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,IAAI;IAK9C,gBAAgB,CAAC,OAAO,EAAE,KAAK,GAAG,IAAI,GAAG,IAAI;IAK7C,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI;IAK9C,oBAAoB,CAAC,QAAQ,EAAE,KAAK,EAAE,GAAG,IAAI;IAK7C,SAAS,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI;IAO3C,OAAO,CAAC,eAAe;IAIvB,QAAQ,IAAI,mBAAmB;CAOhC;AAGD,eAAO,MAAM,iBAAiB,mBAA0B,CAAA"}
@@ -1,54 +0,0 @@
1
- export class ConnectionManager {
2
- connectionMode = 'injected';
3
- session = {
4
- activeNetwork: null,
5
- activeAddress: null,
6
- availableNetworks: []
7
- };
8
- listeners = new Set();
9
- getConnectionMode() {
10
- return this.connectionMode;
11
- }
12
- getSession() {
13
- return this.session;
14
- }
15
- setConnectionMode(mode) {
16
- this.connectionMode = mode;
17
- this.notifyListeners();
18
- }
19
- updateSession(updates) {
20
- this.session = { ...this.session, ...updates };
21
- this.notifyListeners();
22
- }
23
- setActiveNetwork(network) {
24
- this.session.activeNetwork = network;
25
- this.notifyListeners();
26
- }
27
- setActiveAddress(address) {
28
- this.session.activeAddress = address;
29
- this.notifyListeners();
30
- }
31
- setAvailableNetworks(networks) {
32
- this.session.availableNetworks = networks;
33
- this.notifyListeners();
34
- }
35
- subscribe(listener) {
36
- this.listeners.add(listener);
37
- return () => {
38
- this.listeners.delete(listener);
39
- };
40
- }
41
- notifyListeners() {
42
- this.listeners.forEach(listener => listener());
43
- }
44
- getState() {
45
- return {
46
- connectionMode: this.connectionMode,
47
- setConnectionMode: this.setConnectionMode.bind(this),
48
- session: this.session
49
- };
50
- }
51
- }
52
- // Default singleton instance for convenience
53
- export const connectionManager = new ConnectionManager();
54
- //# sourceMappingURL=connection-manager.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"connection-manager.js","sourceRoot":"","sources":["../src/connection-manager.ts"],"names":[],"mappings":"AAOA,MAAM,OAAO,iBAAiB;IACpB,cAAc,GAAmB,UAAU,CAAA;I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