Skip to content

Commit aaca2b8

Browse files
Revert "refactor!: make Bip32Account methods synchronous"
This reverts commit b56110c.
1 parent 8b3da5e commit aaca2b8

File tree

9 files changed

+59
-52
lines changed

9 files changed

+59
-52
lines changed

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

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

8383
await unDelegateWallet(wallet);
8484

85-
const poolPubKey = wallet1.bip32Account.derivePublicKey({
85+
const poolPubKey = await 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 = wallet1.bip32Account.deriveAddress(
93-
{
94-
index: 0,
95-
type: AddressType.External
96-
},
97-
0
92+
const poolRewardAccount = (
93+
await wallet1.bip32Account.deriveAddress(
94+
{
95+
index: 0,
96+
type: AddressType.External
97+
},
98+
0
99+
)
98100
).rewardAccount;
99101

100102
const registrationCert: Cardano.PoolRegistrationCertificate = {
@@ -167,19 +169,21 @@ describe('local-network/register-pool', () => {
167169
await walletReady(wallet);
168170
await unDelegateWallet(wallet);
169171

170-
const poolPubKey = wallet2.bip32Account.derivePublicKey({
172+
const poolPubKey = await wallet2.bip32Account.derivePublicKey({
171173
index: 0,
172174
role: KeyRole.External
173175
});
174176

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

185189
const registrationCert: Cardano.PoolRegistrationCertificate = {

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

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

108108
await walletReady(wallet);
109109

110-
const poolPubKey = wallet1.bip32Account.derivePublicKey({
110+
const poolPubKey = await 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 = wallet1.bip32Account.deriveAddress(
118-
{
119-
index: 0,
120-
type: AddressType.External
121-
},
122-
0
117+
const poolRewardAccount = (
118+
await wallet1.bip32Account.deriveAddress(
119+
{
120+
index: 0,
121+
type: AddressType.External
122+
},
123+
0
124+
)
123125
).rewardAccount;
124126

125127
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 = await bip32Account.derivePublicKey({ index, role: KeyRole.External });
72+
const paymentKeyHex = 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 = await bip32Account.derivePublicKey({ index, role: KeyRole.External });
82+
const paymentKeyHex = 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 = await bip32Account.derivePublicKey({ index, role: KeyRole.Stake });
95+
const stakeKeyHex = 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 = await bip32Account.derivePublicKey({ index, role: KeyRole.Stake });
109+
const stakeKeyHex = 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-
derivePublicKey(derivationPath: AccountKeyDerivationPath) {
50+
async 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-
deriveAddress(
58+
async deriveAddress(
5959
paymentKeyDerivationPath: AccountAddressDerivationPath,
6060
stakeKeyDerivationIndex: number
61-
): GroupedAddress {
61+
): Promise<GroupedAddress> {
6262
const stakeKeyDerivationPath = {
6363
index: stakeKeyDerivationIndex,
6464
role: KeyRole.Stake
6565
};
6666

67-
const derivedPublicPaymentKey = this.derivePublicKey({
67+
const derivedPublicPaymentKey = await 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 = this.derivePublicKey(stakeKeyDerivationPath);
74+
const publicStakeKey = await 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((x: AccountKeyDerivationPath) =>
86+
testnetAccount.derivePublicKey = jest.fn(async (x: AccountKeyDerivationPath) =>
8787
Crypto.Ed25519PublicKeyHex(keyMap.get(x.index)!)
8888
);
8989

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

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

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

1719
const DREP_REG_REQUIRED_PROTOCOL_VERSION = 10;
1820

packages/wallet/src/services/PublicStakeKeysTracker.ts

Lines changed: 9 additions & 6 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, distinctUntilChanged, map, switchMap } from 'rxjs';
4+
import { Observable, defaultIfEmpty, distinctUntilChanged, forkJoin, from, map, mergeMap, switchMap } from 'rxjs';
55
import { TrackerSubject } from '@cardano-sdk/util-rxjs';
66
import { deepEquals } from '@cardano-sdk/util';
77

@@ -48,11 +48,14 @@ export const createPublicStakeKeysTracker = ({
4848
new TrackerSubject(
4949
rewardAccounts$.pipe(
5050
withStakeKeyDerivationPaths(addresses$),
51-
map((derivationPathsAndStatus) =>
52-
derivationPathsAndStatus.map(({ stakeKeyDerivationPath, credentialStatus }) => ({
53-
credentialStatus,
54-
publicStakeKey: addressManager.derivePublicKey(stakeKeyDerivationPath)
55-
}))
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([]))
5659
),
5760
distinctUntilChanged(deepEquals)
5861
)

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ describe('BaseWallet methods', () => {
107107

108108
const asyncKeyAgent = await testAsyncKeyAgent();
109109
bip32Account = await Bip32Account.fromAsyncKeyAgent(asyncKeyAgent);
110-
bip32Account.deriveAddress = jest.fn().mockReturnValue(groupedAddress);
110+
bip32Account.deriveAddress = jest.fn().mockResolvedValue(groupedAddress);
111111
witnesser = util.createBip32Ed25519Witnesser(asyncKeyAgent);
112112
wallet = createPersonalWallet(
113113
{ name: 'Test Wallet' },
@@ -671,12 +671,7 @@ describe('BaseWallet methods', () => {
671671

672672
it('will retry deriving pubDrepKey if one does not exist', async () => {
673673
wallet.shutdown();
674-
bip32Account.derivePublicKey = jest
675-
.fn()
676-
.mockImplementationOnce(() => {
677-
throw new Error('error');
678-
})
679-
.mockReturnValue('string');
674+
bip32Account.derivePublicKey = jest.fn().mockRejectedValueOnce('error').mockResolvedValue('string');
680675
wallet = createPersonalWallet(
681676
{ name: 'Test Wallet' },
682677
{
@@ -819,7 +814,7 @@ describe('BaseWallet methods', () => {
819814
beforeEach(() => {
820815
wallet.shutdown();
821816

822-
bip32Account.deriveAddress = jest.fn((args) => {
817+
bip32Account.deriveAddress = jest.fn(async (args) => {
823818
if (args.index === 0) {
824819
return groupedAddress;
825820
}

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

Lines changed: 7 additions & 6 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 { delay, firstValueFrom, from, lastValueFrom, of, shareReplay, toArray } from 'rxjs';
5+
import { firstValueFrom, from, lastValueFrom, of, shareReplay, toArray } from 'rxjs';
66
import { mockProviders as mocks } from '@cardano-sdk/util-dev';
77

88
describe('PublicStakeKeysTracker', () => {
@@ -64,7 +64,9 @@ describe('PublicStakeKeysTracker', () => {
6464
}
6565
];
6666

67-
derivePublicKey = jest.fn().mockImplementation((path: AccountKeyDerivationPath) => `abc-${path.index}`);
67+
derivePublicKey = jest
68+
.fn()
69+
.mockImplementation((path: AccountKeyDerivationPath) => Promise.resolve(`abc-${path.index}`));
6870
bip32Account = {
6971
accountIndex: 0,
7072
chainId: Cardano.ChainIds.Preview,
@@ -135,7 +137,7 @@ describe('PublicStakeKeysTracker', () => {
135137

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

140142
const stakePubKeys$ = createPublicStakeKeysTracker({
141143
addresses$,
@@ -155,7 +157,7 @@ describe('PublicStakeKeysTracker', () => {
155157
});
156158

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

161163
const stakePubKeys$ = createPublicStakeKeysTracker({
@@ -176,9 +178,8 @@ describe('PublicStakeKeysTracker', () => {
176178
});
177179

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

0 commit comments

Comments
 (0)