Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1830 from 0xProject/feature/fixTrezorInefficiency
Browse files Browse the repository at this point in the history
subproviders: Fix Trezor subprovider inefficiency
  • Loading branch information
fabioberger authored May 24, 2019
2 parents f1d96d9 + bdb2a01 commit fad5000
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 21 deletions.
13 changes: 13 additions & 0 deletions packages/subproviders/CHANGELOG.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
[
{
"version": "4.1.0",
"changes": [
{
"note": "Improve performance of Trezor subprovider via caching",
"pr": 1830
},
{
"note": "Add Trezor handware wallet subprovider",
"pr": 1431
}
]
},
{
"timestamp": 1557507213,
"version": "4.0.6",
Expand Down
48 changes: 27 additions & 21 deletions packages/subproviders/src/subproviders/trezor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class TrezorSubprovider extends BaseWalletSubprovider {
private readonly _trezorConnectClientApi: any;
private readonly _networkId: number;
private readonly _addressSearchLimit: number;
private _initialDerivedKeyInfo: DerivedHDKeyInfo | undefined;
/**
* Instantiates a TrezorSubprovider. Defaults to private key path set to `44'/60'/0'/0/`.
* Must be initialized with trezor-connect API module https://github.com/trezor/connect.
Expand Down Expand Up @@ -161,28 +162,33 @@ export class TrezorSubprovider extends BaseWalletSubprovider {
throw new Error(WalletSubproviderErrors.MethodNotSupported);
}
private async _initialDerivedKeyInfoAsync(): Promise<DerivedHDKeyInfo> {
const parentKeyDerivationPath = `m/${this._privateKeyPath}`;

const response: TrezorConnectResponse = await this._trezorConnectClientApi.getPublicKey({
path: parentKeyDerivationPath,
});

if (response.success) {
const payload: TrezorGetPublicKeyResponsePayload = response.payload;
const hdKey = new HDNode();
hdKey.publicKey = new Buffer(payload.publicKey, 'hex');
hdKey.chainCode = new Buffer(payload.chainCode, 'hex');
const address = walletUtils.addressOfHDKey(hdKey);
const initialDerivedKeyInfo = {
hdKey,
address,
derivationPath: parentKeyDerivationPath,
baseDerivationPath: this._privateKeyPath,
};
return initialDerivedKeyInfo;
if (this._initialDerivedKeyInfo) {
return this._initialDerivedKeyInfo;
} else {
const payload: TrezorResponseErrorPayload = response.payload;
throw new Error(payload.error);
const parentKeyDerivationPath = `m/${this._privateKeyPath}`;

const response: TrezorConnectResponse = await this._trezorConnectClientApi.getPublicKey({
path: parentKeyDerivationPath,
});

if (response.success) {
const payload: TrezorGetPublicKeyResponsePayload = response.payload;
const hdKey = new HDNode();
hdKey.publicKey = new Buffer(payload.publicKey, 'hex');
hdKey.chainCode = new Buffer(payload.chainCode, 'hex');
const address = walletUtils.addressOfHDKey(hdKey);
const initialDerivedKeyInfo = {
hdKey,
address,
derivationPath: parentKeyDerivationPath,
baseDerivationPath: this._privateKeyPath,
};
this._initialDerivedKeyInfo = initialDerivedKeyInfo;
return initialDerivedKeyInfo;
} else {
const payload: TrezorResponseErrorPayload = response.payload;
throw new Error(payload.error);
}
}
}
private _findDerivedKeyInfoForAddress(initalHDKey: DerivedHDKeyInfo, address: string): DerivedHDKeyInfo {
Expand Down

0 comments on commit fad5000

Please sign in to comment.