@rango-dev/provider-brave 0.1.11-next.69

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/src/index.ts ADDED
@@ -0,0 +1,131 @@
1
+ import {
2
+ Network,
3
+ evmBlockchains,
4
+ solanaBlockchain,
5
+ WalletType,
6
+ canSwitchNetworkToEvm,
7
+ chooseInstance,
8
+ getEvmAccounts,
9
+ switchNetworkForEvm,
10
+ CanSwitchNetwork,
11
+ Connect,
12
+ ProviderConnectResult,
13
+ Subscribe,
14
+ SwitchNetwork,
15
+ WalletSigners,
16
+ isEvmBlockchain,
17
+ isSolanaBlockchain,
18
+ getSolanaAccounts,
19
+ BlockchainMeta,
20
+ WalletInfo,
21
+ } from '@rango-dev/wallets-shared';
22
+ import { brave as brave_instances } from './helpers';
23
+ import signer from './signer';
24
+
25
+ const WALLET = WalletType.BRAVE;
26
+
27
+ export const config = {
28
+ type: WALLET,
29
+ defaultNetwork: Network.ETHEREUM,
30
+ };
31
+
32
+ export const getInstance = brave_instances;
33
+
34
+ export const connect: Connect = async ({ instance, meta }) => {
35
+ const evm_instance = chooseInstance(instance, meta, Network.ETHEREUM);
36
+ const sol_instance = chooseInstance(instance, meta, Network.SOLANA);
37
+ const results: ProviderConnectResult[] = [];
38
+ const emptyWalletErrorCode = -32603;
39
+ const emptyWalletCustomErrorMessage = 'Please create or import a wallet';
40
+ let numberOfEmptyWallets = 0;
41
+
42
+ if (evm_instance) {
43
+ try {
44
+ const evmAccounts = await getEvmAccounts(evm_instance);
45
+ results.push(evmAccounts);
46
+ } catch (error) {
47
+ // To resolve this error: Catch clause variable type annotation must be any or unknown if specified
48
+ const err = error as { code: number };
49
+ if (err.code === emptyWalletErrorCode) {
50
+ numberOfEmptyWallets += 1;
51
+ } else throw error;
52
+ }
53
+ }
54
+
55
+ if (sol_instance) {
56
+ try {
57
+ const solanaAccounts = await getSolanaAccounts({
58
+ instance: sol_instance,
59
+ meta,
60
+ });
61
+ results.push(solanaAccounts as ProviderConnectResult);
62
+ } catch (error) {
63
+ // To resolve this error: Catch clause variable type annotation must be any or unknown if specified
64
+ const err = error as { code: number };
65
+ if (err.code === emptyWalletErrorCode) {
66
+ numberOfEmptyWallets += 1;
67
+ } else throw error;
68
+ }
69
+ }
70
+
71
+ if (numberOfEmptyWallets === instance.size)
72
+ throw new Error(emptyWalletCustomErrorMessage);
73
+
74
+ return results;
75
+ };
76
+
77
+ export const subscribe: Subscribe = ({
78
+ instance,
79
+ updateAccounts,
80
+ meta,
81
+ state,
82
+ updateChainId,
83
+ }) => {
84
+ const evm_instance = chooseInstance(instance, meta, Network.ETHEREUM);
85
+ const sol_instance = chooseInstance(instance, meta, Network.SOLANA);
86
+
87
+ evm_instance?.on('accountsChanged', (addresses: string[]) => {
88
+ const eth_chainId = meta
89
+ .filter(isEvmBlockchain)
90
+ .find((blockchain) => blockchain.name === Network.ETHEREUM)?.chainId;
91
+ if (state.connected) {
92
+ if (state.network != Network.ETHEREUM && eth_chainId)
93
+ updateChainId(eth_chainId);
94
+ updateAccounts(addresses);
95
+ }
96
+ });
97
+
98
+ evm_instance?.on('chainChanged', (chainId: string) => {
99
+ updateChainId(chainId);
100
+ });
101
+
102
+ sol_instance?.on('accountChanged', async () => {
103
+ if (state.network != Network.SOLANA)
104
+ updateChainId(meta.filter(isSolanaBlockchain)[0].chainId);
105
+ const response = await sol_instance.connect();
106
+ const account: string = response.publicKey.toString();
107
+ updateAccounts([account]);
108
+ });
109
+ };
110
+
111
+ export const switchNetwork: SwitchNetwork = switchNetworkForEvm;
112
+
113
+ export const canSwitchNetworkTo: CanSwitchNetwork = canSwitchNetworkToEvm;
114
+
115
+ export const getSigners: (provider: any) => WalletSigners = signer;
116
+
117
+ export const getWalletInfo: (allBlockChains: BlockchainMeta[]) => WalletInfo = (
118
+ allBlockChains
119
+ ) => {
120
+ const evms = evmBlockchains(allBlockChains);
121
+ const solana = solanaBlockchain(allBlockChains);
122
+ return {
123
+ name: 'Brave',
124
+ img: 'https://raw.githubusercontent.com/rango-exchange/rango-types/main/assets/icons/wallets/brave.png',
125
+ installLink: {
126
+ DEFAULT: 'https://brave.com/wallet/',
127
+ },
128
+ color: '#ef342f',
129
+ supportedChains: [...evms, ...solana],
130
+ };
131
+ };
package/src/signer.ts ADDED
@@ -0,0 +1,14 @@
1
+ import {
2
+ WalletType,
3
+ WalletSigners,
4
+ defaultSigners,
5
+ } from '@rango-dev/wallets-shared';
6
+
7
+ export default function getSigners(provider: any): WalletSigners {
8
+ return defaultSigners({
9
+ provider,
10
+ walletType: WalletType.BRAVE,
11
+ supportEvm: true,
12
+ supportSolana: true,
13
+ });
14
+ }