diff --git a/src/models/profile.ts b/src/models/profile.ts index b9c1f6c00..88688f249 100644 --- a/src/models/profile.ts +++ b/src/models/profile.ts @@ -2,6 +2,7 @@ export class Profile { contacts: any = {}; name: string; networkId: string; + profileId?: string; wallets: any = {}; deserialize(input: any): Profile { diff --git a/src/pages/wallet/wallet-dashboard/wallet-dashboard.ts b/src/pages/wallet/wallet-dashboard/wallet-dashboard.ts index cde620b1b..216c36e73 100644 --- a/src/pages/wallet/wallet-dashboard/wallet-dashboard.ts +++ b/src/pages/wallet/wallet-dashboard/wallet-dashboard.ts @@ -386,7 +386,7 @@ export class WalletDashboardPage implements OnInit, OnDestroy { } private saveWallet() { - this.userDataProvider.saveWallet(this.wallet); + this.userDataProvider.updateWallet(this.wallet, this.profile.profileId); } private deleteWallet() { diff --git a/src/pages/wallet/wallet-list/wallet-list.ts b/src/pages/wallet/wallet-list/wallet-list.ts index 141b3dd86..bc004222b 100644 --- a/src/pages/wallet/wallet-list/wallet-list.ts +++ b/src/pages/wallet/wallet-list/wallet-list.ts @@ -74,7 +74,7 @@ export class WalletListPage implements OnDestroy { this.navCtrl.push('WalletDashboardPage', { address: wallet.address }).then(() => { - this.userDataProvider.saveWallet(wallet).subscribe(() => { + this.userDataProvider.updateWallet(wallet, this.currentProfile.profileId).subscribe(() => { this.loadWallets(); this.slider.slideTo(0); }); diff --git a/src/providers/local-notifications/local-notifications.ts b/src/providers/local-notifications/local-notifications.ts index 0a7575e94..42179f362 100644 --- a/src/providers/local-notifications/local-notifications.ts +++ b/src/providers/local-notifications/local-notifications.ts @@ -96,7 +96,7 @@ export class LocalNotificationsProvider { w.loadTransactions(response.transactions, this.arkApiProvider.network); - this.userDataProvider.saveWallet(w, wallet.profileId); + this.userDataProvider.updateWallet(w, wallet.profileId); // Get only the new transaction and notify the user const newTransactions = lodash.drop(w.transactions, localLength); diff --git a/src/providers/user-data/user-data.ts b/src/providers/user-data/user-data.ts index 19661fd40..f732485a7 100644 --- a/src/providers/user-data/user-data.ts +++ b/src/providers/user-data/user-data.ts @@ -21,7 +21,7 @@ import { StoredNetwork } from '@models/stored-network'; @Injectable() export class UserDataProvider { - public profiles = {}; + public profiles: { [key: string]: Profile } = {}; public networks = {}; public currentProfile: Profile; @@ -127,7 +127,7 @@ export class UserDataProvider { if (wallet && !wallet.cipherSecondKey) { // wallet.secondBip38 = this.forgeProvider.encryptBip38(secondWif, pinCode, this.currentNetwork); wallet.cipherSecondKey = this.forgeProvider.encrypt(secondPassphrase, pinCode, wallet.address, wallet.iv); - return this.saveWallet(wallet, profileId, true); + return this.updateWallet(wallet, profileId, true); } return this.saveProfiles(); @@ -173,7 +173,7 @@ export class UserDataProvider { // wallet.secondBip38 = this.forgeProvider.encryptBip38(secondWif, newPassword, this.currentNetwork); } - this.saveWallet(wallet, profileId); + this.updateWallet(wallet, profileId); } } @@ -201,7 +201,7 @@ export class UserDataProvider { wallet.isDelegate = true; wallet.username = userName; - this.saveWallet(wallet, undefined, true); + this.updateWallet(wallet, this.currentProfile.profileId, true); } getWalletByAddress(address: string, profileId: string = this.authProvider.loggedProfileId): Wallet { @@ -219,6 +219,18 @@ export class UserDataProvider { return null; } + // Save only if wallet exists in profile + updateWallet(wallet: Wallet, profileId: string, notificate: boolean = false): Observable { + if (lodash.isUndefined(profileId)) { return; } + + const profile = this.getProfileById(profileId); + if (profile && profile.wallets[wallet.address]) { + return this.saveWallet(wallet, profileId, notificate); + } + + return Observable.empty(); + } + saveWallet(wallet: Wallet, profileId: string = this.authProvider.loggedProfileId, notificate: boolean = false) { if (lodash.isUndefined(profileId)) { return; } @@ -247,7 +259,7 @@ export class UserDataProvider { } wallet.label = label; - return this.saveWallet(wallet); + return this.updateWallet(wallet, this.currentProfile.profileId); } public getWalletLabel(walletOrAddress: Wallet | string, profileId?: string): string { @@ -283,10 +295,11 @@ export class UserDataProvider { .map(profiles => { // we have to create "real" contacts here, because the "address" property was not on the contact object // in the first versions of the app - return lodash.mapValues(profiles, profile => { - profile.contacts = lodash.transform(profile.contacts, UserDataProvider.mapContact, {}); - return profile; - }); + return lodash.mapValues(profiles, (profile, profileId) => ({ + ...profile, + profileId, + contacts: lodash.transform(profile.contacts, UserDataProvider.mapContact, {}) + })); }); }