Skip to content

Commit a094395

Browse files
committed
Enhance DRep registration logic and wallet type detection
- Improved error handling in the registerDRep function to accommodate both multisig and legacy wallets. - Refactored DRep ID and script retrieval to ensure proper handling of wallet types. - Updated comments for clarity on wallet type distinctions and script handling. - Enhanced address building logic to support external stake credential hashes for legacy wallets.
1 parent 7dcd832 commit a094395

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

src/components/pages/wallet/governance/drep/registerDrep.tsx

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,21 +114,47 @@ export default function RegisterDRep({ onClose }: RegisterDRepProps = {}) {
114114
}
115115

116116
async function registerDrep(): Promise<void> {
117-
if (!connected || !userAddress || !multisigWallet || !appWallet)
118-
throw new Error("Multisig wallet not connected");
117+
if (!connected || !userAddress || !appWallet)
118+
throw new Error("Wallet not connected");
119119

120120
setLoading(true);
121121
const txBuilder = getTxBuilder(network);
122122

123-
const drepData = multisigWallet?.getDRep(appWallet);
124-
if (!drepData) {
125-
throw new Error("DRep not found");
123+
// For legacy wallets (no multisigWallet), use appWallet values directly (preserves input order)
124+
// For SDK wallets, use multisigWallet to compute DRep ID and script
125+
let dRepId: string;
126+
let drepCbor: string;
127+
let scriptCbor: string;
128+
let changeAddress: string;
129+
130+
if (multisigWallet) {
131+
const drepData = multisigWallet.getDRep(appWallet);
132+
if (!drepData) {
133+
throw new Error("DRep not found");
134+
}
135+
dRepId = drepData.dRepId;
136+
drepCbor = drepData.drepCbor;
137+
const multisigScript = multisigWallet.getScript();
138+
const multisigScriptCbor = multisigScript.scriptCbor;
139+
const appScriptCbor = appWallet.scriptCbor;
140+
if (!multisigScriptCbor && !appScriptCbor) {
141+
throw new Error("Script CBOR not found");
142+
}
143+
scriptCbor = multisigWallet.getKeysByRole(3) ? (multisigScriptCbor || appScriptCbor!) : (appScriptCbor || multisigScriptCbor!);
144+
changeAddress = multisigScript.address;
145+
} else {
146+
// Legacy wallet: use appWallet values (computed with input order preserved)
147+
if (!appWallet.dRepId || !appWallet.scriptCbor) {
148+
throw new Error("DRep ID or script not found for legacy wallet");
149+
}
150+
dRepId = appWallet.dRepId;
151+
drepCbor = appWallet.scriptCbor; // Use payment script CBOR for legacy wallets
152+
scriptCbor = appWallet.scriptCbor;
153+
changeAddress = appWallet.address;
126154
}
127-
const { dRepId, drepCbor } = drepData;
128155

129-
const scriptCbor = multisigWallet?.getKeysByRole(3) ? multisigWallet?.getScript().scriptCbor : appWallet.scriptCbor;
130-
if (!scriptCbor) {
131-
throw new Error("Script not found");
156+
if (!scriptCbor || !changeAddress) {
157+
throw new Error("Script or change address not found");
132158
}
133159
try {
134160
const { anchorUrl, anchorHash } = await createAnchor();
@@ -157,7 +183,7 @@ export default function RegisterDRep({ onClose }: RegisterDRepProps = {}) {
157183
anchorDataHash: anchorHash,
158184
})
159185
.certificateScript(drepCbor)
160-
.changeAddress(multisigWallet.getScript().address);
186+
.changeAddress(changeAddress);
161187

162188

163189

src/utils/common.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ export type WalletType = 'legacy' | 'sdk' | 'summon';
2727
export function getWalletType(wallet: DbWalletWithLegacy): WalletType {
2828
if (wallet.rawImportBodies?.multisig) return 'summon';
2929

30-
// Legacy: no stake keys, no stake credential hash, no DRep keys
30+
// Legacy: only payment keys (no stake keys, no DRep keys)
31+
// External stake credential hash doesn't make it SDK - it's still legacy if only payment keys
3132
const hasStakeKeys = wallet.signersStakeKeys && wallet.signersStakeKeys.length > 0;
3233
const hasDRepKeys = wallet.signersDRepKeys && wallet.signersDRepKeys.length > 0;
33-
if (!hasStakeKeys && !wallet.stakeCredentialHash && !hasDRepKeys) return 'legacy';
34+
if (!hasStakeKeys && !hasDRepKeys) return 'legacy';
3435

3536
return 'sdk';
3637
}
@@ -262,10 +263,11 @@ export function buildWallet(
262263
nativeScript.required = wallet.numRequiredSigners!;
263264
}
264265

265-
// Build address from payment script only (no staking)
266+
// Build address from payment script with external stake credential hash if available
267+
// Legacy wallets can have external stake key hash but no individual stake keys
266268
const address = serializeNativeScript(
267269
nativeScript as NativeScript,
268-
undefined, // No stake credential hash for legacy wallets
270+
wallet.stakeCredentialHash as undefined | string, // Use external stake credential hash if available
269271
network,
270272
).address;
271273

0 commit comments

Comments
 (0)