Skip to content

Commit

Permalink
Change key-gen command to accept multiple inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidTranDucVL committed Nov 25, 2020
1 parent 55b1d7f commit d8d031f
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 37 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"homepage": "https://github.com/vacuumlabs/cardano-hw-cli#readme",
"dependencies": {
"@babel/runtime": "^7.11.2",
"@cardano-foundation/ledgerjs-hw-app-cardano": "https://github.com/vacuumlabs/ledgerjs-cardano-shelley/releases/download/v2.0.2-rc.2/cardano-foundation-ledgerjs-hw-app-cardano-2.0.2-rc.1.tgz",
"@cardano-foundation/ledgerjs-hw-app-cardano": "https://github.com/vacuumlabs/ledgerjs-cardano-shelley/releases/download/bulk-export/cardano-foundation-ledgerjs-hw-app-cardano-2.0.2-rc.1.tgz",
"@ledgerhq/hw-transport-node-hid": "^5.25.0",
"argparse": "^2.0.1",
"bignumber": "^1.1.0",
Expand Down
8 changes: 6 additions & 2 deletions src/command-parser/parserConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,21 @@ export const parserConfig = {
'key-gen': {
'--path': {
required: true,
action: 'append',
type: (path: string) => parsePath(path),
dest: 'paths',
help: 'Derivation path to the key to sign with.',
},
'--hw-signing-file': {
required: true,
dest: 'hwSigningFile',
action: 'append',
dest: 'hwSigningFiles',
help: 'Output filepath of the verification key.',
},
'--verification-key-file': {
required: true,
dest: 'verificationKeyFile',
action: 'append',
dest: 'verificationKeyFiles',
help: 'Output filepath of the hardware wallet signing file.',
},
},
Expand Down
11 changes: 6 additions & 5 deletions src/commandExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from './types'
import { LedgerCryptoProvider } from './crypto-providers/ledgerCryptoProvider'
import { TrezorCryptoProvider } from './crypto-providers/trezorCryptoProvider'
import { validateSigning, validateWitnessing } from './crypto-providers/util'
import { validateSigning, validateWitnessing, validateKeyGenInputs } from './crypto-providers/util'
import { Errors } from './errors'

const promiseTimeout = <T> (promise: Promise<T>, ms: number): Promise<T> => {
Expand Down Expand Up @@ -61,11 +61,12 @@ const CommandExecutor = async () => {
}

const createSigningKeyFile = async (
{ path, hwSigningFile, verificationKeyFile }: ParsedKeyGenArguments,
{ paths, hwSigningFiles, verificationKeyFiles }: ParsedKeyGenArguments,
) => {
const xPubKey = await cryptoProvider.getXPubKey(path)
write(hwSigningFile, HwSigningKeyOutput(xPubKey, path))
write(verificationKeyFile, HwVerificationKeyOutput(xPubKey, path))
validateKeyGenInputs(paths, hwSigningFiles, verificationKeyFiles)
const xPubKeys = await cryptoProvider.getXPubKeys(paths)
xPubKeys.forEach((xPubKey, i) => write(hwSigningFiles[i], HwSigningKeyOutput(xPubKey, paths[i])))
xPubKeys.forEach((xPubKey, i) => write(verificationKeyFiles[i], HwVerificationKeyOutput(xPubKey, paths[i])))
}

const createVerificationKeyFile = (
Expand Down
8 changes: 4 additions & 4 deletions src/crypto-providers/ledgerCryptoProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,16 +350,16 @@ export const LedgerCryptoProvider: () => Promise<CryptoProvider> = async () => {
return _shelleyWitnesses.length === 1 ? _shelleyWitnesses[0] : _byronWitnesses[0]
}

const getXPubKey = async (path: BIP32Path): Promise<XPubKeyHex> => {
const { publicKeyHex, chainCodeHex } = await ledger.getExtendedPublicKey(path)
return publicKeyHex + chainCodeHex
const getXPubKeys = async (paths: BIP32Path[]): Promise<XPubKeyHex[]> => {
const xPubKeys = await ledger.getExtendedPublicKeys(paths)
return xPubKeys.map((xPubKey) => xPubKey.publicKeyHex + xPubKey.chainCodeHex)
}

return {
getVersion,
showAddress,
signTx,
witnessTx,
getXPubKey,
getXPubKeys,
}
}
13 changes: 8 additions & 5 deletions src/crypto-providers/trezorCryptoProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
_StakingKeyRegistrationCert,
_StakingKeyDeregistrationCert,
_PoolRelay,
XPubKeyHex,
} from '../transaction/types'
import { CryptoProvider, _AddressParameters } from './types'
import {
Expand Down Expand Up @@ -86,12 +87,14 @@ const TrezorCryptoProvider: () => Promise<CryptoProvider> = async () => {
if (response.error || !response.success) throw Error(Errors.InvalidAddressParametersProvidedError)
}

const getXPubKey = async (path: BIP32Path): Promise<string> => {
const getXPubKeys = async (paths: BIP32Path[]): Promise<XPubKeyHex[]> => {
const { payload } = await TrezorConnect.cardanoGetPublicKey({
path,
showOnTrezor: false,
bundle: paths.map((path) => ({
path,
showOnTrezor: false,
}))
})
return payload.publicKey
return payload.map(result => result.publicKey)
}

const prepareInput = (input: _Input, path?: BIP32Path): TrezorInput => ({
Expand Down Expand Up @@ -298,7 +301,7 @@ const TrezorCryptoProvider: () => Promise<CryptoProvider> = async () => {
showAddress,
witnessTx,
signTx,
getXPubKey,
getXPubKeys,
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/crypto-providers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export type CryptoProvider = {
network: Network,
changeOutputFiles: HwSigningData[],
) => Promise<_ShelleyWitness | _ByronWitness>
getXPubKey: (path: BIP32Path) => Promise<XPubKeyHex>
getXPubKeys: (paths: BIP32Path[]) => Promise<XPubKeyHex[]>
}

export type _AddressParameters = {
Expand Down
18 changes: 18 additions & 0 deletions src/crypto-providers/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { HARDENED_THRESHOLD } from '../constants'
import { Errors } from '../errors'
import { isBIP32Path } from '../guards'
import { XPubKey } from '../transaction/transaction'
import { TxCertificateKeys, _Certificate, _TxAux } from '../transaction/types'
import {
Expand Down Expand Up @@ -142,6 +143,22 @@ const validateSigning = (
if (!paymentSigningFiles.length) throw Error(Errors.MissingPaymentSigningFileError)
}

const validateKeyGenInputs = (
paths: BIP32Path[],
hwSigningFiles: string[],
verificationKeyFiles: string[],
): void => {
if (
!Array.isArray(paths) ||
!paths.every(isBIP32Path) ||
!Array.isArray(hwSigningFiles) ||
!Array.isArray(verificationKeyFiles) ||
paths.length < 1 ||
paths.length !== hwSigningFiles.length ||
paths.length !== verificationKeyFiles.length
) throw Error(Errors.InvalidKeyGenInputsError)
}

const _packBootStrapAddress = (
file: HwSigningData, network: Network,
): _AddressParameters => {
Expand Down Expand Up @@ -257,6 +274,7 @@ export {
isStakingPath,
validateSigning,
validateWitnessing,
validateKeyGenInputs,
getSigningPath,
filterSigningFiles,
findSigningPath,
Expand Down
1 change: 1 addition & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ enum Errors {
InvalidAddressError = 'Invalid address',
LedgerOperationError = 'Ledger operation error',
InvalidAddressParametersProvidedError = 'Invalid address parameters provided',
InvalidKeyGenInputsError = 'Invalid key gen inputs error',
}

export {
Expand Down
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ export type ParsedShowAddressArguments = {

export type ParsedKeyGenArguments = {
command: CommandType.KEY_GEN,
path: BIP32Path,
hwSigningFile: string,
verificationKeyFile: string,
paths: BIP32Path[],
hwSigningFiles: string[],
verificationKeyFiles: string[],
}

export type ParsedVerificationKeyArguments = {
Expand Down
6 changes: 3 additions & 3 deletions test/unit/commandParser/commandParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ describe('Command parser', () => {
const { parsedArgs } = parse(args)
const expectedResult = {
command: CommandType.KEY_GEN,
path: [2147485500, 2147485463, 2147483648, 2, 1],
hwSigningFile: 'test/unit/commandParser/res/payment.hwsfile',
verificationKeyFile: 'test/unit/commandParser/res/payment.vkey',
paths: [[2147485500, 2147485463, 2147483648, 2, 1]],
hwSigningFiles: ['test/unit/commandParser/res/payment.hwsfile'],
verificationKeyFiles: ['test/unit/commandParser/res/payment.vkey'],
}
assert.deepEqual(parsedArgs, expectedResult)
})
Expand Down
15 changes: 2 additions & 13 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,9 @@
resolved "https://registry.yarnpkg.com/@calebboyd/semaphore/-/semaphore-1.3.1.tgz#4faa403d12f80e5d5c1d6e0d7916d048b6fa79cb"
integrity sha512-17z9me12RgAEcMhIgR7f+BiXKbzwF9p1VraI69OxrUUSWGuSMOyOTEHQNVtMKuVrkEDVD0/Av5uiGZPBMYZljw==

"@cardano-foundation/ledgerjs-hw-app-cardano@https://github.com/vacuumlabs/ledgerjs-cardano-shelley/releases/download/v2.0.2-rc.1/cardano-foundation-ledgerjs-hw-app-cardano-2.0.2-rc.1.tgz":
"@cardano-foundation/ledgerjs-hw-app-cardano@https://github.com/vacuumlabs/ledgerjs-cardano-shelley/releases/download/bulk-export/cardano-foundation-ledgerjs-hw-app-cardano-2.0.2-rc.1.tgz":
version "2.0.2-rc.1"
resolved "https://github.com/vacuumlabs/ledgerjs-cardano-shelley/releases/download/v2.0.2-rc.1/cardano-foundation-ledgerjs-hw-app-cardano-2.0.2-rc.1.tgz#4586ac3712a285bd6d77d26c9191891820d2dcde"
dependencies:
"@ledgerhq/hw-transport" "^5.12.0"
babel-polyfill "^6.26.0"
babel-runtime "^6.26.0"
base-x "^3.0.5"
bech32 "^1.1.4"
node-int64 "^0.4.0"

"@cardano-foundation/ledgerjs-hw-app-cardano@https://github.com/vacuumlabs/ledgerjs-cardano-shelley/releases/download/v2.0.2-rc.2/cardano-foundation-ledgerjs-hw-app-cardano-2.0.2-rc.1.tgz":
version "2.0.2-rc.1"
resolved "https://github.com/vacuumlabs/ledgerjs-cardano-shelley/releases/download/v2.0.2-rc.2/cardano-foundation-ledgerjs-hw-app-cardano-2.0.2-rc.1.tgz#015e0223559a3788868f396a73e2e2490b162487"
resolved "https://github.com/vacuumlabs/ledgerjs-cardano-shelley/releases/download/bulk-export/cardano-foundation-ledgerjs-hw-app-cardano-2.0.2-rc.1.tgz#91d7ca5108190f98685c2c559f6449e3b47797bd"
dependencies:
"@ledgerhq/hw-transport" "^5.12.0"
babel-polyfill "^6.26.0"
Expand Down

0 comments on commit d8d031f

Please sign in to comment.