forked from MetaMask/metamask-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json-rpc-coverage.js
262 lines (237 loc) · 7.5 KB
/
json-rpc-coverage.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
'use strict';
import { web } from 'detox';
import detox from 'detox/internals';
import rpcCoverageTool from '@open-rpc/test-coverage';
import { parseOpenRPCDocument } from '@open-rpc/schema-utils-js';
import JsonSchemaFakerRule from '@open-rpc/test-coverage/build/rules/json-schema-faker-rule';
import HtmlReporter from '@open-rpc/test-coverage/build/reporters/html-reporter';
import Browser from '../pages/Browser/BrowserView';
// eslint-disable-next-line import/no-commonjs
const mockServer = require('@open-rpc/mock-server/build/index').default;
import TabBarComponent from '../pages/TabBarComponent';
import FixtureBuilder from '../fixtures/fixture-builder';
import {
withFixtures,
defaultGanacheOptions,
} from '../fixtures/fixture-helper';
import { loginToApp } from '../viewHelper';
import ExamplesRule from '@open-rpc/test-coverage/build/rules/examples-rule';
import ConfirmationsRejectRule from './ConfirmationsRejectionRule';
import { createDriverTransport } from './helpers';
import { BrowserViewSelectorsIDs } from '../selectors/Browser/BrowserView.selectors';
import { getGanachePort } from '../fixtures/utils';
const port = getGanachePort(8545, process.pid);
const chainId = 1337;
const main = async () => {
const openrpcDocument = await parseOpenRPCDocument(
'https://metamask.github.io/api-specs/0.10.8/openrpc.json',
);
const signTypedData4 = openrpcDocument.methods.find(
(m) => m.name === 'eth_signTypedData_v4',
);
const switchEthereumChain = openrpcDocument.methods.find(
(m) => m.name === 'wallet_switchEthereumChain',
);
switchEthereumChain.examples = [
{
name: 'wallet_switchEthereumChain',
description: 'Example of a wallet_switchEthereumChain request to sepolia',
params: [
{
name: 'SwitchEthereumChainParameter',
value: {
chainId: '0xaa36a7',
},
},
],
result: {
name: 'wallet_switchEthereumChain',
value: null,
},
},
];
const chainIdMethod = openrpcDocument.methods.find(
(m) => m.name === 'eth_chainId',
);
chainIdMethod.examples = [
{
name: 'chainIdExample',
description: 'Example of a chainId request',
params: [],
result: {
name: 'chainIdResult',
value: `0x${chainId.toString(16)}`,
},
},
];
const blockNumber = openrpcDocument.methods.find(
(m) => m.name === 'eth_blockNumber',
);
blockNumber.examples = [
{
name: 'blockNumberExample',
description: 'Example of a blockNumber request',
params: [],
result: {
name: 'blockNumberResult',
value: '0x1',
},
},
];
// just update address for signTypedData
signTypedData4.examples[0].params[0].value =
'0x76cf1CdD1fcC252442b50D6e97207228aA4aefC3';
signTypedData4.examples[0].params[1].value.domain.chainId = chainId;
const personalSign = openrpcDocument.methods.find(
(m) => m.name === 'personal_sign',
);
personalSign.examples = [
{
name: 'personalSignExample',
description: 'Example of a personalSign request',
params: [
{
name: 'data',
value: '0xdeadbeef',
},
{
name: 'address',
value: '0x76cf1CdD1fcC252442b50D6e97207228aA4aefC3',
},
],
result: {
name: 'personalSignResult',
value: '0x1a8819e0c9bab700',
},
},
];
const transaction =
openrpcDocument.components?.schemas?.TransactionInfo?.allOf?.[0];
if (transaction) {
delete transaction.unevaluatedProperties;
}
// net_version missing from execution-apis. see here: https://github.com/ethereum/execution-apis/issues/540
const netVersion = {
name: 'net_version',
params: [],
result: {
description: 'Returns the current network ID.',
name: 'net_version',
schema: {
type: 'string',
},
},
description: 'Returns the current network ID.',
examples: [
{
name: 'net_version',
description: 'Example of a net_version request',
params: [],
result: {
name: 'net_version',
description: 'The current network ID',
value: '0x1',
},
},
],
};
// add net_version
openrpcDocument.methods.push(netVersion);
const server = mockServer(port, openrpcDocument);
server.start();
await withFixtures(
{
dapp: true,
fixture: new FixtureBuilder().withGanacheNetwork().build(),
ganacheOptions: defaultGanacheOptions,
disableGanache: true,
restartDevice: true,
},
async () => {
await loginToApp();
await TabBarComponent.tapBrowser();
await Browser.navigateToTestDApp();
const myWebView = web(by.id(BrowserViewSelectorsIDs.BROWSER_WEBVIEW_ID));
const webElement = await myWebView.element(by.web.tag('body'));
const transport = createDriverTransport(webElement);
const methodsWithConfirmations = [
'wallet_requestPermissions',
// 'eth_requestAccounts', // mobile is missing revokePermissions to reset this to prompt and cancel
'wallet_watchAsset',
'personal_sign', // requires permissions for eth_accounts
'wallet_addEthereumChain',
'eth_signTypedData_v4', // requires permissions for eth_accounts
'wallet_switchEthereumChain',
'eth_getEncryptionPublicKey', // requires permissions for eth_accounts
];
// replace this with pulling tags out of the api-spec
// tag: Confirmations
const filteredMethods = openrpcDocument.methods
.filter(
(m) =>
m.name.includes('snap') ||
m.name.includes('Snap') ||
m.name.toLowerCase().includes('account') ||
m.name.includes('crypt') ||
m.name.includes('blob') ||
m.name.includes('sendTransaction') ||
m.name.startsWith('wallet_scanQRCode') ||
m.name.includes('filter') ||
m.name.includes('Filter') ||
m.name.includes('getBlockReceipts') || // eth_getBlockReceipts not support
m.name.includes('maxPriorityFeePerGas') || // eth_maxPriorityFeePerGas not supported
methodsWithConfirmations.includes(m.name),
)
.map((m) => m.name);
const skip = [
'wallet_revokePermissions', // mobile is missing revokePermissions
'eth_coinbase',
'wallet_registerOnboarding',
'eth_getEncryptionPublicKey',
'wallet_watchAsset',
];
const results = await rpcCoverageTool({
openrpcDocument,
transport,
reporters: [
'console-streaming',
new HtmlReporter({ autoOpen: !process.env.CI }),
],
rules: [
new JsonSchemaFakerRule({
only: [],
skip: filteredMethods,
numCalls: 1,
}),
new ExamplesRule({
only: [],
skip: filteredMethods,
}),
new ConfirmationsRejectRule({
driver: webElement,
only: methodsWithConfirmations,
}),
],
skip,
});
const failing = results.filter((r) => !r.valid);
await detox.cleanup();
// wait 1s to allow for cleanup
await new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, 1000);
});
process.exit(failing.length > 0 ? 1 : 0);
},
);
};
const start = async () => {
await detox.init({ workerId: null });
await detox.installWorker({
global: this.global,
workerId: `w1`,
});
await main();
};
start();