William O'Beirne cb92f59e57 Electron Ledger + Trezor Support (#1836)
* Initial scaffold of enclave

* Cleanup types

* Add comments

* Do not truncate errors, pretty output

* Introduce helpers for sagas

* Update yarn lock

* Convert enclave into its own lib. Implement client and server.

* Check in progress

* Initial types

* Remove unused lib

* Finish types

* cleanup

* Switch over to using electron protocol, remove code thats no longer necessary

* Refactor Ledger and Trezor wallets to provide all functionality via libs. Run chain code generation thru Enclave.

* Check in trezor work

* Transaction signing

* Message signing

* Display address

* Fix deallocation of trezor

* Adjust API

* Remove unused getAddresses

* Fix imports and filenames to cooperate with internal typings

* Fix type uncertainty

* Add persistent message to Ledger unlock.

* Update ledger help link to kb

* Convert ledger over to updated libs

* Fix jest config

* Enclave README

* Unnecessary assertion

* Adjust tip

* Type ledger errors

* Reduce enclave client code.

* No default exports

* l18n user facing enclave errors

* Reduce repeated enclave code by splitting it into its own wallet lib. Fix some types

* tslint

* Reduce repeated enclave code by splitting it into its own wallet lib. Fix some types and error messages.

* Electron TREZOR Support (#1946)

* Type trezor connect.

* Check in trezor code

* Implement TREZOR wallet

* Convert TREZOR to use enclave class like Ledger.

* Switch to mycrypto fork of trezor lib. Remove unused dependencies.

* remove unnecessary window attachment

* tslint
2018-06-15 18:25:29 -05:00

115 lines
3.3 KiB
TypeScript

import BN from 'bn.js';
import EthTx, { TxObj } from 'ethereumjs-tx';
import { addHexPrefix } from 'ethereumjs-util';
import { stripHexPrefixAndLower, padLeftEven } from 'libs/values';
import TrezorConnect from 'vendor/trezor-connect';
import { HardwareWallet, ChainCodeResponse } from './hardware';
import { getTransactionFields } from 'libs/transaction';
import mapValues from 'lodash/mapValues';
import { translateRaw } from 'translations';
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
);
});
}
public signRawTransaction(tx: EthTx): Promise<Buffer> {
return new Promise((resolve, reject) => {
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));
}
// 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);
}
);
});
}
public signMessage() {
return Promise.reject(new Error('Signing via Trezor not yet supported.'));
}
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
);
});
}
public getWalletType(): string {
return translateRaw('X_TREZOR');
}
// works, but returns a signature that can only be verified with a Trezor device
/*
public signMessage = (message: string): Promise<string> => {
return new Promise((resolve, reject) => {
TrezorConnect.ethereumSignMessage(
this.getPath(),
message,
response => {
if (response.success) {
resolve(addHexPrefix(response.signature))
} else{
console.error(response.error)
reject(response.error)
}
}
)
})
}
*/
}