aboutsummaryrefslogtreecommitdiffstats
path: root/packages/subproviders/src
diff options
context:
space:
mode:
Diffstat (limited to 'packages/subproviders/src')
-rw-r--r--packages/subproviders/src/index.ts1
-rw-r--r--packages/subproviders/src/subproviders/ledger.ts18
-rw-r--r--packages/subproviders/src/subproviders/nonce_tracker.ts3
-rw-r--r--packages/subproviders/src/subproviders/subprovider.ts5
-rw-r--r--packages/subproviders/src/utils/subprovider_utils.ts15
-rw-r--r--packages/subproviders/src/utils/wallet_utils.ts4
6 files changed, 35 insertions, 11 deletions
diff --git a/packages/subproviders/src/index.ts b/packages/subproviders/src/index.ts
index ff28b8a8d..6cc650a4d 100644
--- a/packages/subproviders/src/index.ts
+++ b/packages/subproviders/src/index.ts
@@ -4,6 +4,7 @@ export { ECSignature } from '@0xproject/types';
import { LedgerEthereumClient } from './types';
+export { prependSubprovider } from './utils/subprovider_utils';
export { EmptyWalletSubprovider } from './subproviders/empty_wallet_subprovider';
export { FakeGasEstimateSubprovider } from './subproviders/fake_gas_estimate_subprovider';
export { InjectedWeb3Subprovider } from './subproviders/injected_web3';
diff --git a/packages/subproviders/src/subproviders/ledger.ts b/packages/subproviders/src/subproviders/ledger.ts
index 347eda55f..467299db0 100644
--- a/packages/subproviders/src/subproviders/ledger.ts
+++ b/packages/subproviders/src/subproviders/ledger.ts
@@ -113,9 +113,12 @@ export class LedgerSubprovider extends BaseWalletSubprovider {
const tx = new EthereumTx(txParams);
// Set the EIP155 bits
- tx.raw[6] = Buffer.from([this._networkId]); // v
- tx.raw[7] = Buffer.from([]); // r
- tx.raw[8] = Buffer.from([]); // s
+ const vIndex = 6;
+ tx.raw[vIndex] = Buffer.from([this._networkId]); // v
+ const rIndex = 7;
+ tx.raw[rIndex] = Buffer.from([]); // r
+ const sIndex = 8;
+ tx.raw[sIndex] = Buffer.from([]); // s
const txHex = tx.serialize().toString('hex');
try {
@@ -127,7 +130,8 @@ export class LedgerSubprovider extends BaseWalletSubprovider {
tx.v = Buffer.from(result.v, 'hex');
// EIP155: v should be chain_id * 2 + {35, 36}
- const signedChainId = Math.floor((tx.v[0] - 35) / 2);
+ const eip55Constant = 35;
+ const signedChainId = Math.floor((tx.v[0] - eip55Constant) / 2);
if (signedChainId !== this._networkId) {
await this._destroyLedgerClientAsync();
const err = new Error(LedgerSubproviderErrors.TooOldLedgerFirmware);
@@ -169,8 +173,10 @@ export class LedgerSubprovider extends BaseWalletSubprovider {
fullDerivationPath,
ethUtil.stripHexPrefix(data),
);
- const v = result.v - 27;
- let vHex = v.toString(16);
+ const lowestValidV = 27;
+ const v = result.v - lowestValidV;
+ const hexBase = 16;
+ let vHex = v.toString(hexBase);
if (vHex.length < 2) {
vHex = `0${v}`;
}
diff --git a/packages/subproviders/src/subproviders/nonce_tracker.ts b/packages/subproviders/src/subproviders/nonce_tracker.ts
index 907330111..345e5e975 100644
--- a/packages/subproviders/src/subproviders/nonce_tracker.ts
+++ b/packages/subproviders/src/subproviders/nonce_tracker.ts
@@ -93,7 +93,8 @@ export class NonceTrackerSubprovider extends Subprovider {
// Increment the nonce from the previous successfully submitted transaction
let nonce = ethUtil.bufferToInt(transaction.nonce);
nonce++;
- let nextHexNonce = nonce.toString(16);
+ const hexBase = 16;
+ let nextHexNonce = nonce.toString(hexBase);
if (nextHexNonce.length % 2) {
nextHexNonce = `0${nextHexNonce}`;
}
diff --git a/packages/subproviders/src/subproviders/subprovider.ts b/packages/subproviders/src/subproviders/subprovider.ts
index 56d2381a0..cb6dffc4a 100644
--- a/packages/subproviders/src/subproviders/subprovider.ts
+++ b/packages/subproviders/src/subproviders/subprovider.ts
@@ -13,10 +13,11 @@ export abstract class Subprovider {
// Ported from: https://github.com/MetaMask/provider-engine/blob/master/util/random-id.js
private static _getRandomId(): number {
const extraDigits = 3;
+ const baseTen = 10;
// 13 time digits
- const datePart = new Date().getTime() * Math.pow(10, extraDigits);
+ const datePart = new Date().getTime() * Math.pow(baseTen, extraDigits);
// 3 random digits
- const extraPart = Math.floor(Math.random() * Math.pow(10, extraDigits));
+ const extraPart = Math.floor(Math.random() * Math.pow(baseTen, extraDigits));
// 16 digits
return datePart + extraPart;
}
diff --git a/packages/subproviders/src/utils/subprovider_utils.ts b/packages/subproviders/src/utils/subprovider_utils.ts
new file mode 100644
index 000000000..24ebedd06
--- /dev/null
+++ b/packages/subproviders/src/utils/subprovider_utils.ts
@@ -0,0 +1,15 @@
+import ProviderEngine = require('web3-provider-engine');
+
+import { Subprovider } from '../subproviders/subprovider';
+
+/**
+ * Prepends a subprovider to a provider
+ * @param provider Given provider
+ * @param subprovider Subprovider to prepend
+ */
+export function prependSubprovider(provider: ProviderEngine, subprovider: Subprovider): void {
+ subprovider.setEngine(provider);
+ // HACK: We use implementation details of provider engine here
+ // https://github.com/MetaMask/provider-engine/blob/master/index.js#L68
+ (provider as any)._providers = [subprovider, ...(provider as any)._providers];
+}
diff --git a/packages/subproviders/src/utils/wallet_utils.ts b/packages/subproviders/src/utils/wallet_utils.ts
index cd5cd9ba0..c36fdb9fc 100644
--- a/packages/subproviders/src/utils/wallet_utils.ts
+++ b/packages/subproviders/src/utils/wallet_utils.ts
@@ -30,10 +30,10 @@ class DerivedHDKeyInfoIterator implements IterableIterator<DerivedHDKeyInfo> {
baseDerivationPath,
derivationPath: fullDerivationPath,
};
- const done = this._index === this._searchLimit;
+ const isDone = this._index === this._searchLimit;
this._index++;
return {
- done,
+ done: isDone,
value: derivedKey,
};
}