Skip to content

Commit

Permalink
Merge pull request #2348 from IntersectMBO/fix/2333--sanchonet-govtoo…
Browse files Browse the repository at this point in the history
…l-cert-wrong-order-vote-deleg-before-drep-reg

fix(#2333): fix wrong order of vote and registration certificates
  • Loading branch information
MSzalowski authored Nov 7, 2024
2 parents 09b1eca + d83374a commit f36cc43
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 32 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ changes.

### Fixed

-
- Fix certificates order where vote delegation cert is before the DRep registration cert [Issue 2333](https://github.com/IntersectMBO/govtool/issues/2333)

### Changed

Expand Down
66 changes: 38 additions & 28 deletions govtool/frontend/src/context/wallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ interface CardanoContextType {
setStakeKey: (key: string) => void;
stakeKeys: string[];
walletApi?: CardanoApiWallet;
registeredStakeKeysListState: string[];
buildSignSubmitConwayCertTx: ({
certBuilder,
govActionBuilder,
Expand All @@ -192,8 +193,9 @@ interface CardanoContextType {
votingBuilder,
voter,
}: BuildSignSubmitConwayCertTxArgs) => Promise<string>;
buildStakeKeyRegCert: () => Promise<Certificate>;
buildDRepRegCert: (url?: string, hash?: string) => Promise<Certificate>;
buildVoteDelegationCert: (vote: string) => Promise<CertificatesBuilder>;
buildVoteDelegationCert: (vote: string) => Promise<Certificate>;
buildDRepUpdateCert: (url?: string, hash?: string) => Promise<Certificate>;
buildDRepRetirementCert: (voterDeposit: string) => Promise<Certificate>;
buildVote: (
Expand Down Expand Up @@ -626,28 +628,34 @@ const CardanoProvider = (props: Props) => {
[isPendingTransaction, stakeKey, updateTransaction, walletApi, walletState],
);

const buildStakeKeyRegCert = useCallback(async (): Promise<Certificate> => {
try {
if (!stakeKey) {
throw new Error(t("errors.noStakeKeySelected"));
}
const stakeKeyHash = Ed25519KeyHash.from_hex(stakeKey.substring(2));
const stakeCred = Credential.from_keyhash(stakeKeyHash);
const stakeKeyRegCert = StakeRegistration.new_with_explicit_deposit(
stakeCred,
BigNum.from_str(`${epochParams.key_deposit}`),
);
return Certificate.new_stake_registration(stakeKeyRegCert);
} catch (e) {
console.error(e);
throw e;
}
}, [epochParams]);

const buildVoteDelegationCert = useCallback(
async (target: string): Promise<CertificatesBuilder> => {
async (target: string): Promise<Certificate> => {
try {
// Build Vote Delegation Certificate
const certBuilder = CertificatesBuilder.new();
let stakeCred;
if (!stakeKey) {
throw new Error(t("errors.noStakeKeySelected"));
}
// Remove network tag from stake key hash
const stakeKeyHash = Ed25519KeyHash.from_hex(stakeKey.substring(2));
// if chosen stake key is registered use it, else register it
if (registeredStakeKeysListState.length > 0) {
stakeCred = Credential.from_keyhash(stakeKeyHash);
} else {
stakeCred = Credential.from_keyhash(stakeKeyHash);
const stakeKeyRegCert = StakeRegistration.new_with_explicit_deposit(
stakeCred,
BigNum.from_str(`${epochParams.key_deposit}`),
);
certBuilder.add(Certificate.new_stake_registration(stakeKeyRegCert));
}
const stakeCred = Credential.from_keyhash(stakeKeyHash);

// Create correct DRep
let targetDRep;
Expand All @@ -665,9 +673,7 @@ const CardanoProvider = (props: Props) => {
// Create cert object
const voteDelegationCert = VoteDelegation.new(stakeCred, targetDRep);
// add cert to tbuilder
certBuilder.add(Certificate.new_vote_delegation(voteDelegationCert));

return certBuilder;
return Certificate.new_vote_delegation(voteDelegationCert);
} catch (e) {
console.error(e);
throw e;
Expand Down Expand Up @@ -1039,24 +1045,26 @@ const CardanoProvider = (props: Props) => {
buildDRepRegCert,
buildDRepRetirementCert,
buildDRepUpdateCert,
buildHardForkGovernanceAction,
buildNewInfoGovernanceAction,
buildProtocolParameterChangeGovernanceAction,
buildSignSubmitConwayCertTx,
buildStakeKeyRegCert,
buildTreasuryGovernanceAction,
buildProtocolParameterChangeGovernanceAction,
buildHardForkGovernanceAction,
buildVote,
buildVoteDelegationCert,
disconnectWallet,
getChangeAddress,
dRepID,
disconnectWallet,
enable,
error,
isEnabled,
getChangeAddress,
isEnableLoading,
isEnabled,
isMainnet,
isPendingTransaction,
pendingTransaction,
pubDRepKey,
registeredStakeKeysListState,
setStakeKey,
stakeKey,
stakeKeys,
Expand All @@ -1067,24 +1075,26 @@ const CardanoProvider = (props: Props) => {
buildDRepRegCert,
buildDRepRetirementCert,
buildDRepUpdateCert,
buildHardForkGovernanceAction,
buildNewInfoGovernanceAction,
buildProtocolParameterChangeGovernanceAction,
buildSignSubmitConwayCertTx,
buildStakeKeyRegCert,
buildTreasuryGovernanceAction,
buildProtocolParameterChangeGovernanceAction,
buildHardForkGovernanceAction,
buildVote,
buildVoteDelegationCert,
disconnectWallet,
getChangeAddress,
dRepID,
disconnectWallet,
enable,
error,
isEnabled,
getChangeAddress,
isEnableLoading,
isEnabled,
isMainnet,
isPendingTransaction,
pendingTransaction,
pubDRepKey,
registeredStakeKeysListState,
setStakeKey,
stakeKey,
stakeKeys,
Expand Down
13 changes: 12 additions & 1 deletion govtool/frontend/src/hooks/forms/useRegisterAsdRepForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useFormContext } from "react-hook-form";
import { blake2bHex } from "blakejs";
import * as Sentry from "@sentry/react";
import { NodeObject } from "jsonld";
import { CertificatesBuilder } from "@emurgo/cardano-serialization-lib-asmjs";

import {
CIP_119,
Expand Down Expand Up @@ -53,6 +54,8 @@ export const useRegisterAsdRepForm = (
buildSignSubmitConwayCertTx,
buildVoteDelegationCert,
dRepID,
registeredStakeKeysListState,
buildStakeKeyRegCert,
} = useCardano();

// App Management
Expand Down Expand Up @@ -131,14 +134,22 @@ export const useRegisterAsdRepForm = (
if (!hash) return;
const uri = data.storingURL;
try {
const certBuilder = await buildVoteDelegationCert(dRepID);
const certBuilder = CertificatesBuilder.new();

const registerCert = voter?.isRegisteredAsSoleVoter
? await buildDRepUpdateCert(uri, hash)
: await buildDRepRegCert(uri, hash);

certBuilder.add(registerCert);

if (!registeredStakeKeysListState.length) {
const stakeKeyRegCert = await buildStakeKeyRegCert();
certBuilder.add(stakeKeyRegCert);
}

const voteDelegationCert = await buildVoteDelegationCert(dRepID);
certBuilder.add(voteDelegationCert);

return certBuilder;
} catch (error) {
Sentry.setTag("hook", "useRegisterAsdRepForm");
Expand Down
14 changes: 13 additions & 1 deletion govtool/frontend/src/hooks/useDelegateToDrep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ import * as Sentry from "@sentry/react";

import { useCardano, useSnackbar } from "@context";
import { useGetVoterInfo, useTranslation, useWalletErrorModal } from "@hooks";
import { CertificatesBuilder } from "@emurgo/cardano-serialization-lib-asmjs";

export const useDelegateTodRep = () => {
const {
buildSignSubmitConwayCertTx,
buildVoteDelegationCert,
buildDRepRetirementCert,
buildStakeKeyRegCert,
registeredStakeKeysListState,
} = useCardano();
const { t } = useTranslation();
const { addSuccessAlert, addErrorAlert } = useSnackbar();
Expand All @@ -25,14 +28,23 @@ export const useDelegateTodRep = () => {
if (voter?.isRegisteredAsSoleVoter && !voter?.deposit) {
throw new Error(t("errors.appCannotGetDeposit"));
}
const certBuilder = CertificatesBuilder.new();

const certBuilder = await buildVoteDelegationCert(dRepId);
if (voter?.isRegisteredAsSoleVoter) {
const retirementCert = await buildDRepRetirementCert(
voter?.deposit?.toString(),
);
certBuilder.add(retirementCert);
}

if (!registeredStakeKeysListState.length) {
const stakeKeyRegCert = await buildStakeKeyRegCert();
certBuilder.add(stakeKeyRegCert);
}

const voteDelegationCert = await buildVoteDelegationCert(dRepId);
certBuilder.add(voteDelegationCert);

await buildSignSubmitConwayCertTx({
certBuilder,
type: "delegate",
Expand Down
15 changes: 14 additions & 1 deletion govtool/frontend/src/pages/RegisterAsDirectVoter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
openInNewTab,
} from "@utils";
import { WrongRouteInfo } from "@organisms";
import { CertificatesBuilder } from "@emurgo/cardano-serialization-lib-asmjs";

export const RegisterAsDirectVoter = () => {
const { cExplorerBaseUrl } = useAppContext();
Expand All @@ -32,23 +33,35 @@ export const RegisterAsDirectVoter = () => {
const { voter } = useGetVoterInfo();
const openWalletErrorModal = useWalletErrorModal();
const {
buildStakeKeyRegCert,
buildSignSubmitConwayCertTx,
buildDRepRegCert,
buildDRepUpdateCert,
buildVoteDelegationCert,
dRepID,
registeredStakeKeysListState,
} = useCardano();
const { openModal, closeModal } = useModal();

const onRegister = useCallback(async () => {
setIsLoading(true);

try {
const certBuilder = await buildVoteDelegationCert(dRepID);
const certBuilder = CertificatesBuilder.new();

const registerCert = voter?.isRegisteredAsDRep
? await buildDRepUpdateCert()
: await buildDRepRegCert();
certBuilder.add(registerCert);

if (!registeredStakeKeysListState.length) {
const stakeKeyRegCert = await buildStakeKeyRegCert();
certBuilder.add(stakeKeyRegCert);
}

const voteDelegationCert = await buildVoteDelegationCert(dRepID);
certBuilder.add(voteDelegationCert);

const result = await buildSignSubmitConwayCertTx({
certBuilder,
type: "registerAsDirectVoter",
Expand Down

0 comments on commit f36cc43

Please sign in to comment.