Skip to content

Commit

Permalink
feat(utxo-bin): bring back parameter --limit
Browse files Browse the repository at this point in the history
This is a convenience parameter that is equivalent to `--index 0-<limit-1>`.

Issue: BTC-0
  • Loading branch information
OttoAllmendinger committed Jul 31, 2023
1 parent e16c18d commit 193d960
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
27 changes: 23 additions & 4 deletions modules/utxo-bin/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
formatAddressWithFormatString,
generateAddress,
getAddressPlaceholderDescription,
getRange,
parseIndexRange,
} from './generateAddress';

Expand Down Expand Up @@ -76,7 +77,8 @@ export type ArgsGenerateAddress = {
bitgoKey: string;
chain?: number[];
format: string;
index: string;
index?: string[];
limit?: number;
};

async function getClient({ cache }: { cache: boolean }): Promise<HttpClient> {
Expand Down Expand Up @@ -333,19 +335,36 @@ export const cmdGenerateAddress = {
.option('userKey', { type: 'string', demandOption: true })
.option('backupKey', { type: 'string', demandOption: true })
.option('bitgoKey', { type: 'string', demandOption: true })
.option('chain', { type: 'number' })
.option('format', {
type: 'string',
default: '%p0\t%a',
description: `Format string. Placeholders: ${getAddressPlaceholderDescription()}`,
})
.option('chain', { type: 'number', description: 'Address chain' })
.array('chain')
.option('index', { type: 'string', default: '0-99' });
.option('index', {
type: 'string',
description: 'Address index. Can be given as a range (e.g. 0-99). Takes precedence over --limit.',
})
.array('index')
.option('limit', {
type: 'number',
description: 'Alias for --index with range starting at 0 to limit-1.',
default: 100,
});
},
handler(argv: yargs.Arguments<ArgsGenerateAddress>): void {
let indexRange: number[];
if (argv.index) {
indexRange = parseIndexRange(argv.index);
} else if (argv.limit) {
indexRange = getRange(0, argv.limit - 1);
} else {
throw new Error(`no index or limit`);
}
for (const address of generateAddress({
...argv,
index: parseIndexRange(argv.index),
index: indexRange,
network: getNetworkForName(argv.network ?? 'bitcoin'),
})) {
if (argv.format === 'tree') {
Expand Down
9 changes: 6 additions & 3 deletions modules/utxo-bin/src/generateAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,15 @@ export function formatAddressWithFormatString(props: AddressProperties, format:
});
}

export function parseIndexRange(indexRange: string): number[] {
const ranges = indexRange.split(',');
export function getRange(start: number, end: number): number[] {
return Array.from({ length: end - start + 1 }, (_, i) => start + i);
}

export function parseIndexRange(ranges: string[]): number[] {
return ranges.flatMap((range) => {
const [start, end] = range.split('-');
if (end) {
return Array.from({ length: Number(end) - Number(start) + 1 }, (_, i) => Number(start) + i);
return getRange(Number(start), Number(end));
}
return [Number(start)];
});
Expand Down
2 changes: 1 addition & 1 deletion modules/utxo-bin/test/generateAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('generateAddresses', function () {
userKey,
backupKey,
bitgoKey,
index: parseIndexRange('0-1'),
index: parseIndexRange(['0-1']),
format: '%a',
chain: [0, 1],
})) {
Expand Down

0 comments on commit 193d960

Please sign in to comment.