Skip to content

Commit

Permalink
refactor: vote key parsing (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
janmazak committed Feb 7, 2023
1 parent 28e5337 commit fb1f553
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions src/command-parser/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import {
isCborHex,
isDerivationType,
isHwSigningData,
isPubKeyCborHex,
isTxFileData,
isVotePublicKeyHex,
isXPubKeyHex,
isXPubKeyCborHex,
} from '../guards'
import { Errors } from '../errors'
import {
Expand All @@ -25,11 +26,10 @@ import {
NativeScriptType,
TxFileData,
VotePublicKeyHex,
VOTE_PUBLIC_KEY_HEX_LENGTH,
} from '../types'
import { KesVKey, OpCertIssueCounter } from '../opCert/opCert'
import { decodeCbor } from '../util'
import { classifyPath, PathTypes } from '../crypto-providers/util'
import { classifyPath, PathTypes, splitXPubKeyCborHex } from '../crypto-providers/util'
import { getHwSigningFileType } from '../fileWriter'

const { bech32 } = require('cardano-crypto.js')
Expand Down Expand Up @@ -206,18 +206,17 @@ export const parseVotePubFileCli = (path: string): VotePublicKeyHex => {
const { type, cborHex } = data

if (type === `${PathLabel.GOVERNANCE_VOTING}VerificationKey_ed25519`) {
if (isCborHex(cborHex)) {
if (isPubKeyCborHex(cborHex)) {
const keyHex = decodeCbor(cborHex).toString('hex')
if (isVotePublicKeyHex(keyHex)) {
// key with expected length
return keyHex
}
if (isXPubKeyHex(keyHex)) {
// extended key in the key file (not generated by hw-cli)
const voteKeyHex = keyHex.slice(0, 2 * VOTE_PUBLIC_KEY_HEX_LENGTH)
if (isVotePublicKeyHex(voteKeyHex)) {
return voteKeyHex
}
}
if (isXPubKeyCborHex(cborHex)) {
// extended key in the key file (not generated by hw-cli)
const keyHex = splitXPubKeyCborHex(cborHex).pubKey.toString('hex')
if (isVotePublicKeyHex(keyHex)) {
return keyHex
}
}
}
Expand All @@ -230,8 +229,8 @@ export const parseVotePubFileHw = (path: string): VotePublicKeyHex => {
const { type, cborXPubKeyHex } = data

if (type === getHwSigningFileType(PathLabel.GOVERNANCE_VOTING, PathTypes.PATH_GOVERNANCE_VOTING_KEY)) {
if (isCborHex(cborXPubKeyHex)) {
const keyHex = decodeCbor(cborXPubKeyHex).slice(0, 32).toString('hex')
if (isXPubKeyCborHex(cborXPubKeyHex)) {
const keyHex = splitXPubKeyCborHex(cborXPubKeyHex).pubKey.toString('hex')
if (isVotePublicKeyHex(keyHex)) {
return keyHex
}
Expand Down

0 comments on commit fb1f553

Please sign in to comment.