-
Notifications
You must be signed in to change notification settings - Fork 1
/
runNode.js
132 lines (129 loc) · 3.16 KB
/
runNode.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const readline = require("readline")
const { stdin: input, stdout: output } = require("process")
const rl = readline.createInterface({ input, output })
const SDK = require("../sdk")
const fs = require("fs")
const path = require("path")
const { expect } = require("chai")
const ethSigUtil = require("@metamask/eth-sig-util")
const Wallet = require("ethereumjs-wallet").default
const {
PstContract,
PstState,
Warp,
WarpNodeFactory,
LoggerFactory,
InteractionResult,
} = require("warp-contracts")
const { isNil, keys, includes } = require("ramda")
const ArLocal = require("arlocal").default
async function addFunds(arweave, wallet) {
const walletAddress = await arweave.wallets.getAddress(wallet)
await arweave.api.get(`/mint/${walletAddress}/1000000000000000`)
}
let arlocal, arweave, warp, arweave_wallet, walletAddress, contractSrc, sdk
let isInit = false
let stopto = null
async function init() {
arlocal = new ArLocal(1820, false)
await arlocal.start()
sdk = new SDK({
arweave: {
host: "localhost",
port: 1820,
protocol: "http",
},
})
arweave = sdk.arweave
warp = sdk.warp
const wallet = Wallet.generate()
arweave_wallet = await arweave.wallets.generate()
await addFunds(arweave, arweave_wallet)
walletAddress = await arweave.wallets.jwkToAddress(arweave_wallet)
console.log(`Arweave wallet generated: ` + walletAddress)
console.log(`Ethereum wallet generated: ` + wallet.getAddressString())
contractSrc = fs.readFileSync(
path.join(__dirname, "../dist/contract.js"),
"utf8"
)
const stateFromFile = JSON.parse(
fs.readFileSync(
path.join(__dirname, "../dist/contracts/initial-state.json"),
"utf8"
)
)
const initialState = {
...stateFromFile,
...{
secure: true,
owner: walletAddress,
},
}
const contract = await warp.createContract.deploy({
wallet: arweave_wallet,
initState: JSON.stringify(initialState),
src: contractSrc,
})
console.log(contract)
const name = "weavedb"
const version = "1"
sdk.initialize({
contractTxId: contract.contractTxId,
wallet: arweave_wallet,
name,
version,
EthWallet: wallet,
})
await sdk.mineBlock()
const metadata = {
ethereum: {
privateKey: wallet.getPrivateKeyString(),
publicKey: wallet.getPublicKeyString(),
address: wallet.getAddressString(),
},
arweave: arweave_wallet,
weavedb: { ...contract, name, version },
}
fs.writeFileSync(
path.resolve(__dirname, "../console/lib/weavedb.json"),
JSON.stringify(metadata)
)
waitForCommand()
}
function waitForCommand() {
const methods = [
"add",
"get",
"cget",
"set",
"update",
"upsert",
"delete",
"addIndex",
"getIndex",
"removeIndex",
"setRules",
"getRules",
"setSchema",
"getSchema",
"nonce",
"ids",
"batch",
"evolve",
]
rl.question("> ", async method => {
try {
let pr = eval(`sdk.${method}`)
const res = await pr
if (!isNil(res.err)) {
console.log(`error: ${res.err.errorMessage}`)
} else {
console.log(res)
}
} catch (e) {
console.log(e)
}
waitForCommand()
})
}
init()