From a435da910fc71fa81976eca3f0bd8c1313bd0623 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Thu, 23 May 2019 14:26:15 +0100 Subject: [PATCH 1/3] Cache the derived key info since it does not change and currently we request it from the Trezor device for each request --- .../subproviders/src/subproviders/trezor.ts | 48 +++++++++++-------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/packages/subproviders/src/subproviders/trezor.ts b/packages/subproviders/src/subproviders/trezor.ts index acb8529974..71fa7aaf02 100644 --- a/packages/subproviders/src/subproviders/trezor.ts +++ b/packages/subproviders/src/subproviders/trezor.ts @@ -29,6 +29,7 @@ export class TrezorSubprovider extends BaseWalletSubprovider { private readonly _trezorConnectClientApi: any; private readonly _networkId: number; private readonly _addressSearchLimit: number; + private _initialDerivedKeyInfo: any; /** * 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. @@ -161,28 +162,33 @@ export class TrezorSubprovider extends BaseWalletSubprovider { throw new Error(WalletSubproviderErrors.MethodNotSupported); } private async _initialDerivedKeyInfoAsync(): Promise { - 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 { From f372cd2d1304996f7950c5ab6e1d8028ea43782e Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Thu, 23 May 2019 15:52:53 +0100 Subject: [PATCH 2/3] Add CHANGELOG entry for new Trezor subprovider --- packages/subproviders/CHANGELOG.json | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/packages/subproviders/CHANGELOG.json b/packages/subproviders/CHANGELOG.json index dc3dab92c6..23939d432f 100644 --- a/packages/subproviders/CHANGELOG.json +++ b/packages/subproviders/CHANGELOG.json @@ -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", From bdb2a01385cb3857b344503299bb6da3928aa662 Mon Sep 17 00:00:00 2001 From: Fabio B Date: Fri, 24 May 2019 00:52:33 +0100 Subject: [PATCH 3/3] Update packages/subproviders/src/subproviders/trezor.ts Co-Authored-By: Jacob Evans --- packages/subproviders/src/subproviders/trezor.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/subproviders/src/subproviders/trezor.ts b/packages/subproviders/src/subproviders/trezor.ts index 71fa7aaf02..d61961c059 100644 --- a/packages/subproviders/src/subproviders/trezor.ts +++ b/packages/subproviders/src/subproviders/trezor.ts @@ -29,7 +29,7 @@ export class TrezorSubprovider extends BaseWalletSubprovider { private readonly _trezorConnectClientApi: any; private readonly _networkId: number; private readonly _addressSearchLimit: number; - private _initialDerivedKeyInfo: any; + 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.