-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
116 lines (86 loc) · 2.83 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { readFile } from 'fs/promises';
import transportLocalFile from "./src/doichain/test/transportLocalFile.js"
import createOrReadPeerId from './src/p2p/createOrReadPeerId.js'
import createNode from './src/p2p/createNode.js'
import peerDiscovery from './src/p2p/peerDiscovery.js'
import quiz from './src/p2p/quiz.js'
import { createOrReadSeed } from "./src/p2p/createOrReadSeed.js";
import { network } from 'doichainjs-lib';
import { createNewWallet } from "doichainjs-lib";
import { s } from "./src/p2p/sharedState.js";
import { createRequire } from "module";
import OrbitDb from 'orbit-db';
const require = createRequire(import.meta.url);
const ElectrumClient = require('@codewarriorr/electrum-client-js')
import 'dotenv/config'
const ORBIT_DB = process.env.ORBIT_DB
const IPFS = require('ipfs')
import bootstrapers from './src/p2p/peerIds/bootstrapers.js'
import * as ipfs from 'ipfs-core'
s.ipfs = await ipfs.create()
var peerIdConf
var id
var node
var firstPeer
const main = async () => {
const settingsTable = JSON.parse(await readFile(new URL('./settings.json', import.meta.url)));
s.options = settingsTable.options
transportLocalFile(s.options.transportLocalFilePath);
// Start Quiz
console.info('Starting p2p Quiz')
peerIdConf = process.env.PEER;
id = await createOrReadPeerId(peerIdConf)
s.node = await createNode(id)
await peerDiscovery(s.node)
s.id = id.toB58String()
s.network = network.DOICHAIN_REGTEST
global.DEFAULT_NETWORK = network.DOICHAIN_REGTEST
let o_options
// check if seed file is available
s.addrType = "p2wpkh"
switch (s.addrType) {
case "p2pkh":
s.purpose = "m/44"
break;
case "p2sh":
s.purpose = "m/49"
break;
case "p2wpkh":
s.purpose = "m/84"
break;
}
s.coinType = global.DEFAULT_NETWORK.name == "mainnet" ? 0 : 1
s.account = 0
s.basePath = `${s.purpose}/${s.coinType}/${s.account}`
global.client = new ElectrumClient("172.22.0.6", 50002, "ssl");
try {
await global.client.connect(
"electrum-client-js", // optional client name
"1.4.2" // optional protocol version
)
} catch (err) {
console.error(err);
}
await createOrReadSeed(id)
s.wallet = await createNewWallet(s.hdkey, s.purpose, s.coinType, o_options, s.addrType, s.id)
if (ORBIT_DB !== "") {
// Create OrbictDB
s.orbitDb = await OrbitDb.createInstance(s.ipfs, { directory: './orbitdb1' })
// Create docstore DB
s.docstore = await s.orbitDb.open(ORBIT_DB);
console.log("Successfully created docstore");
await s.docstore.load()
await s.docstore.events.on('replicated', () => console.log("Replicated Db"))
}
function getWinnerPeerId() {
if (peerIdConf.includes('id-1')) {
firstPeer = true
quiz(firstPeer)
} else {
firstPeer = false
quiz(firstPeer)
}
}
getWinnerPeerId()
}
main()