Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds handling for fetching more than 10 addresses #545

Merged
merged 3 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/__test__/e2e/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { encode } from 'rlp';
import {
fetchActiveWallets,
fetchAddresses,
fetchBtcLegacyAddresses,
fetchBtcSegwitAddresses,
pair,
signBtcLegacyTx,
signBtcSegwitTx,
Expand Down Expand Up @@ -156,6 +158,26 @@ describe('API', () => {
expect(addresses).toHaveLength(10);
});

test('fetchAddresses[1]', async () => {
const addresses = await fetchAddresses({ n: 1 });
expect(addresses).toHaveLength(1);
});

test('fetchAddresses[12]', async () => {
const addresses = await fetchAddresses({ n: 12 });
expect(addresses).toHaveLength(12);
});

test('fetchBtcLegacyAddresses', async () => {
const addresses = await fetchBtcLegacyAddresses();
expect(addresses).toHaveLength(10);
});

test('fetchBtcSegwitAddresses[12]', async () => {
const addresses = await fetchBtcSegwitAddresses({ n: 12 });
expect(addresses).toHaveLength(12);
});

test('fetchLedgerLiveAddresses', async () => {
const addresses = await fetchLedgerLiveAddresses();
expect(addresses).toHaveLength(10);
Expand Down
94 changes: 63 additions & 31 deletions src/api/addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,64 +10,94 @@ import {
} from '../constants';
import { getStartPath, queue } from './utilities';

export const fetchAddresses = async (
overrides?: GetAddressesRequestParams,
): Promise<string[]> => {
return queue((client) =>
client
.getAddresses({
startPath: DEFAULT_ETH_DERIVATION,
n: MAX_ADDR,
...overrides,
})
.then((addrs) => addrs.map((addr) => `${addr}`)),
);
type FetchAddressesParams = {
n?: number;
startPathIndex?: number;
};

export const fetchAddresses = async (overrides?: GetAddressesRequestParams) => {
let allAddresses: string[] = [];
let totalFetched = 0;
const totalToFetch = overrides?.n || MAX_ADDR;

while (totalFetched < totalToFetch) {
const batchSize = Math.min(MAX_ADDR, totalToFetch - totalFetched);
const startPath = getStartPath(DEFAULT_ETH_DERIVATION, totalFetched);
await queue((client) =>
client
.getAddresses({
startPath,
...overrides,
n: batchSize,
})
.then((addresses: string[]) => {
if (addresses.length > 0) {
allAddresses = [...allAddresses, ...addresses];
totalFetched += addresses.length;
netbonus marked this conversation as resolved.
Show resolved Hide resolved
}
}),
);
// Exit loop if we've fetched the requested number of addresses
if (totalFetched >= totalToFetch) break;
netbonus marked this conversation as resolved.
Show resolved Hide resolved
}

return allAddresses;
};

export const fetchBtcLegacyAddresses = async (
n = MAX_ADDR,
startPathIndex?: number,
): Promise<string[]> => {
{ n, startPathIndex }: FetchAddressesParams = {
n: MAX_ADDR,
startPathIndex: 0,
},
) => {
return fetchAddresses({
startPath: getStartPath(BTC_LEGACY_DERIVATION, startPathIndex),
n,
});
};

export const fetchBtCSegwitAddresses = async (
n = MAX_ADDR,
startPathIndex?: number,
): Promise<string[]> => {
export const fetchBtcSegwitAddresses = async (
{ n, startPathIndex }: FetchAddressesParams = {
n: MAX_ADDR,
startPathIndex: 0,
},
) => {
return fetchAddresses({
startPath: getStartPath(BTC_SEGWIT_DERIVATION, startPathIndex),
n,
});
};

export const fetchBtcWrappedSegwitAddresses = async (
n = MAX_ADDR,
startPathIndex?: number,
): Promise<string[]> => {
{ n, startPathIndex }: FetchAddressesParams = {
n: MAX_ADDR,
startPathIndex: 0,
},
) => {
return fetchAddresses({
startPath: getStartPath(BTC_WRAPPED_SEGWIT_DERIVATION, startPathIndex),
n,
});
};

export const fetchSolanaAddresses = async (
n = MAX_ADDR,
startPathIndex?: number,
): Promise<string[]> => {
{ n, startPathIndex }: FetchAddressesParams = {
n: MAX_ADDR,
startPathIndex: 0,
},
) => {
return fetchAddresses({
startPath: getStartPath(SOLANA_DERIVATION, startPathIndex, 2),
n,
});
};

export const fetchLedgerLiveAddresses = async (
n = MAX_ADDR,
startPathIndex?: number,
): Promise<string[]> => {
{ n, startPathIndex }: FetchAddressesParams = {
n: MAX_ADDR,
startPathIndex: 0,
},
) => {
const addresses = [];
for (let i = 0; i < n; i++) {
addresses.push(
Expand All @@ -89,9 +119,11 @@ export const fetchLedgerLiveAddresses = async (
};

export const fetchLedgerLegacyAddresses = async (
n = MAX_ADDR,
startPathIndex?: number,
): Promise<string[]> => {
{ n, startPathIndex }: FetchAddressesParams = {
n: MAX_ADDR,
startPathIndex: 0,
},
) => {
const addresses = [];
for (let i = 0; i < n; i++) {
addresses.push(
Expand Down
Loading