Removing PK, adding metamask support

WIP
This commit is contained in:
emizzle 2019-06-10 22:11:29 +10:00
parent 23b67e8704
commit 9c85edf728
No known key found for this signature in database
GPG Key ID: 1FD4BAB3C37EE9BA
2 changed files with 63 additions and 48 deletions

View File

@ -13,6 +13,7 @@
"> 0.2%"
],
"files": [
"src",
"dist"
],
"scripts": {

View File

@ -1,4 +1,4 @@
/* global Web3 */
/* global EmbarkJS */
import BigNumber from "bn.js";
import ChildChain from "@omisego/omg-js-childchain";
import RootChain from "@omisego/omg-js-rootchain";
@ -19,46 +19,60 @@ export default class BaseEmbarkOmg {
// plugin opts
this.plasmaContractAddress = pluginConfig.PLASMA_CONTRACT_ADDRESS;
this.web3ProviderUrl = pluginConfig.WEB3_PROVIDER_URL;
//this.web3ProviderUrl = pluginConfig.WEB3_PROVIDER_URL;
this.watcherUrl = pluginConfig.WATCHER_URL;
this.childChainUrl = pluginConfig.CHILDCHAIN_URL;
}
async init(accounts, web3Path) {
async init(web3) { //}, web3Path) {
try {
if (this.initing) {
const message = "Already intializing the Plasma chain, please wait...";
this.logger.error(message);
//this.logger.error(message);
throw new Error(message);
}
this.initing = true;
if (!(accounts && accounts.length)) {
this.logger.error(ACCOUNT_CONFIG_ERROR);
throw new Error(ACCOUNT_CONFIG_ERROR);
}
const { address, privateKey } = accounts[0];
this.address = address;
this.addressPrivateKey = privateKey;
// if (!(accounts && accounts.length)) {
// //this.logger.error(ACCOUNT_CONFIG_ERROR);
// throw new Error(ACCOUNT_CONFIG_ERROR);
// }
//const { address, privateKey } = accounts[0];
//this.address = address;
//this.addressPrivateKey = privateKey;
// this.address = accounts[0];
// init Web3
const web3Lib = web3Path ? require(web3Path) : Web3;
this.web3 = new web3Lib();
const web3Provider = new web3Lib.providers.HttpProvider(this.web3ProviderUrl);
this.web3.setProvider(web3Provider);
// const web3Lib = web3Path ? require(web3Path) : Web3;
// this.web3 = new web3Lib();
// if (!web3) {
// web3 = EmbarkJS.Blockchain.providers["web3"];
// }
this.web3 = web3;
let accounts = await this.web3.eth.getAccounts();
this.address = accounts.length > 1 ? accounts[1] : accounts[0]; // ignore the first account because it is our deployer account, we want the manually added account
// if(!this.web3) {
// throw new Error("web3 cannot be found. Please ensure you have the 'embarkjs-connector-web3' plugin installed in your DApp.");
// }
// const web3Provider = new web3Lib.providers.HttpProvider(this.web3ProviderUrl);
//this.web3.setProvider(web3Provider);
// check account balance on the main chain
try {
this.maxDeposit = await this.web3.eth.getBalance(this.address);
if (!this.maxDeposit || new BigNumber(this.maxDeposit).lte(0)) {
this.logger.error(ACCOUNT_BALANCE_ERROR);
throw new Error(ACCOUNT_BALANCE_ERROR);
}
this.maxDeposit = new BigNumber(this.maxDeposit);
}
catch (e) {
this.logger.error(`Error getting balance for account ${this.address}: ${e}`);
}
// try {
// this.maxDeposit = await this.web3.eth.getBalance(this.address);
// if (!this.maxDeposit || new BigNumber(this.maxDeposit).lte(0)) {
// //this.logger.error(ACCOUNT_BALANCE_ERROR);
// throw new Error(ACCOUNT_BALANCE_ERROR);
// }
// this.maxDeposit = new BigNumber(this.maxDeposit);
// }
// catch (e) {
// this.logger.warn(`Error getting balance for account ${this.address}: ${e}`);
// }
// set up the Plasma chain
this.rootChain = new RootChain(this.web3, this.plasmaContractAddress);
@ -70,7 +84,7 @@ export default class BaseEmbarkOmg {
}
catch (e) {
const message = `Error initializing Plasma chain: ${e}`;
this.logger.error(message);
//this.logger.error(message);
throw new Error(message);
}
}
@ -78,37 +92,37 @@ export default class BaseEmbarkOmg {
async deposit(amount) {
if (!this.inited) {
const message = "Please wait for the Plasma chain to initialize...";
this.logger.error(message);
// this.logger.error(message);
throw new Error(message);
}
amount = new BigNumber(amount);
if (!amount || amount.lte(0)) {
const message = "You must deposit more than 0 wei.";
this.logger.error(message);
// this.logger.error(message);
throw new Error(message);
}
if (amount.gt(this.maxDeposit)) {
// recheck balance in case it was updated in a recent tx
this.maxDeposit = await this.web3.eth.getBalance(this.address);
if (amount.gt(this.maxDeposit)) {
const message = `You do not have enough funds for this deposit. Please deposit more funds in to ${this.address} and then try again.`;
this.logger.error(message);
throw new Error(message);
}
}
// if (amount.gt(this.maxDeposit) && this.maxDeposit.gt(0)) {
// // recheck balance in case it was updated in a recent tx
// this.maxDeposit = await this.web3.eth.getBalance(this.address);
// if (amount.gt(this.maxDeposit)) {
// const message = `You do not have enough funds for this deposit. Please deposit more funds in to ${this.address} and then try again.`;
// // this.logger.error(message);
// throw new Error(message);
// }
// }
// const DEPOSIT_AMT = "100000";
this.logger.info(`Depositing ${amount} wei...`);
const depositTx = transaction.encodeDeposit(this.address, amount, transaction.ETH_CURRENCY);
try {
const receipt = await this.rootChain.depositEth(depositTx, amount, { from: this.address, privateKey: this.addressPrivateKey });
const receipt = await this.rootChain.depositEth(depositTx, amount, { from: this.address });//, privateKey: this.addressPrivateKey });
this.logger.trace(receipt);
const message = `Successfully deposited ${amount} wei in to the Plasma chain.\nView the transaction: https://rinkeby.etherscan.io/tx/${receipt.transactionHash}`;
this.logger.info(message);
// this.logger.info(message);
return message;
}
catch (e) {
const message = `Error depositing ${amount} wei: ${e}`;
this.logger.error(message);
// this.logger.error(message);
throw new Error(message);
}
}
@ -119,11 +133,11 @@ export default class BaseEmbarkOmg {
const utxos = await this.childChain.getUtxos(this.address);
const utxosToSpend = this.selectUtxos(utxos, val, transaction.ETH_CURRENCY);
if (!utxosToSpend) {
return this.logger.error(`No utxo big enough to cover the amount ${val}`);
throw new Error(`No utxo big enough to cover the amount ${val}`);
}
val = new BigNumber(val);
if (!val || val.lte(0)) {
return this.logger.error("Transaction value must be more than 0 wei.");
throw new Error("Transaction value must be more than 0 wei.");
}
const txBody = {
@ -151,7 +165,7 @@ export default class BaseEmbarkOmg {
try {
const unsignedTx = await this.childChain.createTransaction(txBody);
const signatures = await this.childChain.signTransaction(unsignedTx, [this.addressPrivateKey]);
const signatures = await this.childChain.signTransaction(unsignedTx);//, [this.addressPrivateKey]);
const signedTx = await this.childChain.buildSignedTransaction(unsignedTx, signatures);
@ -159,11 +173,11 @@ export default class BaseEmbarkOmg {
const message = `Successfully submitted tx on the child chain: ${JSON.stringify(result)}\nView the transaction: http://quest.ari.omg.network/transaction/${result.txhash}`;
this.logger.info(message);
//this.logger.info(message);
return message;
}
catch (e) {
this.logger.error(e);
// this.logger.error(e);
throw e;
}
}
@ -191,12 +205,12 @@ export default class BaseEmbarkOmg {
}
);
const message = `Exited UTXO from address ${fromAddress} with value ${utxo.amount}. View the transaction: https://rinkeby.etherscan.io/tx/${receipt.transactionHash}`;
this.logger.info(message);
// this.logger.info(message);
return message;
}
catch (e) {
const message = `Error exiting the Plasma chain for UTXO ${JSON.stringify(utxo)}: ${e}`;
this.logger.error(message);
// this.logger.error(message);
errors.push(message);
}
});