Skip to content

Commit

Permalink
update trezor connect
Browse files Browse the repository at this point in the history
  • Loading branch information
blurpesec committed Feb 21, 2019
1 parent 0776cc7 commit 7ee9247
Show file tree
Hide file tree
Showing 3 changed files with 7,136 additions and 1,017 deletions.
117 changes: 54 additions & 63 deletions common/libs/wallet/deterministic/trezor.ts
@@ -1,6 +1,4 @@
import BN from 'bn.js';
import EthTx, { TxObj } from 'ethereumjs-tx';
import { addHexPrefix } from 'ethereumjs-util';
import mapValues from 'lodash/mapValues';

import { translateRaw } from 'translations';
Expand All @@ -15,20 +13,18 @@ export const TREZOR_MINIMUM_FIRMWARE = '1.5.2';
export class TrezorWallet extends HardwareWallet {
public static getChainCode(dpath: string): Promise<ChainCodeResponse> {
return new Promise(resolve => {
TrezorConnect.getXPubKey(
dpath,
res => {
if (res.success) {
resolve({
publicKey: res.publicKey,
chainCode: res.chainCode
});
} else {
throw new Error(res.error);
}
},
TREZOR_MINIMUM_FIRMWARE
);
TrezorConnect.getPublicKey({
path: dpath
}).then((res: any) => {
if (res.success) {
resolve({
publicKey: res.payload.publicKey,
chainCode: res.payload.chainCode
});
} else {
throw new Error(res.error);
}
});
});
}

Expand All @@ -37,43 +33,40 @@ export class TrezorWallet extends HardwareWallet {
const { chainId, ...strTx } = getTransactionFields(tx);
// stripHexPrefixAndLower identical to ethFuncs.getNakedAddress
const cleanedTx = mapValues(mapValues(strTx, stripHexPrefixAndLower), padLeftEven);

TrezorConnect.ethereumSignTx(
// Args
this.getPath(),
cleanedTx.nonce,
cleanedTx.gasPrice,
cleanedTx.gasLimit,
cleanedTx.to,
cleanedTx.value,
cleanedTx.data,
chainId,
// Callback
result => {
if (!result.success) {
return reject(Error(result.error));
}

// check the returned signature_v and recalc signature_v if it needed
// see also https://github.com/trezor/trezor-mcu/pull/399
if (Number(result.v) <= 1) {
// for larger chainId, only signature_v returned. simply recalc signature_v
result.v += 2 * chainId + 35;
}

// TODO: Explain what's going on here? Add tests? Adapted from:
// https://github.com/kvhnuke/etherwallet/blob/v3.10.2.6/app/scripts/uiFuncs.js#L24
const txToSerialize: TxObj = {
...strTx,
v: addHexPrefix(new BN(result.v).toString(16)),
r: addHexPrefix(result.r.toString()),
s: addHexPrefix(result.s)
};
const eTx = new EthTx(txToSerialize);
const serializedTx = eTx.serialize();
resolve(serializedTx);
TrezorConnect.ethereumSignTransaction({
path: this.getPath(),
transaction: {
nonce: cleanedTx.nonce,
gasPrice: cleanedTx.gasPrice,
gasLimit: cleanedTx.gasLimit,
to: cleanedTx.to,
value: cleanedTx.value,
data: cleanedTx.data,
chainId
}
}).then((res: any) => {
if (!res.success) {
return reject(Error(res.error));
}
// check the returned signature_v and recalc signature_v if it needed
// see also https://github.com/trezor/trezor-mcu/pull/399
if (Number(res.payload.v) <= 1) {
// for larger chainId, only signature_v returned. simply recalc signature_v
res.payload.v += 2 * chainId + 35;
}
);

// TODO: Explain what's going on here? Add tests? Adapted from:
// https://github.com/kvhnuke/etherwallet/blob/v3.10.2.6/app/scripts/uiFuncs.js#L24
const txToSerialize: TxObj = {
...strTx,
v: res.payload.v,
r: res.payload.r,
s: res.payload.s
};
const eTx = new EthTx(txToSerialize);
const serializedTx = eTx.serialize();
resolve(serializedTx);
});
});
}

Expand All @@ -83,17 +76,15 @@ export class TrezorWallet extends HardwareWallet {

public displayAddress(): Promise<boolean> {
return new Promise(resolve => {
TrezorConnect.ethereumGetAddress(
`${this.dPath}/${this.index}`,
res => {
if (res.error) {
resolve(false);
} else {
resolve(true);
}
},
TREZOR_MINIMUM_FIRMWARE
);
TrezorConnect.ethereumGetAddress({
path: `${this.dPath}/${this.index}`
}).then((res: any) => {
if (!res.success) {
resolve(false);
} else {
resolve(true);
}
});
});
}

Expand Down
46 changes: 23 additions & 23 deletions common/typescript/trezor-connect.d.ts
Expand Up @@ -20,6 +20,25 @@ declare module 'vendor/trezor-connect' {
publicKey: string;
}

interface signTransactionMessage {
path: Path;
transaction: Transaction;
}

interface Transaction {
nonce: string;
gasPrice: string;
gasLimit: string;
to: string;
value: string;
data: string | null;
chainId: number | null;
}

interface pathObj {
path: Path;
}

interface ErrorResponse {
success: false;
error: string;
Expand All @@ -31,38 +50,19 @@ declare module 'vendor/trezor-connect' {
type Response<T> = ErrorResponse | SuccessResponse<T>;

namespace TrezorConnect {
export function getXPubKey(
path: Path,
cb: (res: Response<PublicKey>) => void,
minFirmware?: string
): void;
export function getPublicKey(pathObj): any;

export function ethereumSignTx(
path: Path,
nonce: string,
gasPrice: string,
gasLimit: string,
to: string,
value: string,
data: string | null,
chainId: number | null,
cb: (signature: Response<TxSignature>) => void,
minFirmware?: string
): void;
export function ethereumSignTransaction(signTransactionMessage): any;

export function signMessage(
path: Path,
message: string,
cb: (res: Response<MessageSignature>) => void,
coin?: string,
minFirmware?: string
): void;
): any;

export function ethereumGetAddress(
path: Path,
cb: (res: Response<{ address: string }>) => void,
minFirmware?: string
): void;
export function ethereumGetAddress(pathObj): any;
}

export default TrezorConnect;
Expand Down

0 comments on commit 7ee9247

Please sign in to comment.