1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-28 03:08:30 +00:00

before merge

This commit is contained in:
Szymon Lesisz 2018-09-12 13:25:32 +02:00
parent 26978fe984
commit a6ac8239f0
6 changed files with 102 additions and 42 deletions

55
src/actions/TxActions.js Normal file
View File

@ -0,0 +1,55 @@
/* @flow */
import EthereumjsTx from 'ethereumjs-tx';
import EthereumjsUnits from 'ethereumjs-units';
import BigNumber from 'bignumber.js';
import { toHex } from 'web3-utils';
import { initWeb3 } from './Web3Actions';
import type {
Dispatch,
GetState,
PromiseAction,
EthereumTxRequest,
EthereumPreparedTx
} from 'flowtype';
export const prepareEthereumTx = (tx: EthereumTxRequest): PromiseAction<EthereumPreparedTx> => async (dispatch: Dispatch, getState: GetState): Promise<EthereumPreparedTx> => {
const instance = await dispatch( initWeb3(tx.network) );
const token = tx.token;
let data: string = `0x${tx.data}`; // TODO: check if already prefixed
let value: string = toHex( EthereumjsUnits.convert(tx.amount, 'ether', 'wei') );
let to: string = tx.to;
if (token) {
// smart contract transaction
const contract = instance.erc20.clone();
contract.options.address = token.address;
const tokenAmount: string = new BigNumber(tx.amount).times(Math.pow(10, token.decimals)).toString(10);
data = instance.erc20.methods.transfer(to, tokenAmount).encodeABI();
value = '0x00';
to = token.address;
}
return {
to,
value,
data,
chainId: instance.chainId,
nonce: toHex(tx.nonce),
gasLimit: toHex(tx.gasLimit),
gasPrice: toHex( EthereumjsUnits.convert(tx.gasPrice, 'gwei', 'wei') ),
r: '',
s: '',
v: '',
}
};
export const serializeEthereumTx = (tx: EthereumPreparedTx): PromiseAction<string> => async (dispatch: Dispatch, getState: GetState): Promise<string> => {
const ethTx = new EthereumjsTx(tx);
console.warn("SERIALIZE 1", `0x${ ethTx.serialize().toString('hex') }`)
console.warn("SERIALIZE 2", toHex( ethTx.serialize() ))
return `0x${ ethTx.serialize().toString('hex') }`;
// return toHex( ethTx.serialize() );
}

View File

@ -7,7 +7,6 @@ import * as ACCOUNT from 'actions/constants/account';
import type { Action, TrezorDevice } from 'flowtype'; import type { Action, TrezorDevice } from 'flowtype';
import type { import type {
AccountCreateAction,
AccountSetBalanceAction, AccountSetBalanceAction,
AccountSetNonceAction, AccountSetNonceAction,
} from 'actions/AccountsActions'; } from 'actions/AccountsActions';
@ -22,6 +21,8 @@ export type Account = {
+address: string; +address: string;
balance: string; balance: string;
nonce: number; nonce: number;
block: number;
transactions: number;
} }
export type State = Array<Account>; export type State = Array<Account>;
@ -37,28 +38,14 @@ export const findDeviceAccounts = (state: State, device: TrezorDevice, network:
return state.filter(addr => addr.deviceState === device.state); return state.filter(addr => addr.deviceState === device.state);
}; };
const createAccount = (state: State, action: AccountCreateAction): State => { const createAccount = (state: State, account: Account): State => {
// TODO check with device_id // TODO check with device_id
// check if account was created before // check if account was created before
// const exist: ?Account = state.find(account => account.address === action.address && account.network === action.network && action.device.features && account.deviceID === action.device.features.device_id); const exist: ?Account = state.find(a => a.address === account.address && a.network === account.network && a.deviceState === account.deviceState);
const exist: ?Account = state.find(account => account.address === action.address && account.network === action.network && account.deviceState === action.device.state);
if (exist) { if (exist) {
return state; return state;
} }
const newState: State = [ ...state ];
const account: Account = {
loaded: false,
network: action.network,
deviceID: action.device.features ? action.device.features.device_id : '0',
deviceState: action.device.state || 'undefined',
index: action.index,
addressPath: action.path,
address: action.address,
balance: '0',
nonce: 0,
};
const newState: State = [...state];
newState.push(account); newState.push(account);
return newState; return newState;
}; };
@ -74,8 +61,16 @@ const clear = (state: State, devices: Array<TrezorDevice>): State => {
return newState; return newState;
}; };
const updateAccount = (state: State, account: Account): State => {
const index: number = state.findIndex(a => a.address === account.address && a.network === account.network && a.deviceState === account.deviceState);
const newState: State = [...state];
newState[index] = account;
return newState;
}
const setBalance = (state: State, action: AccountSetBalanceAction): State => { const setBalance = (state: State, action: AccountSetBalanceAction): State => {
const index: number = state.findIndex(account => account.address === action.address && account.network === action.network && account.deviceState === action.deviceState); // const index: number = state.findIndex(account => account.address === action.address && account.network === action.network && account.deviceState === action.deviceState);
const index: number = state.findIndex(account => account.address === action.address && account.network === action.network);
const newState: State = [...state]; const newState: State = [...state];
newState[index].loaded = true; newState[index].loaded = true;
newState[index].balance = action.balance; newState[index].balance = action.balance;
@ -93,7 +88,7 @@ const setNonce = (state: State, action: AccountSetNonceAction): State => {
export default (state: State = initialState, action: Action): State => { export default (state: State = initialState, action: Action): State => {
switch (action.type) { switch (action.type) {
case ACCOUNT.CREATE: case ACCOUNT.CREATE:
return createAccount(state, action); return createAccount(state, action.payload);
case CONNECT.FORGET: case CONNECT.FORGET:
case CONNECT.FORGET_SINGLE: case CONNECT.FORGET_SINGLE:
@ -105,6 +100,9 @@ export default (state: State = initialState, action: Action): State => {
//case CONNECT.FORGET_SINGLE : //case CONNECT.FORGET_SINGLE :
// return forgetAccounts(state, action); // return forgetAccounts(state, action);
case ACCOUNT.UPDATE :
return updateAccount(state, action.payload);
case ACCOUNT.SET_BALANCE: case ACCOUNT.SET_BALANCE:
return setBalance(state, action); return setBalance(state, action);
case ACCOUNT.SET_NONCE: case ACCOUNT.SET_NONCE:

View File

@ -16,9 +16,7 @@ import type {
DiscoveryCompleteAction, DiscoveryCompleteAction,
} from 'actions/DiscoveryActions'; } from 'actions/DiscoveryActions';
import type { import type { Account } from './AccountsReducer';
AccountCreateAction,
} from 'actions/AccountsActions';
export type Discovery = { export type Discovery = {
network: string; network: string;
@ -75,8 +73,8 @@ const complete = (state: State, action: DiscoveryCompleteAction): State => {
return newState; return newState;
}; };
const accountCreate = (state: State, action: AccountCreateAction): State => { const accountCreate = (state: State, account: Account): State => {
const index: number = findIndex(state, action.network, action.device.state || '0'); const index: number = findIndex(state, account.network, account.deviceState);
const newState: State = [...state]; const newState: State = [...state];
newState[index].accountIndex++; newState[index].accountIndex++;
return newState; return newState;
@ -162,7 +160,7 @@ export default function discovery(state: State = initialState, action: Action):
case DISCOVERY.START: case DISCOVERY.START:
return start(state, action); return start(state, action);
case ACCOUNT.CREATE: case ACCOUNT.CREATE:
return accountCreate(state, action); return accountCreate(state, action.payload);
case DISCOVERY.STOP: case DISCOVERY.STOP:
return stop(state, action); return stop(state, action);
case DISCOVERY.COMPLETE: case DISCOVERY.COMPLETE:

View File

@ -22,7 +22,8 @@ export type Coin = {
backends: Array<{ backends: Array<{
name: string; name: string;
urls: Array<string>; urls: Array<string>;
}> }>;
web3: Array<string>;
} }
export type NetworkToken = { export type NetworkToken = {

View File

@ -6,6 +6,7 @@ import type { Action } from 'flowtype';
import type { SendTxAction } from 'actions/SendFormActions'; import type { SendTxAction } from 'actions/SendFormActions';
export type PendingTx = { export type PendingTx = {
+type: 'send' | 'recv';
+id: string; +id: string;
+network: string; +network: string;
+currency: string; +currency: string;
@ -21,19 +22,25 @@ export type State = Array<PendingTx>;
const initialState: State = []; const initialState: State = [];
const add = (state: State, action: SendTxAction): State => { // const add01 = (state: State, action: SendTxAction): State => {
// const newState = [...state];
// newState.push({
// id: action.txid,
// network: action.account.network,
// currency: action.selectedCurrency,
// amount: action.amount,
// total: action.total,
// tx: action.tx,
// nonce: action.nonce,
// address: action.account.address,
// rejected: false,
// });
// return newState;
// };
const add = (state: State, payload: any): State => {
const newState = [...state]; const newState = [...state];
newState.push({ newState.push(payload);
id: action.txid,
network: action.account.network,
currency: action.selectedCurrency,
amount: action.amount,
total: action.total,
tx: action.tx,
nonce: action.nonce,
address: action.account.address,
rejected: false,
});
return newState; return newState;
}; };
@ -48,9 +55,11 @@ const reject = (state: State, id: string): State => state.map((tx) => {
export default function pending(state: State = initialState, action: Action): State { export default function pending(state: State = initialState, action: Action): State {
switch (action.type) { switch (action.type) {
case SEND.TX_COMPLETE: // case SEND.TX_COMPLETE:
return add(state, action); // return add(state, action);
case PENDING.ADD:
return add(state, action.payload);
case PENDING.TX_RESOLVED: case PENDING.TX_RESOLVED:
return remove(state, action.tx.id); return remove(state, action.tx.id);
case PENDING.TX_NOT_FOUND: case PENDING.TX_NOT_FOUND:

View File

@ -4,7 +4,6 @@
import { LOCATION_CHANGE } from 'react-router-redux'; import { LOCATION_CHANGE } from 'react-router-redux';
import { DEVICE, TRANSPORT } from 'trezor-connect'; import { DEVICE, TRANSPORT } from 'trezor-connect';
import * as MODAL from 'actions/constants/modal'; import * as MODAL from 'actions/constants/modal';
import * as WEB3 from 'actions/constants/web3';
import * as WALLET from 'actions/constants/wallet'; import * as WALLET from 'actions/constants/wallet';
import * as CONNECT from 'actions/constants/TrezorConnect'; import * as CONNECT from 'actions/constants/TrezorConnect';