Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#2583): fix encoding and decoding cip-129 drep_script identifiers #2595

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 DRep 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")) {
Ryun1 marked this conversation as resolved.
Show resolved Hide resolved
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");
}
Loading