This repository has been archived by the owner on Nov 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
84 lines (72 loc) · 2.62 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
// Copyright (C) 2018 WeTrustPlatform
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
const glob = require('glob');
const path = require('path');
const fs = require('fs');
const assert = require('assert');
const Web3 = require('web3');
const validator = require('./lib/validators').locaEthletInit;
const walletProvider = require('./lib/wallet-provider');
/**
* Main Class
*
*/
class Ethlet {
/**
* @param {object} options: { walletProvider, rpc }
*/
constructor(options) {
if (!validator(options)) {
throw new Error(JSON.stringify(validator.errors));
}
const { walletProvider, rpc } = options;
this.web3 = new Web3(new Web3.providers.HttpProvider(rpc));
this.walletProvider = walletProvider;
}
/**
* Execute an action
* @param {string} actionName: Name of the action i.e. deploy, interact, send
* @param {uri | object} data: Location of data file or a data json object
* @return {Promise} result: Result of web3.eth.sendSignedTransaction
*/
async execute(actionName, data) {
assert.ok(
supportedActions[actionName],
`Action '${actionName}' is invalid. Supported actions: ${[
...Object.keys(supportedActions),
]}`,
);
assert.ok(data, `Missing the location of datafile`);
let params;
if (typeof data === 'string') {
params = JSON.parse(fs.readFileSync(data, 'utf-8'));
} else if (typeof data === 'object') {
params = data;
} else {
assert.fail('Expect data to be typeof string or object');
}
return supportedActions[actionName](params, this.walletProvider, this.web3);
}
}
// Load the wallet providers
Ethlet.WalletProvider = walletProvider;
// Get all the actions
let actionModules = glob.sync(path.resolve(__dirname, './actions/*.js'));
let supportedActions = {};
actionModules.forEach(f => {
const fileName = path.basename(f);
const actionName = fileName.split('.')[0].toLowerCase();
supportedActions[actionName] = require(f);
});
exports = module.exports = Ethlet;
// Expose to ES6 downstream
module.exports.default = Ethlet;