Skip to content

Commit

Permalink
Lift part of voting registration logic to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidTranDucVL committed Sep 6, 2021
1 parent 88d4ae4 commit 38c226d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 42 deletions.
29 changes: 9 additions & 20 deletions src/crypto-providers/ledgerCryptoProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ import {
TxRelayTypes,
TxWitnessKeys,
_MultiAsset,
VotingRegistrationAuxiliaryData,
VotingRegistrationMetaDataCborHex,
_UnsignedTxParsed,
TxWitnesses,
} from '../transaction/types'
import {
Address,
BIP32Path,
HexString,
HwSigningData,
Network,
VotePublicKeyHex,
XPubKeyHex,
} from '../types'
import { encodeCbor, partition } from '../util'
import { partition } from '../util'
import { LEDGER_VERSIONS } from './constants'
import { LedgerCryptoProviderFeature, LedgerWitness } from './ledgerTypes'
import { CryptoProvider, _AddressParameters } from './types'
Expand All @@ -58,14 +58,13 @@ import {
isDeviceVersionGTE,
filterSigningFiles,
getAddressParameters,
formatVotingRegistrationMetaData,
splitXPubKeyCborHex,
extractStakePubKeyFromHwSigningData,
validateVotingRegistrationAddressType,
findSigningPathForKey,
encodeVotingRegistrationMetaData,
} from './util'

const { blake2b, bech32 } = require('cardano-crypto.js')
const { bech32 } = require('cardano-crypto.js')

const TransportNodeHid = require('@ledgerhq/hw-transport-node-hid').default

Expand Down Expand Up @@ -607,24 +606,14 @@ export const LedgerCryptoProvider: () => Promise<CryptoProvider> = async () => {
const response = await ledger.signTransaction(dummyTx)
if (!response?.auxiliaryDataSupplement) throw Error(Errors.MissingAuxiliaryDataSupplement)

const stakePubHex = extractStakePubKeyFromHwSigningData(hwStakeSigningFile)
const votingRegistrationMetaData = formatVotingRegistrationMetaData(
Buffer.from(votePublicKeyHex, 'hex'),
Buffer.from(stakePubHex, 'hex'),
return encodeVotingRegistrationMetaData(
hwStakeSigningFile,
votePublicKeyHex,
address,
nonce,
Buffer.from(response.auxiliaryDataSupplement.catalystRegistrationSignatureHex, 'hex'),
response.auxiliaryDataSupplement.auxiliaryDataHashHex as HexString,
response.auxiliaryDataSupplement.catalystRegistrationSignatureHex as HexString,
)

const auxiliaryData: VotingRegistrationAuxiliaryData = [votingRegistrationMetaData, []]
const auxiliaryDataCbor = encodeCbor(auxiliaryData)

const auxiliaryDataHashHex = blake2b(auxiliaryDataCbor, 32).toString('hex')
if (response.auxiliaryDataSupplement.auxiliaryDataHashHex !== auxiliaryDataHashHex) {
throw Error(Errors.MetadataSerializationMismatchError)
}

return encodeCbor(votingRegistrationMetaData).toString('hex')
}

const createWitnesses = (ledgerWitnesses: LedgerWitness[], signingFiles: HwSigningData[]): TxWitnesses => {
Expand Down
29 changes: 9 additions & 20 deletions src/crypto-providers/trezorCryptoProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
_MultiAsset,
VotingRegistrationMetaDataCborHex,
_UnsignedTxParsed,
VotingRegistrationAuxiliaryData,
TxWitnesses,
TxWitnessKeys,
} from '../transaction/types'
Expand All @@ -34,6 +33,7 @@ import {
import {
Address,
BIP32Path,
HexString,
HwSigningData,
Network,
PubKeyHex,
Expand All @@ -52,17 +52,16 @@ import {
isDeviceVersionGTE,
getAddressParameters,
validateVotingRegistrationAddressType,
formatVotingRegistrationMetaData,
extractStakePubKeyFromHwSigningData,
splitXPubKeyCborHex,
encodeVotingRegistrationMetaData,
} from './util'
import { Errors } from '../errors'
import { encodeCbor, partition, removeNullFields } from '../util'
import { partition, removeNullFields } from '../util'
import { TREZOR_VERSIONS } from './constants'
import { KesVKey, OpCertIssueCounter, SignedOpCertCborHex } from '../opCert/opCert'
import { parseBIP32Path } from '../command-parser/parsers'

const { blake2b, bech32 } = require('cardano-crypto.js')
const { bech32 } = require('cardano-crypto.js')

const TrezorCryptoProvider: () => Promise<CryptoProvider> = async () => {
const initTrezorConnect = async (): Promise<void> => {
Expand Down Expand Up @@ -594,24 +593,14 @@ const TrezorCryptoProvider: () => Promise<CryptoProvider> = async () => {
throw Error(Errors.MissingCatalystVotingSignature)
}

const stakePubHex = extractStakePubKeyFromHwSigningData(hwStakeSigningFile)
const votingRegistrationMetaData = formatVotingRegistrationMetaData(
Buffer.from(votePublicKeyHex, 'hex'),
Buffer.from(stakePubHex, 'hex'),
return encodeVotingRegistrationMetaData(
hwStakeSigningFile,
votePublicKeyHex,
address,
nonce,
Buffer.from(response.payload.auxiliaryDataSupplement.catalystSignature, 'hex'),
response.payload.auxiliaryDataSupplement.auxiliaryDataHash as HexString,
response.payload.auxiliaryDataSupplement.catalystSignature as HexString,
)

const auxiliaryData: VotingRegistrationAuxiliaryData = [votingRegistrationMetaData, []]
const auxiliaryDataCbor = encodeCbor(auxiliaryData)

const auxiliaryDataHashHex = blake2b(auxiliaryDataCbor, 32).toString('hex')
if (response.payload.auxiliaryDataSupplement.auxiliaryDataHash !== auxiliaryDataHashHex) {
throw Error(Errors.MetadataSerializationMismatchError)
}

return encodeCbor(votingRegistrationMetaData).toString('hex')
}

const signOperationalCertificate = async (
Expand Down
40 changes: 38 additions & 2 deletions src/crypto-providers/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,27 @@ import { HARDENED_THRESHOLD } from '../constants'
import { Errors } from '../errors'
import { isBIP32Path, isPubKeyHex } from '../guards'
import {
TxCertificateKeys, VotingRegistrationMetaData, _Certificate, _UnsignedTxParsed, _XPubKey,
TxCertificateKeys,
VotingRegistrationAuxiliaryData,
VotingRegistrationMetaData,
_Certificate,
_UnsignedTxParsed,
_XPubKey,
} from '../transaction/types'
import {
Address,
BIP32Path,
HexString,
HwSigningData,
HwSigningType,
Network,
NetworkIds,
ProtocolMagics,
PubKeyHex,
VotePublicKeyHex,
XPubKeyCborHex,
} from '../types'
import { decodeCbor } from '../util'
import { decodeCbor, encodeCbor } from '../util'
import {
DeviceVersion,
_AddressParameters,
Expand All @@ -26,6 +33,7 @@ const {
AddressTypes,
base58,
bech32,
blake2b,
} = require('cardano-crypto.js')

enum PathTypes {
Expand Down Expand Up @@ -522,6 +530,33 @@ const formatVotingRegistrationMetaData = (
])
)

const encodeVotingRegistrationMetaData = (
hwStakeSigningFile: HwSigningData,
votePublicKeyHex: VotePublicKeyHex,
address: Buffer,
nonce: BigInt,
auxiliaryDataHashHex: HexString,
catalystRegistrationSignatureHex: HexString,
) => {
const stakePubHex = extractStakePubKeyFromHwSigningData(hwStakeSigningFile)
const votingRegistrationMetaData = formatVotingRegistrationMetaData(
Buffer.from(votePublicKeyHex, 'hex'),
Buffer.from(stakePubHex, 'hex'),
address,
nonce,
Buffer.from(catalystRegistrationSignatureHex, 'hex'),
)

const auxiliaryData: VotingRegistrationAuxiliaryData = [votingRegistrationMetaData, []]
const auxiliaryDataCbor = encodeCbor(auxiliaryData)

if (blake2b(auxiliaryDataCbor, 32).toString('hex') !== auxiliaryDataHashHex) {
throw Error(Errors.MetadataSerializationMismatchError)
}

return encodeCbor(votingRegistrationMetaData).toString('hex')
}

const areHwSigningDataNonByron = (hwSigningData: HwSigningData[]) => (
hwSigningData
.map((signingFile) => classifyPath(signingFile.path))
Expand Down Expand Up @@ -554,6 +589,7 @@ export {
rewardAddressToPubKeyHash,
isDeviceVersionGTE,
formatVotingRegistrationMetaData,
encodeVotingRegistrationMetaData,
areHwSigningDataNonByron,
validateVotingRegistrationAddressType,
}

0 comments on commit 38c226d

Please sign in to comment.