Skip to content

Commit b56110c

Browse files
committed
refactor!: make Bip32Account methods synchronous
implementation of those methods no longer requires them to be async
1 parent 51a821d commit b56110c

File tree

9 files changed

+49
-57
lines changed

9 files changed

+49
-57
lines changed

packages/e2e/test/local-network/register-pool.test.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,19 @@ describe('local-network/register-pool', () => {
8282

8383
await unDelegateWallet(wallet);
8484

85-
const poolPubKey = await wallet1.bip32Account.derivePublicKey({
85+
const poolPubKey = wallet1.bip32Account.derivePublicKey({
8686
index: 0,
8787
role: KeyRole.External
8888
});
8989

9090
const poolKeyHash = bip32Ed25519.getPubKeyHash(poolPubKey);
9191
const poolId = Cardano.PoolId.fromKeyHash(poolKeyHash);
92-
const poolRewardAccount = (
93-
await wallet1.bip32Account.deriveAddress(
94-
{
95-
index: 0,
96-
type: AddressType.External
97-
},
98-
0
99-
)
92+
const poolRewardAccount = wallet1.bip32Account.deriveAddress(
93+
{
94+
index: 0,
95+
type: AddressType.External
96+
},
97+
0
10098
).rewardAccount;
10199

102100
const registrationCert: Cardano.PoolRegistrationCertificate = {
@@ -169,21 +167,19 @@ describe('local-network/register-pool', () => {
169167
await walletReady(wallet);
170168
await unDelegateWallet(wallet);
171169

172-
const poolPubKey = await wallet2.bip32Account.derivePublicKey({
170+
const poolPubKey = wallet2.bip32Account.derivePublicKey({
173171
index: 0,
174172
role: KeyRole.External
175173
});
176174

177175
const poolKeyHash = bip32Ed25519.getPubKeyHash(poolPubKey);
178176
const poolId = Cardano.PoolId.fromKeyHash(poolKeyHash);
179-
const poolRewardAccount = (
180-
await wallet2.bip32Account.deriveAddress(
181-
{
182-
index: 0,
183-
type: AddressType.External
184-
},
185-
0
186-
)
177+
const poolRewardAccount = wallet2.bip32Account.deriveAddress(
178+
{
179+
index: 0,
180+
type: AddressType.External
181+
},
182+
0
187183
).rewardAccount;
188184

189185
const registrationCert: Cardano.PoolRegistrationCertificate = {

packages/e2e/test/long-running/cache-invalidation.test.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,21 +107,19 @@ describe('cache invalidation', () => {
107107

108108
await walletReady(wallet);
109109

110-
const poolPubKey = await wallet1.bip32Account.derivePublicKey({
110+
const poolPubKey = wallet1.bip32Account.derivePublicKey({
111111
index: 0,
112112
role: KeyRole.External
113113
});
114114

115115
const poolKeyHash = bip32Ed25519.getPubKeyHash(poolPubKey);
116116
const poolId = Cardano.PoolId.fromKeyHash(poolKeyHash);
117-
const poolRewardAccount = (
118-
await wallet1.bip32Account.deriveAddress(
119-
{
120-
index: 0,
121-
type: AddressType.External
122-
},
123-
0
124-
)
117+
const poolRewardAccount = wallet1.bip32Account.deriveAddress(
118+
{
119+
index: 0,
120+
type: AddressType.External
121+
},
122+
0
125123
).rewardAccount;
126124

127125
const registrationCert: Cardano.PoolRegistrationCertificate = {

packages/e2e/test/wallet_epoch_0/PersonalWallet/cip30WalletApi.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ describe('PersonalWallet/cip30WalletApi', () => {
6969

7070
it('can signData with bech32 base address', async () => {
7171
const [{ address, index }] = await firstValueFrom(wallet.addresses$);
72-
const paymentKeyHex = bip32Account.derivePublicKey({ index, role: KeyRole.External });
72+
const paymentKeyHex = await bip32Account.derivePublicKey({ index, role: KeyRole.External });
7373

7474
const signature = await walletApi.signData({ sender: '' } as unknown as SenderContext, address, HexBlob('abc123'));
7575

@@ -79,7 +79,7 @@ describe('PersonalWallet/cip30WalletApi', () => {
7979
it('can signData with hex-encoded base address', async () => {
8080
const [{ address, index }] = await firstValueFrom(wallet.addresses$);
8181
const addressHex = Cardano.Address.fromBech32(address).toBytes();
82-
const paymentKeyHex = bip32Account.derivePublicKey({ index, role: KeyRole.External });
82+
const paymentKeyHex = await bip32Account.derivePublicKey({ index, role: KeyRole.External });
8383

8484
const signature = await walletApi.signData(
8585
{ sender: '' } as unknown as SenderContext,
@@ -92,7 +92,7 @@ describe('PersonalWallet/cip30WalletApi', () => {
9292

9393
it('can signData with bech32 base address', async () => {
9494
const [{ rewardAccount, index }] = await firstValueFrom(wallet.addresses$);
95-
const stakeKeyHex = bip32Account.derivePublicKey({ index, role: KeyRole.Stake });
95+
const stakeKeyHex = await bip32Account.derivePublicKey({ index, role: KeyRole.Stake });
9696

9797
const signature = await walletApi.signData(
9898
{ sender: '' } as unknown as SenderContext,
@@ -106,7 +106,7 @@ describe('PersonalWallet/cip30WalletApi', () => {
106106
it('can signData with hex-encoded reward account', async () => {
107107
const [{ rewardAccount, index }] = await firstValueFrom(wallet.addresses$);
108108
const rewardAccountHex = Cardano.Address.fromBech32(rewardAccount).toBytes();
109-
const stakeKeyHex = bip32Account.derivePublicKey({ index, role: KeyRole.Stake });
109+
const stakeKeyHex = await bip32Account.derivePublicKey({ index, role: KeyRole.Stake });
110110

111111
const signature = await walletApi.signData(
112112
{ sender: '' } as unknown as SenderContext,

packages/key-management/src/Bip32Account.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,31 +47,31 @@ export class Bip32Account {
4747
this.accountIndex = accountIndex;
4848
}
4949

50-
async derivePublicKey(derivationPath: AccountKeyDerivationPath) {
50+
derivePublicKey(derivationPath: AccountKeyDerivationPath) {
5151
const extendedKey = this.#bip32Ed25519.derivePublicKey(this.extendedAccountPublicKeyHex, [
5252
derivationPath.role,
5353
derivationPath.index
5454
]);
5555
return Ed25519PublicKeyHex.fromBip32PublicKey(extendedKey);
5656
}
5757

58-
async deriveAddress(
58+
deriveAddress(
5959
paymentKeyDerivationPath: AccountAddressDerivationPath,
6060
stakeKeyDerivationIndex: number
61-
): Promise<GroupedAddress> {
61+
): GroupedAddress {
6262
const stakeKeyDerivationPath = {
6363
index: stakeKeyDerivationIndex,
6464
role: KeyRole.Stake
6565
};
6666

67-
const derivedPublicPaymentKey = await this.derivePublicKey({
67+
const derivedPublicPaymentKey = this.derivePublicKey({
6868
index: paymentKeyDerivationPath.index,
6969
role: Number(paymentKeyDerivationPath.type)
7070
});
7171

7272
const derivedPublicPaymentKeyHash = this.#blake2b.hash(derivedPublicPaymentKey, BIP32_PUBLIC_KEY_HASH_LENGTH);
7373

74-
const publicStakeKey = await this.derivePublicKey(stakeKeyDerivationPath);
74+
const publicStakeKey = this.derivePublicKey(stakeKeyDerivationPath);
7575
const publicStakeKeyHash = this.#blake2b.hash(publicStakeKey, BIP32_PUBLIC_KEY_HASH_LENGTH);
7676

7777
const stakeCredential = { hash: publicStakeKeyHash, type: Cardano.CredentialType.KeyHash };

packages/key-management/test/Bip32Account.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ describe('Bip32Account', () => {
8383
[4, '4444444444444444444444444444444444444444444444444444444444444444']
8484
]);
8585

86-
testnetAccount.derivePublicKey = jest.fn(async (x: AccountKeyDerivationPath) =>
86+
testnetAccount.derivePublicKey = jest.fn((x: AccountKeyDerivationPath) =>
8787
Crypto.Ed25519PublicKeyHex(keyMap.get(x.index)!)
8888
);
8989

packages/tx-construction/src/tx-builder/initializeTx.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ import { hasCorrectVoteDelegation } from './hasCorrectVoteDelegation';
1212

1313
const dRepPublicKeyHash = async (addressManager?: Bip32Account): Promise<Ed25519KeyHashHex | undefined> =>
1414
addressManager &&
15-
Ed25519PublicKey.fromHex(await addressManager.derivePublicKey(util.DREP_KEY_DERIVATION_PATH))
16-
.hash()
17-
.hex();
15+
Ed25519PublicKey.fromHex(addressManager.derivePublicKey(util.DREP_KEY_DERIVATION_PATH)).hash().hex();
1816

1917
const DREP_REG_REQUIRED_PROTOCOL_VERSION = 10;
2018

packages/wallet/src/services/PublicStakeKeysTracker.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { AccountKeyDerivationPath, Bip32Account, GroupedAddress } from '@cardano-sdk/key-management';
22
import { Cardano } from '@cardano-sdk/core';
33
import { Ed25519PublicKeyHex } from '@cardano-sdk/crypto';
4-
import { Observable, defaultIfEmpty, distinctUntilChanged, forkJoin, from, map, mergeMap, switchMap } from 'rxjs';
4+
import { Observable, distinctUntilChanged, map, switchMap } from 'rxjs';
55
import { TrackerSubject } from '@cardano-sdk/util-rxjs';
66
import { deepEquals } from '@cardano-sdk/util';
77

@@ -48,14 +48,11 @@ export const createPublicStakeKeysTracker = ({
4848
new TrackerSubject(
4949
rewardAccounts$.pipe(
5050
withStakeKeyDerivationPaths(addresses$),
51-
mergeMap((derivationPathsAndStatus) =>
52-
forkJoin(
53-
derivationPathsAndStatus.map(({ stakeKeyDerivationPath, credentialStatus }) =>
54-
from(addressManager.derivePublicKey(stakeKeyDerivationPath)).pipe(
55-
map((publicStakeKey) => ({ credentialStatus, publicStakeKey }))
56-
)
57-
)
58-
).pipe(defaultIfEmpty([]))
51+
map((derivationPathsAndStatus) =>
52+
derivationPathsAndStatus.map(({ stakeKeyDerivationPath, credentialStatus }) => ({
53+
credentialStatus,
54+
publicStakeKey: addressManager.derivePublicKey(stakeKeyDerivationPath)
55+
}))
5956
),
6057
distinctUntilChanged(deepEquals)
6158
)

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ describe('BaseWallet methods', () => {
105105

106106
const asyncKeyAgent = await testAsyncKeyAgent();
107107
bip32Account = await Bip32Account.fromAsyncKeyAgent(asyncKeyAgent);
108-
bip32Account.deriveAddress = jest.fn().mockResolvedValue(groupedAddress);
108+
bip32Account.deriveAddress = jest.fn().mockReturnValue(groupedAddress);
109109
witnesser = util.createBip32Ed25519Witnesser(asyncKeyAgent);
110110
wallet = createPersonalWallet(
111111
{ name: 'Test Wallet' },
@@ -611,8 +611,10 @@ describe('BaseWallet methods', () => {
611611
wallet.shutdown();
612612
bip32Account.derivePublicKey = jest
613613
.fn()
614-
.mockRejectedValueOnce('error')
615-
.mockResolvedValue({ hex: () => 'string' });
614+
.mockImplementationOnce(() => {
615+
throw new Error('error');
616+
})
617+
.mockReturnValue('string');
616618
wallet = createPersonalWallet(
617619
{ name: 'Test Wallet' },
618620
{
@@ -755,7 +757,7 @@ describe('BaseWallet methods', () => {
755757
beforeEach(() => {
756758
wallet.shutdown();
757759

758-
bip32Account.deriveAddress = jest.fn(async (args) => {
760+
bip32Account.deriveAddress = jest.fn((args) => {
759761
if (args.index === 0) {
760762
return groupedAddress;
761763
}

packages/wallet/test/services/PublicStakeKeysTracker.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { AccountKeyDerivationPath, Bip32Account, GroupedAddress, KeyRole } from
22
import { Cardano } from '@cardano-sdk/core';
33
import { ObservableWallet } from '../../src';
44
import { PubStakeKeyAndStatus, createPublicStakeKeysTracker } from '../../src/services/PublicStakeKeysTracker';
5-
import { firstValueFrom, from, lastValueFrom, of, shareReplay, toArray } from 'rxjs';
5+
import { delay, firstValueFrom, from, lastValueFrom, of, shareReplay, toArray } from 'rxjs';
66
import { mockProviders as mocks } from '@cardano-sdk/util-dev';
77

88
describe('PublicStakeKeysTracker', () => {
@@ -135,7 +135,7 @@ describe('PublicStakeKeysTracker', () => {
135135

136136
it('emits when reward accounts change', async () => {
137137
const addresses$ = of(addresses);
138-
const rewardAccounts$ = from([[rewardAccounts[0]], rewardAccounts]);
138+
const rewardAccounts$ = from([[rewardAccounts[0]], rewardAccounts]).pipe(delay(1));
139139

140140
const stakePubKeys$ = createPublicStakeKeysTracker({
141141
addresses$,
@@ -155,7 +155,7 @@ describe('PublicStakeKeysTracker', () => {
155155
});
156156

157157
it('emits when addresses change', async () => {
158-
const addresses$ = from([[addresses[0]], addresses]);
158+
const addresses$ = from([[addresses[0]], addresses]).pipe(delay(1));
159159
const rewardAccounts$ = of(rewardAccounts);
160160

161161
const stakePubKeys$ = createPublicStakeKeysTracker({
@@ -176,8 +176,9 @@ describe('PublicStakeKeysTracker', () => {
176176
});
177177

178178
it('does not emit duplicates', async () => {
179-
const rewardAccounts$ = from([rewardAccounts, rewardAccounts]);
179+
const rewardAccounts$ = from([rewardAccounts, rewardAccounts]).pipe(delay(1));
180180
const addresses$ = from([[addresses[0]], addresses, addresses]).pipe(
181+
delay(1),
181182
shareReplay({ bufferSize: 1, refCount: true })
182183
);
183184

0 commit comments

Comments
 (0)