Refactor Wallet Base Class to Wallet Interface and Implement (#184)

This commit is contained in:
HenryNguyen5 2017-09-11 20:26:16 -04:00 committed by Daniel Ternyak
parent 8854d42fd9
commit 481e6e89b6
15 changed files with 45 additions and 53 deletions

View File

@ -1,5 +1,5 @@
// @flow // @flow
import BaseWallet from 'libs/wallet/base'; import type { IWallet } from 'libs/wallet/IWallet';
import Big from 'bignumber.js'; import Big from 'bignumber.js';
import { Wei } from 'libs/units'; import { Wei } from 'libs/units';
@ -46,10 +46,10 @@ export function unlockKeystore(
/*** Set Wallet ***/ /*** Set Wallet ***/
export type SetWalletAction = { export type SetWalletAction = {
type: 'WALLET_SET', type: 'WALLET_SET',
payload: BaseWallet payload: IWallet
}; };
export function setWallet(value: BaseWallet): SetWalletAction { export function setWallet(value: IWallet): SetWalletAction {
return { return {
type: 'WALLET_SET', type: 'WALLET_SET',
payload: value payload: value

View File

@ -5,13 +5,13 @@ import translate from 'translations';
import { Identicon } from 'components/ui'; import { Identicon } from 'components/ui';
import { formatNumber } from 'utils/formatters'; import { formatNumber } from 'utils/formatters';
import type Big from 'bignumber.js'; import type Big from 'bignumber.js';
import type { BaseWallet } from 'libs/wallet'; import type { IWallet } from 'libs/wallet';
import type { NetworkConfig } from 'config/data'; import type { NetworkConfig } from 'config/data';
import { Ether } from 'libs/units'; import { Ether } from 'libs/units';
type Props = { type Props = {
balance: Ether, balance: Ether,
wallet: BaseWallet, wallet: IWallet,
network: NetworkConfig network: NetworkConfig
}; };

View File

@ -1,7 +1,7 @@
// @flow // @flow
import React from 'react'; import React from 'react';
import Big from 'bignumber.js'; import Big from 'bignumber.js';
import { BaseWallet } from 'libs/wallet'; import { IWallet } from 'libs/wallet';
import type { NetworkConfig } from 'config/data'; import type { NetworkConfig } from 'config/data';
import type { State } from 'reducers'; import type { State } from 'reducers';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
@ -18,7 +18,7 @@ import EquivalentValues from './EquivalentValues';
import { Ether } from 'libs/units'; import { Ether } from 'libs/units';
type Props = { type Props = {
wallet: BaseWallet, wallet: IWallet,
balance: Ether, balance: Ether,
network: NetworkConfig, network: NetworkConfig,
tokenBalances: TokenBalance[], tokenBalances: TokenBalance[],

View File

@ -3,13 +3,13 @@ import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import translate from 'translations'; import translate from 'translations';
import WalletDecrypt from 'components/WalletDecrypt'; import WalletDecrypt from 'components/WalletDecrypt';
import BaseWallet from 'libs/wallet/base'; import type { IWallet } from 'libs/wallet/IWallet';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import type { State } from 'reducers'; import type { State } from 'reducers';
type Props = { type Props = {
title: string, title: string,
wallet: BaseWallet wallet: IWallet
}; };
export class UnlockHeader extends React.Component { export class UnlockHeader extends React.Component {

View File

@ -5,7 +5,7 @@ import translate, { translateRaw } from 'translations';
import Big from 'bignumber.js'; import Big from 'bignumber.js';
import EthTx from 'ethereumjs-tx'; import EthTx from 'ethereumjs-tx';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import BaseWallet from 'libs/wallet/base'; import type { IWallet } from 'libs/wallet/IWallet';
import { toUnit, toTokenDisplay } from 'libs/units'; import { toUnit, toTokenDisplay } from 'libs/units';
import ERC20 from 'libs/erc20'; import ERC20 from 'libs/erc20';
import { getTransactionFields } from 'libs/transaction'; import { getTransactionFields } from 'libs/transaction';
@ -22,7 +22,7 @@ import type { BroadcastTransactionStatus } from 'libs/transaction';
type Props = { type Props = {
signedTx: string, signedTx: string,
transaction: EthTx, transaction: EthTx,
wallet: BaseWallet, wallet: IWallet,
node: NodeConfig, node: NodeConfig,
token: ?Token, token: ?Token,
network: NetworkConfig, network: NetworkConfig,
@ -86,7 +86,7 @@ class ConfirmationModal extends React.Component {
clearInterval(this.readTimer); clearInterval(this.readTimer);
} }
async _setWalletAddress(wallet: BaseWallet) { async _setWalletAddress(wallet: IWallet) {
// TODO move getAddress to saga // TODO move getAddress to saga
const fromAddress = await wallet.getAddress(); const fromAddress = await wallet.getAddress();
this.setState({ fromAddress }); this.setState({ fromAddress });

View File

@ -41,7 +41,7 @@ import type { BroadcastTxRequestedAction } from 'actions/wallet';
import { showNotification } from 'actions/notifications'; import { showNotification } from 'actions/notifications';
import type { ShowNotificationAction } from 'actions/notifications'; import type { ShowNotificationAction } from 'actions/notifications';
// LIBS // LIBS
import BaseWallet from 'libs/wallet/base'; import type { IWallet } from 'libs/wallet/IWallet';
import { isValidETHAddress } from 'libs/validators'; import { isValidETHAddress } from 'libs/validators';
import type { RPCNode } from 'libs/nodes'; import type { RPCNode } from 'libs/nodes';
import type { import type {
@ -93,7 +93,7 @@ type Props = {
[string]: string [string]: string
} }
}, },
wallet: BaseWallet, wallet: IWallet,
balance: Ether, balance: Ether,
node: NodeConfig, node: NodeConfig,
nodeLib: RPCNode, nodeLib: RPCNode,

View File

@ -9,7 +9,7 @@ import { Wei, Ether, toTokenUnit } from 'libs/units';
import { RPCNode } from 'libs/nodes'; import { RPCNode } from 'libs/nodes';
import { TransactionWithoutGas } from 'libs/messages'; import { TransactionWithoutGas } from 'libs/messages';
import type { INode } from 'libs/nodes/INode'; import type { INode } from 'libs/nodes/INode';
import type { BaseWallet } from 'libs/wallet'; import type { IWallet } from 'libs/wallet';
import type { Token } from 'config/data'; import type { Token } from 'config/data';
import type EthTx from 'ethereumjs-tx'; import type EthTx from 'ethereumjs-tx';
import type { UNIT } from 'libs/units'; import type { UNIT } from 'libs/units';
@ -79,7 +79,7 @@ export function getTransactionFields(tx: EthTx) {
export async function generateCompleteTransactionFromRawTransaction( export async function generateCompleteTransactionFromRawTransaction(
node: INode, node: INode,
tx: ExtendedRawTransaction, tx: ExtendedRawTransaction,
wallet: BaseWallet, wallet: IWallet,
token: ?Token token: ?Token
): Promise<CompleteTransaction> { ): Promise<CompleteTransaction> {
const { to, data, gasLimit, gasPrice, from, chainId, nonce } = tx; const { to, data, gasLimit, gasPrice, from, chainId, nonce } = tx;
@ -161,7 +161,7 @@ export async function generateCompleteTransactionFromRawTransaction(
} }
export async function formatTxInput( export async function formatTxInput(
wallet: BaseWallet, wallet: IWallet,
{ token, unit, value, to, data }: TransactionInput { token, unit, value, to, data }: TransactionInput
): Promise<TransactionWithoutGas> { ): Promise<TransactionWithoutGas> {
if (unit === 'ether') { if (unit === 'ether') {
@ -187,7 +187,7 @@ export async function formatTxInput(
} }
export async function generateCompleteTransaction( export async function generateCompleteTransaction(
wallet: BaseWallet, wallet: IWallet,
nodeLib: RPCNode, nodeLib: RPCNode,
gasPrice: Wei, gasPrice: Wei,
gasLimit: Big, gasLimit: Big,

View File

@ -0,0 +1,6 @@
import type { RawTransaction } from 'libs/transaction';
export interface IWallet {
getAddress: () => Promise<string>,
signRawTransaction: (_tx: RawTransaction) => Promise<string>
}

View File

@ -1,21 +0,0 @@
// @flow
import { stripHex } from 'libs/values';
import type { RawTransaction } from 'libs/transaction';
export default class BaseWallet {
getAddress(): Promise<string> {
return Promise.reject('Implement me');
}
getNakedAddress(): Promise<string> {
return new Promise(resolve => {
this.getAddress().then(address => {
resolve(stripHex(address));
});
});
}
signRawTransaction(_tx: RawTransaction): Promise<string> {
return Promise.reject('Implement me');
}
}

View File

@ -1,12 +1,11 @@
// @flow // @flow
import BaseWallet from './base'; import type { IWallet } from './IWallet';
export default class DeterministicWallet extends BaseWallet { export default class DeterministicWallet implements IWallet {
address: string; address: string;
dPath: string; dPath: string;
constructor(address: string, dPath: string) { constructor(address: string, dPath: string) {
super();
this.address = address; this.address = address;
this.dPath = dPath; this.dPath = dPath;
} }

View File

@ -1,6 +1,6 @@
// @flow // @flow
export { default as BaseWallet } from './base'; export { default as IWallet } from './IWallet';
export { default as PrivKeyWallet } from './privkey'; export { default as PrivKeyWallet } from './privkey';
export { default as EncryptedPrivKeyWallet } from './encprivkey'; export { default as EncryptedPrivKeyWallet } from './encprivkey';
export { default as PresaleWallet } from './presale'; export { default as PresaleWallet } from './presale';

View File

@ -1,5 +1,5 @@
// @flow // @flow
import BaseWallet from './base'; import type { IWallet } from './IWallet';
import { import {
privateToPublic, privateToPublic,
publicToAddress, publicToAddress,
@ -11,8 +11,9 @@ import { signRawTxWithPrivKey, signMessageWithPrivKey } from 'libs/signing';
import { isValidPrivKey } from 'libs/validators'; import { isValidPrivKey } from 'libs/validators';
import type { RawTransaction } from 'libs/transaction'; import type { RawTransaction } from 'libs/transaction';
import type { UtcKeystore } from 'libs/keystore'; import type { UtcKeystore } from 'libs/keystore';
import { stripHex } from 'libs/values';
export default class PrivKeyWallet extends BaseWallet { export default class PrivKeyWallet implements IWallet {
privKey: Buffer; privKey: Buffer;
pubKey: Buffer; pubKey: Buffer;
address: Buffer; address: Buffer;
@ -20,7 +21,6 @@ export default class PrivKeyWallet extends BaseWallet {
if (!isValidPrivKey(privkey)) { if (!isValidPrivKey(privkey)) {
throw new Error('Invalid private key'); throw new Error('Invalid private key');
} }
super();
this.privKey = privkey; this.privKey = privkey;
this.pubKey = privateToPublic(this.privKey); this.pubKey = privateToPublic(this.privKey);
this.address = publicToAddress(this.pubKey); this.address = publicToAddress(this.pubKey);
@ -40,6 +40,14 @@ export default class PrivKeyWallet extends BaseWallet {
return new PrivKeyWallet(randomBytes(32)); return new PrivKeyWallet(randomBytes(32));
} }
getNakedAddress(): Promise<string> {
return new Promise(resolve => {
this.getAddress().then(address => {
resolve(stripHex(address));
});
});
}
toKeystore(password: string): Promise<UtcKeystore> { toKeystore(password: string): Promise<UtcKeystore> {
return new Promise(resolve => { return new Promise(resolve => {
this.getNakedAddress().then(address => { this.getNakedAddress().then(address => {

View File

@ -5,7 +5,7 @@ import type {
SetBalanceAction, SetBalanceAction,
SetTokenBalancesAction SetTokenBalancesAction
} from 'actions/wallet'; } from 'actions/wallet';
import { BaseWallet } from 'libs/wallet'; import { IWallet } from 'libs/wallet';
import { toUnit } from 'libs/units'; import { toUnit } from 'libs/units';
import Big from 'bignumber.js'; import Big from 'bignumber.js';
import { getTxFromBroadcastTransactionStatus } from 'selectors/wallet'; import { getTxFromBroadcastTransactionStatus } from 'selectors/wallet';
@ -13,7 +13,7 @@ import type { BroadcastTransactionStatus } from 'libs/transaction';
import { Ether } from 'libs/units'; import { Ether } from 'libs/units';
export type State = { export type State = {
inst: ?BaseWallet, inst: ?IWallet,
// in ETH // in ETH
balance: Ether, balance: Ether,
tokens: { tokens: {

View File

@ -19,7 +19,7 @@ import {
UtcWallet, UtcWallet,
EncryptedPrivKeyWallet, EncryptedPrivKeyWallet,
PrivKeyWallet, PrivKeyWallet,
BaseWallet IWallet
} from 'libs/wallet'; } from 'libs/wallet';
import { INode } from 'libs/nodes/INode'; import { INode } from 'libs/nodes/INode';
import { determineKeystoreType } from 'libs/keystore'; import { determineKeystoreType } from 'libs/keystore';
@ -31,7 +31,7 @@ import TransactionSucceeded from 'components/ExtendedNotifications/TransactionSu
function* updateAccountBalance(): Generator<Yield, Return, Next> { function* updateAccountBalance(): Generator<Yield, Return, Next> {
try { try {
const wallet: ?BaseWallet = yield select(getWalletInst); const wallet: ?IWallet = yield select(getWalletInst);
if (!wallet) { if (!wallet) {
return; return;
} }
@ -48,7 +48,7 @@ function* updateAccountBalance(): Generator<Yield, Return, Next> {
function* updateTokenBalances(): Generator<Yield, Return, Next> { function* updateTokenBalances(): Generator<Yield, Return, Next> {
try { try {
const node: INode = yield select(getNodeLib); const node: INode = yield select(getNodeLib);
const wallet: ?BaseWallet = yield select(getWalletInst); const wallet: ?IWallet = yield select(getWalletInst);
const tokens = yield select(getTokens); const tokens = yield select(getTokens);
if (!wallet || !node) { if (!wallet || !node) {
return; return;

View File

@ -1,12 +1,12 @@
// @flow // @flow
import type { State } from 'reducers'; import type { State } from 'reducers';
import { BaseWallet } from 'libs/wallet'; import { IWallet } from 'libs/wallet';
import { getNetworkConfig } from 'selectors/config'; import { getNetworkConfig } from 'selectors/config';
import Big from 'bignumber.js'; import Big from 'bignumber.js';
import type { Token } from 'config/data'; import type { Token } from 'config/data';
import type { BroadcastTransactionStatus } from 'libs/transaction'; import type { BroadcastTransactionStatus } from 'libs/transaction';
export function getWalletInst(state: State): ?BaseWallet { export function getWalletInst(state: State): ?IWallet {
return state.wallet.inst; return state.wallet.inst;
} }