HenryNguyen5 04eaa08d6c Shepherd MVP Integration (#1413)
* initial mvp

* First functioning pass

* Add token balance shim

* Add working web3 implementation

* Fix tests

* Fix tsc errors

* Implement token batch splitting

* Undo logger change

* Fix linting errors

* Revert makeconfig change

* Add typing to token proxy + use string interpolation

* Remove useless parameter

* Remove logging

* Use type coercion to fix proxied methods

* Update shepherd

* Update to typescript 2.8.1

* Fix merged typings

* Address PR comments

* replace myc-shepherd with mycrypto-shepherd
2018-04-06 15:52:48 -05:00

88 lines
2.4 KiB
TypeScript

import { getTransactionFields, makeTransaction } from 'libs/transaction';
import { IFullWallet } from '../IWallet';
import { bufferToHex } from 'ethereumjs-util';
import { configuredStore } from 'store';
import { getNodeLib, getNetworkNameByChainId } from 'selectors/config';
import Web3Node from 'libs/nodes/web3';
import { INode } from 'libs/nodes/INode';
export default class Web3Wallet implements IFullWallet {
private address: string;
private network: string;
constructor(address: string, network: string) {
this.address = address;
this.network = network;
}
public getAddressString(): string {
return this.address;
}
public signRawTransaction(): Promise<Buffer> {
return Promise.reject(new Error('Web3 wallets cannot sign raw transactions.'));
}
public async signMessage(msg: string): Promise<string> {
const msgHex = bufferToHex(Buffer.from(msg));
const state = configuredStore.getState();
const nodeLib: Web3Node | INode = getNodeLib(state);
if (!nodeLib) {
throw new Error('');
}
/*
if (!isWeb3Node(nodeLib)) {
throw new Error('Web3 wallets can only be used with a Web3 node.');
}*/
return (nodeLib as Web3Node).signMessage(msgHex, this.address);
}
public async sendTransaction(serializedTransaction: string): Promise<string> {
const transactionInstance = makeTransaction(serializedTransaction);
const { to, value, gasLimit: gas, gasPrice, data, nonce, chainId } = getTransactionFields(
transactionInstance
);
const from = this.address;
const web3Tx = {
from,
to,
value,
gas,
gasPrice,
data,
nonce,
chainId
};
const state = configuredStore.getState();
const nodeLib: Web3Node = getNodeLib(state) as any;
if (!nodeLib) {
throw new Error('');
}
/*
if (!isWeb3Node(nodeLib)) {
throw new Error('Web3 wallets can only be used with a Web3 node.');
}*/
await this.networkCheck(nodeLib);
return nodeLib.sendTransaction(web3Tx);
}
private async networkCheck(lib: Web3Node) {
const netId = await lib.getNetVersion();
const netName = getNetworkNameByChainId(configuredStore.getState(), netId);
if (this.network !== netName) {
throw new Error(
`Expected MetaMask / Mist network to be ${
this.network
}, but got ${netName}. Please change the network or refresh the page.`
);
}
}
}