1
1
import fetch from 'node-fetch' ;
2
+ import crypto from 'crypto' ;
3
+ import djson from 'deterministic-json' ;
2
4
import path from 'path' ;
3
5
import fs from 'fs' ;
4
6
@@ -22,11 +24,46 @@ async function addChain(basedir, chainConfig, force = false) {
22
24
}
23
25
}
24
26
25
- console . log ( 'downloading netconfig from' , actualConfig ) ;
26
- const r = await fetch ( actualConfig ) ;
27
- const netconf = await r . json ( ) ;
27
+ const url = new URL ( actualConfig , `file://${ process . cwd ( ) } ` ) ;
28
+ console . log ( 'downloading netconfig from' , url . href ) ;
29
+ let netconf ;
30
+ if ( url . protocol === 'file:' ) {
31
+ const f = fs . readFileSync ( url . pathname , 'utf-8' ) ;
32
+ netconf = JSON . parse ( f ) ;
33
+ } else {
34
+ const r = await fetch ( url . href ) ;
35
+ netconf = await r . json ( ) ;
36
+ }
37
+ const gciUrl = new URL ( netconf . gci , 'hex://' ) ;
38
+ if ( gciUrl . protocol !== 'hex:' ) {
39
+ const g = await fetch ( gciUrl . href ) ;
40
+ const resp = await g . json ( ) ;
41
+ let genesis = resp ;
42
+ if ( resp . jsonrpc === '2.0' ) {
43
+ // JSON-RPC embedded genesis.
44
+ genesis = resp . result . genesis ;
45
+ }
46
+
47
+ if ( ! genesis . genesis_time ) {
48
+ throw Error (
49
+ `Malformed JSON genesis response from ${ gciUrl . href } : ${ JSON . stringify (
50
+ resp ,
51
+ null ,
52
+ 2 ,
53
+ ) } `,
54
+ ) ;
55
+ }
56
+ const s = djson . stringify ( resp . result ) ;
57
+ const gci = crypto
58
+ . createHash ( 'sha256' )
59
+ . update ( s )
60
+ . digest ( 'hex' ) ;
61
+
62
+ netconf . gci = gci ;
63
+ }
64
+
28
65
if ( ! netconf . gci ) {
29
- throw Error ( `${ actualConfig } does not contain a "gci" entry` ) ;
66
+ throw Error ( `${ url . href } does not contain a "gci" entry` ) ;
30
67
}
31
68
32
69
const connFile = path . join ( basedir , 'connections.json' ) ;
0 commit comments