From 193d960d50e29eabd9351bb73f2d14bdd3373aa3 Mon Sep 17 00:00:00 2001 From: Otto Allmendinger Date: Mon, 31 Jul 2023 17:52:39 +0200 Subject: [PATCH] feat(utxo-bin): bring back parameter `--limit` This is a convenience parameter that is equivalent to `--index 0-`. Issue: BTC-0 --- modules/utxo-bin/src/commands.ts | 27 ++++++++++++++++++++---- modules/utxo-bin/src/generateAddress.ts | 9 +++++--- modules/utxo-bin/test/generateAddress.ts | 2 +- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/modules/utxo-bin/src/commands.ts b/modules/utxo-bin/src/commands.ts index 59b520893b..cfe770994e 100644 --- a/modules/utxo-bin/src/commands.ts +++ b/modules/utxo-bin/src/commands.ts @@ -29,6 +29,7 @@ import { formatAddressWithFormatString, generateAddress, getAddressPlaceholderDescription, + getRange, parseIndexRange, } from './generateAddress'; @@ -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 { @@ -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): 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') { diff --git a/modules/utxo-bin/src/generateAddress.ts b/modules/utxo-bin/src/generateAddress.ts index bc0b37cfc3..48f73e55bb 100644 --- a/modules/utxo-bin/src/generateAddress.ts +++ b/modules/utxo-bin/src/generateAddress.ts @@ -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)]; }); diff --git a/modules/utxo-bin/test/generateAddress.ts b/modules/utxo-bin/test/generateAddress.ts index 8f753dcd34..ec5a28037c 100644 --- a/modules/utxo-bin/test/generateAddress.ts +++ b/modules/utxo-bin/test/generateAddress.ts @@ -10,7 +10,7 @@ describe('generateAddresses', function () { userKey, backupKey, bitgoKey, - index: parseIndexRange('0-1'), + index: parseIndexRange(['0-1']), format: '%a', chain: [0, 1], })) {