-
Notifications
You must be signed in to change notification settings - Fork 272
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
refactor(utxo-bin): add bip32 subcommand
- Loading branch information
Showing
17 changed files
with
151 additions
and
89 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,14 +1,15 @@ | ||
#!/usr/bin/env node | ||
import * as yargs from 'yargs'; | ||
|
||
import { cmdParseTx, cmdParseAddress, cmdParseScript, cmdGenerateAddress, cmdParseXpub } from '../src/commands'; | ||
import { cmdParseTx, cmdParseScript, cmdBip32, cmdGenerateAddress, cmdParseAddress } from '../src/commands'; | ||
|
||
yargs | ||
.command(cmdParseTx) | ||
.command(cmdParseAddress) | ||
.command(cmdParseScript) | ||
.command(cmdGenerateAddress) | ||
.command(cmdParseXpub) | ||
.command(cmdBip32) | ||
.strict() | ||
.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export type FormatTreeOrJson = 'tree' | 'json'; | ||
|
||
export const formatTreeOrJson = { | ||
type: 'string', | ||
choices: ['tree', 'json'] as const, | ||
default: 'tree', | ||
coerce(arg: string): 'tree' | 'json' { | ||
if (arg !== 'tree' && arg !== 'json') { | ||
throw new Error(`invalid format ${arg}`); | ||
} | ||
return arg; | ||
}, | ||
} as const; |
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,3 +1,4 @@ | ||
export * from './parseString'; | ||
export * from './walletKeys'; | ||
export * from './parseNetwork'; | ||
export * from './format'; |
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,60 @@ | ||
import * as crypto from 'crypto'; | ||
|
||
import * as yargs from 'yargs'; | ||
import { CommandModule } from 'yargs'; | ||
import * as utxolib from '@bitgo/utxo-lib'; | ||
|
||
import { parseBip32 } from '../bip32'; | ||
import { formatTreeOrJson, FormatTreeOrJson } from '../args'; | ||
|
||
import { formatString } from './formatString'; | ||
|
||
type ArgsBip32Generate = { | ||
format: FormatTreeOrJson; | ||
bip32Key: string; | ||
derive?: string; | ||
all: boolean; | ||
}; | ||
|
||
export const cmdBip32Parse: CommandModule<unknown, ArgsBip32Generate> = { | ||
command: 'parse [xpub|xprv]', | ||
describe: 'show xpub info', | ||
builder(b: yargs.Argv<unknown>): yargs.Argv<ArgsBip32Generate> { | ||
return b | ||
.options({ format: formatTreeOrJson }) | ||
.positional('bip32Key', { type: 'string', demandOption: true }) | ||
.option('all', { type: 'boolean', default: false }) | ||
.option('derive', { type: 'string', description: 'show xpub derived with path' }); | ||
}, | ||
handler(argv): void { | ||
console.log(formatString(parseBip32(argv.bip32Key, { derive: argv.derive }), argv)); | ||
}, | ||
}; | ||
|
||
type GenerateBip32Args = { | ||
seed: string; | ||
}; | ||
|
||
export const cmdBip32Generate: CommandModule<unknown, GenerateBip32Args> = { | ||
command: 'generateFromSeed', | ||
describe: 'generate keypair from seed - do not use for real keys!', | ||
builder(b) { | ||
return b.option('seed', { type: 'string', demandOption: true, default: 'setec astronomy' }); | ||
}, | ||
handler(argv) { | ||
const key = utxolib.bip32.fromSeed(crypto.createHash('sha256').update(argv.seed).digest()); | ||
console.log(key.toBase58()); | ||
console.log(key.neutered().toBase58()); | ||
}, | ||
}; | ||
|
||
export const cmdBip32: CommandModule<unknown, unknown> = { | ||
command: 'bip32 <command>', | ||
describe: 'bip32 commands', | ||
builder(b: yargs.Argv<unknown>): yargs.Argv<unknown> { | ||
return b.strict().command(cmdBip32Parse).command(cmdBip32Generate).demandCommand(); | ||
}, | ||
handler(): void { | ||
// do nothing | ||
}, | ||
}; |
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
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 was deleted.
Oops, something went wrong.
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
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,22 +1,23 @@ | ||
import * as assert from 'assert'; | ||
|
||
import { parseXpub } from '../src/bip32'; | ||
import { parseBip32 } from '../src/bip32'; | ||
|
||
import { formatTreeNoColor, getFixtureString } from './fixtures'; | ||
import { getKey } from './bip32.util'; | ||
|
||
function runTest(xpub: string, args: { derive?: string }) { | ||
describe('parse xpub', function () { | ||
it('parses xpub', async function () { | ||
const formatted = formatTreeNoColor(parseXpub(xpub, args), { showAll: true }); | ||
const filename = [xpub]; | ||
function runTest(bip32Key: string, args: { derive?: string }) { | ||
describe(`parse bip32 ${JSON.stringify(args)}`, function () { | ||
it('has expected output', async function () { | ||
const formatted = formatTreeNoColor(parseBip32(bip32Key, args), { showAll: true }); | ||
const filename = [bip32Key]; | ||
if (args.derive) { | ||
filename.push(args.derive.replace(/\//g, '_')); | ||
} | ||
assert.strictEqual(await getFixtureString(`test/fixtures/xpub/${filename.join('_')}.txt`, formatted), formatted); | ||
assert.strictEqual(await getFixtureString(`test/fixtures/bip32/${filename.join('_')}.txt`, formatted), formatted); | ||
}); | ||
}); | ||
} | ||
|
||
runTest(getKey('parseXpub').neutered().toBase58(), {}); | ||
runTest(getKey('parseXpub').neutered().toBase58(), { derive: 'm/0/0' }); | ||
runTest(getKey('bip32').toBase58(), {}); | ||
runTest(getKey('bip32').toBase58(), { derive: 'm/0/0' }); | ||
runTest(getKey('bip32').neutered().toBase58(), { derive: 'm/0/0' }); |
12 changes: 12 additions & 0 deletions
12
...w1ckdpUZkV5DLHGmw82uiQo1rY59ZiZYhpG2u5HGENgJ5eykfEBC9nuFibB2cjiKXYUwrZ7q8aMRbpWA7N2Pt.txt
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,12 @@ | ||
xprv: xprv9s21ZrQH143K2Fnv8HGStbw1ckdpUZkV5DLHGmw82uiQo1rY59ZiZYhpG2u5HGENgJ5eykfEBC9nuFibB2cjiKXYUwrZ7q8aMRbpWA7N2Pt | ||
├── xpub: xpub661MyMwAqRbcEjsPEJoTFjskAnUJt2ULSSFt5ALjbFFPfpBgcgsy7M2J7Kz66TNWwfYUBSKbhqNp7YJTAeVRfQMkjFjyTw8GzJiNaPQifio | ||
├── lowR: false | ||
├── chainCode: 13cd969b6f5948b45fe7a5e3e22e9d94830c82e9c2b61448af16565131736757 (32 bytes) | ||
├── depth: 0 | ||
├── index: 0 | ||
├── parentFingerprint: 0 | ||
├── identifier: c3b7cdefd973965dd7aef07784908ed11a3e13fb (20 bytes) | ||
├── fingerprint: c3b7cdef (4 bytes) | ||
├── compressed: true | ||
├── publicKey: 03faab4c92cfdb174adb4df27dddbd984dcec499d57b308a6ae343876d8015d0da (33 bytes) | ||
└── privateKey: ebe706fb107727a8af3a75ef3c96672c9ddf4ccd4377b047df7ec43a5020c1ab (32 bytes) |
12 changes: 12 additions & 0 deletions
12
...UZkV5DLHGmw82uiQo1rY59ZiZYhpG2u5HGENgJ5eykfEBC9nuFibB2cjiKXYUwrZ7q8aMRbpWA7N2Pt_m_0_0.txt
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,12 @@ | ||
xprv: xprv9wLeZuTuzBVPggSspWwPzWp9WDL2NTDx5KhyfS3MNGdMpoYME6dZzXVukBSj1sWAJttfUoXck2GrnFHV2sUpmcRiFv26YGSe373QvEaMKpc | ||
├── xpub: xpub6AKzyQzopZ3guAXLvYUQMekt4FAWmuwoSYdaTpSxvcALhbsVmdwpYKpPbST69B5kuSSn5LJFoGSmkcohTCc2Q5kjfa5vcuJaSVEqBY7B2Qk | ||
├── lowR: false | ||
├── chainCode: 4161a1f543ed7c95cc138bd8bfca549a9ba304349e926c6a0bc612a0c5a4e293 (32 bytes) | ||
├── depth: 2 | ||
├── index: 0 | ||
├── parentFingerprint: 1273784303 | ||
├── identifier: 7f8e7c282a46dbeb6b2634faea4d204eaad4eb85 (20 bytes) | ||
├── fingerprint: 7f8e7c28 (4 bytes) | ||
├── compressed: true | ||
├── publicKey: 0224051902c8f2ccd8cf2f27bc5b25dfa12f84573b994007a0943acdd764d873f3 (33 bytes) | ||
└── privateKey: 27340d788194503cf1fd5a1b453e3400537f0c26d40f317211a36d02435584f4 (32 bytes) |
11 changes: 11 additions & 0 deletions
11
...t2ULSSFt5ALjbFFPfpBgcgsy7M2J7Kz66TNWwfYUBSKbhqNp7YJTAeVRfQMkjFjyTw8GzJiNaPQifio_m_0_0.txt
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,11 @@ | ||
xpub: xpub6AKzyQzopZ3guAXLvYUQMekt4FAWmuwoSYdaTpSxvcALhbsVmdwpYKpPbST69B5kuSSn5LJFoGSmkcohTCc2Q5kjfa5vcuJaSVEqBY7B2Qk | ||
├── lowR: false | ||
├── chainCode: 4161a1f543ed7c95cc138bd8bfca549a9ba304349e926c6a0bc612a0c5a4e293 (32 bytes) | ||
├── depth: 2 | ||
├── index: 0 | ||
├── parentFingerprint: 1273784303 | ||
├── identifier: 7f8e7c282a46dbeb6b2634faea4d204eaad4eb85 (20 bytes) | ||
├── fingerprint: 7f8e7c28 (4 bytes) | ||
├── compressed: true | ||
├── publicKey: 0224051902c8f2ccd8cf2f27bc5b25dfa12f84573b994007a0943acdd764d873f3 (33 bytes) | ||
└── privateKey: undefined |
11 changes: 0 additions & 11 deletions
11
...mtDt8q8fz671p5CKGbHe2WuJvJD2kBfdooYurUNqQ9SF6cK1d5TdKzUKEL1di1ZevzZEyUg13vEBzFs5ciRm7.txt
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
...8fz671p5CKGbHe2WuJvJD2kBfdooYurUNqQ9SF6cK1d5TdKzUKEL1di1ZevzZEyUg13vEBzFs5ciRm7_m_0_0.txt
This file was deleted.
Oops, something went wrong.