Skip to content

Commit d15a26e

Browse files
committed
feat: allow network-config.gci to specify a URL to genesis
1 parent 1d334fc commit d15a26e

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

packages/cosmic-swingset/lib/ag-solo/add-chain.js

+41-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import fetch from 'node-fetch';
2+
import crypto from 'crypto';
3+
import djson from 'deterministic-json';
24
import path from 'path';
35
import fs from 'fs';
46

@@ -22,11 +24,46 @@ async function addChain(basedir, chainConfig, force = false) {
2224
}
2325
}
2426

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+
2865
if (!netconf.gci) {
29-
throw Error(`${actualConfig} does not contain a "gci" entry`);
66+
throw Error(`${url.href} does not contain a "gci" entry`);
3067
}
3168

3269
const connFile = path.join(basedir, 'connections.json');

0 commit comments

Comments
 (0)