Skip to content

Commit

Permalink
refactor(hardware-ledger)!: async not needed for certificate mapping
Browse files Browse the repository at this point in the history
mapping certificates requires the drep key hash, not the public key.
I updated the context to require teh key hash, not the key, thus removing
the need to await Crypto helpers that transform it.

BREAKING CHANGE: SignTransactionContext.dRepPublicKey was
changed to dRepKeyHashHex of type Crypto.Ed25519KeyHashHex
  • Loading branch information
mirceahasegan committed Jul 16, 2024
1 parent 061e12a commit 19f0404
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 98 deletions.
4 changes: 3 additions & 1 deletion packages/hardware-ledger/src/LedgerKeyAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -552,10 +552,12 @@ export class LedgerKeyAgent extends KeyAgentBase {
{ knownAddresses, txInKeyPathMap }: SignTransactionContext
): Promise<Cardano.Signatures> {
try {
const dRepPublicKey = await this.derivePublicKey(util.DREP_KEY_DERIVATION_PATH);
const dRepKeyHashHex = (await Crypto.Ed25519PublicKey.fromHex(dRepPublicKey).hash()).hex();
const ledgerTxData = await toLedgerTx(body, {
accountIndex: this.accountIndex,
chainId: this.chainId,
dRepPublicKey: await this.derivePublicKey(util.DREP_KEY_DERIVATION_PATH),
dRepKeyHashHex,
knownAddresses,
txInKeyPathMap
});
Expand Down
35 changes: 16 additions & 19 deletions packages/hardware-ledger/src/transformers/certificates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,25 +283,22 @@ const poolRetirementCertificate: Transform<
};
};

const checkDrepPublicKeyAgainstCredential = async (
dRepPublicKey: Crypto.Ed25519PublicKeyHex | undefined,
const checkDrepPublicKeyAgainstCredential = (
dRepKeyHashHex: Crypto.Ed25519KeyHashHex | undefined,
hash: Crypto.Hash28ByteBase16
) => {
if (
!dRepPublicKey ||
(await Crypto.Ed25519PublicKey.fromHex(dRepPublicKey).hash()).hex() !== Crypto.Ed25519KeyHashHex(hash)
) {
if (!dRepKeyHashHex || dRepKeyHashHex !== Crypto.Ed25519KeyHashHex(hash)) {
throw new InvalidArgumentError('certificate', 'dRepPublicKey does not match certificate drep credential.');
}
};

const drepRegistrationCertificate: Transform<
Cardano.RegisterDelegateRepresentativeCertificate | Cardano.UnRegisterDelegateRepresentativeCertificate,
Promise<Ledger.Certificate>,
Ledger.Certificate,
LedgerTxTransformerContext
> = async (certificate, context): Promise<Ledger.Certificate> => {
> = (certificate, context): Ledger.Certificate => {
if (!context) throw new InvalidArgumentError('LedgerTxTransformerContext', 'values was not provided');
await checkDrepPublicKeyAgainstCredential(context?.dRepPublicKey, certificate.dRepCredential.hash);
checkDrepPublicKeyAgainstCredential(context?.dRepKeyHashHex, certificate.dRepCredential.hash);

const params: Ledger.DRepRegistrationParams = {
...mapAnchorToParams(certificate),
Expand All @@ -324,12 +321,12 @@ const drepRegistrationCertificate: Transform<

const updateDRepCertificate: Transform<
Cardano.UpdateDelegateRepresentativeCertificate,
Promise<Ledger.Certificate>,
Ledger.Certificate,
LedgerTxTransformerContext
> = async (certificate, context): Promise<Ledger.Certificate> => {
> = (certificate, context): Ledger.Certificate => {
if (!context) throw new InvalidArgumentError('LedgerTxTransformerContext', 'values was not provided');

await checkDrepPublicKeyAgainstCredential(context?.dRepPublicKey, certificate.dRepCredential.hash);
checkDrepPublicKeyAgainstCredential(context?.dRepKeyHashHex, certificate.dRepCredential.hash);

const params: Ledger.DRepUpdateParams = {
...mapAnchorToParams(certificate),
Expand Down Expand Up @@ -377,7 +374,7 @@ export const voteDelegationCertificate: Transform<
type: Ledger.CertificateType.VOTE_DELEGATION
});

const toCert = async (cert: Cardano.Certificate, context: LedgerTxTransformerContext): Promise<Ledger.Certificate> => {
const toCert = (cert: Cardano.Certificate, context: LedgerTxTransformerContext): Ledger.Certificate => {
switch (cert.__typename) {
case Cardano.CertificateType.StakeRegistration:
return getStakeAddressCertificate(cert, context);
Expand All @@ -398,21 +395,21 @@ const toCert = async (cert: Cardano.Certificate, context: LedgerTxTransformerCon
case Cardano.CertificateType.VoteDelegation:
return voteDelegationCertificate(cert, context);
case Cardano.CertificateType.RegisterDelegateRepresentative:
return await drepRegistrationCertificate(cert, context);
return drepRegistrationCertificate(cert, context);
case Cardano.CertificateType.UnregisterDelegateRepresentative:
return await drepRegistrationCertificate(cert, context);
return drepRegistrationCertificate(cert, context);
case Cardano.CertificateType.UpdateDelegateRepresentative:
return await updateDRepCertificate(cert, context);
return updateDRepCertificate(cert, context);
default:
throw new InvalidArgumentError('cert', `Certificate ${cert.__typename} not supported.`);
}
};

export const mapCerts = async (
export const mapCerts = (
certs: Cardano.Certificate[] | undefined,
context: LedgerTxTransformerContext
): Promise<Ledger.Certificate[] | null> => {
): Ledger.Certificate[] | null => {
if (!certs) return null;

return Promise.all(certs.map((coreCert) => toCert(coreCert, context)));
return certs.map((coreCert) => toCert(coreCert, context));
};
2 changes: 1 addition & 1 deletion packages/hardware-ledger/src/transformers/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { mapWithdrawals } from './withdrawals';

export const LedgerTxTransformer: Transformer<Cardano.TxBody, Ledger.Transaction, LedgerTxTransformerContext> = {
auxiliaryData: ({ auxiliaryDataHash }) => mapAuxiliaryData(auxiliaryDataHash),
certificates: async ({ certificates }, context) => await mapCerts(certificates, context!),
certificates: ({ certificates }, context) => mapCerts(certificates, context!),
collateralInputs: ({ collaterals }, context) => mapCollateralTxIns(collaterals, context!),
collateralOutput: ({ collateralReturn }, context) => mapCollateralTxOut(collateralReturn, context!),
donation: ({ donation }) => donation,
Expand Down
2 changes: 1 addition & 1 deletion packages/hardware-ledger/test/testData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ export const CONTEXT_WITH_KNOWN_ADDRESSES: LedgerTxTransformerContext = {
networkId: Cardano.NetworkId.Testnet,
networkMagic: 999
},
dRepPublicKey: Crypto.Ed25519PublicKeyHex('deeb8f82f2af5836ebbc1b450b6dbf0b03c93afe5696f10d49e8a8304ebfac01'),
dRepKeyHashHex: Crypto.Ed25519KeyHashHex(dRepCredential.hash),
knownAddresses: [
{
accountIndex: 0,
Expand Down
Loading

0 comments on commit 19f0404

Please sign in to comment.