mirror of
https://github.com/status-im/MyCrypto.git
synced 2025-01-11 03:26:14 +00:00
Refactor Wallet Base Class to Wallet Interface and Implement (#184)
This commit is contained in:
parent
8854d42fd9
commit
481e6e89b6
@ -1,5 +1,5 @@
|
||||
// @flow
|
||||
import BaseWallet from 'libs/wallet/base';
|
||||
import type { IWallet } from 'libs/wallet/IWallet';
|
||||
import Big from 'bignumber.js';
|
||||
import { Wei } from 'libs/units';
|
||||
|
||||
@ -46,10 +46,10 @@ export function unlockKeystore(
|
||||
/*** Set Wallet ***/
|
||||
export type SetWalletAction = {
|
||||
type: 'WALLET_SET',
|
||||
payload: BaseWallet
|
||||
payload: IWallet
|
||||
};
|
||||
|
||||
export function setWallet(value: BaseWallet): SetWalletAction {
|
||||
export function setWallet(value: IWallet): SetWalletAction {
|
||||
return {
|
||||
type: 'WALLET_SET',
|
||||
payload: value
|
||||
|
@ -5,13 +5,13 @@ import translate from 'translations';
|
||||
import { Identicon } from 'components/ui';
|
||||
import { formatNumber } from 'utils/formatters';
|
||||
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 { Ether } from 'libs/units';
|
||||
|
||||
type Props = {
|
||||
balance: Ether,
|
||||
wallet: BaseWallet,
|
||||
wallet: IWallet,
|
||||
network: NetworkConfig
|
||||
};
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
// @flow
|
||||
import React from 'react';
|
||||
import Big from 'bignumber.js';
|
||||
import { BaseWallet } from 'libs/wallet';
|
||||
import { IWallet } from 'libs/wallet';
|
||||
import type { NetworkConfig } from 'config/data';
|
||||
import type { State } from 'reducers';
|
||||
import { connect } from 'react-redux';
|
||||
@ -18,7 +18,7 @@ import EquivalentValues from './EquivalentValues';
|
||||
import { Ether } from 'libs/units';
|
||||
|
||||
type Props = {
|
||||
wallet: BaseWallet,
|
||||
wallet: IWallet,
|
||||
balance: Ether,
|
||||
network: NetworkConfig,
|
||||
tokenBalances: TokenBalance[],
|
||||
|
@ -3,13 +3,13 @@ import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
import translate from 'translations';
|
||||
import WalletDecrypt from 'components/WalletDecrypt';
|
||||
import BaseWallet from 'libs/wallet/base';
|
||||
import type { IWallet } from 'libs/wallet/IWallet';
|
||||
import { connect } from 'react-redux';
|
||||
import type { State } from 'reducers';
|
||||
|
||||
type Props = {
|
||||
title: string,
|
||||
wallet: BaseWallet
|
||||
wallet: IWallet
|
||||
};
|
||||
|
||||
export class UnlockHeader extends React.Component {
|
||||
|
@ -5,7 +5,7 @@ import translate, { translateRaw } from 'translations';
|
||||
import Big from 'bignumber.js';
|
||||
import EthTx from 'ethereumjs-tx';
|
||||
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 ERC20 from 'libs/erc20';
|
||||
import { getTransactionFields } from 'libs/transaction';
|
||||
@ -22,7 +22,7 @@ import type { BroadcastTransactionStatus } from 'libs/transaction';
|
||||
type Props = {
|
||||
signedTx: string,
|
||||
transaction: EthTx,
|
||||
wallet: BaseWallet,
|
||||
wallet: IWallet,
|
||||
node: NodeConfig,
|
||||
token: ?Token,
|
||||
network: NetworkConfig,
|
||||
@ -86,7 +86,7 @@ class ConfirmationModal extends React.Component {
|
||||
clearInterval(this.readTimer);
|
||||
}
|
||||
|
||||
async _setWalletAddress(wallet: BaseWallet) {
|
||||
async _setWalletAddress(wallet: IWallet) {
|
||||
// TODO move getAddress to saga
|
||||
const fromAddress = await wallet.getAddress();
|
||||
this.setState({ fromAddress });
|
||||
|
@ -41,7 +41,7 @@ import type { BroadcastTxRequestedAction } from 'actions/wallet';
|
||||
import { showNotification } from 'actions/notifications';
|
||||
import type { ShowNotificationAction } from 'actions/notifications';
|
||||
// LIBS
|
||||
import BaseWallet from 'libs/wallet/base';
|
||||
import type { IWallet } from 'libs/wallet/IWallet';
|
||||
import { isValidETHAddress } from 'libs/validators';
|
||||
import type { RPCNode } from 'libs/nodes';
|
||||
import type {
|
||||
@ -93,7 +93,7 @@ type Props = {
|
||||
[string]: string
|
||||
}
|
||||
},
|
||||
wallet: BaseWallet,
|
||||
wallet: IWallet,
|
||||
balance: Ether,
|
||||
node: NodeConfig,
|
||||
nodeLib: RPCNode,
|
||||
|
@ -9,7 +9,7 @@ import { Wei, Ether, toTokenUnit } from 'libs/units';
|
||||
import { RPCNode } from 'libs/nodes';
|
||||
import { TransactionWithoutGas } from 'libs/messages';
|
||||
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 EthTx from 'ethereumjs-tx';
|
||||
import type { UNIT } from 'libs/units';
|
||||
@ -79,7 +79,7 @@ export function getTransactionFields(tx: EthTx) {
|
||||
export async function generateCompleteTransactionFromRawTransaction(
|
||||
node: INode,
|
||||
tx: ExtendedRawTransaction,
|
||||
wallet: BaseWallet,
|
||||
wallet: IWallet,
|
||||
token: ?Token
|
||||
): Promise<CompleteTransaction> {
|
||||
const { to, data, gasLimit, gasPrice, from, chainId, nonce } = tx;
|
||||
@ -161,7 +161,7 @@ export async function generateCompleteTransactionFromRawTransaction(
|
||||
}
|
||||
|
||||
export async function formatTxInput(
|
||||
wallet: BaseWallet,
|
||||
wallet: IWallet,
|
||||
{ token, unit, value, to, data }: TransactionInput
|
||||
): Promise<TransactionWithoutGas> {
|
||||
if (unit === 'ether') {
|
||||
@ -187,7 +187,7 @@ export async function formatTxInput(
|
||||
}
|
||||
|
||||
export async function generateCompleteTransaction(
|
||||
wallet: BaseWallet,
|
||||
wallet: IWallet,
|
||||
nodeLib: RPCNode,
|
||||
gasPrice: Wei,
|
||||
gasLimit: Big,
|
||||
|
6
common/libs/wallet/IWallet.js
Normal file
6
common/libs/wallet/IWallet.js
Normal file
@ -0,0 +1,6 @@
|
||||
import type { RawTransaction } from 'libs/transaction';
|
||||
|
||||
export interface IWallet {
|
||||
getAddress: () => Promise<string>,
|
||||
signRawTransaction: (_tx: RawTransaction) => Promise<string>
|
||||
}
|
@ -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');
|
||||
}
|
||||
}
|
@ -1,12 +1,11 @@
|
||||
// @flow
|
||||
import BaseWallet from './base';
|
||||
import type { IWallet } from './IWallet';
|
||||
|
||||
export default class DeterministicWallet extends BaseWallet {
|
||||
export default class DeterministicWallet implements IWallet {
|
||||
address: string;
|
||||
dPath: string;
|
||||
|
||||
constructor(address: string, dPath: string) {
|
||||
super();
|
||||
this.address = address;
|
||||
this.dPath = dPath;
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
// @flow
|
||||
|
||||
export { default as BaseWallet } from './base';
|
||||
export { default as IWallet } from './IWallet';
|
||||
export { default as PrivKeyWallet } from './privkey';
|
||||
export { default as EncryptedPrivKeyWallet } from './encprivkey';
|
||||
export { default as PresaleWallet } from './presale';
|
||||
|
@ -1,5 +1,5 @@
|
||||
// @flow
|
||||
import BaseWallet from './base';
|
||||
import type { IWallet } from './IWallet';
|
||||
import {
|
||||
privateToPublic,
|
||||
publicToAddress,
|
||||
@ -11,8 +11,9 @@ import { signRawTxWithPrivKey, signMessageWithPrivKey } from 'libs/signing';
|
||||
import { isValidPrivKey } from 'libs/validators';
|
||||
import type { RawTransaction } from 'libs/transaction';
|
||||
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;
|
||||
pubKey: Buffer;
|
||||
address: Buffer;
|
||||
@ -20,7 +21,6 @@ export default class PrivKeyWallet extends BaseWallet {
|
||||
if (!isValidPrivKey(privkey)) {
|
||||
throw new Error('Invalid private key');
|
||||
}
|
||||
super();
|
||||
this.privKey = privkey;
|
||||
this.pubKey = privateToPublic(this.privKey);
|
||||
this.address = publicToAddress(this.pubKey);
|
||||
@ -40,6 +40,14 @@ export default class PrivKeyWallet extends BaseWallet {
|
||||
return new PrivKeyWallet(randomBytes(32));
|
||||
}
|
||||
|
||||
getNakedAddress(): Promise<string> {
|
||||
return new Promise(resolve => {
|
||||
this.getAddress().then(address => {
|
||||
resolve(stripHex(address));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
toKeystore(password: string): Promise<UtcKeystore> {
|
||||
return new Promise(resolve => {
|
||||
this.getNakedAddress().then(address => {
|
||||
|
@ -5,7 +5,7 @@ import type {
|
||||
SetBalanceAction,
|
||||
SetTokenBalancesAction
|
||||
} from 'actions/wallet';
|
||||
import { BaseWallet } from 'libs/wallet';
|
||||
import { IWallet } from 'libs/wallet';
|
||||
import { toUnit } from 'libs/units';
|
||||
import Big from 'bignumber.js';
|
||||
import { getTxFromBroadcastTransactionStatus } from 'selectors/wallet';
|
||||
@ -13,7 +13,7 @@ import type { BroadcastTransactionStatus } from 'libs/transaction';
|
||||
import { Ether } from 'libs/units';
|
||||
|
||||
export type State = {
|
||||
inst: ?BaseWallet,
|
||||
inst: ?IWallet,
|
||||
// in ETH
|
||||
balance: Ether,
|
||||
tokens: {
|
||||
|
@ -19,7 +19,7 @@ import {
|
||||
UtcWallet,
|
||||
EncryptedPrivKeyWallet,
|
||||
PrivKeyWallet,
|
||||
BaseWallet
|
||||
IWallet
|
||||
} from 'libs/wallet';
|
||||
import { INode } from 'libs/nodes/INode';
|
||||
import { determineKeystoreType } from 'libs/keystore';
|
||||
@ -31,7 +31,7 @@ import TransactionSucceeded from 'components/ExtendedNotifications/TransactionSu
|
||||
|
||||
function* updateAccountBalance(): Generator<Yield, Return, Next> {
|
||||
try {
|
||||
const wallet: ?BaseWallet = yield select(getWalletInst);
|
||||
const wallet: ?IWallet = yield select(getWalletInst);
|
||||
if (!wallet) {
|
||||
return;
|
||||
}
|
||||
@ -48,7 +48,7 @@ function* updateAccountBalance(): Generator<Yield, Return, Next> {
|
||||
function* updateTokenBalances(): Generator<Yield, Return, Next> {
|
||||
try {
|
||||
const node: INode = yield select(getNodeLib);
|
||||
const wallet: ?BaseWallet = yield select(getWalletInst);
|
||||
const wallet: ?IWallet = yield select(getWalletInst);
|
||||
const tokens = yield select(getTokens);
|
||||
if (!wallet || !node) {
|
||||
return;
|
||||
|
@ -1,12 +1,12 @@
|
||||
// @flow
|
||||
import type { State } from 'reducers';
|
||||
import { BaseWallet } from 'libs/wallet';
|
||||
import { IWallet } from 'libs/wallet';
|
||||
import { getNetworkConfig } from 'selectors/config';
|
||||
import Big from 'bignumber.js';
|
||||
import type { Token } from 'config/data';
|
||||
import type { BroadcastTransactionStatus } from 'libs/transaction';
|
||||
|
||||
export function getWalletInst(state: State): ?BaseWallet {
|
||||
export function getWalletInst(state: State): ?IWallet {
|
||||
return state.wallet.inst;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user