-
Notifications
You must be signed in to change notification settings - Fork 0
/
Eos.js
61 lines (54 loc) · 1.67 KB
/
Eos.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
const { Api, JsonRpc, Serialize, RpcError } = require("@jafri/eosjs2");
const {JsSignatureProvider} = require('@jafri/eosjs2/dist/eosjs-jssig');
const fetch = require("node-fetch");
const { TextEncoder, TextDecoder } = require("util");
const ecc = require('eosjs-ecc'); //https://github.com/EOSIO/eosjs-ecc
class Eos {
constructor(nodes=[], private_keys=[]){
console.log(`Eos initialize`)
this.nodes = Array.isArray(nodes) ? nodes : [nodes];
this.pks = Array.isArray(private_keys) ? private_keys : [private_keys];
this.Serialize = Serialize;
this.RpcError = RpcError;
this.api = null;
this.ecc = ecc;
this.validatePks();
this.build();
}
build(){
const signatureProvider = this.pks.length ? new JsSignatureProvider(this.pks) : null;
const rpc = new JsonRpc(this.nodes, { fetch });
const api = new Api({
rpc,
signatureProvider,
textDecoder: new TextDecoder(),
textEncoder: new TextEncoder()
});
this.api = api;
}
switchNode(){
if(this.nodes.length > 1){
this.nodes.push(this.nodes.shift());
this.build();
}
else{
console.log(`Only one Rpc node available.`);
}
}
setNode(rpc_node_url){
//todo
}
validatePks(){
for(let i = 0; i < this.pks.length; i++){
if(this.ecc.isValidPrivate(this.pks[i]) === false){
throw new Error("Invalid private key detected.");
}
else{
console.log(`Private key #${i+1} validated`);
}
}
}
}
module.exports = {
Eos
};