-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
57 lines (49 loc) · 1.34 KB
/
utils.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
// utils.js
const Eos = require('eosjs');
const passwordPrompt = require('password-prompt');
async function getNewKeys() {
const privateKey = await Eos.modules.ecc.PrivateKey.randomKey();
const publicKey = privateKey.toPublic().toString();
return { privateKey: privateKey.toWif(), publicKey };
}
async function getApi(config) {
const { chainId, httpEndpoint } = config;
const privateKey = config.master.privateKey || await passwordPrompt('Enter private key: ', { method: 'hide' });
if (!privateKey || !isKeyPairValid(config.master.publicKey, privateKey)) {
throw new Error('Private key is invalid');
}
return Eos({
httpEndpoint,
chainId,
broadcast: true,
debug: true,
sign: true,
expireInSeconds: 60,
keyProvider: [privateKey],
});
}
function isKeyPairValid(publicKey, privateKey) {
try {
return Eos.modules.ecc.privateToPublic(privateKey) === publicKey;
} catch (err) {
return false;
}
}
function generateAccountNameFromAddress({ address }) {
const accountName =
address
.toLowerCase()
.replace(new RegExp('[67890]', 'g'), '')
.substring(3, 15);
return `${accountName}`;
}
function formatAmount(amount) {
return `${parseFloat(amount).toFixed(4)} EOS`;
}
module.exports = {
getApi,
getNewKeys,
generateAccountNameFromAddress,
isKeyPairValid,
formatAmount,
}