Merge pull request #1830 from 0xProject/feature/fixTrezorInefficiency

subproviders: Fix Trezor subprovider inefficiency
This commit is contained in:
Fabio B
2019-05-24 01:24:40 +01:00
committed by GitHub
2 changed files with 40 additions and 21 deletions

View File

@@ -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",

View File

@@ -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.
@@ -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 {