2017-11-29 23:14:57 +00:00
|
|
|
import { IFullWallet } from '../IWallet';
|
2017-11-10 03:30:20 +00:00
|
|
|
import { ExtendedRawTransaction } from 'libs/transaction';
|
2017-12-01 16:32:29 +00:00
|
|
|
import { networkIdToName, sanitizeHex } from 'libs/values';
|
2017-11-10 03:30:20 +00:00
|
|
|
import { bufferToHex } from 'ethereumjs-util';
|
2017-12-01 16:32:29 +00:00
|
|
|
import { configuredStore } from 'store';
|
|
|
|
import { getNodeLib } from 'selectors/config';
|
|
|
|
import Web3Node, { isWeb3Node } from 'libs/nodes/web3';
|
|
|
|
import { INode } from 'libs/nodes/INode';
|
|
|
|
import BN from 'bn.js';
|
2017-11-10 03:30:20 +00:00
|
|
|
|
2017-11-29 23:14:57 +00:00
|
|
|
export default class Web3Wallet implements IFullWallet {
|
2017-11-10 03:30:20 +00:00
|
|
|
private address: string;
|
|
|
|
private network: string;
|
|
|
|
|
2017-12-01 16:32:29 +00:00
|
|
|
constructor(address: string, network: string) {
|
2017-11-10 03:30:20 +00:00
|
|
|
this.address = address;
|
|
|
|
this.network = network;
|
|
|
|
}
|
|
|
|
|
|
|
|
public getAddressString(): Promise<string> {
|
|
|
|
return Promise.resolve(this.address);
|
|
|
|
}
|
|
|
|
|
|
|
|
public signRawTransaction(): Promise<string> {
|
|
|
|
return Promise.reject(
|
|
|
|
new Error('Web3 wallets cannot sign raw transactions.')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-12-01 16:32:29 +00:00
|
|
|
public async signMessage(msg: string): Promise<string> {
|
|
|
|
const msgHex = bufferToHex(Buffer.from(msg));
|
|
|
|
const state = configuredStore.getState();
|
|
|
|
const nodeLib: Web3Node | INode = getNodeLib(state);
|
2017-11-10 03:30:20 +00:00
|
|
|
|
2017-12-01 16:32:29 +00:00
|
|
|
if (!isWeb3Node(nodeLib)) {
|
|
|
|
throw new Error('Web3 wallets can only be used with a Web3 node.');
|
|
|
|
}
|
2017-11-10 03:30:20 +00:00
|
|
|
|
2017-12-01 16:32:29 +00:00
|
|
|
return nodeLib.signMessage(msgHex, this.address);
|
2017-11-10 03:30:20 +00:00
|
|
|
}
|
|
|
|
|
2017-12-01 16:32:29 +00:00
|
|
|
public async sendTransaction(
|
|
|
|
transaction: ExtendedRawTransaction
|
|
|
|
): Promise<string> {
|
|
|
|
const state = configuredStore.getState();
|
|
|
|
const nodeLib: Web3Node | INode = getNodeLib(state);
|
|
|
|
const { from, to, value, gasLimit, gasPrice, data, nonce } = transaction;
|
|
|
|
const web3Tx = {
|
|
|
|
from,
|
|
|
|
to,
|
|
|
|
value,
|
|
|
|
gas:
|
|
|
|
gasLimit instanceof BN ? sanitizeHex(gasLimit.toString(16)) : gasLimit,
|
|
|
|
gasPrice:
|
|
|
|
gasPrice instanceof BN ? sanitizeHex(gasPrice.toString(16)) : gasPrice,
|
|
|
|
data,
|
|
|
|
nonce
|
|
|
|
};
|
2017-11-10 03:30:20 +00:00
|
|
|
|
2017-12-01 16:32:29 +00:00
|
|
|
if (!isWeb3Node(nodeLib)) {
|
|
|
|
throw new Error('Web3 wallets can only be used with a Web3 node.');
|
|
|
|
}
|
|
|
|
await this.networkCheck(nodeLib);
|
2017-11-10 03:30:20 +00:00
|
|
|
|
2017-12-01 16:32:29 +00:00
|
|
|
return nodeLib.sendTransaction(web3Tx);
|
|
|
|
}
|
2017-11-10 03:30:20 +00:00
|
|
|
|
2017-12-01 16:32:29 +00:00
|
|
|
private async networkCheck(lib: Web3Node) {
|
|
|
|
const netId = await lib.getNetVersion();
|
|
|
|
const netName = networkIdToName(netId);
|
|
|
|
if (this.network !== netName) {
|
|
|
|
throw new Error(
|
|
|
|
`Expected MetaMask / Mist network to be ${this.network}, but got ${
|
|
|
|
netName
|
|
|
|
}. Please change the network or restart MyEtherWallet.`
|
|
|
|
);
|
|
|
|
}
|
2017-11-10 03:30:20 +00:00
|
|
|
}
|
|
|
|
}
|