@@ -10,12 +10,11 @@ import EventEmitter from 'events';
10
10
import EventNames from './web3Provider/web3-provider/events' ;
11
11
import { Transaction } from 'ethereumjs-tx' ;
12
12
import { messageConstants } from '../messages' ;
13
- // import parseTokensData from './web3Provider/helpers/parseTokensData';
14
13
import debugLogger from 'debug' ;
15
14
import PopUpCreator from '../connectWindow/popUpCreator' ;
16
15
import { nativeCheck , mobileCheck } from './platformDeepLinking' ;
17
16
import { DISCONNECTED , CONNECTING , CONNECTED } from '../config' ;
18
- import packageJson from '../../package.json'
17
+ import packageJson from '../../package.json' ;
19
18
20
19
const debugConnectionState = debugLogger ( 'MEWconnect:connection-state' ) ;
21
20
const debugErrors = debugLogger ( 'MEWconnectError' ) ;
@@ -61,33 +60,74 @@ export default class Integration extends EventEmitter {
61
60
this . CHAIN_ID = options . chainId || 1 ;
62
61
this . RPC_URL = options . rpcUrl || false ;
63
62
this . noUrlCheck = options . noUrlCheck || false ;
63
+ this . newNetworks = options . newNetworks || [ ] ;
64
+ this . knownNetworks = new Set ( ) ;
64
65
this . lastHash = null ;
65
66
this . initiator = new Initiator ( ) ;
66
67
this . popUpHandler = new PopUpHandler ( ) ;
67
68
this . connectionState = false ;
68
- this . chainIdMapping = this . createChainMapping ( ) ;
69
+ this . chainIdMapping = this . createChainMapping ( this . newNetworks ) ;
69
70
this . returnPromise = null ;
70
71
this . disconnectComplete = false ;
71
72
popUpCreator = new PopUpCreator ( ) ;
72
73
}
73
74
74
- closeDataChannelForDemo ( ) {
75
+ closeDataChannelForDemo ( ) {
75
76
const connection = state . wallet . getConnection ( ) ;
76
- connection . webRtcCommunication . closeDataChannelForDemo ( )
77
+ connection . webRtcCommunication . closeDataChannelForDemo ( ) ;
77
78
}
78
79
79
- createChainMapping ( ) {
80
- return Object . keys ( Networks ) . reduce (
80
+ formatNewNetworks ( newNetwork ) {
81
+ return {
82
+ type : {
83
+ name : newNetwork . name ,
84
+ name_long : newNetwork . name_long || newNetwork . name ,
85
+ homePage : newNetwork . homePage || '' ,
86
+ blockExplorerTX : newNetwork . blockExplorerTX || '' ,
87
+ blockExplorerAddr : newNetwork . blockExplorerAddr || '' ,
88
+ chainID : newNetwork . chainId
89
+ ? newNetwork . chainId
90
+ : newNetwork . chainID
91
+ ? newNetwork . chainID
92
+ : this . CHAIN_ID ,
93
+ tokens : newNetwork . tokens || [ ] ,
94
+ contracts : [ ] ,
95
+ currencyName : newNetwork . currencyName || newNetwork . name
96
+ } ,
97
+ service : newNetwork . serviceName || newNetwork . name ,
98
+ url : newNetwork . url || this . RPC_URL ,
99
+ port : 443 ,
100
+ auth : false ,
101
+ username : '' ,
102
+ password : ''
103
+ } ;
104
+ }
105
+
106
+ createChainMapping ( newNetworks ) {
107
+ let networks = Networks ;
108
+ try {
109
+ const additional = newNetworks
110
+ . map ( this . formatNewNetworks )
111
+ . reduce ( ( acc , curr ) => {
112
+ acc [ curr . type . name ] = curr ;
113
+ } , { } ) ;
114
+ networks = { ...networks , ...additional } ;
115
+ } catch ( e ) {
116
+ // eslint-disable-next-line
117
+ console . error ( e ) ;
118
+ }
119
+ return Object . keys ( networks ) . reduce (
81
120
( acc , curr ) => {
82
- if ( Networks [ curr ] . length === 0 ) return acc ;
121
+ if ( networks [ curr ] . length === 0 ) return acc ;
83
122
acc . push ( {
84
123
name :
85
- Networks [ curr ] [ 0 ] . type . name_long === 'Ethereum'
124
+ networks [ curr ] [ 0 ] . type . name_long === 'Ethereum'
86
125
? 'mainnet'
87
- : Networks [ curr ] [ 0 ] . type . name_long . toLowerCase ( ) ,
88
- chainId : Networks [ curr ] [ 0 ] . type . chainID ,
89
- key : Networks [ curr ] [ 0 ] . type . name
126
+ : networks [ curr ] [ 0 ] . type . name_long . toLowerCase ( ) ,
127
+ chainId : networks [ curr ] [ 0 ] . type . chainID ,
128
+ key : networks [ curr ] [ 0 ] . type . name
90
129
} ) ;
130
+ this . knownNetworks . add ( networks [ curr ] [ 0 ] . type . chainID )
91
131
return acc ;
92
132
} ,
93
133
[ { name : 'mainnet' , chainId : 1 , key : 'ETH' } ]
@@ -122,16 +162,16 @@ export default class Integration extends EventEmitter {
122
162
) ;
123
163
}
124
164
125
- get getWalletOnly ( ) {
126
- if ( state . wallet ) {
127
- return state . wallet
165
+ get getWalletOnly ( ) {
166
+ if ( state . wallet ) {
167
+ return state . wallet ;
128
168
}
129
169
}
130
170
131
171
enable ( ) {
132
172
popUpCreator . on ( 'fatalError' , ( ) => {
133
173
MEWconnectWallet . setConnectionState ( DISCONNECTED ) ;
134
- } )
174
+ } ) ;
135
175
if ( this . runningInApp ) {
136
176
return new Promise ( ( resolve , reject ) => {
137
177
state . web3Provider
@@ -185,7 +225,7 @@ export default class Integration extends EventEmitter {
185
225
popUpCreator ,
186
226
this . popUpHandler
187
227
) ;
188
- console . log ( `Using MEWconnect v${ packageJson . version } ` ) ; // todo remove dev item
228
+ console . log ( `Using MEWconnect v${ packageJson . version } ` ) ;
189
229
this . popUpHandler . showConnectedNotice ( ) ;
190
230
this . popUpHandler . hideNotifier ( ) ;
191
231
this . createDisconnectNotifier ( ) ;
@@ -203,7 +243,7 @@ export default class Integration extends EventEmitter {
203
243
if ( state . web3Provider . accountsChanged ) {
204
244
state . web3Provider . emit ( 'accountsChanged' , [
205
245
state . wallet . getChecksumAddressString ( )
206
- ] )
246
+ ] ) ;
207
247
// state.web3Provider.accountsChanged([
208
248
// state.wallet.getChecksumAddressString()
209
249
// ]);
@@ -253,9 +293,17 @@ export default class Integration extends EventEmitter {
253
293
web3Provider = window . web3 . currentProvider ;
254
294
}
255
295
} else {
256
- const chain = this . identifyChain ( CHAIN_ID || 1 ) ;
257
- const defaultNetwork = Networks [ chain . key ] [ 0 ] ;
258
- state . network = defaultNetwork ;
296
+ let chain , defaultNetwork ;
297
+ if ( this . knownNetworks . has ( CHAIN_ID ) ) {
298
+ chain = this . identifyChain ( CHAIN_ID || 1 ) ;
299
+ defaultNetwork = Networks [ chain . key ] [ 0 ] ;
300
+ state . network = defaultNetwork ;
301
+ } else {
302
+ chain = { name : 'unknown' }
303
+ defaultNetwork = this . formatNewNetworks ( { name : 'unknown' } )
304
+ state . network = defaultNetwork
305
+ }
306
+
259
307
if ( this . infuraId && ! this . RPC_URL ) {
260
308
RPC_URL = infuraUrlFormater ( chain . name , this . infuraId ) ;
261
309
}
@@ -267,7 +315,9 @@ export default class Integration extends EventEmitter {
267
315
! / [ w W ] / . test ( hostUrl . protocol ) &&
268
316
! / [ h t p H T P ] / . test ( hostUrl . protocol )
269
317
) {
270
- throw Error ( 'Invalid rpc endpoint supplied to MEWconnect during setup' ) ;
318
+ throw Error (
319
+ 'Invalid rpc endpoint supplied to MEWconnect during setup'
320
+ ) ;
271
321
}
272
322
if ( ! _noCheck && ! this . infuraId ) {
273
323
if (
@@ -297,7 +347,7 @@ export default class Integration extends EventEmitter {
297
347
}
298
348
299
349
state . enable = this . enable . bind ( this ) ;
300
- web3Provider . close = this . disconnect //.bind(this);
350
+ web3Provider . close = this . disconnect ; //.bind(this);
301
351
web3Provider . disconnect = this . disconnect . bind ( this ) ;
302
352
state . web3Provider = web3Provider ;
303
353
state . web3 = new Web3 ( web3Provider ) ;
@@ -329,11 +379,14 @@ export default class Integration extends EventEmitter {
329
379
connection . lifeCycle . RtcDisconnectEvent ,
330
380
( ) => {
331
381
try {
332
- if ( this . popupCreator ) this . popUpHandler . showNotice ( messageConstants . disconnect ) ;
333
- MEWconnectWallet . setConnectionState ( connection . lifeCycle . disconnected ) ;
334
- if ( state . wallet !== null ) {
335
- this . emit ( 'close' )
336
- this . emit ( 'disconnect' )
382
+ if ( this . popupCreator )
383
+ this . popUpHandler . showNotice ( messageConstants . disconnect ) ;
384
+ MEWconnectWallet . setConnectionState (
385
+ connection . lifeCycle . disconnected
386
+ ) ;
387
+ if ( state . wallet !== null ) {
388
+ this . emit ( 'close' ) ;
389
+ this . emit ( 'disconnect' ) ;
337
390
}
338
391
if ( state . wallet !== null && state . web3Provider ) {
339
392
state . web3Provider . emit ( 'disconnect' ) ;
@@ -347,11 +400,10 @@ export default class Integration extends EventEmitter {
347
400
}
348
401
state . wallet = null ;
349
402
this . emit ( DISCONNECTED ) ;
350
- this . emit ( 'close' )
351
- this . emit ( 'disconnect' )
352
-
403
+ this . emit ( 'close' ) ;
404
+ this . emit ( 'disconnect' ) ;
353
405
} catch ( e ) {
354
- if ( this . popUpHandler ) {
406
+ if ( this . popUpHandler ) {
355
407
this . popUpHandler . showNotice ( messageConstants . disconnectError ) ;
356
408
}
357
409
}
@@ -363,10 +415,12 @@ export default class Integration extends EventEmitter {
363
415
( ) => {
364
416
try {
365
417
this . popUpHandler . showNotice ( messageConstants . disconnect ) ;
366
- MEWconnectWallet . setConnectionState ( connection . lifeCycle . disconnected ) ;
367
- if ( state . wallet !== null ) {
368
- this . emit ( 'close' )
369
- this . emit ( 'disconnect' )
418
+ MEWconnectWallet . setConnectionState (
419
+ connection . lifeCycle . disconnected
420
+ ) ;
421
+ if ( state . wallet !== null ) {
422
+ this . emit ( 'close' ) ;
423
+ this . emit ( 'disconnect' ) ;
370
424
}
371
425
if ( state . wallet !== null && state . web3Provider ) {
372
426
state . web3Provider . emit ( 'disconnect' ) ;
@@ -381,26 +435,23 @@ export default class Integration extends EventEmitter {
381
435
382
436
state . wallet = null ;
383
437
this . emit ( connection . lifeCycle . disconnected ) ;
384
-
385
438
} catch ( e ) {
386
- if ( this . popUpHandler ) {
439
+ if ( this . popUpHandler ) {
387
440
this . popUpHandler . showNotice ( messageConstants . disconnectError ) ;
388
441
}
389
442
}
390
443
}
391
444
) ;
392
445
}
393
446
394
- createCommunicationError ( ) {
447
+ createCommunicationError ( ) {
395
448
const connection = state . wallet . getConnection ( ) ;
396
- connection . webRtcCommunication . on (
397
- connection . lifeCycle . decryptError ,
398
- ( ) => {
399
- if ( this . popupCreator ) this . popUpHandler . showNoticePersistentEnter (
449
+ connection . webRtcCommunication . on ( connection . lifeCycle . decryptError , ( ) => {
450
+ if ( this . popupCreator )
451
+ this . popUpHandler . showNoticePersistentEnter (
400
452
messageConstants . communicationError
401
453
) ;
402
- }
403
- ) ;
454
+ } ) ;
404
455
}
405
456
406
457
disconnect ( ) {
@@ -420,7 +471,7 @@ export default class Integration extends EventEmitter {
420
471
return true ;
421
472
} catch ( e ) {
422
473
debugErrors ( 'disconnect ERROR' ) ;
423
- if ( this . popUpHandler ) {
474
+ if ( this . popUpHandler ) {
424
475
this . popUpHandler . showNotice ( messageConstants . disconnectError ) ;
425
476
}
426
477
// eslint-disable-next-line
@@ -449,12 +500,11 @@ export default class Integration extends EventEmitter {
449
500
state . wallet
450
501
. signTransaction ( tx )
451
502
. then ( _response => {
503
+ if ( ! transactionCache . includes ( _response . tx . hash ) ) {
504
+ transactionCache . push ( _response . tx . hash ) ;
452
505
453
- if ( ! transactionCache . includes ( _response . tx . hash ) ) {
454
- transactionCache . push ( _response . tx . hash )
455
-
456
- this . popUpHandler . showNoticePersistentExit ( ) ;
457
- resolve ( _response ) ;
506
+ this . popUpHandler . showNoticePersistentExit ( ) ;
507
+ resolve ( _response ) ;
458
508
}
459
509
} )
460
510
. catch ( err => {
@@ -482,10 +532,10 @@ export default class Integration extends EventEmitter {
482
532
}
483
533
} ) ;
484
534
eventHub . on ( EventNames . ERROR_NOTIFY , err => {
485
- if ( err && err . message ) {
535
+ if ( err && err . message ) {
486
536
this . popUpHandler . showNotice ( err . message ) ;
487
537
}
488
- } )
538
+ } ) ;
489
539
eventHub . on ( EventNames . SHOW_MSG_CONFIRM_MODAL , ( msg , resolve ) => {
490
540
if ( ! state . wallet ) {
491
541
this . popUpHandler . showNoticePersistentEnter (
0 commit comments