forked from MetaMask/metamask-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-ethereum-chain.js
168 lines (153 loc) · 3.95 KB
/
add-ethereum-chain.js
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { ethErrors } from 'eth-rpc-errors';
import { ApprovalType } from '@metamask/controller-utils';
import { MESSAGE_TYPE } from '../../../../../shared/constants/app';
import {
findExistingNetwork,
validateAddEthereumChainParams,
switchChain,
} from './ethereum-chain-utils';
const addEthereumChain = {
methodNames: [MESSAGE_TYPE.ADD_ETHEREUM_CHAIN],
implementation: addEthereumChainHandler,
hookNames: {
upsertNetworkConfiguration: true,
getCurrentRpcUrl: true,
findNetworkConfigurationBy: true,
setActiveNetwork: true,
requestUserApproval: true,
startApprovalFlow: true,
endApprovalFlow: true,
getCurrentChainIdForDomain: true,
getCaveat: true,
requestPermittedChainsPermission: true,
getChainPermissionsFeatureFlag: true,
},
};
export default addEthereumChain;
async function addEthereumChainHandler(
req,
res,
_next,
end,
{
upsertNetworkConfiguration,
getCurrentRpcUrl,
findNetworkConfigurationBy,
setActiveNetwork,
requestUserApproval,
startApprovalFlow,
endApprovalFlow,
getCurrentChainIdForDomain,
getCaveat,
requestPermittedChainsPermission,
getChainPermissionsFeatureFlag,
},
) {
let validParams;
try {
validParams = validateAddEthereumChainParams(req.params[0], end);
} catch (error) {
return end(error);
}
const {
chainId,
chainName,
firstValidBlockExplorerUrl,
firstValidRPCUrl,
ticker,
} = validParams;
const { origin } = req;
const currentChainIdForDomain = getCurrentChainIdForDomain(origin);
const currentNetworkConfiguration = findExistingNetwork(
currentChainIdForDomain,
findNetworkConfigurationBy,
);
const existingNetwork = findExistingNetwork(
chainId,
findNetworkConfigurationBy,
);
if (
existingNetwork &&
existingNetwork.chainId === chainId &&
existingNetwork.ticker !== ticker
) {
return end(
ethErrors.rpc.invalidParams({
message: `nativeCurrency.symbol does not match currency symbol for a network the user already has added with the same chainId. Received:\n${ticker}`,
}),
);
}
let networkClientId;
let requestData;
let approvalFlowId;
if (!existingNetwork || existingNetwork.rpcUrl !== firstValidRPCUrl) {
({ id: approvalFlowId } = await startApprovalFlow());
try {
await requestUserApproval({
origin,
type: ApprovalType.AddEthereumChain,
requestData: {
chainId,
rpcPrefs: { blockExplorerUrl: firstValidBlockExplorerUrl },
chainName,
rpcUrl: firstValidRPCUrl,
ticker,
},
});
networkClientId = await upsertNetworkConfiguration(
{
chainId,
rpcPrefs: { blockExplorerUrl: firstValidBlockExplorerUrl },
nickname: chainName,
rpcUrl: firstValidRPCUrl,
ticker,
},
{ source: 'dapp', referrer: origin },
);
} catch (error) {
endApprovalFlow({ id: approvalFlowId });
return end(error);
}
requestData = {
toNetworkConfiguration: {
rpcUrl: firstValidRPCUrl,
chainId,
nickname: chainName,
ticker,
networkClientId,
},
fromNetworkConfiguration: currentNetworkConfiguration,
};
} else {
networkClientId = existingNetwork.id ?? existingNetwork.type;
const currentRpcUrl = getCurrentRpcUrl();
if (
currentChainIdForDomain === chainId &&
currentRpcUrl === firstValidRPCUrl
) {
res.result = null;
return end();
}
requestData = {
toNetworkConfiguration: existingNetwork,
fromNetworkConfiguration: currentNetworkConfiguration,
};
}
return switchChain(
res,
end,
origin,
chainId,
requestData,
networkClientId,
approvalFlowId,
{
getChainPermissionsFeatureFlag,
setActiveNetwork,
requestUserApproval,
getCaveat,
requestPermittedChainsPermission,
endApprovalFlow,
},
);
}