-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
83 lines (63 loc) · 2.24 KB
/
client.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import RPC from '@hyperswarm/rpc';
import Hyperbee from 'hyperbee';
import Hypercore from 'hypercore';
import crypto from 'hypercore-crypto';
import DHT from 'hyperdht';
import b4a from 'b4a';
const environment = 'development'
// const arguments = Pear.config.args
const args = process.argv.slice(2)
console.log("Arguments:", args)
// const id = Pear.config.args[0]
const id = args[0]
// const databaseFolder = Pear.config.storage
const databaseFolder = `./resources/databases/${id}`
let topic
let topicBuffer
if (environment === 'development') {
// topic = b4a.toString(crypto.randomBytes(32), 'hex')
topic = '0000000000000000000000000000000000000000000000000000000000000000'
topicBuffer = b4a.from(topic, 'hex')
// topicBuffer = crypto.randomBytes(32)
// topic = b4a.toString(topicBuffer, 'hex')
} else {
topic = args[1] ? args[1] : b4a.toString(crypto.randomBytes(64), 'hex')
topicBuffer = b4a.from(topic, 'hex')
}
const core = new Hypercore(databaseFolder)
const db = new Hyperbee(core, { keyEncoding: 'utf-8', valueEncoding: 'binary' })
await db.ready()
const encodeHex = (data) => b4a.from(data, 'hex')
const decodeHex = (data) => b4a.toString(data, 'hex')
const encodeString = (data) => b4a.from(data, 'utf8')
const decodeString = (data) => b4a.toString(data)
const encodeObject = (data) => b4a.from(JSON.stringify(data), 'utf8')
const decodeObject = (data) => JSON.parse(b4a.toString(data, 'utf8'))
async function getOrCreateSeed(seedKey) {
let seedEntry = await db.get(seedKey)
let seed
if (seedEntry && seedEntry.value) {
seed = seedEntry.value
} else {
seed = crypto.randomBytes(32)
await db.put(seedKey, seed)
}
return seed
}
const dhtBootstrap = { host: '127.0.0.1', port: 30001 }
const dhtSeed = await getOrCreateSeed('dht-seed')
const dhtKeyPair = DHT.keyPair(dhtSeed)
const dht = new DHT({
port: 50001,
keyPair: dhtKeyPair,
bootstrap: [dhtBootstrap]
})
const serverPubKey = Buffer.from('e3e0c862cd43df96965bdae867515ba780ff20195337bbdf7b2721ad4e45c75d', 'hex')
const rpc = new RPC({ dht })
const payload = {a: 'b', c: 1, d: true}
const encoded = encodeObject(payload)
const response = await rpc.request(serverPubKey, 'echo', encoded)
const decoded = decodeObject(response)
console.log(decoded)
await rpc.destroy()
await dht.destroy()