Skip to content

Commit 9cf346f

Browse files
committed
refactor(wallet): hoist ObservableWallet getPubDRepKey under ObservableWallet.governance
BREAKING CHANGE: hoist ObservableWallet getPubDRepKey under ObservableWallet.governance
1 parent 06a1de5 commit 9cf346f

File tree

8 files changed

+24
-22
lines changed

8 files changed

+24
-22
lines changed

packages/wallet/src/Wallets/BaseWallet.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ export class BaseWallet implements ObservableWallet {
259259
readonly publicStakeKeys$: TrackerSubject<PubStakeKeyAndStatus[]>;
260260
readonly governance: {
261261
readonly isRegisteredAsDRep$: Observable<boolean>;
262+
getPubDRepKey(): Promise<Ed25519PublicKeyHex | undefined>;
262263
};
263264
handles$: Observable<HandleInfo[]>;
264265

@@ -557,10 +558,19 @@ export class BaseWallet implements ObservableWallet {
557558
utxo: this.utxo
558559
});
559560

561+
const getPubDRepKey = async (): Promise<Ed25519PublicKeyHex | undefined> => {
562+
if (isBip32PublicCredentialsManager(this.#publicCredentialsManager)) {
563+
return (await this.#publicCredentialsManager.bip32Account.derivePublicKey(util.DREP_KEY_DERIVATION_PATH)).hex();
564+
}
565+
566+
return undefined;
567+
};
568+
560569
this.governance = {
570+
getPubDRepKey,
561571
isRegisteredAsDRep$: createDRepRegistrationTracker({
562572
historyTransactions$: this.transactions.history$,
563-
pubDRepKeyHash$: from(this.getPubDRepKey().then(getDRepKeyHash))
573+
pubDRepKeyHash$: from(getPubDRepKey().then(getDRepKeyHash))
564574
})
565575
};
566576

@@ -584,7 +594,7 @@ export class BaseWallet implements ObservableWallet {
584594
witness
585595
}: FinalizeTxProps): Promise<Cardano.Tx> {
586596
const knownAddresses = await firstValueFrom(this.addresses$);
587-
const dRepPublicKey = await this.getPubDRepKey();
597+
const dRepPublicKey = await this.governance.getPubDRepKey();
588598

589599
const context = {
590600
...signingContext,
@@ -792,14 +802,6 @@ export class BaseWallet implements ObservableWallet {
792802
throw new Error('getPubDRepKey is not supported by script wallets');
793803
}
794804

795-
async getPubDRepKey(): Promise<Ed25519PublicKeyHex | undefined> {
796-
if (isBip32PublicCredentialsManager(this.#publicCredentialsManager)) {
797-
return (await this.#publicCredentialsManager.bip32Account.derivePublicKey(util.DREP_KEY_DERIVATION_PATH)).hex();
798-
}
799-
800-
return undefined;
801-
}
802-
803805
async discoverAddresses(): Promise<GroupedAddress[]> {
804806
if (isBip32PublicCredentialsManager(this.#publicCredentialsManager)) {
805807
const addresses = await this.#publicCredentialsManager.addressDiscovery.discover(

packages/wallet/src/cip30.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ const extendedCip95WalletApi = (
564564
logger.debug('getting public DRep key');
565565
try {
566566
const wallet = await firstValueFrom(wallet$);
567-
const dReKey = await wallet.getPubDRepKey();
567+
const dReKey = await wallet.governance.getPubDRepKey();
568568

569569
if (!dReKey) throw new Error('Shared wallet does not support DRep key');
570570

packages/wallet/src/services/WalletUtil.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ export const requiresForeignSignatures = async (tx: Cardano.Tx, wallet: Observab
296296
})
297297
.filter((acct): acct is KeyManagementUtil.StakeKeySignerData => acct.derivationPath !== null);
298298

299-
const dRepKey = await wallet.getPubDRepKey();
299+
const dRepKey = await wallet.governance.getPubDRepKey();
300300
const dRepKeyHash = dRepKey ? (await Crypto.Ed25519PublicKey.fromHex(dRepKey).hash()).hex() : undefined;
301301

302302
return (

packages/wallet/src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ export interface ObservableWallet {
8787
readonly governance: {
8888
/** true this wallet is registered as drep */
8989
readonly isRegisteredAsDRep$: Observable<boolean>;
90+
/** Returns the wallet account's public DRep Key or undefined if the wallet doesn't control any DRep key */
91+
getPubDRepKey(): Promise<Ed25519PublicKeyHex | undefined>;
9092
};
9193
/** All owned and historical assets */
9294
readonly assetInfo$: Observable<Assets>;
@@ -100,8 +102,6 @@ export interface ObservableWallet {
100102

101103
getName(): Promise<string>;
102104

103-
/** Returns the wallet account's public DRep Key or undefined if the wallet doesn't control any DRep key */
104-
getPubDRepKey(): Promise<Ed25519PublicKeyHex | undefined>;
105105
/**
106106
* @deprecated Use `createTxBuilder()` instead.
107107
* @throws InputSelectionError

packages/wallet/test/PersonalWallet/methods.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ describe('BaseWallet methods', () => {
447447
});
448448

449449
test('rejects if bech32 DRepID is not a type 6 address', async () => {
450-
const dRepKey = await wallet.getPubDRepKey();
450+
const dRepKey = await wallet.governance.getPubDRepKey();
451451
for (const type in Cardano.AddressType) {
452452
if (!Number.isNaN(Number(type)) && Number(type) !== Cardano.AddressType.EnterpriseKey) {
453453
const drepid = buildDRepIDFromDRepKey(dRepKey!, 0, type as unknown as Cardano.AddressType);
@@ -458,7 +458,7 @@ describe('BaseWallet methods', () => {
458458
});
459459

460460
it('getPubDRepKey', async () => {
461-
const response = await wallet.getPubDRepKey();
461+
const response = await wallet.governance.getPubDRepKey();
462462
expect(typeof response).toBe('string');
463463
});
464464

@@ -486,7 +486,7 @@ describe('BaseWallet methods', () => {
486486
);
487487
await waitForWalletStateSettle(wallet);
488488

489-
const response = await wallet.getPubDRepKey();
489+
const response = await wallet.governance.getPubDRepKey();
490490
expect(response).toBe('string');
491491
expect(bip32Account.derivePublicKey).toHaveBeenCalledTimes(3);
492492
});

packages/wallet/test/integration/cip30mapping.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ const createWalletAndApiWithStores = async (
7676
submitTx: createMockGenericCallback(),
7777
...(!!getCollateralCallback && { getCollateral: getCollateralCallback })
7878
};
79-
wallet.getPubDRepKey = jest.fn(wallet.getPubDRepKey);
79+
wallet.governance.getPubDRepKey = jest.fn(wallet.governance.getPubDRepKey);
8080

8181
const api = cip30.createWalletApi(of(wallet), confirmationCallback, { logger });
8282
if (settle) await waitForWalletStateSettle(wallet);
@@ -606,10 +606,10 @@ describe('cip30', () => {
606606
describe('api.getPubDRepKey', () => {
607607
test("returns the DRep key derived from the wallet's public key", async () => {
608608
const cip95PubDRepKey = await api.getPubDRepKey(context);
609-
expect(cip95PubDRepKey).toEqual(await wallet.getPubDRepKey());
609+
expect(cip95PubDRepKey).toEqual(await wallet.governance.getPubDRepKey());
610610
});
611611
test('throws an ApiError on unexpected error', async () => {
612-
(wallet.getPubDRepKey as jest.Mock).mockRejectedValueOnce(new Error('unexpected error'));
612+
(wallet.governance.getPubDRepKey as jest.Mock).mockRejectedValueOnce(new Error('unexpected error'));
613613
try {
614614
await api.getPubDRepKey(context);
615615
} catch (error) {

packages/wallet/test/services/WalletUtil.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ describe('WalletUtil', () => {
554554
walletUtil: wallet.util
555555
});
556556

557-
dRepCredential = (await wallet.getPubDRepKey())!;
557+
dRepCredential = (await wallet.governance.getPubDRepKey())!;
558558
dRepKeyHash = Crypto.Hash28ByteBase16.fromEd25519KeyHashHex(
559559
(await Crypto.Ed25519PublicKey.fromHex(dRepCredential).hash()).hex()
560560
);

packages/web-extension/src/observableWallet/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ export const observableWalletProperties: RemoteApiProperties<ObservableWallet> =
116116
finalizeTx: RemoteApiPropertyType.MethodReturningPromise,
117117
genesisParameters$: RemoteApiPropertyType.HotObservable,
118118
getName: RemoteApiPropertyType.MethodReturningPromise,
119-
getPubDRepKey: RemoteApiPropertyType.MethodReturningPromise,
120119
governance: {
120+
getPubDRepKey: RemoteApiPropertyType.MethodReturningPromise,
121121
isRegisteredAsDRep$: RemoteApiPropertyType.HotObservable
122122
},
123123
handles$: RemoteApiPropertyType.HotObservable,

0 commit comments

Comments
 (0)