-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.js
69 lines (59 loc) · 1.91 KB
/
config.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
const { Api, JsonRpc } = require(`eosjs`)
const { JsSignatureProvider } = require(`eosjs/dist/eosjs-jssig`)
const fetch = require(`node-fetch`) // node only; not needed in browsers
const { TextEncoder, TextDecoder } = require(`util`) // node only; native TextEncoder/Decoder
const ecc = require(`eosjs-ecc`)
const map = require(`lodash/map`)
const mapValues = require(`lodash/mapValues`)
const uniq = require(`lodash/uniq`)
const dotenv = require(`dotenv`)
function loadEnvironmentVars() {
const SCRIPT_ENV = [`testnet`, `production`].includes(process.env.NODE_ENV)
? process.env.NODE_ENV
: `development`
console.log(`Loading environment "${SCRIPT_ENV}"`)
dotenv.config({ path: `.${SCRIPT_ENV}.env` })
return {
...process.env,
SCRIPT_ENV,
}
}
const {
SCRIPT_ENV,
CONTRACT_ACCOUNT,
CONTRACT_PRIVATE_KEY,
EOS_HTTP_ENDPOINT,
EOS_CHAIN_ID,
EOSIO_PRIVATE_KEY, // used in dev only
} = loadEnvironmentVars()
function getKeys() {
// New deterministic key for the testi account.
// Do NOT use this in production
const contractPrivate = CONTRACT_PRIVATE_KEY || ecc.seedPrivate(CONTRACT_ACCOUNT)
const keys = mapValues(
{
[CONTRACT_ACCOUNT]: contractPrivate,
testvest1: ecc.seedPrivate(`testvest1`),
testvest2: ecc.seedPrivate(`testvest2`),
},
privateKey => [privateKey, ecc.privateToPublic(privateKey)],
)
if (SCRIPT_ENV === `development`) console.log(keys)
return keys
}
const keys = getKeys()
const signatureProvider = new JsSignatureProvider(
[EOSIO_PRIVATE_KEY, ...uniq(map(keys, ([privateKey]) => privateKey))].filter(Boolean),
)
const rpc = new JsonRpc(EOS_HTTP_ENDPOINT, { fetch })
const api = new Api({
rpc,
signatureProvider,
chainId: EOS_CHAIN_ID,
textDecoder: new TextDecoder(),
textEncoder: new TextEncoder(),
})
module.exports = {
api,
keys,
}