forked from OpenSTFoundation/mosaic-anchor.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.js
85 lines (82 loc) · 3.13 KB
/
run.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
const program = require('commander');
const Web3 = require('web3');
const fs = require('fs');
const index = require('./index');
const Job = index.Job;
program
.command('commit <direction> <config-file-path> <password>')
.action(async (direction, configFilePath, password) => {
console.log('direction', direction);
console.log('configFilePath', configFilePath);
const config = require(configFilePath);
const originWeb3 = new Web3(config.origin.chain.ws);
const auxiliaryWeb3 = new Web3(config.auxiliary.chain.ws);
const originAnchor = config.origin.chain.anchor;
const auxiliaryAnchor = config.auxiliary.chain.anchor;
const originOrganizationOwner = config.origin.chain.organizationOwner;
const auxiliaryOrganizationOwner = config.auxiliary.chain.organizationOwner;
const originConfirmations = 24;
const auxiliaryConfirmations = 6;
const originAnchorInterval = parseInt(config.origin.commitInterval) * 60 * 1000; /* minute to ms */
const auxiliaryAnchorInterval = parseInt(config.auxiliary.commitInterval) * 60 * 1000; /* minute to ms */
let oJob;
if (direction === 'o2a') {
const account = auxiliaryWeb3.eth.accounts.decrypt(config.auxiliary.signer, password);
auxiliaryWeb3.eth.accounts.wallet.add(account);
oJob = new Job(
originWeb3,
auxiliaryWeb3,
auxiliaryAnchor,
auxiliaryOrganizationOwner,
auxiliaryConfirmations,
auxiliaryAnchorInterval
);
} else if (direction === 'a2o') {
const account = originWeb3.eth.accounts.decrypt(config.origin.signer, password);
originWeb3.eth.accounts.wallet.add(account);
oJob = new Job(
auxiliaryWeb3,
originWeb3,
originAnchor,
originOrganizationOwner,
originConfirmations,
originAnchorInterval
);
} else {
throw `unknown direction ${direction}`;
}
try {
oJob
.execute(50)
.on('StateRootAvailable', function(receipt) {
console.log('State root has been anchored. receipt', JSON.stringify(receipt, null, 2));
})
.then(function() {
console.log('Job completed. Exiting!');
process.exit(0);
})
.catch(function(error) {
console.error('Something went wrong.', error);
process.exit(1);
});
} catch (e) {
console.error('unhandled error', e);
process.exit(1);
}
});
program
.command('createWorker <chain> <config-file-path>' + ' <password>')
.action(async (chain, configFilePath, password) => {
if (!(chain === 'origin' || chain === 'auxiliary')) {
console.error(`Valid chain parameter value is ${chain}`);
process.exit(1);
}
const web3 = new Web3('');
const account = web3.eth.accounts.create();
const encryptedAccount = web3.eth.accounts.encrypt(account.privateKey, password);
let configObject = require(configFilePath);
configObject[chain]['signer'] = encryptedAccount;
fs.writeFileSync(configFilePath, JSON.stringify(configObject, null, ' '));
console.log(`Created worker address ${account.address} for chain ${chain}`);
});
program.parse(process.argv);