Skip to content

Commit 7bd5a0c

Browse files
committed
feat(governance): implement governance checks and upgrade wallet component
1 parent c121e29 commit 7bd5a0c

File tree

5 files changed

+74
-5
lines changed

5 files changed

+74
-5
lines changed

src/components/multisig/inspect-multisig-script.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ export default function InspectMultisigScript({
7272
);
7373
}
7474

75-
if (mWallet?.buildScript(3) !== undefined) {
75+
if (mWallet?.buildScript(3) !== undefined && mWallet.isGovernanceEnabled()) {
7676
slides.push(
7777
<RowLabelInfo
7878
key="stake-3"
79-
label="stake:"
79+
label="drep:"
8080
value={<Code>{JSON.stringify(mWallet.buildScript(3), null, 2)}</Code>}
8181
/>,
8282
);
@@ -98,7 +98,7 @@ export default function InspectMultisigScript({
9898
/>
9999
)}
100100
{/* add pending rewards like balance */}
101-
{mWallet.stakingEnabled() && <RowLabelInfo
101+
{mWallet.isGovernanceEnabled() && <RowLabelInfo
102102
label="dRep ID"
103103
value={<Code>{mWallet.getDRepId()}</Code>}
104104
copyString={mWallet.getDRepId()}

src/components/pages/wallet/info/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { ArchiveWallet } from "./archive-wallet";
88
import InspectMultisigScript from "@/components/multisig/inspect-multisig-script";
99
import { UpgradeStakingWallet } from "./upgrade-staking-wallet";
1010
import { RegisterWallet } from "./register-wallet";
11+
import { UpgradeGovernanceWallet } from "./upgrade-governance-wallet";
1112

1213
export default function WalletInfo() {
1314
const { appWallet } = useAppWallet();
@@ -25,6 +26,7 @@ export default function WalletInfo() {
2526
{multisigWallet && multisigWallet.stakingEnabled() && <InspectMultisigScript mWallet={multisigWallet} />}
2627
{multisigWallet && <RegisterWallet mWallet={multisigWallet} appWallet={appWallet} />}
2728
{multisigWallet && <UpgradeStakingWallet mWallet={multisigWallet} appWallet={appWallet} />}
29+
{multisigWallet && <UpgradeGovernanceWallet mWallet={multisigWallet} />}
2830
<ArchiveWallet appWallet={appWallet} />
2931
<MigrateWallet appWallet={appWallet} />
3032
</div>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import {useMemo} from "react";
2+
3+
import type { MultisigWallet } from "@/utils/multisigSDK";
4+
5+
import CardUI from "@/components/ui/card-content";
6+
7+
export function UpgradeGovernanceWallet({
8+
mWallet,
9+
}: {
10+
mWallet?: MultisigWallet;
11+
}) {
12+
13+
14+
15+
const upgraded = useMemo(() => {
16+
return mWallet?.isGovernanceEnabled();
17+
}, [mWallet]);
18+
19+
if (!mWallet || upgraded ) return null;
20+
21+
return (
22+
<CardUI
23+
title="Upgrade Wallet for Governance"
24+
description="Add a drep key script to your multisig Wallet."
25+
cardClassName="col-span-2"
26+
>
27+
{!mWallet.stakingEnabled() && (
28+
<div>
29+
Not all drep keys have been added. Click Edit Signers to add your
30+
drep key!
31+
</div>
32+
)}
33+
</CardUI>
34+
);
35+
}

src/hooks/common.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,21 @@ export function buildMultisigWallet(
5050
}
5151
});
5252
}
53+
if (wallet.signersDRepKeys && wallet.signersDRepKeys.length > 0) {
54+
wallet.signersDRepKeys.forEach((dRepKey, i) => {
55+
if (dRepKey) {
56+
try {
57+
keys.push({
58+
keyHash: dRepKey,
59+
role: 3,
60+
name: wallet.signersDescriptions[i] || "",
61+
});
62+
} catch (e) {
63+
console.warn(`Invalid dRep address at index ${i}:`, dRepKey);
64+
}
65+
}
66+
});
67+
}
5368
if (keys.length === 0) return;
5469
const multisigWallet = new MultisigWallet(
5570
wallet.name,

src/utils/multisigSDK.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export class MultisigWallet {
102102
);
103103
return undefined;
104104
}
105-
return getScript(paymentScript, this.network, ).scriptCbor;
105+
return getScript(paymentScript, this.network).scriptCbor;
106106
}
107107

108108
getStakingScript(): string | undefined {
@@ -183,6 +183,16 @@ export class MultisigWallet {
183183
);
184184
}
185185

186+
isGovernanceEnabled(): boolean {
187+
const paymentKeys = this.getKeysByRole(0);
188+
const dRepKeys = this.getKeysByRole(3);
189+
if (!paymentKeys || !dRepKeys || dRepKeys.length === 0) return false;
190+
console.log(
191+
`Governance enabled: ${dRepKeys.length} dRep keys and ${paymentKeys.length} payment keys`
192+
);
193+
return dRepKeys.length === paymentKeys.length;
194+
}
195+
186196
getDRepId(): string | undefined {
187197
return getDRepIds(this.getDRepId105()!).cip129;
188198
}
@@ -192,6 +202,13 @@ export class MultisigWallet {
192202
* built from role-3 keys (DRep keys). Throws if no script is built.
193203
*/
194204
getDRepId105(): string | undefined {
205+
// Check if all signers have supplied DRep keys
206+
if (this.isGovernanceEnabled()) {
207+
const dRepScript = this.buildScript(3);
208+
if (dRepScript) {
209+
return resolveScriptHashDRepId(resolveNativeScriptHash(dRepScript));
210+
}
211+
}
195212
return resolveScriptHashDRepId(
196213
resolveNativeScriptHash(this.buildScript(0)!), // Still Wrong should be 3 -> for drep keys.
197214
);
@@ -288,7 +305,7 @@ function getScript(
288305
script,
289306
stakeCredentialHash,
290307
network,
291-
enabled
308+
enabled,
292309
);
293310
if (!scriptCbor) {
294311
throw new Error("Failed to serialize multisig script");

0 commit comments

Comments
 (0)