-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.mjs
More file actions
46 lines (41 loc) · 1.39 KB
/
index.mjs
File metadata and controls
46 lines (41 loc) · 1.39 KB
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
import { decode } from 'hypercore-id-encoding'
import DHT from 'hyperdht'
import Protomux from 'protomux'
import Channel from 'jsonrpc-mux'
import { enact, proxy } from '@agree-able/contract'
export { enact, loadAgreement, z } from '@agree-able/contract'
export async function host (agreement, implementation, opts) {
if (!opts) opts = {}
if (!opts.validator) opts.validator = async () => {}
const dht = opts.dht? opts.dht : new DHT(opts.dhtOpts)
const seedBuf = opts.seed ? decode(opts.seed) : null
const keyPair = DHT.keyPair(seedBuf)
let channel = null
const connect = c => {
channel = new Channel(new Protomux(c))
enact(channel, agreement, implementation, opts.validator)
}
const server = dht.createServer(connect)
await server.listen(keyPair)
return { dht, keyPair, server, channel }
}
export function seedFromArgv () {
let seed = null
if (global.Bare) seed = global.Bare.argv[2]
else seed = process.argv[2]
return seed
}
export function Caller (peerKey, setHeaders) {
if (!setHeaders) setHeaders = () => {}
this.setHeaders = setHeaders
this.publicKey = decode(peerKey)
this.node = new DHT()
this.conn = this.node.connect(this.publicKey)
this.framed = new Channel(new Protomux(this.conn))
}
Caller.prototype.proxy = function (agreement) {
return proxy(this.framed, agreement, this.setHeaders)
}
Caller.prototype.destroy = function () {
this.node.destroy()
}