-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat: add generateAddresses command
- Loading branch information
Showing
3 changed files
with
80 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} | ||
} |