Skip to content

Commit 788a20c

Browse files
authored
Merge pull request #2595 from IntersectMBO/fix/2583-wrong-drep-script-cip-129-identifier-generation
fix(#2583): fix encoding and decoding cip-129 drep_script identifiers
2 parents 82ae303 + 67a199f commit 788a20c

File tree

7 files changed

+55
-43
lines changed

7 files changed

+55
-43
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ changes.
1616

1717
### Fixed
1818

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

2121
### Changed
2222

govtool/frontend/src/components/organisms/DRepCard.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export const DRepCard = ({
3434
metadataStatus,
3535
image,
3636
drepId,
37+
isScriptBased,
3738
},
3839
isConnected,
3940
isDelegationLoading,
@@ -59,8 +60,8 @@ export const DRepCard = ({
5960
});
6061

6162
const cip129Identifier = encodeCIP129Identifier({
62-
txID: `22${drepId}`,
63-
bech32Prefix: "drep",
63+
txID: `${isScriptBased ? "23" : "22"}${drepId}`,
64+
bech32Prefix: isScriptBased ? "drep_script" : "drep",
6465
});
6566

6667
return (

govtool/frontend/src/components/organisms/DRepDetailsCard.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export const DRepDetailsCard = ({
4747
view,
4848
drepId,
4949
votingPower,
50+
isScriptBased,
5051
} = dRepData;
5152

5253
const groupedReferences = references?.reduce<Record<string, Reference[]>>(
@@ -120,8 +121,8 @@ export const DRepDetailsCard = ({
120121
>
121122
<CopyableText
122123
value={encodeCIP129Identifier({
123-
txID: `22${drepId}`,
124-
bech32Prefix: "drep",
124+
txID: `${isScriptBased ? "23" : "22"}${drepId}`,
125+
bech32Prefix: isScriptBased ? "drep_script" : "drep",
125126
})}
126127
dataTestId="copy-cip-129-drep-id-button"
127128
/>

govtool/frontend/src/services/requests/getDRepList.ts

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { bech32 } from "bech32";
21
import {
32
type Infinite,
43
type DRepStatus,
@@ -7,11 +6,7 @@ import {
76
DrepDataDTO,
87
} from "@models";
98
import { API } from "../API";
10-
import {
11-
decodeCIP129Identifier,
12-
encodeCIP129Identifier,
13-
mapDtoToDrep,
14-
} from "@/utils";
9+
import { dRepSearchPhraseProcessor, mapDtoToDrep } from "@/utils";
1510

1611
export type GetDRepListArguments = {
1712
filters?: string[];
@@ -30,37 +25,7 @@ export const getDRepList = async ({
3025
searchPhrase: rawSearchPhrase = "",
3126
status = [],
3227
}: GetDRepListArguments): Promise<Infinite<DRepData>> => {
33-
// DBSync contains wrong representation of DRep view for script based DReps,
34-
// but it's still used by BE
35-
const searchPhraseProcessor = async () => {
36-
try {
37-
if (rawSearchPhrase.startsWith("drep_script")) {
38-
const { words } = bech32.decode(rawSearchPhrase);
39-
return bech32.encode("drep", words);
40-
}
41-
if (rawSearchPhrase.startsWith("drep")) {
42-
const decodedIdentifier = decodeCIP129Identifier(rawSearchPhrase);
43-
if (decodedIdentifier) {
44-
const isCIP129Identifier = decodedIdentifier.txID.startsWith("22");
45-
if (isCIP129Identifier) {
46-
return encodeCIP129Identifier({
47-
txID: decodedIdentifier.txID.slice(2),
48-
bech32Prefix: "drep",
49-
});
50-
}
51-
return encodeCIP129Identifier({
52-
txID: decodedIdentifier.txID,
53-
bech32Prefix: "drep",
54-
});
55-
}
56-
}
57-
return rawSearchPhrase;
58-
} catch (e) {
59-
return rawSearchPhrase;
60-
}
61-
};
62-
63-
const searchPhrase = await searchPhraseProcessor();
28+
const searchPhrase = await dRepSearchPhraseProcessor(rawSearchPhrase);
6429

6530
const response = await API.get<Infinite<DrepDataDTO>>("/drep/list", {
6631
params: {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { decodeCIP129Identifier } from "./cip129identifier";
2+
3+
/**
4+
* Processes the search phrase for dRep and returns the dRep ID.
5+
* If the phrase starts with "drep_script" or "drep",
6+
* it decodes the CIP129 identifier and extracts the transaction ID.
7+
* If the DRep ID starts with "22" or "23", it returns the ID without the prefix.
8+
* If any error occurs during processing, it returns the original phrase.
9+
*
10+
* @param phrase - The search phrase to be processed.
11+
* @returns The dRep ID extracted from the search phrase or the original phrase if an error occurs.
12+
*/
13+
export const dRepSearchPhraseProcessor = async (phrase: string) => {
14+
let drepIDPhrase = phrase;
15+
16+
try {
17+
if (
18+
drepIDPhrase.startsWith("drep_script") ||
19+
drepIDPhrase.startsWith("drep")
20+
) {
21+
const { txID } = decodeCIP129Identifier(drepIDPhrase);
22+
23+
drepIDPhrase = txID;
24+
}
25+
if (drepIDPhrase.startsWith("22") || drepIDPhrase.startsWith("23")) {
26+
return drepIDPhrase.slice(2);
27+
}
28+
29+
return drepIDPhrase;
30+
} catch (e) {
31+
return phrase;
32+
}
33+
};

govtool/frontend/src/utils/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export * from "./checkIsMaintenanceOn";
88
export * from "./checkIsWalletConnected";
99
export * from "./cip129identifier";
1010
export * from "./dRep";
11+
export * from "./drepSearchPhraseProcessor";
1112
export * from "./ellipsizeText";
1213
export * from "./filterOutNullParams";
1314
export * from "./filterUpdatableProtocolParams";
@@ -33,5 +34,5 @@ export * from "./removeDuplicatedProposals";
3334
export * from "./removeMarkdown";
3435
export * from "./setProtocolParameterUpdate";
3536
export * from "./testIdFromLabel";
36-
export * from "./wait";
3737
export * from "./uniqBy";
38+
export * from "./wait";

govtool/frontend/src/utils/isValidFormat.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
Address,
3+
DRep,
34
RewardAddress,
45
} from "@emurgo/cardano-serialization-lib-asmjs";
56
import i18n from "@/i18n";
@@ -52,3 +53,13 @@ export async function isReceivingAddress(address?: string) {
5253
return i18n.t("forms.errors.mustBeReceivingAddress");
5354
}
5455
}
56+
57+
export async function isDRepView(view?: string) {
58+
if (!view) {
59+
return true;
60+
}
61+
if (DRep.from_bech32(view)) {
62+
return true;
63+
}
64+
return i18n.t("forms.errors.mustBeDRepView");
65+
}

0 commit comments

Comments
 (0)