Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/command-parser/commandParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export enum CommandType {
SIGN_OPERATIONAL_CERTIFICATE = 'node.issue-op-cert',
NODE_KEY_GEN = 'node.key-gen',
CATALYST_VOTING_KEY_REGISTRATION_METADATA = 'catalyst.voting-key-registration-metadata',
PUBKEY_QUERY = 'pubkey.query',
}

const initParser = (parser: ArgumentParser | ArgumentGroup, config: any): void => {
Expand Down
49 changes: 34 additions & 15 deletions src/command-parser/parserConfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable max-len */
/* eslint quote-props: ["error", "consistent"] */
import {
extractHWSigningData,
parseAddressFile,
parseHwSigningFile,
parseNetwork,
Expand All @@ -23,6 +24,17 @@ const derivationTypeArg = {
},
}

const pubkeyArgs = {
'--path': {
required: true,
action: 'append',
type: (path: string) => parseBIP32Path(path),
dest: 'paths',
help: 'Derivation path to the key to sign with.',
},
...derivationTypeArg,
}

const keyGenArgs = {
'--path': {
required: true,
Expand Down Expand Up @@ -100,12 +112,18 @@ const txSigningArgs = {
help: 'Input filepath of the tx. Use --cddl-format when building transactions with cardano-cli.',
},
},
'--hw-signing-file': {
dest: 'hwSigningFileData',
required: true,
action: 'append',
type: (path: string) => parseHwSigningFile(path),
help: 'Input filepath of the hardware wallet signing file.',
'_mutually-exclusive-group-required-signing-files': {
'--hw-signing-file': {
dest: 'hwSigningFileData',
action: 'append',
type: (path: string) => parseHwSigningFile(path),
help: 'Input filepath of the hardware wallet signing file.',
},
'--sign-request': {
type: (str: string) => JSON.parse(str).map((request) => extractHWSigningData(request)),
dest: 'hwSigningFileData',
help: 'JSON string with the hardware wallet signature request',
},
},
'--change-output-key-file': {
dest: 'changeOutputKeyFileData',
Expand All @@ -114,6 +132,12 @@ const txSigningArgs = {
type: (path: string) => parseHwSigningFile(path),
help: 'Input filepath of change output file.',
},
'--out-file': {
required: true,
action: 'append',
dest: 'outFiles',
help: 'Output filepath.',
},
...derivationTypeArg,
}

Expand Down Expand Up @@ -184,6 +208,9 @@ export const parserConfig = {
},
},
},
'pubkey': {
'query': pubkeyArgs,
},

// =============== commands taken from cardano-cli interface ===============
'address': {
Expand Down Expand Up @@ -250,15 +277,7 @@ export const parserConfig = {
},
...derivationTypeArg,
},
'witness': {
...txSigningArgs,
'--out-file': {
required: true,
action: 'append',
dest: 'outFiles',
help: 'Output filepath.',
},
},
'witness': txSigningArgs,
'validate-raw': {
'--tx-body-file': {
required: true,
Expand Down
22 changes: 15 additions & 7 deletions src/command-parser/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,27 @@ export const parseFileTypeMagic = (fileTypeMagic: string, pathType: PathTypes):
}
}

export const parseHwSigningFile = (path: string): HwSigningData => {
const data = JSON.parse(rw.readFileSync(path, 'utf8'))
data.path = parseBIP32Path(data.path)
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { type: fileTypeMagic, description, ...parsedData } = data

const result = { type: parseFileTypeMagic(fileTypeMagic, classifyPath(data.path)), ...parsedData }
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export const extractHWSigningData = ({
type: fileTypeMagic, description, path: bip32pathstr, ...parsedData
}: any): HwSigningData => {
const path = parseBIP32Path(bip32pathstr)
const result = {
type: parseFileTypeMagic(fileTypeMagic, classifyPath(path)),
path,
...parsedData,
}
if (isHwSigningData(result)) {
return result
}
throw Error(Errors.InvalidHwSigningFileError)
}

export const parseHwSigningFile = (path: string): HwSigningData => {
const data = JSON.parse(rw.readFileSync(path, 'utf8'))
return extractHWSigningData(data)
}

export const parseRawTxFile = (path: string): RawTxFileData => {
const json = JSON.parse(rw.readFileSync(path, 'utf8'))
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down
11 changes: 11 additions & 0 deletions src/commandExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from './fileWriter'
import {
ParsedShowAddressArguments,
ParsedPubkeyQueryArguments,
ParsedAddressKeyGenArguments,
ParsedTransactionSignArguments,
ParsedTransactionPolicyIdArguments,
Expand All @@ -27,6 +28,7 @@ import {
import { LedgerCryptoProvider } from './crypto-providers/ledgerCryptoProvider'
import { TrezorCryptoProvider } from './crypto-providers/trezorCryptoProvider'
import {
validBIP32Paths,
validateKeyGenInputs,
classifyPath,
PathTypes,
Expand Down Expand Up @@ -80,6 +82,14 @@ const CommandExecutor = async () => {
return cryptoProvider.showAddress(args)
}

const printPubkeys = async ({ paths, derivationType }: ParsedPubkeyQueryArguments) => {
if (!validBIP32Paths(paths)) { throw Error(Errors.InvalidKeyGenInputsError) }

const xPubKeys = await cryptoProvider.getXPubKeys(paths, derivationType)
// eslint-disable-next-line no-console
console.log(JSON.stringify(paths.map((path, index) => [path, xPubKeys[index]])))
}

const createSigningKeyFile = async (
{
paths, hwSigningFiles, verificationKeyFiles, derivationType,
Expand Down Expand Up @@ -285,6 +295,7 @@ const CommandExecutor = async () => {
return {
printDeviceVersion,
showAddress,
printPubkeys,
createSigningKeyFile,
createVerificationKeyFile,
createSignedTx,
Expand Down
6 changes: 3 additions & 3 deletions src/crypto-providers/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,18 +274,17 @@ const determineSigningMode = (
? SigningMode.MULTISIG_TRANSACTION
: SigningMode.ORDINARY_TRANSACTION
}
const validBIP32Paths = (paths: BIP32Path[]): boolean => Array.isArray(paths) && paths.every(isBIP32Path) && paths.length > 0

const validateKeyGenInputs = (
paths: BIP32Path[],
hwSigningFiles: string[],
verificationKeyFiles: string[],
): void => {
if (
!Array.isArray(paths)
|| !paths.every(isBIP32Path)
!validBIP32Paths
|| !Array.isArray(hwSigningFiles)
|| !Array.isArray(verificationKeyFiles)
|| paths.length < 1
|| paths.length !== hwSigningFiles.length
|| paths.length !== verificationKeyFiles.length
) throw Error(Errors.InvalidKeyGenInputsError)
Expand Down Expand Up @@ -558,6 +557,7 @@ export {
classifyPath,
pathEquals,
splitXPubKeyCborHex,
validBIP32Paths,
validateKeyGenInputs,
filterSigningFiles,
findSigningPathForKeyHash,
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ const executeCommand = async (): Promise<ExitCode> => {
case (CommandType.SHOW_ADDRESS):
await commandExecutor.showAddress(parsedArgs)
break
case (CommandType.PUBKEY_QUERY):
await commandExecutor.printPubkeys(parsedArgs)
break
case (CommandType.ADDRESS_KEY_GEN):
await commandExecutor.createSigningKeyFile(parsedArgs)
break
Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ export type ParsedShowAddressArguments = {
derivationType?: DerivationType,
}

export type ParsedPubkeyQueryArguments = {
command: CommandType.PUBKEY_QUERY,
paths: BIP32Path[],
derivationType?: DerivationType,
}

export type ParsedAddressKeyGenArguments = {
command: CommandType.ADDRESS_KEY_GEN,
paths: BIP32Path[],
Expand Down Expand Up @@ -244,6 +250,7 @@ export type ParsedArguments =
| ParsedAppVersionArguments
| ParsedDeviceVersionArguments
| ParsedShowAddressArguments
| ParsedPubkeyQueryArguments
| ParsedAddressKeyGenArguments
| ParsedVerificationKeyArguments
| ParsedTransactionSignArguments
Expand Down