Skip to content

Commit

Permalink
feat(utxo-bin): add --format tree option
Browse files Browse the repository at this point in the history
Issue: BTC-0
  • Loading branch information
OttoAllmendinger committed Jul 31, 2023
1 parent b4fccda commit 2ba0eb9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
14 changes: 12 additions & 2 deletions modules/utxo-bin/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ import { parseUnknown } from './parseUnknown';
import { getParserTxProperties } from './ParserTx';
import { ScriptParser } from './ScriptParser';
import { stringToBuffer } from './parseString';
import { generateAddress, getAddressPlaceholderDescription, parseIndexRange } from './generateAddress';
import {
formatAddressTree,
formatAddressWithFormatString,
generateAddress,
getAddressPlaceholderDescription,
parseIndexRange,
} from './generateAddress';

type OutputFormat = 'tree' | 'json';

Expand Down Expand Up @@ -342,7 +348,11 @@ export const cmdGenerateAddress = {
index: parseIndexRange(argv.index),
network: getNetworkForName(argv.network ?? 'bitcoin'),
})) {
console.log(address);
if (argv.format === 'tree') {
console.log(formatAddressTree(address));
} else {
console.log(formatAddressWithFormatString(address, argv.format));
}
}
},
};
21 changes: 11 additions & 10 deletions modules/utxo-bin/src/generateAddress.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import * as assert from 'assert';
import * as utxolib from '@bitgo/utxo-lib';
import { Parser } from './Parser';
import { parseUnknown } from './parseUnknown';
import { formatTree } from './format';

function getDefaultChainCodes(): number[] {
return utxolib.bitgo.chainCodes.filter(
Expand Down Expand Up @@ -74,15 +77,12 @@ function getAddressProperties(
};
}

function formatAddress(
keys: utxolib.bitgo.RootWalletKeys,
chain: utxolib.bitgo.ChainCode,
index: number,
network: utxolib.Network,
format: string
): string {
const props = getAddressProperties(keys, chain, index, network);
export function formatAddressTree(props: AddressProperties): string {
const parser = new Parser();
return formatTree(parseUnknown(parser, 'address', props));
}

export function formatAddressWithFormatString(props: AddressProperties, format: string): string {
// replace all patterns with a % prefix from format string with the corresponding property
// e.g. %p0 -> userPath, %k1 -> backupKey, etc.
return format.replace(/%[a-z0-9]+/gi, (match) => {
Expand Down Expand Up @@ -113,15 +113,16 @@ export function* generateAddress(argv: {
chain?: number[];
format: string;
index: number[];
}): Generator<string> {
}): Generator<AddressProperties> {
const xpubs = [argv.userKey, argv.backupKey, argv.bitgoKey].map((k) => utxolib.bip32.fromBase58(k));
assert(utxolib.bitgo.isTriple(xpubs));
const rootXpubs = new utxolib.bitgo.RootWalletKeys(xpubs);
const chains = argv.chain ?? getDefaultChainCodes();
for (const i of argv.index) {
for (const chain of chains) {
assert(utxolib.bitgo.isChainCode(chain));
yield formatAddress(rootXpubs, chain, i, argv.network ?? utxolib.networks.bitcoin, argv.format);
// yield formatAddress(rootXpubs, chain, i, argv.network ?? utxolib.networks.bitcoin, argv.format);
yield getAddressProperties(rootXpubs, chain, i, argv.network ?? utxolib.networks.bitcoin);
}
}
}

0 comments on commit 2ba0eb9

Please sign in to comment.