forked from keppel/lotion-connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (55 loc) · 1.69 KB
/
index.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
let {
startLightClientFromGCI,
startLightClientFromGenesis
} = require('./lib/connect-by-address.js')
let GetState = require('./lib/get-state.js')
let SendTx = require('./lib/send-tx.js')
let discoverPeerByGCI = require('./lib/gci-get-peer.js')
let Proxmise = require('proxmise')
let { parse, stringify } = require('deterministic-json')
let { EventEmitter } = require('events')
async function connect(GCI, opts = {}) {
let nodes = opts.nodes || []
let genesis = opts.genesis
if (!GCI) {
if (nodes.length === 0) {
throw Error('Cannot connect, must specify GCI or node addresses')
} else if (genesis == null) {
throw Error('Cannot verify state, must specify GCI or genesis')
}
}
// TODO: support bootstrapping from known recent header hash
// instead of just by GCI->genesis->lc_state
let lc
if (nodes.length) {
// randomly sample from supplied seed nodes
// TODO: pass all of these into light client
// once it supports multiple peers
let randomIndex = Math.floor(Math.random() * nodes.length)
let address = nodes[randomIndex]
if (genesis) {
lc = await startLightClientFromGenesis(genesis, address)
} else {
lc = await startLightClientFromGCI(GCI, address)
}
} else {
// gci discovery magic...
lc = await discoverPeerByGCI(GCI)
}
let getState = GetState(lc)
let sendTx = SendTx(lc)
let bus = new EventEmitter()
lc.on('error', e => bus.emit('error', e))
return Object.assign(bus, {
getState,
send: sendTx,
lightClient: lc,
state: Proxmise(path => {
return getState(path.join('.'))
}),
get validators() {
return lc._state.validators
}
})
}
module.exports = connect