From a27b24a846bc93213a670d1e049043c79a9fedc5 Mon Sep 17 00:00:00 2001 From: QSchlegel Date: Thu, 9 Oct 2025 10:29:55 +0200 Subject: [PATCH] fix(wallet): improve DRep ID retrieval and error handling - Enhanced the DRep ID retrieval logic across multiple components to prioritize multisig wallet DRep ID based on role, with a fallback to appWallet DRep ID. - Added error handling to throw an error when DRep ID is not found, ensuring better user feedback and robustness in wallet operations. --- src/components/common/overall-layout/layout.tsx | 3 --- src/components/pages/wallet/governance/card-info.tsx | 5 ++++- .../pages/wallet/governance/drep/registerDrep.tsx | 7 +++++-- .../pages/wallet/governance/drep/retire.tsx | 7 +++++-- .../pages/wallet/governance/drep/updateDrep.tsx | 7 +++++-- .../pages/wallet/governance/proposal/voteButtton.tsx | 12 +++++++++++- src/components/pages/wallet/info/card-info.tsx | 5 ++++- 7 files changed, 34 insertions(+), 12 deletions(-) diff --git a/src/components/common/overall-layout/layout.tsx b/src/components/common/overall-layout/layout.tsx index 379bd682..a23aa539 100644 --- a/src/components/common/overall-layout/layout.tsx +++ b/src/components/common/overall-layout/layout.tsx @@ -135,13 +135,10 @@ export default function RootLayout({ let drepKeyHash = ""; try { const dRepKey = await wallet.getDRep(); - console.log("DRep key:", dRepKey); if (dRepKey && dRepKey.publicKeyHash) { drepKeyHash = dRepKey.publicKeyHash; } } catch (error) { - // DRep key is optional, so we continue without it - console.log("No DRep key available for this wallet"); } // 4) Create or update user (upsert pattern handles both cases) diff --git a/src/components/pages/wallet/governance/card-info.tsx b/src/components/pages/wallet/governance/card-info.tsx index 8c65674e..eb71e6a6 100644 --- a/src/components/pages/wallet/governance/card-info.tsx +++ b/src/components/pages/wallet/governance/card-info.tsx @@ -19,7 +19,10 @@ export default function CardInfo({ appWallet }: { appWallet: Wallet }) { const { toast } = useToast(); // Get DRep ID from multisig wallet if available, otherwise fallback to appWallet - const dRepId = multisigWallet?.getDRepId() || appWallet.dRepId; + const dRepId = multisigWallet?.getKeysByRole(3) ? multisigWallet?.getDRepId() : appWallet?.dRepId; + if (!dRepId) { + throw new Error("DRep not found"); + } // Check if DRep is actually registered (has info from Blockfrost) const isDRepRegistered = drepInfo?.active === true; diff --git a/src/components/pages/wallet/governance/drep/registerDrep.tsx b/src/components/pages/wallet/governance/drep/registerDrep.tsx index 8935e237..1784161f 100644 --- a/src/components/pages/wallet/governance/drep/registerDrep.tsx +++ b/src/components/pages/wallet/governance/drep/registerDrep.tsx @@ -84,7 +84,10 @@ export default function RegisterDRep() { setLoading(true); const txBuilder = getTxBuilder(network); - const drepIds = getDRepIds(multisigWallet.getDRepId()!); + const dRepId = multisigWallet?.getKeysByRole(3) ? multisigWallet?.getDRepId() : appWallet?.dRepId; + if (!dRepId) { + throw new Error("DRep not found"); + } try { const { anchorUrl, anchorHash } = await createAnchor(); @@ -107,7 +110,7 @@ export default function RegisterDRep() { } txBuilder - .drepRegistrationCertificate(drepIds.cip105, { + .drepRegistrationCertificate(dRepId, { anchorUrl: anchorUrl, anchorDataHash: anchorHash, }) diff --git a/src/components/pages/wallet/governance/drep/retire.tsx b/src/components/pages/wallet/governance/drep/retire.tsx index e2f0c9ad..f76a7cee 100644 --- a/src/components/pages/wallet/governance/drep/retire.tsx +++ b/src/components/pages/wallet/governance/drep/retire.tsx @@ -36,7 +36,10 @@ export default function Retire({ appWallet }: { appWallet: Wallet }) { if (selectedUtxos.length === 0) throw new Error("No relevant UTxOs found"); const txBuilder = getTxBuilder(network); - + const dRepId = multisigWallet?.getKeysByRole(3) ? multisigWallet?.getDRepId() : appWallet?.dRepId; + if (!dRepId) { + throw new Error("DRep not found"); + } for (const utxo of selectedUtxos) { txBuilder.txIn( utxo.input.txHash, @@ -49,7 +52,7 @@ export default function Retire({ appWallet }: { appWallet: Wallet }) { txBuilder .txInScript(multisigWallet.getScript().scriptCbor!) .changeAddress(multisigWallet.getScript().address) - .drepDeregistrationCertificate(multisigWallet.getDRepId()!, "500000000") + .drepDeregistrationCertificate(dRepId, "500000000") .certificateScript(multisigWallet.getDRepScript()!); await newTransaction({ diff --git a/src/components/pages/wallet/governance/drep/updateDrep.tsx b/src/components/pages/wallet/governance/drep/updateDrep.tsx index 8d32ed0f..5725c22a 100644 --- a/src/components/pages/wallet/governance/drep/updateDrep.tsx +++ b/src/components/pages/wallet/governance/drep/updateDrep.tsx @@ -84,7 +84,10 @@ export default function UpdateDRep() { setLoading(true); const txBuilder = getTxBuilder(network); - const drepIds = getDRepIds(multisigWallet.getDRepId()!); + const dRepId = multisigWallet?.getKeysByRole(3) ? multisigWallet?.getDRepId() : appWallet?.dRepId; + if (!dRepId) { + throw new Error("DRep not found"); + } try { const { anchorUrl, anchorHash } = await createAnchor(); @@ -107,7 +110,7 @@ export default function UpdateDRep() { } txBuilder - .drepUpdateCertificate(drepIds.cip105, { + .drepUpdateCertificate(dRepId, { anchorUrl: anchorUrl, anchorDataHash: anchorHash, }) diff --git a/src/components/pages/wallet/governance/proposal/voteButtton.tsx b/src/components/pages/wallet/governance/proposal/voteButtton.tsx index ebadff12..fc161453 100644 --- a/src/components/pages/wallet/governance/proposal/voteButtton.tsx +++ b/src/components/pages/wallet/governance/proposal/voteButtton.tsx @@ -92,7 +92,17 @@ export default function VoteButton({ } if (!multisigWallet) throw new Error("Multisig Wallet could not be built."); - const dRepId = appWallet.dRepId; + const dRepId = multisigWallet?.getKeysByRole(3) ? multisigWallet?.getDRepId() : appWallet?.dRepId; + if (!dRepId) { + setAlert("DRep not found"); + toast({ + title: "DRep not found", + description: `Please register as a DRep and retry.`, + duration: 10000, + variant: "destructive", + }); + return; + } const txBuilder = getTxBuilder(network); const assetMap = new Map(); diff --git a/src/components/pages/wallet/info/card-info.tsx b/src/components/pages/wallet/info/card-info.tsx index fc03e45f..4c0203ad 100644 --- a/src/components/pages/wallet/info/card-info.tsx +++ b/src/components/pages/wallet/info/card-info.tsx @@ -175,7 +175,10 @@ function ShowInfo({ appWallet }: { appWallet: Wallet }) { const { multisigWallet } = useMultisigWallet(); // Get DRep ID from multisig wallet if available, otherwise fallback to appWallet - const dRepId = multisigWallet?.getDRepId() || appWallet.dRepId; + const dRepId = multisigWallet?.getKeysByRole(3) ? multisigWallet?.getDRepId() : appWallet?.dRepId; + if (!dRepId) { + throw new Error("DRep not found"); + } return ( <>