Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Commit

Permalink
fix: update the wallet in the correct profile (#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
luciorubeens committed Jun 5, 2019
1 parent ae5dc21 commit 50b0481
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/models/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export class Profile {
contacts: any = {};
name: string;
networkId: string;
profileId?: string;
wallets: any = {};

deserialize(input: any): Profile {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/wallet/wallet-dashboard/wallet-dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/wallet/wallet-list/wallet-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
2 changes: 1 addition & 1 deletion src/providers/local-notifications/local-notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
31 changes: 22 additions & 9 deletions src/providers/user-data/user-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -173,7 +173,7 @@ export class UserDataProvider {
// wallet.secondBip38 = this.forgeProvider.encryptBip38(secondWif, newPassword, this.currentNetwork);
}

this.saveWallet(wallet, profileId);
this.updateWallet(wallet, profileId);
}
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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<any> {
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; }

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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, {})
}));
});
}

Expand Down

0 comments on commit 50b0481

Please sign in to comment.