2018-11-23 12:07:22 +00:00
|
|
|
/* @flow */
|
|
|
|
|
|
|
|
import TrezorConnect from 'trezor-connect';
|
|
|
|
import * as DISCOVERY from 'actions/constants/discovery';
|
2018-12-03 17:03:26 +00:00
|
|
|
import { toDecimalAmount } from 'utils/formatUtils';
|
2018-11-23 12:07:22 +00:00
|
|
|
|
2019-03-04 12:33:02 +00:00
|
|
|
import type { PromiseAction, GetState, Dispatch, TrezorDevice, Network, Account } from 'flowtype';
|
2018-11-23 13:46:24 +00:00
|
|
|
import type { Discovery } from 'reducers/DiscoveryReducer';
|
2018-11-23 12:07:22 +00:00
|
|
|
|
|
|
|
export type DiscoveryStartAction = {
|
|
|
|
type: typeof DISCOVERY.START,
|
2018-11-23 13:46:24 +00:00
|
|
|
networkType: 'ripple',
|
2018-11-23 12:07:22 +00:00
|
|
|
network: Network,
|
|
|
|
device: TrezorDevice,
|
|
|
|
};
|
|
|
|
|
2019-03-04 12:33:02 +00:00
|
|
|
export const begin = (
|
|
|
|
device: TrezorDevice,
|
|
|
|
network: Network
|
|
|
|
): PromiseAction<DiscoveryStartAction> => async (): Promise<DiscoveryStartAction> => ({
|
2018-11-23 13:46:24 +00:00
|
|
|
type: DISCOVERY.START,
|
|
|
|
networkType: 'ripple',
|
|
|
|
network,
|
|
|
|
device,
|
|
|
|
});
|
2018-11-23 12:07:22 +00:00
|
|
|
|
2019-03-04 12:33:02 +00:00
|
|
|
export const discoverAccount = (
|
|
|
|
device: TrezorDevice,
|
|
|
|
discoveryProcess: Discovery
|
|
|
|
): PromiseAction<Account> => async (dispatch: Dispatch, getState: GetState): Promise<Account> => {
|
2018-11-23 12:07:22 +00:00
|
|
|
const { config } = getState().localStorage;
|
|
|
|
const network = config.networks.find(c => c.shortcut === discoveryProcess.network);
|
2018-11-23 13:46:24 +00:00
|
|
|
if (!network) throw new Error('Discovery network not found');
|
2018-11-23 12:07:22 +00:00
|
|
|
|
2018-11-23 13:46:24 +00:00
|
|
|
const { accountIndex } = discoveryProcess;
|
2018-11-23 12:07:22 +00:00
|
|
|
const path = network.bip44.slice(0).replace('a', accountIndex.toString());
|
|
|
|
|
|
|
|
const response = await TrezorConnect.rippleGetAccountInfo({
|
|
|
|
device: {
|
|
|
|
path: device.path,
|
|
|
|
instance: device.instance,
|
|
|
|
state: device.state,
|
|
|
|
},
|
|
|
|
account: {
|
|
|
|
path,
|
|
|
|
block: 0,
|
|
|
|
},
|
|
|
|
keepSession: true, // acquire and hold session
|
|
|
|
useEmptyPassphrase: device.useEmptyPassphrase,
|
2018-12-18 21:03:59 +00:00
|
|
|
coin: network.shortcut,
|
2018-11-23 12:07:22 +00:00
|
|
|
});
|
|
|
|
|
2018-11-23 13:46:24 +00:00
|
|
|
// handle TREZOR response error
|
2018-11-23 12:07:22 +00:00
|
|
|
if (!response.success) {
|
2018-11-23 13:46:24 +00:00
|
|
|
throw new Error(response.payload.error);
|
2018-11-23 12:07:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const account = response.payload;
|
2018-11-23 13:46:24 +00:00
|
|
|
const empty = account.sequence <= 0 && account.balance === '0';
|
|
|
|
|
|
|
|
return {
|
2018-12-21 09:58:53 +00:00
|
|
|
imported: false,
|
2018-11-23 13:46:24 +00:00
|
|
|
index: discoveryProcess.accountIndex,
|
|
|
|
network: network.shortcut,
|
|
|
|
deviceID: device.features ? device.features.device_id : '0',
|
|
|
|
deviceState: device.state || '0',
|
2018-12-21 09:58:53 +00:00
|
|
|
accountPath: account.path || [],
|
2019-01-04 13:32:14 +00:00
|
|
|
descriptor: account.descriptor,
|
2018-12-21 09:58:53 +00:00
|
|
|
|
|
|
|
balance: toDecimalAmount(account.balance, network.decimals),
|
|
|
|
availableBalance: toDecimalAmount(account.availableBalance, network.decimals),
|
2018-11-23 13:46:24 +00:00
|
|
|
block: account.block,
|
|
|
|
transactions: account.transactions,
|
|
|
|
empty,
|
2018-12-21 09:58:53 +00:00
|
|
|
|
|
|
|
networkType: 'ripple',
|
|
|
|
sequence: account.sequence,
|
2019-01-09 13:32:57 +00:00
|
|
|
reserve: toDecimalAmount(account.reserve, network.decimals),
|
2018-11-23 13:46:24 +00:00
|
|
|
};
|
2019-03-04 12:33:02 +00:00
|
|
|
};
|