Sign transfer using PK

As a temp solution, sign transfer txs using the PK passed in. This will be removed once embark can support `eth_signTypedData_v3`.

Bump version.
This commit is contained in:
emizzle 2019-06-24 20:43:15 +10:00
parent 1d4ee2a2ee
commit 93db99cd08
No known key found for this signature in database
GPG Key ID: 1FD4BAB3C37EE9BA
2 changed files with 84 additions and 56 deletions

View File

@ -1,6 +1,6 @@
{
"name": "embarkjs-plasma",
"version": "1.0.1",
"version": "1.0.2",
"description": "EmbarkJS library for the OmiseGO Plasma plugin for Embark",
"main": "dist/index.js",
"browser": {

View File

@ -19,6 +19,8 @@ export default class EmbarkJSPlasma {
this.initing = false;
this.inited = false;
this.currentAddress = "";
// TODO: Remove PK support in favour of signing in Embark proxy
this.currentPrivateKey = "";
this.maxDeposit = 0;
this.state = {
account: {
@ -41,7 +43,7 @@ export default class EmbarkJSPlasma {
};
}
async init(embarkWeb3, useEmbarkWeb3 = false) {
async init(embarkWeb3, useEmbarkWeb3 = false, configAccounts = []) {
try {
if (this.initing) {
const message = "Already intializing the Plasma chain, please wait...";
@ -65,9 +67,18 @@ export default class EmbarkJSPlasma {
this.rootChain = new RootChain(this.web3, this.config.plasmaContractAddress);
this.childChain = new ChildChain(this.config.watcherUrl); //, this.config.childChainUrl);
let accounts = await this.web3.eth.getAccounts();
let accounts = [];
if (configAccounts.length) {
const account = configAccounts.length > 1 ? configAccounts[1] : configAccounts[0];
this.currentAddress = account.address;
this.currentPrivateKey = account.privateKey; // TODO: remove PK in favour of signing in Embark
this.web3.eth.accounts.wallet.add(account.privateKey);
}
else {
const accounts = await this.web3.eth.getAccounts();
const address = accounts.length > 1 ? accounts[1] : accounts[0]; // ignore the first account because it is our deployer account, we want the manually added account
this.currentAddress = address;
}
// check account balance on the main chain
// try {
@ -119,7 +130,7 @@ export default class EmbarkJSPlasma {
// ETH deposit
try {
const receipt = await this.rootChain.depositEth(depositTx, amount, {from: this.currentAddress});
this.logger.trace(receipt);
this.logger.info(receipt);
const message = `Successfully deposited ${amount} ${currency === transaction.ETH_CURRENCY ? "wei" : currency} in to the Plasma chain.\nView the transaction: https://rinkeby.etherscan.io/tx/${receipt.transactionHash}`;
return message;
} catch (e) {
@ -160,55 +171,64 @@ export default class EmbarkJSPlasma {
throw new Error(`No utxo big enough to cover the amount ${amount}`);
}
const txBody = {
inputs: utxosToSpend,
outputs: [
{
owner: toAddress,
currency,
amount: amount.toString()
}
]
};
// const txBody = {
// inputs: utxosToSpend,
// outputs: [
// {
// owner: toAddress,
// currency,
// amount: amount.toString()
// }
// ]
// };
const bnAmount = new BigNumber(utxosToSpend[0].amount);
if (bnAmount.gt(new BigNumber(amount))) {
// Need to add a 'change' output
const CHANGE_AMOUNT = bnAmount.sub(new BigNumber(amount));
txBody.outputs.push({
owner: this.currentAddress,
currency,
amount: CHANGE_AMOUNT
});
}
// const bnAmount = new BigNumber(utxosToSpend[0].amount);
// if (bnAmount.gt(new BigNumber(amount))) {
// // Need to add a 'change' output
// const CHANGE_AMOUNT = bnAmount.sub(new BigNumber(amount));
// txBody.outputs.push({
// owner: this.currentAddress,
// currency,
// amount: CHANGE_AMOUNT
// });
// }
if (currency !== transaction.ETH_CURRENCY && utxosToSpend.length > 1) {
// The fee input can be returned
txBody.outputs.push({
owner: this.currentAddress,
currency: utxosToSpend[utxosToSpend.length - 1].currency,
amount: utxosToSpend[utxosToSpend.length - 1].amount
});
}
// if (currency !== transaction.ETH_CURRENCY && utxosToSpend.length > 1) {
// // The fee input can be returned
// txBody.outputs.push({
// owner: this.currentAddress,
// currency: utxosToSpend[utxosToSpend.length - 1].currency,
// amount: utxosToSpend[utxosToSpend.length - 1].amount
// });
// }
const txBody = transaction.createTransactionBody(this.currentAddress, utxosToSpend, toAddress, amount, currency);
// Get the transaction data
const typedData = transaction.getTypedData(txBody, verifyingContract);
let signature;
if (this.isMetaMask) {
// We should really sign each input separately but in this we know that they're all
// from the same address, so we can sign once and use that signature for each input.
//
// const sigs = await Promise.all(utxosToSpend.map(input => signTypedData(web3, web3.utils.toChecksumAddress(from), typedData)))
//
const signature = await signTypedData(
signature = await signTypedData(
this.web3,
this.web3.utils.toChecksumAddress(this.currentAddress),
JSON.stringify(typedData)
);
} else {
// Sign the hashed data (once for each input spent)
signature = await this.childChain.signTransaction(typedData, [this.currentPrivateKey]);
//signature = await this.web3.eth.accounts.signTransaction(typedData, this.currentPrivateKey);
}
const sigs = new Array(utxosToSpend.length).fill(signature);
// Build the signed transaction
const signedTx = this.childChain.buildSignedTransaction(typedData, sigs);
const signedTx = this.childChain.buildSignedTransaction(typedData, this.isMetaMask ? sigs : signature);
// Submit the signed transaction to the childchain
const result = await this.childChain.submitTransaction(signedTx);
@ -221,6 +241,14 @@ export default class EmbarkJSPlasma {
return message;
}
get isMetaMask() {
return this.web3 &&
((this.web3.currentProvider &&
this.web3.currentProvider.isMetaMask) ||
(this.web3.givenProvider &&
this.web3.givenProvider.isMetaMask));
}
async exitAllUtxos(fromAddress) {
if (!this.inited) {
const message = "Please wait for the Plasma chain to initialize...";