diff --git a/CHANGELOG.md b/CHANGELOG.md index 2186fff03..0c7b20fe3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/govtool/frontend/src/components/organisms/DRepCard.tsx b/govtool/frontend/src/components/organisms/DRepCard.tsx index 75f17b725..8a64f63d5 100644 --- a/govtool/frontend/src/components/organisms/DRepCard.tsx +++ b/govtool/frontend/src/components/organisms/DRepCard.tsx @@ -34,6 +34,7 @@ export const DRepCard = ({ metadataStatus, image, drepId, + isScriptBased, }, isConnected, isDelegationLoading, @@ -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 ( diff --git a/govtool/frontend/src/components/organisms/DRepDetailsCard.tsx b/govtool/frontend/src/components/organisms/DRepDetailsCard.tsx index 196802c30..db673d200 100644 --- a/govtool/frontend/src/components/organisms/DRepDetailsCard.tsx +++ b/govtool/frontend/src/components/organisms/DRepDetailsCard.tsx @@ -47,6 +47,7 @@ export const DRepDetailsCard = ({ view, drepId, votingPower, + isScriptBased, } = dRepData; const groupedReferences = references?.reduce>( @@ -120,8 +121,8 @@ export const DRepDetailsCard = ({ > diff --git a/govtool/frontend/src/services/requests/getDRepList.ts b/govtool/frontend/src/services/requests/getDRepList.ts index 835894481..b61707411 100644 --- a/govtool/frontend/src/services/requests/getDRepList.ts +++ b/govtool/frontend/src/services/requests/getDRepList.ts @@ -1,4 +1,3 @@ -import { bech32 } from "bech32"; import { type Infinite, type DRepStatus, @@ -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[]; @@ -30,37 +25,7 @@ export const getDRepList = async ({ searchPhrase: rawSearchPhrase = "", status = [], }: GetDRepListArguments): Promise> => { - // 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>("/drep/list", { params: { diff --git a/govtool/frontend/src/utils/drepSearchPhraseProcessor.ts b/govtool/frontend/src/utils/drepSearchPhraseProcessor.ts new file mode 100644 index 000000000..49befcb72 --- /dev/null +++ b/govtool/frontend/src/utils/drepSearchPhraseProcessor.ts @@ -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")) { + return drepIDPhrase.slice(2); + } + + return drepIDPhrase; + } catch (e) { + return phrase; + } +}; diff --git a/govtool/frontend/src/utils/index.ts b/govtool/frontend/src/utils/index.ts index af99375d3..c2ee4a045 100644 --- a/govtool/frontend/src/utils/index.ts +++ b/govtool/frontend/src/utils/index.ts @@ -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"; @@ -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"; diff --git a/govtool/frontend/src/utils/isValidFormat.ts b/govtool/frontend/src/utils/isValidFormat.ts index 96b6a6bd1..34ffb841a 100644 --- a/govtool/frontend/src/utils/isValidFormat.ts +++ b/govtool/frontend/src/utils/isValidFormat.ts @@ -1,5 +1,6 @@ import { Address, + DRep, RewardAddress, } from "@emurgo/cardano-serialization-lib-asmjs"; import i18n from "@/i18n"; @@ -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"); +}