1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-15 21:08:57 +00:00
trezor-wallet/src/actions/ripple/DiscoveryActions.js

61 lines
1.8 KiB
JavaScript
Raw Normal View History

2018-11-23 12:07:22 +00:00
/* @flow */
import TrezorConnect from 'trezor-connect';
import * as DISCOVERY from 'actions/constants/discovery';
import { enhanceAccount } from 'utils/accountUtils';
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';
import type { Discovery } from 'reducers/DiscoveryReducer';
2018-11-23 12:07:22 +00:00
export type DiscoveryStartAction = {
type: typeof DISCOVERY.START,
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> => ({
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);
if (!network) throw new Error('Discovery network not found');
2018-11-23 12:07:22 +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.getAccountInfo({
2018-11-23 12:07:22 +00:00
device: {
path: device.path,
instance: device.instance,
state: device.state,
},
path,
2018-11-23 12:07:22 +00:00
keepSession: true, // acquire and hold session
useEmptyPassphrase: device.useEmptyPassphrase,
coin: network.shortcut,
2018-11-23 12:07:22 +00:00
});
// handle TREZOR response error
2018-11-23 12:07:22 +00:00
if (!response.success) {
throw new Error(response.payload.error);
2018-11-23 12:07:22 +00:00
}
return enhanceAccount(response.payload, {
index: discoveryProcess.accountIndex,
network,
device,
});
2019-03-04 12:33:02 +00:00
};