Skip to content

Commit faaf9b0

Browse files
committed
feat(key-management)!: change behavior of ensureStakeKeys to return all reward accounts
1 parent 2100e72 commit faaf9b0

File tree

2 files changed

+18
-22
lines changed

2 files changed

+18
-22
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { AddressType, AsyncKeyAgent, GroupedAddress, KeyRole } from '../types';
1+
import { AddressType, AsyncKeyAgent, KeyRole } from '../types';
22
import { Cardano } from '@cardano-sdk/core';
33
import { Logger } from 'ts-log';
44
import { firstValueFrom } from 'rxjs';
5-
import uniq from 'lodash/uniq';
65

76
export interface EnsureStakeKeysParams {
87
/** Key agent to use */
@@ -17,7 +16,7 @@ export interface EnsureStakeKeysParams {
1716
/**
1817
* Given a count, checks if enough stake keys exist and derives
1918
* more if needed.
20-
* Returns the newly created reward accounts
19+
* Returns all reward accounts
2120
*/
2221
export const ensureStakeKeys = async ({
2322
keyAgent,
@@ -26,35 +25,27 @@ export const ensureStakeKeys = async ({
2625
paymentKeyIndex: index = 0
2726
}: EnsureStakeKeysParams): Promise<Cardano.RewardAccount[]> => {
2827
const knownAddresses = await firstValueFrom(keyAgent.knownAddresses$);
29-
// Get current number of derived stake keys
30-
const stakeKeyIndices = uniq(
28+
const stakeKeys = new Map(
3129
knownAddresses
3230
.filter(
3331
({ stakeKeyDerivationPath }) =>
3432
stakeKeyDerivationPath?.role === KeyRole.Stake && stakeKeyDerivationPath?.index !== undefined
3533
)
36-
.map(({ stakeKeyDerivationPath }) => stakeKeyDerivationPath!.index)
34+
.map(({ rewardAccount, stakeKeyDerivationPath }) => [stakeKeyDerivationPath!.index, rewardAccount])
3735
);
3836

39-
const countToDerive = count - stakeKeyIndices.length;
40-
41-
if (countToDerive <= 0) {
42-
// Sufficient stake keys are already created
43-
return [];
44-
}
45-
46-
logger.debug(`Stake keys requested: ${count}; got ${stakeKeyIndices.length}`);
37+
logger.debug(`Stake keys requested: ${count}; got ${stakeKeys.size}`);
4738

4839
// Need more stake keys for the portfolio
49-
const derivedAddresses: Promise<GroupedAddress>[] = [];
50-
for (let stakeKeyIdx = 0; derivedAddresses.length < countToDerive; stakeKeyIdx++) {
51-
if (!stakeKeyIndices.includes(stakeKeyIdx)) {
52-
logger.debug(`No derivation with stake key index ${stakeKeyIdx} exists. Deriving a new stake key.`);
53-
derivedAddresses.push(keyAgent.deriveAddress({ index, type: AddressType.External }, stakeKeyIdx));
40+
for (let stakeKeyIdx = 0; stakeKeys.size < count; stakeKeyIdx++) {
41+
if (!stakeKeys.has(stakeKeyIdx)) {
42+
const address = await keyAgent.deriveAddress({ index, type: AddressType.External }, stakeKeyIdx);
43+
logger.debug(
44+
`No derivation with stake key index ${stakeKeyIdx} exists. Derived a new stake key ${address.rewardAccount}.`
45+
);
46+
stakeKeys.set(stakeKeyIdx, address.rewardAccount);
5447
}
5548
}
5649

57-
const newAddresses = await Promise.all(derivedAddresses);
58-
logger.debug('Derived new addresses:', newAddresses);
59-
return newAddresses.map(({ rewardAccount }) => rewardAccount);
50+
return [...stakeKeys.values()];
6051
};

packages/key-management/test/util/ensureStakeKeys.test.ts

+5
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ describe('ensureStakeKeys', () => {
7373
expect(knownAddresses.length).toBe(1);
7474
});
7575

76+
it('returns all reward accounts', async () => {
77+
await keyAgent.deriveAddress({ index: 0, type: AddressType.External }, 0);
78+
await expect(util.ensureStakeKeys({ count: 2, keyAgent, logger })).resolves.toHaveLength(2);
79+
});
80+
7681
it('takes into account addresses with multiple stake keys and payment keys', async () => {
7782
await Promise.all([
7883
keyAgent.deriveAddress({ index: 0, type: AddressType.External }, 0),

0 commit comments

Comments
 (0)