poa-dapps-keys-generation/src/keysManager.js

60 lines
1.6 KiB
JavaScript
Raw Normal View History

2018-10-15 07:52:03 -07:00
import Web3 from "web3";
import addressGenerator from "./addressGenerator";
import helpers from "./helpers";
import { constants } from "./constants";
2017-12-06 23:02:38 -08:00
export default class KeysManager {
2018-10-15 07:52:03 -07:00
async init({ web3, netId, addresses }) {
this.web3_10 = new Web3(web3.currentProvider);
2018-10-15 07:52:03 -07:00
const { KEYS_MANAGER_ADDRESS } = addresses;
console.log("Keys Manager ", KEYS_MANAGER_ADDRESS);
const branch = helpers.getBranch(netId);
2018-10-15 07:52:03 -07:00
const KeysManagerAbi = await helpers.getABI(branch, "KeysManager");
2018-10-15 07:52:03 -07:00
this.keysInstance = new this.web3_10.eth.Contract(
KeysManagerAbi,
KEYS_MANAGER_ADDRESS
);
this.netId = netId;
2017-12-06 23:02:38 -08:00
}
2017-12-06 23:02:38 -08:00
async isInitialKeyValid(initialKey) {
2018-02-23 05:31:56 -08:00
return new Promise((resolve, reject) => {
2018-10-15 07:52:03 -07:00
const methods = this.keysInstance.methods;
let getInitialKeyStatus;
if (methods.getInitialKeyStatus) {
2018-10-15 07:52:03 -07:00
getInitialKeyStatus = methods.getInitialKeyStatus;
} else {
2018-10-15 07:52:03 -07:00
getInitialKeyStatus = methods.initialKeys;
}
2018-10-15 07:52:03 -07:00
getInitialKeyStatus(initialKey)
.call()
.then(function(result) {
resolve(result);
})
.catch(function(e) {
reject(false);
});
});
2017-12-06 23:02:38 -08:00
}
async generateKeys() {
return await addressGenerator();
}
2018-10-15 07:52:03 -07:00
createKeys({ mining, voting, payout, sender }) {
let gasPrice = "2";
if (
this.netId === constants.NETID_DAI_TEST ||
this.netId === constants.NETID_DAI
) {
gasPrice = "0";
}
return this.keysInstance.methods.createKeys(mining, voting, payout).send({
from: sender,
2018-10-15 07:52:03 -07:00
gasPrice: this.web3_10.utils.toWei(gasPrice, "gwei")
});
2017-12-06 23:02:38 -08:00
}
2018-10-15 07:52:03 -07:00
}