Skip to content

Commit

Permalink
Merge pull request #3768 from BitGo/BTC-0.add-generateaddress-cmd
Browse files Browse the repository at this point in the history
feat: add generateAddresses command
  • Loading branch information
OttoAllmendinger authored Jul 28, 2023
2 parents 3889cd1 + e92ebbe commit 9eb52b3
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
11 changes: 9 additions & 2 deletions modules/utxo-bin/bin/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
#!/usr/bin/env node
import * as yargs from 'yargs';
import { cmdParseAddress, cmdParseScript, cmdParseTx } from '../src/commands';
import { cmdGenerateAddress, cmdParseAddress, cmdParseScript, cmdParseTx } from '../src/commands';

yargs.command(cmdParseTx).command(cmdParseAddress).command(cmdParseScript).demandCommand().help().parse();
yargs
.command(cmdParseTx)
.command(cmdParseAddress)
.command(cmdParseScript)
.command(cmdGenerateAddress)
.demandCommand()
.help()
.parse();
36 changes: 36 additions & 0 deletions modules/utxo-bin/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { parseUnknown } from './parseUnknown';
import { getParserTxProperties } from './ParserTx';
import { ScriptParser } from './ScriptParser';
import { stringToBuffer } from './parseString';
import { generateAddress } from './generateAddress';

type OutputFormat = 'tree' | 'json';

Expand Down Expand Up @@ -62,6 +63,16 @@ type ArgsParseScript = {
script: string;
};

export type ArgsGenerateAddress = {
network?: string;
userKey: string;
backupKey: string;
bitgoKey: string;
chain?: number[];
showDerivationPath?: boolean;
limit: number;
};

async function getClient({ cache }: { cache: boolean }): Promise<HttpClient> {
if (cache) {
const mkdir = promisify(fs.mkdir);
Expand Down Expand Up @@ -306,3 +317,28 @@ export const cmdParseScript = {
console.log(formatString(parsed, { ...argv, all: true }));
},
};

export const cmdGenerateAddress = {
command: 'generateAddresses',
describe: 'generate addresses',
builder(b: yargs.Argv<unknown>): yargs.Argv<ArgsGenerateAddress> {
return b
.option('network', { alias: 'n', type: 'string' })
.option('userKey', { type: 'string', demandOption: true })
.option('backupKey', { type: 'string', demandOption: true })
.option('bitgoKey', { type: 'string', demandOption: true })
.option('chain', { type: 'number' })
.option('showDerivationPath', { type: 'boolean', default: true })
.array('chain')
.option('limit', { type: 'number', default: 100 });
},
handler(argv: yargs.Arguments<ArgsGenerateAddress>): void {
console.log('generating addresses..');
for (const address of generateAddress({
...argv,
network: getNetworkForName(argv.network ?? 'bitcoin'),
})) {
console.log(address);
}
},
};
35 changes: 35 additions & 0 deletions modules/utxo-bin/src/generateAddress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import * as assert from 'assert';
import * as utxolib from '@bitgo/utxo-lib';

export function* generateAddress(argv: {
network?: utxolib.Network;
userKey: string;
backupKey: string;
bitgoKey: string;
chain?: number[];
limit: number;
showDerivationPath?: boolean;
}): Generator<string> {
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 ??
utxolib.bitgo.chainCodes.filter(
// these are rare and show an annoying warning in stderr
(c) => utxolib.bitgo.scriptTypeForChain(c) !== 'p2tr' && utxolib.bitgo.scriptTypeForChain(c) !== 'p2trMusig2'
);
for (let i = 0; i < argv.limit; i++) {
for (const chain of chains) {
assert(utxolib.bitgo.isChainCode(chain));
const scripts = utxolib.bitgo.getWalletOutputScripts(rootXpubs, chain, i);
const parts = [];
if (argv.showDerivationPath) {
// FIXME: can be different for other keys
parts.push(rootXpubs.getDerivationPath(rootXpubs.user, chain, i));
}
parts.push(utxolib.address.fromOutputScript(scripts.scriptPubKey, argv.network ?? utxolib.networks.bitcoin));
yield parts.join('\t');
}
}
}

0 comments on commit 9eb52b3

Please sign in to comment.