1
- import { AddressType , AsyncKeyAgent , GroupedAddress , KeyRole } from '../types' ;
1
+ import { AddressType , AsyncKeyAgent , KeyRole } from '../types' ;
2
2
import { Cardano } from '@cardano-sdk/core' ;
3
3
import { Logger } from 'ts-log' ;
4
4
import { firstValueFrom } from 'rxjs' ;
5
- import uniq from 'lodash/uniq' ;
6
5
7
6
export interface EnsureStakeKeysParams {
8
7
/** Key agent to use */
@@ -17,7 +16,7 @@ export interface EnsureStakeKeysParams {
17
16
/**
18
17
* Given a count, checks if enough stake keys exist and derives
19
18
* more if needed.
20
- * Returns the newly created reward accounts
19
+ * Returns all reward accounts
21
20
*/
22
21
export const ensureStakeKeys = async ( {
23
22
keyAgent,
@@ -26,35 +25,27 @@ export const ensureStakeKeys = async ({
26
25
paymentKeyIndex : index = 0
27
26
} : EnsureStakeKeysParams ) : Promise < Cardano . RewardAccount [ ] > => {
28
27
const knownAddresses = await firstValueFrom ( keyAgent . knownAddresses$ ) ;
29
- // Get current number of derived stake keys
30
- const stakeKeyIndices = uniq (
28
+ const stakeKeys = new Map (
31
29
knownAddresses
32
30
. filter (
33
31
( { stakeKeyDerivationPath } ) =>
34
32
stakeKeyDerivationPath ?. role === KeyRole . Stake && stakeKeyDerivationPath ?. index !== undefined
35
33
)
36
- . map ( ( { stakeKeyDerivationPath } ) => stakeKeyDerivationPath ! . index )
34
+ . map ( ( { rewardAccount , stakeKeyDerivationPath } ) => [ stakeKeyDerivationPath ! . index , rewardAccount ] )
37
35
) ;
38
36
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 } ` ) ;
47
38
48
39
// 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 ) ;
54
47
}
55
48
}
56
49
57
- const newAddresses = await Promise . all ( derivedAddresses ) ;
58
- logger . debug ( 'Derived new addresses:' , newAddresses ) ;
59
- return newAddresses . map ( ( { rewardAccount } ) => rewardAccount ) ;
50
+ return [ ...stakeKeys . values ( ) ] ;
60
51
} ;
0 commit comments