Skip to content

Commit

Permalink
chore: bump ledgerjs-hw-app-cardano to 7.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmain committed Apr 25, 2024
1 parent 2f129d5 commit 00b41af
Show file tree
Hide file tree
Showing 16 changed files with 268 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ const testVotingProcedure = (procedureType: string, cbor: HexBlob, core: Cardano
// Test data used in the following tests was generated with the cardano-serialization-lib
testVotingProcedure('vote no with null anchor', HexBlob('8200f6'), { anchor: null, vote: Cardano.Vote.no });
testVotingProcedure('vote yes with null anchor', HexBlob('8201f6'), { anchor: null, vote: Cardano.Vote.yes });
testVotingProcedure('vote abstain with null anchor', HexBlob('8202f6'), { anchor: null, vote: Cardano.Vote.abstain });
testVotingProcedure('vote abstain with null anchor', HexBlob('8202f6'), {
anchor: null,
vote: Cardano.Vote.abstain
});
testVotingProcedure(
'vote no with anchor',
HexBlob(
Expand Down
2 changes: 1 addition & 1 deletion packages/e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"access": "public"
},
"dependencies": {
"@cardano-foundation/ledgerjs-hw-app-cardano": "^6.0.0",
"@cardano-foundation/ledgerjs-hw-app-cardano": "^7.1.2",
"@cardano-ogmios/client": "5.6.0",
"@cardano-sdk/cardano-services": "workspace:~",
"@cardano-sdk/cardano-services-client": "workspace:~",
Expand Down
2 changes: 1 addition & 1 deletion packages/hardware-ledger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"typescript": "^4.7.4"
},
"dependencies": {
"@cardano-foundation/ledgerjs-hw-app-cardano": "^6.0.0",
"@cardano-foundation/ledgerjs-hw-app-cardano": "^7.1.2",
"@cardano-sdk/core": "workspace:~",
"@cardano-sdk/crypto": "workspace:~",
"@cardano-sdk/key-management": "workspace:~",
Expand Down
11 changes: 5 additions & 6 deletions packages/hardware-ledger/src/LedgerKeyAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ import TransportNodeHid from '@ledgerhq/hw-transport-node-hid-noevents';
import _LedgerConnection, {
Certificate,
CertificateType,
CredentialParams,
CredentialParamsType,
GetVersionResponse,
PoolKeyType,
PoolOwnerType,
StakeCredentialParams,
StakeCredentialParamsType,
Transaction,
TransactionSigningMode,
TxOutputDestinationType
Expand Down Expand Up @@ -127,21 +127,20 @@ const parseEstablishDeviceConnectionSecondParam = (
};

interface StakeCredentialCertificateParams {
stakeCredential: StakeCredentialParams;
stakeCredential: CredentialParams;
}

const containsOnlyScriptHashCreds = (tx: Transaction): boolean => {
const withdrawalsAllScriptHash = !tx.withdrawals?.some(
(withdrawal) =>
!areNumbersEqualInConstantTime(withdrawal.stakeCredential.type, StakeCredentialParamsType.SCRIPT_HASH)
(withdrawal) => !areNumbersEqualInConstantTime(withdrawal.stakeCredential.type, CredentialParamsType.SCRIPT_HASH)
);

if (tx.certificates) {
for (const cert of tx.certificates) {
if (!stakeCredentialCert(cert)) return false;

const certParams = cert.params as unknown as StakeCredentialCertificateParams;
if (!areNumbersEqualInConstantTime(certParams.stakeCredential.type, StakeCredentialParamsType.SCRIPT_HASH))
if (!areNumbersEqualInConstantTime(certParams.stakeCredential.type, CredentialParamsType.SCRIPT_HASH))
return false;
}
}
Expand Down
22 changes: 11 additions & 11 deletions packages/hardware-ledger/src/transformers/certificates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type StakeKeyCertificateType = Ledger.CertificateType.STAKE_REGISTRATION | Ledge

type StakeKeyCertificate = {
params: {
stakeCredential: Ledger.StakeCredentialParams;
stakeCredential: Ledger.CredentialParams;
};
type: StakeKeyCertificateType;
};
Expand Down Expand Up @@ -52,28 +52,28 @@ const getStakeAddressCertificate = (
const path = util.stakeKeyPathFromGroupedAddress(knownAddress);
const credentialType = rewardAddress
? rewardAddress.getPaymentCredential().type
: Ledger.StakeCredentialParamsType.SCRIPT_HASH;
: Ledger.CredentialParamsType.SCRIPT_HASH;

let credential: Ledger.StakeCredentialParams;
let credential: Ledger.CredentialParams;

switch (credentialType) {
case Cardano.CredentialType.KeyHash: {
credential = path
? {
keyPath: path,
type: Ledger.StakeCredentialParamsType.KEY_PATH
type: Ledger.CredentialParamsType.KEY_PATH
}
: {
keyHashHex: certificate.stakeCredential.hash,
type: Ledger.StakeCredentialParamsType.KEY_HASH
type: Ledger.CredentialParamsType.KEY_HASH
};
break;
}
case Cardano.CredentialType.ScriptHash:
default: {
credential = {
scriptHashHex: certificate.stakeCredential.hash,
type: Ledger.StakeCredentialParamsType.SCRIPT_HASH
type: Ledger.CredentialParamsType.SCRIPT_HASH
};
}
}
Expand All @@ -98,30 +98,30 @@ export const stakeDelegationCertificate: Transform<

const credentialType = rewardAddress
? rewardAddress.getPaymentCredential().type
: Ledger.StakeCredentialParamsType.SCRIPT_HASH;
: Ledger.CredentialParamsType.SCRIPT_HASH;

const path = util.stakeKeyPathFromGroupedAddress(knownAddress);

let credential: Ledger.StakeCredentialParams;
let credential: Ledger.CredentialParams;

switch (credentialType) {
case Cardano.CredentialType.KeyHash: {
credential = path
? {
keyPath: path,
type: Ledger.StakeCredentialParamsType.KEY_PATH
type: Ledger.CredentialParamsType.KEY_PATH
}
: {
keyHashHex: certificate.stakeCredential.hash,
type: Ledger.StakeCredentialParamsType.KEY_HASH
type: Ledger.CredentialParamsType.KEY_HASH
};
break;
}
case Cardano.CredentialType.ScriptHash:
default: {
credential = {
scriptHashHex: certificate.stakeCredential.hash,
type: Ledger.StakeCredentialParamsType.SCRIPT_HASH
type: Ledger.CredentialParamsType.SCRIPT_HASH
};
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/hardware-ledger/src/transformers/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import { mapRequiredSigners } from './requiredSigners';
import { mapTokenMap } from './assets';
import { mapTxIns } from './txIn';
import { mapTxOuts } from './txOut';
import { mapVotingProcedures } from './votingProcedures';
import { mapWithdrawals } from './withdrawals';

export const LedgerTxTransformer: Transformer<Cardano.TxBody, Ledger.Transaction, LedgerTxTransformerContext> = {
auxiliaryData: ({ auxiliaryDataHash }) => mapAuxiliaryData(auxiliaryDataHash),
certificates: ({ certificates }, context) => mapCerts(certificates, context!),
collateralInputs: ({ collaterals }, context) => mapCollateralTxIns(collaterals, context!),
collateralOutput: ({ collateralReturn }, context) => mapCollateralTxOut(collateralReturn, context!),
donation: ({ donation }) => donation,
fee: ({ fee }) => fee,
includeNetworkId: ({ networkId }) => !!networkId,
inputs: ({ inputs }, context) => mapTxIns(inputs, context!),
Expand All @@ -31,8 +33,10 @@ export const LedgerTxTransformer: Transformer<Cardano.TxBody, Ledger.Transaction
requiredSigners: ({ requiredExtraSignatures }, context) => mapRequiredSigners(requiredExtraSignatures, context!),
scriptDataHashHex: ({ scriptIntegrityHash }) => scriptIntegrityHash?.toString(),
totalCollateral: ({ totalCollateral }) => totalCollateral,
treasury: ({ treasuryValue }) => treasuryValue,
ttl: ({ validityInterval }) => validityInterval?.invalidHereafter,
validityIntervalStart: ({ validityInterval }) => validityInterval?.invalidBefore,
votingProcedures: ({ votingProcedures }) => mapVotingProcedures(votingProcedures),
withdrawals: ({ withdrawals }, context) => mapWithdrawals(withdrawals, context!)
};

Expand Down
114 changes: 114 additions & 0 deletions packages/hardware-ledger/src/transformers/votingProcedures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { Cardano } from '@cardano-sdk/core';
import {
CommitteeKeyHashVoter,
CommitteeScriptHashVoter,
DRepKeyHashVoter,
DRepScriptHashVoter,
StakePoolKeyHashVoter,
Vote,
VoteOption,
Voter,
VoterType,
VoterVotes
} from '@cardano-foundation/ledgerjs-hw-app-cardano';

/**
* Maps a Voter to a Voter from the LedgerJS public types.
*
* @param {Cardano.Voter} voter - The voter object defined in Core types.
* @returns {Voter} Corresponding Voter object for use with LedgerJS.
*/
export const mapVoterToLedgerVoter = (voter: Cardano.Voter): Voter => {
switch (voter.__typename) {
case 'ccHotKeyHash':
return {
keyHashHex: voter.credential.hash,
type: VoterType.COMMITTEE_KEY_HASH
} as CommitteeKeyHashVoter;

case 'ccHotScriptHash':
return {
scriptHashHex: voter.credential.hash,
type: VoterType.COMMITTEE_SCRIPT_HASH
} as CommitteeScriptHashVoter;

case 'dRepKeyHash':
return {
keyHashHex: voter.credential.hash,
type: VoterType.DREP_KEY_HASH
} as DRepKeyHashVoter;

case 'dRepScriptHash':
return {
scriptHashHex: voter.credential.hash,
type: VoterType.DREP_SCRIPT_HASH
} as DRepScriptHashVoter;

case 'stakePoolKeyHash':
return {
keyHashHex: voter.credential.hash,
type: VoterType.STAKE_POOL_KEY_HASH
} as StakePoolKeyHashVoter;

default:
throw new Error('Unsupported voter type');
}
};

/**
* Maps Vote to a LedgerJS VoteOption.
*
* @param {Cardano.Vote} vote - The vote integer representing a voting decision.
* @returns {VoteOption} The corresponding LedgerJS VoteOption.
*/
export const mapVoteOption = (vote: Cardano.Vote): VoteOption => {
// Implement this based on how the raw data translates to VoteOption
switch (vote) {
case 0:
return VoteOption.NO;
case 1:
return VoteOption.YES;
case 2:
return VoteOption.ABSTAIN;
default:
throw new Error('Unsupported vote type');
}
};

/**
* Maps voting procedures from Core to LedgerJS.
*
* Converts a list of Core voting procedures into a format compatible with LedgerJS. This includes converting voters,
* votes, and anchoring data.
*
* @param {VotingProcedures | undefined} votingProcedures - Array of voting procedures from Core.
* @returns {VoterVotes[] | null} Array of LedgerJS-compatible voting records or null if input is undefined.
*/
export const mapVotingProcedures = (votingProcedures: Cardano.VotingProcedures | undefined): VoterVotes[] | null => {
if (!votingProcedures) {
return null;
}

return votingProcedures.map(
(procedure): VoterVotes => ({
voter: mapVoterToLedgerVoter(procedure.voter),
votes: procedure.votes.map(
(vote): Vote => ({
govActionId: {
govActionIndex: vote.actionId.actionIndex,
txHashHex: vote.actionId.id
},
votingProcedure: {
...(vote.votingProcedure.anchor && {
anchor: {
hashHex: vote.votingProcedure.anchor.dataHash,
url: vote.votingProcedure.anchor.url
}
}),
vote: mapVoteOption(vote.votingProcedure.vote)
}
})
)
})
);
};
12 changes: 6 additions & 6 deletions packages/hardware-ledger/src/transformers/withdrawals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,23 @@ export const toWithdrawal: Transform<Cardano.Withdrawal, Ledger.Withdrawal, Ledg
amount: withdrawal.quantity,
stakeCredential: {
keyPath,
type: Ledger.StakeCredentialParamsType.KEY_PATH
} as Ledger.KeyPathStakeCredentialParams
type: Ledger.CredentialParamsType.KEY_PATH
} as Ledger.KeyPathCredentialParams
}
: {
amount: withdrawal.quantity,
stakeCredential: {
keyHashHex: rewardAddress.getPaymentCredential().hash.toString(),
type: Ledger.StakeCredentialParamsType.KEY_HASH
} as Ledger.KeyHashStakeCredentialParams
type: Ledger.CredentialParamsType.KEY_HASH
} as Ledger.KeyHashCredentialParams
};
} else {
ledgerWithdrawal = {
amount: withdrawal.quantity,
stakeCredential: {
scriptHashHex: rewardAddress.getPaymentCredential().hash.toString(),
type: Ledger.StakeCredentialParamsType.SCRIPT_HASH
} as Ledger.ScriptStakeCredentialParams
type: Ledger.CredentialParamsType.SCRIPT_HASH
} as Ledger.CredentialParams
};
}

Expand Down
2 changes: 1 addition & 1 deletion packages/hardware-ledger/test/LedgerKeyAgent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ describe('LedgerKeyAgent', () => {
params: {
stakeCredential: {
scriptHashHex: 'cb0ec2692497b458e46812c8a5bfa2931d1a2d965a99893828ec810f',
type: Ledger.StakeCredentialParamsType.SCRIPT_HASH
type: Ledger.CredentialParamsType.SCRIPT_HASH
}
},
type: Ledger.CertificateType.STAKE_DEREGISTRATION
Expand Down
12 changes: 6 additions & 6 deletions packages/hardware-ledger/test/transformers/certificates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ describe('certificates', () => {
params: {
stakeCredential: {
scriptHashHex: 'cb0ec2692497b458e46812c8a5bfa2931d1a2d965a99893828ec810f',
type: Ledger.StakeCredentialParamsType.SCRIPT_HASH
type: Ledger.CredentialParamsType.SCRIPT_HASH
}
},
type: Ledger.CertificateType.STAKE_REGISTRATION
Expand Down Expand Up @@ -143,7 +143,7 @@ describe('certificates', () => {
KeyRole.Stake,
0
],
type: Ledger.StakeCredentialParamsType.KEY_PATH
type: Ledger.CredentialParamsType.KEY_PATH
}
},
type: Ledger.CertificateType.STAKE_REGISTRATION
Expand All @@ -167,7 +167,7 @@ describe('certificates', () => {
params: {
stakeCredential: {
scriptHashHex: 'cb0ec2692497b458e46812c8a5bfa2931d1a2d965a99893828ec810f',
type: Ledger.StakeCredentialParamsType.SCRIPT_HASH
type: Ledger.CredentialParamsType.SCRIPT_HASH
}
},
type: Ledger.CertificateType.STAKE_DEREGISTRATION
Expand Down Expand Up @@ -197,7 +197,7 @@ describe('certificates', () => {
KeyRole.Stake,
0
],
type: Ledger.StakeCredentialParamsType.KEY_PATH
type: Ledger.CredentialParamsType.KEY_PATH
}
},
type: Ledger.CertificateType.STAKE_DEREGISTRATION
Expand Down Expand Up @@ -431,7 +431,7 @@ describe('certificates', () => {
poolKeyHashHex: '153806dbcd134ddee69a8c5204e38ac80448f62342f8c23cfe4b7edf',
stakeCredential: {
scriptHashHex: 'cb0ec2692497b458e46812c8a5bfa2931d1a2d965a99893828ec810f',
type: Ledger.StakeCredentialParamsType.SCRIPT_HASH
type: Ledger.CredentialParamsType.SCRIPT_HASH
}
},
type: Ledger.CertificateType.STAKE_DELEGATION
Expand Down Expand Up @@ -463,7 +463,7 @@ describe('certificates', () => {
2,
0
],
type: Ledger.StakeCredentialParamsType.KEY_PATH
type: Ledger.CredentialParamsType.KEY_PATH
}
},
type: Ledger.CertificateType.STAKE_DELEGATION
Expand Down

0 comments on commit 00b41af

Please sign in to comment.