-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
App.tsx
497 lines (435 loc) · 12.3 KB
/
App.tsx
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
import * as React from "react";
import styled from "styled-components";
import Web3 from "web3";
import Web3Connect from "web3connect";
// @ts-ignore
import WalletConnectProvider from "@walletconnect/web3-provider";
import Portis from "@portis/web3";
// @ts-ignore
// import Squarelink from 'squarelink'
import Torus from '@toruslabs/torus-embed'
// @ts-ignore
import Fortmatic from "fortmatic";
import Arkane from "@arkane-network/web3-arkane-provider";
import Authereum from "authereum";
import { convertUtf8ToHex } from "@walletconnect/utils";
import Button from "./components/Button";
import Column from "./components/Column";
import Wrapper from "./components/Wrapper";
import Modal from "./components/Modal";
import Header from "./components/Header";
import Loader from "./components/Loader";
import AccountAssets from "./components/AccountAssets";
import { apiGetAccountAssets } from "./helpers/api";
import {
hashPersonalMessage,
recoverPublicKey,
recoverPersonalSignature,
formatTestTransaction
} from "./helpers/utilities";
import { IAssetData } from "./helpers/types";
import { fonts } from "./styles";
const SLayout = styled.div`
position: relative;
width: 100%;
min-height: 100vh;
text-align: center;
`;
const SContent = styled(Wrapper)`
width: 100%;
height: 100%;
padding: 0 16px;
`;
const SContainer = styled.div`
height: 100%;
min-height: 200px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
word-break: break-word;
`;
const SLanding = styled(Column)`
height: 600px;
`;
const SModalContainer = styled.div`
width: 100%;
position: relative;
word-wrap: break-word;
`;
const SModalTitle = styled.div`
margin: 1em 0;
font-size: 20px;
font-weight: 700;
`;
const SModalParagraph = styled.p`
margin-top: 30px;
`;
const SBalances = styled(SLanding)`
height: 100%;
& h3 {
padding-top: 30px;
}
`;
const STable = styled(SContainer)`
flex-direction: column;
text-align: left;
`;
const SRow = styled.div`
width: 100%;
display: flex;
margin: 6px 0;
`;
const SKey = styled.div`
width: 30%;
font-weight: 700;
`;
const SValue = styled.div`
width: 70%;
font-family: monospace;
`;
const STestButtonContainer = styled.div`
width: 100%;
display: flex;
justify-content: center;
align-items: center;
flex-wrap: wrap;
`;
const STestButton = styled(Button)`
border-radius: 8px;
font-size: ${fonts.size.medium};
height: 44px;
width: 100%;
max-width: 175px;
margin: 12px;
`;
interface IAppState {
fetching: boolean;
address: string;
web3: any;
connected: boolean;
chainId: number;
networkId: number;
assets: IAssetData[];
showModal: boolean;
pendingRequest: boolean;
result: any | null;
}
const INITIAL_STATE: IAppState = {
fetching: false,
address: "",
web3: null,
connected: false,
chainId: 1,
networkId: 1,
assets: [],
showModal: false,
pendingRequest: false,
result: null
};
class App extends React.Component<any, any> {
public state: IAppState = {
...INITIAL_STATE
};
public onConnect = async (provider: any) => {
const web3: any = new Web3(provider);
const accounts = await web3.eth.getAccounts();
const address = accounts[0];
const networkId = await web3.eth.net.getId();
web3.eth.extend({
methods: [
{
name: "chainId",
call: "eth_chainId",
outputFormatter: web3.utils.hexToNumber
}
]
});
const chainId = await web3.eth.chainId();
await this.setState({
web3,
connected: true,
address,
chainId,
networkId
});
await this.getAccountAssets();
};
public getAccountAssets = async () => {
const { address, chainId } = this.state;
this.setState({ fetching: true });
try {
// get account balances
const assets = await apiGetAccountAssets(address, chainId);
await this.setState({ fetching: false, assets });
} catch (error) {
console.error(error); // tslint:disable-line
await this.setState({ fetching: false });
}
};
public toggleModal = () =>
this.setState({ showModal: !this.state.showModal });
public testSendTransaction = async () => {
const { web3, address, chainId } = this.state;
if (!web3) {
return;
}
const tx = await formatTestTransaction(address, chainId);
try {
// open modal
this.toggleModal();
// toggle pending request indicator
this.setState({ pendingRequest: true });
// @ts-ignore
function sendTransaction(_tx: any) {
return new Promise((resolve, reject) => {
web3.eth
.sendTransaction(_tx)
.once("transactionHash", (txHash: string) => resolve(txHash))
.catch((err: any) => reject(err));
});
}
// send transaction
const result = await sendTransaction(tx);
// format displayed result
const formattedResult = {
method: "eth_sendTransaction",
txHash: result,
from: address,
to: address,
value: "0 ETH"
};
// display result
this.setState({
web3,
pendingRequest: false,
result: formattedResult || null
});
} catch (error) {
console.error(error); // tslint:disable-line
this.setState({ web3, pendingRequest: false, result: null });
}
};
public testSignMessage = async () => {
const { web3, address } = this.state;
if (!web3) {
return;
}
// test message
const message = "My email is john@doe.com - 1537836206101";
// hash message
const hash = hashPersonalMessage(message);
try {
// open modal
this.toggleModal();
// toggle pending request indicator
this.setState({ pendingRequest: true });
// send message
const result = await web3.eth.sign(hash, address);
// verify signature
const signer = recoverPublicKey(result, hash);
const verified = signer.toLowerCase() === address.toLowerCase();
// format displayed result
const formattedResult = {
method: "eth_sign",
address,
signer,
verified,
result
};
// display result
this.setState({
web3,
pendingRequest: false,
result: formattedResult || null
});
} catch (error) {
console.error(error); // tslint:disable-line
this.setState({ web3, pendingRequest: false, result: null });
}
};
public testSignPersonalMessage = async () => {
const { web3, address } = this.state;
if (!web3) {
return;
}
// test message
const message = "My email is john@doe.com - 1537836206101";
// encode message (hex)
const hexMsg = convertUtf8ToHex(message);
try {
// open modal
this.toggleModal();
// toggle pending request indicator
this.setState({ pendingRequest: true });
// send message
const result = await web3.eth.personal.sign(hexMsg, address);
// verify signature
const signer = recoverPersonalSignature(result, message);
const verified = signer.toLowerCase() === address.toLowerCase();
// format displayed result
const formattedResult = {
method: "personal_sign",
address,
signer,
verified,
result
};
// display result
this.setState({
web3,
pendingRequest: false,
result: formattedResult || null
});
} catch (error) {
console.error(error); // tslint:disable-line
this.setState({ web3, pendingRequest: false, result: null });
}
};
public resetApp = async () => {
const { web3 } = this.state;
console.log("web3.currentProvider", web3.currentProvider); // tslint:disable-line
if (web3 && web3.currentProvider && web3.currentProvider.close) {
await web3.currentProvider.close();
}
this.setState({ ...INITIAL_STATE });
};
public render = () => {
const {
assets,
address,
connected,
chainId,
fetching,
showModal,
pendingRequest,
result
} = this.state;
return (
<SLayout>
<Column maxWidth={1000} spanHeight>
<Header
connected={connected}
address={address}
chainId={chainId}
killSession={this.resetApp}
/>
<SContent>
{fetching ? (
<Column center>
<SContainer>
<Loader />
</SContainer>
</Column>
) : !!assets && !!assets.length ? (
<SBalances>
<h3>Actions</h3>
<Column center>
<STestButtonContainer>
<STestButton left onClick={this.testSendTransaction}>
{"eth_sendTransaction"}
</STestButton>
<STestButton left onClick={this.testSignMessage}>
{"eth_sign"}
</STestButton>
<STestButton left onClick={this.testSignPersonalMessage}>
{"personal_sign"}
</STestButton>
</STestButtonContainer>
</Column>
<h3>Balances</h3>
<AccountAssets chainId={chainId} assets={assets} />{" "}
</SBalances>
) : (
<SLanding center>
<h3>{`Test Web3Connect`}</h3>
<Web3Connect.Button
network="mainnet"
providerOptions={{
walletconnect: {
package: WalletConnectProvider,
options: {
infuraId: process.env.REACT_APP_INFURA_ID
}
},
portis: {
package: Portis,
options: {
id: process.env.REACT_APP_PORTIS_ID
}
},
fortmatic: {
package: Fortmatic,
options: {
key: process.env.REACT_APP_FORTMATIC_KEY
}
},
arkane: {
package: Arkane,
options: {
clientId: process.env.REACT_APP_ARKANE_CLIENT_ID,
environment: "staging"
}
},
authereum: {
package: Authereum,
options: {}
},
// squarelink: {
// package: Squarelink,
// options: {
// id: process.env.REACT_APP_SQUARELINK_ID
// }
// },
torus: {
package: Torus
}
}}
onConnect={(provider: any) => {
this.onConnect(provider);
}}
onClose={() => {
// empty
}}
onError={(error: Error) => {
console.error(error); // tslint:disable-line
}}
/>
</SLanding>
)}
</SContent>
</Column>
<Modal show={showModal} toggleModal={this.toggleModal}>
{pendingRequest ? (
<SModalContainer>
<SModalTitle>{"Pending Call Request"}</SModalTitle>
<SContainer>
<Loader />
<SModalParagraph>
{"Approve or reject request using your wallet"}
</SModalParagraph>
</SContainer>
</SModalContainer>
) : result ? (
<SModalContainer>
<SModalTitle>{"Call Request Approved"}</SModalTitle>
<STable>
{Object.keys(result).map(key => (
<SRow key={key}>
<SKey>{key}</SKey>
<SValue>{result[key].toString()}</SValue>
</SRow>
))}
</STable>
</SModalContainer>
) : (
<SModalContainer>
<SModalTitle>{"Call Request Rejected"}</SModalTitle>
</SModalContainer>
)}
</Modal>
</SLayout>
);
};
}
export default App;