@@ -2,7 +2,7 @@ import { AddAccountProps, AddWalletProps, RemoveAccountProps, UpdateMetadataProp
2
2
import { AnyWallet , ScriptWallet , WalletId , WalletType } from '../types' ;
3
3
import { Bip32PublicKey , Hash28ByteBase16 } from '@cardano-sdk/crypto' ;
4
4
import { Logger } from 'ts-log' ;
5
- import { Observable , defer , firstValueFrom , map , shareReplay , switchMap } from 'rxjs' ;
5
+ import { Observable , defer , firstValueFrom , map , shareReplay , switchMap , take } from 'rxjs' ;
6
6
import { Serialization } from '@cardano-sdk/core' ;
7
7
import { WalletConflictError } from '../errors' ;
8
8
import { contextLogger } from '@cardano-sdk/util' ;
@@ -48,14 +48,22 @@ export class WalletRepository<AccountMetadata extends {}> implements WalletRepos
48
48
this . wallets$ = defer ( ( ) => store . observeAll ( ) ) . pipe ( shareReplay ( 1 ) ) ;
49
49
}
50
50
51
+ #getWallets( ) {
52
+ return this . wallets$ . pipe (
53
+ // `setAll` makes the store.observeAll source emit
54
+ // so the pipes are triggered twice otherwise
55
+ take ( 1 )
56
+ ) ;
57
+ }
58
+
51
59
async addWallet ( props : AddWalletProps < AccountMetadata > ) : Promise < WalletId > {
52
60
this . #logger. debug ( 'addWallet' , props . type ) ;
53
61
const walletId =
54
62
props . type === WalletType . Script
55
63
? Serialization . Script . fromCore ( props . script ) . hash ( )
56
64
: Hash28ByteBase16 ( await Bip32PublicKey . fromHex ( props . extendedAccountPublicKey ) . hash ( ) ) ;
57
65
return firstValueFrom (
58
- this . wallets$ . pipe (
66
+ this . #getWallets ( ) . pipe (
59
67
switchMap ( ( wallets ) => {
60
68
if ( wallets . some ( ( wallet ) => wallet . walletId === walletId ) ) {
61
69
throw new WalletConflictError ( `Wallet '${ walletId } ' already exists` ) ;
@@ -90,7 +98,7 @@ export class WalletRepository<AccountMetadata extends {}> implements WalletRepos
90
98
const { walletId, accountIndex, metadata } = props ;
91
99
this . #logger. debug ( 'addAccount' , walletId , accountIndex , metadata ) ;
92
100
return firstValueFrom (
93
- this . wallets$ . pipe (
101
+ this . #getWallets ( ) . pipe (
94
102
switchMap ( ( wallets ) => {
95
103
const walletIndex = wallets . findIndex ( ( w ) => w . walletId === walletId ) ;
96
104
if ( walletIndex < 0 ) {
@@ -126,7 +134,7 @@ export class WalletRepository<AccountMetadata extends {}> implements WalletRepos
126
134
const { walletId, accountIndex, metadata } = props ;
127
135
this . #logger. debug ( 'updateMetadata' , walletId , accountIndex , metadata ) ;
128
136
return firstValueFrom (
129
- this . wallets$ . pipe (
137
+ this . #getWallets ( ) . pipe (
130
138
switchMap ( ( wallets ) => {
131
139
if ( typeof accountIndex !== 'undefined' ) {
132
140
const bip32Account = findAccount ( wallets , walletId , accountIndex ) ;
@@ -165,7 +173,7 @@ export class WalletRepository<AccountMetadata extends {}> implements WalletRepos
165
173
const { walletId, accountIndex } = props ;
166
174
this . #logger. debug ( 'removeAccount' , walletId , accountIndex ) ;
167
175
return firstValueFrom (
168
- this . wallets$ . pipe (
176
+ this . #getWallets ( ) . pipe (
169
177
switchMap ( ( wallets ) => {
170
178
const bip32Account = findAccount ( wallets , walletId , accountIndex ) ;
171
179
if ( ! bip32Account ) {
@@ -196,7 +204,8 @@ export class WalletRepository<AccountMetadata extends {}> implements WalletRepos
196
204
removeWallet ( walletId : WalletId ) : Promise < WalletId > {
197
205
this . #logger. debug ( 'removeWallet' , walletId ) ;
198
206
return firstValueFrom (
199
- this . wallets$ . pipe (
207
+ this . #getWallets( ) . pipe (
208
+ take ( 1 ) ,
200
209
switchMap ( ( wallets ) => {
201
210
const walletIndex = wallets . findIndex ( ( w ) => w . walletId === walletId ) ;
202
211
if ( walletIndex < 0 ) {
0 commit comments