1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-12-25 16:38:08 +00:00

trezor-connect response flowtype

This commit is contained in:
Szymon Lesisz 2018-05-11 19:32:12 +02:00
parent 80fa9cfe21
commit 1a08e4be6a
6 changed files with 71 additions and 39 deletions

View File

@ -135,7 +135,7 @@ const begin = (device: TrezorDevice, network: string): AsyncAction => {
});
// get xpub from TREZOR
const response: Object = await TrezorConnect.getPublicKey({
const response = await TrezorConnect.getPublicKey({
device: {
path: device.path,
instance: device.instance,

View File

@ -434,7 +434,7 @@ export const onAmountChange = (amount: string): ThunkAction => {
}
}
export const onCurrencyChange = (currency: any): ThunkAction => {
export const onCurrencyChange = (currency: { value: string, label: string }): ThunkAction => {
return (dispatch: Dispatch, getState: GetState): void => {
const accountState: ?AccountState = getState().abstractAccount;
if (!accountState) return;
@ -789,7 +789,7 @@ const estimateGasPrice = (): AsyncAction => {
const data: string = '0x' + (currentState.data.length % 2 === 0 ? currentState.data : '0' + currentState.data);
const gasLimit = await estimateGas(web3instance.web3, {
to: '0xdb6e09ddca62d0959dc4725697e66b8152222aee',
to: '0xdb6e09ddca62d0959dc4725697e66b8152222aee', // TODO: real adress
data,
value: web3.toHex(web3.toWei(currentState.amount, 'ether')),
gasPrice: web3.toHex( EthereumjsUnits.convert(currentState.gasPrice, 'gwei', 'wei') ),
@ -861,20 +861,6 @@ export const onSend = (): AsyncAction => {
v: ''
}
// const gasOptions = {
// to: txData.to,
// data: txData.data
// }
// const gasPrice = await getGasPrice(web3);
// txData.nonce = web3.toHex(nonce);
// txData.gasLimit = web3.toHex(gasLimit);
// txData.gasPrice = web3.toHex( EthereumjsUnits.convert(gasPrice, 'gwei', 'wei') );
const selected: ?TrezorDevice = findSelectedDevice(getState().connect);
if (!selected) return;
@ -914,17 +900,13 @@ export const onSend = (): AsyncAction => {
txData.s = '0x' + signedTransaction.payload.s;
txData.v = web3.toHex(signedTransaction.payload.v);
// const gasLimit2 = await estimateGas(web3, txData);
const { config } = getState().localStorage;
if (!config) return;
const selectedCoin: ?Coin = config.coins.find(c => c.network === currentState.network);
if (!selectedCoin) return;
try {
const tx = new EthereumjsTx(txData);
const serializedTx = '0x' + tx.serialize().toString('hex');
const txid: string = await pushTx(web3, serializedTx);
const coin: Coin = accountState.coin;
dispatch({
type: SEND.TX_COMPLETE,
@ -940,13 +922,13 @@ export const onSend = (): AsyncAction => {
payload: {
type: 'success',
title: 'Transaction success',
message: `<a href="${selectedCoin.explorer.tx}${txid}" class="green" target="_blank" rel="noreferrer noopener">See transaction detail</a>`,
message: `<a href="${coin.explorer.tx}${txid}" class="green" target="_blank" rel="noreferrer noopener">See transaction detail</a>`,
cancelable: true,
actions: []
}
});
} catch(error) {
} catch (error) {
dispatch({
type: NOTIFICATION.ADD,

View File

@ -132,7 +132,7 @@ export const init = (): AsyncAction => {
popup: false,
webusb: true
});
} catch (error) {
} catch (error) {
// dispatch({
// type: CONNECT.INITIALIZATION_ERROR,
// error
@ -201,7 +201,7 @@ const sortDevices = (devices: Array<TrezorDevice>): Array<TrezorDevice> => {
});
}
export const initConnectedDevice = (device: any): ThunkAction => {
export const initConnectedDevice = (device: TrezorDevice | Device): ThunkAction => {
return (dispatch: Dispatch, getState: GetState): void => {
const selected = findSelectedDevice(getState().connect);

View File

@ -441,9 +441,9 @@ export const estimateGas = (web3: Web3, options: EstimateGasOptions): Promise<nu
})
}
export const pushTx = (web3: Web3, tx: any): Promise<any> => {
export const pushTx = (web3: Web3, tx: any): Promise<string> => {
return new Promise((resolve, reject) => {
web3.eth.sendRawTransaction(tx, (error, result) => {
web3.eth.sendRawTransaction(tx, (error: Error, result: string) => {
if (error) {
reject(error);
} else {

View File

@ -79,10 +79,10 @@ declare module 'trezor-connect' {
};
declare type T_RESPONSE_EVENT = 'RESPONSE_EVENT';
declare export type ResponseMessage = {
event: string;
type: string;
event: T_RESPONSE_EVENT;
type: T_RESPONSE_EVENT;
id: number;
success: boolean;
payload: Object;
@ -163,7 +163,57 @@ declare module 'trezor-connect' {
declare type TransportEventListener = (type: T_TRANSPORT_EVENT, handler: (event: TransportMessage) => void) => void;
declare type ResponseUnsuccessful = {
success: false;
payload: {
error: string;
}
}
declare type ResponseEthereumSignTransaction = {
success: true;
payload: {
r: string;
s: string;
v: string;
}
} | ResponseUnsuccessful;
declare type ResponseEthereumGetAddress = {
success: true;
payload: {
address: string,
path: Array <number>
}
} | ResponseUnsuccessful;
declare type ResponseGetDeviceState = {
success: true;
payload: {
state: string;
}
} | ResponseUnsuccessful;
declare type ResponseGetFeatures = {
success: true;
payload: {
// TODO
}
} | ResponseUnsuccessful;
declare type ResponseGetPublicKey = {
success: true;
payload: {
chainCode: string,
childNum: number,
depth: number,
fingerprint: number,
path: Array <number>,
publicKey: string,
xpub: string,
xpubFormatted: string
}
} | ResponseUnsuccessful;
declare module.exports: {
@ -174,13 +224,13 @@ declare module 'trezor-connect' {
getVersion: () => any;
renderWebUSBButton: (className?: string) => void;
getDeviceState: (options: Object) => Promise<Object>;
getFeatures: (options: Object) => Promise<Object>;
getPublicKey: (options: Object) => Promise<Object>;
ethereumGetAddress: (options: Object) => Promise<Object>;
getDeviceState: (options: Object) => Promise<ResponseGetDeviceState>;
getFeatures: (options: Object) => Promise<ResponseGetFeatures>;
getPublicKey: (options: Object) => Promise<ResponseGetPublicKey>;
ethereumGetAddress: (options: Object) => Promise<ResponseEthereumGetAddress>;
uiResponse: (options: Object) => Promise<Object>;
ethereumSignTransaction: (options: Object) => Promise<Object>;
ethereumSignTransaction: (options: Object) => Promise<ResponseEthereumSignTransaction>;
// export const RESPONSE_EVENT: string = 'RESPONSE_EVENT';

View File

@ -58,7 +58,7 @@ declare module 'web3' {
// sign: (payload: string, signer: EthereumAddressT) => Promise<string>,
contract: (abi: Array<Object>) => ContractFactory,
estimateGas: (options: EstimateGasOptions, callback: (error: ?Error, gas: ?number) => void) => void,
sendRawTransaction: (tx: any, callback: (error: Error, result: any) => void) => void,
sendRawTransaction: (tx: any, callback: (error: Error, result: string) => void) => void,
filter: (type: string) => Filter; // return intance with "watch"
}