Merge pull request #432 from 0xProject/improveLedgerSubprovider

Allow LedgerSubprovider to return any number of accounts
This commit is contained in:
Fabio Berger
2018-03-04 16:57:25 +01:00
committed by GitHub
4 changed files with 19 additions and 6 deletions

View File

@@ -3,6 +3,7 @@
## v0.6.0 - _TBD_
* Move web3 types from being a devDep to a dep since one cannot use this package without it (#429)
* Add `numberOfAccounts` param to `LedgerSubprovider` method `getAccountsAsync` (#432)
## v0.5.0 - _February 16, 2018_

View File

@@ -19,7 +19,7 @@ import {
import { Subprovider } from './subprovider';
const DEFAULT_DERIVATION_PATH = `44'/60'/0'`;
const NUM_ADDRESSES_TO_FETCH = 10;
const DEFAULT_NUM_ADDRESSES_TO_FETCH = 10;
const ASK_FOR_ON_DEVICE_CONFIRMATION = false;
const SHOULD_GET_CHAIN_CODE = true;
@@ -129,7 +129,7 @@ export class LedgerSubprovider extends Subprovider {
return;
}
}
public async getAccountsAsync(): Promise<string[]> {
public async getAccountsAsync(numberOfAccounts: number = DEFAULT_NUM_ADDRESSES_TO_FETCH): Promise<string[]> {
this._ledgerClientIfExists = await this._createLedgerClientAsync();
let ledgerResponse;
@@ -148,7 +148,7 @@ export class LedgerSubprovider extends Subprovider {
hdKey.chainCode = new Buffer(ledgerResponse.chainCode, 'hex');
const accounts = [];
for (let i = 0; i < NUM_ADDRESSES_TO_FETCH; i++) {
for (let i = 0; i < numberOfAccounts; i++) {
const derivedHDNode = hdKey.derive(`m/${i + this._derivationPathIndex}`);
const derivedPublicKey = derivedHDNode.publicKey;
const shouldSanitizePublicKey = true;

View File

@@ -26,11 +26,17 @@ describe('LedgerSubprovider', () => {
});
});
describe('direct method calls', () => {
it('returns a list of accounts', async () => {
it('returns default number of accounts', async () => {
const accounts = await ledgerSubprovider.getAccountsAsync();
expect(accounts[0]).to.not.be.an('undefined');
expect(accounts.length).to.be.equal(10);
});
it('returns requested number of accounts', async () => {
const numberOfAccounts = 20;
const accounts = await ledgerSubprovider.getAccountsAsync(numberOfAccounts);
expect(accounts[0]).to.not.be.an('undefined');
expect(accounts.length).to.be.equal(numberOfAccounts);
});
it('signs a personal message', async () => {
const data = ethUtils.bufferToHex(ethUtils.toBuffer('hello world'));
const ecSignatureHex = await ledgerSubprovider.signPersonalMessageAsync(data);
@@ -172,7 +178,7 @@ describe('LedgerSubprovider', () => {
};
const callback = reportCallbackErrors(done)((err: Error, response: Web3.JSONRPCResponsePayload) => {
expect(err).to.be.a('null');
const result = response.result.result;
const result = response.result;
expect(result.length).to.be.equal(66);
expect(result.substr(0, 2)).to.be.equal('0x');
done();

View File

@@ -62,11 +62,17 @@ describe('LedgerSubprovider', () => {
});
describe('direct method calls', () => {
describe('success cases', () => {
it('returns a list of accounts', async () => {
it('returns default number of accounts', async () => {
const accounts = await ledgerSubprovider.getAccountsAsync();
expect(accounts[0]).to.be.equal(FAKE_ADDRESS);
expect(accounts.length).to.be.equal(10);
});
it('returns requested number of accounts', async () => {
const numberOfAccounts = 20;
const accounts = await ledgerSubprovider.getAccountsAsync(numberOfAccounts);
expect(accounts[0]).to.be.equal(FAKE_ADDRESS);
expect(accounts.length).to.be.equal(numberOfAccounts);
});
it('signs a personal message', async () => {
const data = ethUtils.bufferToHex(ethUtils.toBuffer('hello world'));
const ecSignatureHex = await ledgerSubprovider.signPersonalMessageAsync(data);