Skip to content

Commit

Permalink
fix(#2583): fix encoding and decoding cip-129 drep_script identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
MSzalowski committed Jan 1, 2025
1 parent 16338f3 commit 0d280b9
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 43 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ changes.

### Fixed

-
- Fix CIP-129 DRep view identifier for script based DReps [Issue 2583](https://github.com/IntersectMBO/govtool/issues/2583)

### Changed

Expand Down
5 changes: 3 additions & 2 deletions govtool/frontend/src/components/organisms/DRepCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const DRepCard = ({
metadataStatus,
image,
drepId,
isScriptBased,
},
isConnected,
isDelegationLoading,
Expand All @@ -59,8 +60,8 @@ export const DRepCard = ({
});

const cip129Identifier = encodeCIP129Identifier({
txID: `22${drepId}`,
bech32Prefix: "drep",
txID: `${isScriptBased ? "23" : "22"}${drepId}`,
bech32Prefix: isScriptBased ? "drep_script" : "drep",
});

return (
Expand Down
5 changes: 3 additions & 2 deletions govtool/frontend/src/components/organisms/DRepDetailsCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const DRepDetailsCard = ({
view,
drepId,
votingPower,
isScriptBased,
} = dRepData;

const groupedReferences = references?.reduce<Record<string, Reference[]>>(
Expand Down Expand Up @@ -120,8 +121,8 @@ export const DRepDetailsCard = ({
>
<CopyableText
value={encodeCIP129Identifier({
txID: `22${drepId}`,
bech32Prefix: "drep",
txID: `${isScriptBased ? "23" : "22"}${drepId}`,
bech32Prefix: isScriptBased ? "drep_script" : "drep",
})}
dataTestId="copy-cip-129-drep-id-button"
/>
Expand Down
39 changes: 2 additions & 37 deletions govtool/frontend/src/services/requests/getDRepList.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { bech32 } from "bech32";
import {
type Infinite,
type DRepStatus,
Expand All @@ -7,11 +6,7 @@ import {
DrepDataDTO,
} from "@models";
import { API } from "../API";
import {
decodeCIP129Identifier,
encodeCIP129Identifier,
mapDtoToDrep,
} from "@/utils";
import { dRepSearchPhraseProcessor, mapDtoToDrep } from "@/utils";

export type GetDRepListArguments = {
filters?: string[];
Expand All @@ -30,37 +25,7 @@ export const getDRepList = async ({
searchPhrase: rawSearchPhrase = "",
status = [],
}: GetDRepListArguments): Promise<Infinite<DRepData>> => {
// DBSync contains wrong representation of DRep view for script based DReps,
// but it's still used by BE
const searchPhraseProcessor = async () => {
try {
if (rawSearchPhrase.startsWith("drep_script")) {
const { words } = bech32.decode(rawSearchPhrase);
return bech32.encode("drep", words);
}
if (rawSearchPhrase.startsWith("drep")) {
const decodedIdentifier = decodeCIP129Identifier(rawSearchPhrase);
if (decodedIdentifier) {
const isCIP129Identifier = decodedIdentifier.txID.startsWith("22");
if (isCIP129Identifier) {
return encodeCIP129Identifier({
txID: decodedIdentifier.txID.slice(2),
bech32Prefix: "drep",
});
}
return encodeCIP129Identifier({
txID: decodedIdentifier.txID,
bech32Prefix: "drep",
});
}
}
return rawSearchPhrase;
} catch (e) {
return rawSearchPhrase;
}
};

const searchPhrase = await searchPhraseProcessor();
const searchPhrase = await dRepSearchPhraseProcessor(rawSearchPhrase);

const response = await API.get<Infinite<DrepDataDTO>>("/drep/list", {
params: {
Expand Down
33 changes: 33 additions & 0 deletions govtool/frontend/src/utils/drepSearchPhraseProcessor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { decodeCIP129Identifier } from "./cip129identifier";

/**
* Processes the search phrase for dRep and returns the dRep ID.
* If the phrase starts with "drep_script" or "drep",
* it decodes the CIP129 identifier and extracts the transaction ID.
* If the transaction ID starts with "22" or "23", it returns the ID without the prefix.
* If any error occurs during processing, it returns the original phrase.
*
* @param phrase - The search phrase to be processed.
* @returns The dRep ID extracted from the search phrase or the original phrase if an error occurs.
*/
export const dRepSearchPhraseProcessor = async (phrase: string) => {
let drepIDPhrase = phrase;

try {
if (
drepIDPhrase.startsWith("drep_script") ||
drepIDPhrase.startsWith("drep")
) {
const { txID } = decodeCIP129Identifier(drepIDPhrase);

drepIDPhrase = txID;
}
if (drepIDPhrase.startsWith("22") || drepIDPhrase.startsWith("23")) {
return drepIDPhrase.slice(2);
}

return drepIDPhrase;
} catch (e) {
return phrase;
}
};
3 changes: 2 additions & 1 deletion govtool/frontend/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export * from "./checkIsMaintenanceOn";
export * from "./checkIsWalletConnected";
export * from "./cip129identifier";
export * from "./dRep";
export * from "./drepSearchPhraseProcessor";
export * from "./ellipsizeText";
export * from "./filterOutNullParams";
export * from "./filterUpdatableProtocolParams";
Expand All @@ -33,5 +34,5 @@ export * from "./removeDuplicatedProposals";
export * from "./removeMarkdown";
export * from "./setProtocolParameterUpdate";
export * from "./testIdFromLabel";
export * from "./wait";
export * from "./uniqBy";
export * from "./wait";
11 changes: 11 additions & 0 deletions govtool/frontend/src/utils/isValidFormat.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
Address,
DRep,
RewardAddress,
} from "@emurgo/cardano-serialization-lib-asmjs";
import i18n from "@/i18n";
Expand Down Expand Up @@ -52,3 +53,13 @@ export async function isReceivingAddress(address?: string) {
return i18n.t("forms.errors.mustBeReceivingAddress");
}
}

export async function isDRepView(view?: string) {
if (!view) {
return true;
}
if (DRep.from_bech32(view)) {
return true;
}
return i18n.t("forms.errors.mustBeDRepView");
}

0 comments on commit 0d280b9

Please sign in to comment.