Skip to content
This repository has been archived by the owner on Jul 7, 2021. It is now read-only.

Commit

Permalink
fix(profiles): only persist and restore known wallet data (#652)
Browse files Browse the repository at this point in the history
  • Loading branch information
faustbrian committed Aug 10, 2020
1 parent f818912 commit 198083d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
10 changes: 8 additions & 2 deletions packages/platform-sdk-profiles/src/wallets/wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,14 @@ describe.each([123, 456, 789])("%s", (slip44) => {
});
expect(actual.network).toBe("devnet");
expect(actual.publicKey).toBe("034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192");
expect(actual.data).toBeObject();
expect(actual.data.key).toBe("value");
expect(actual.data).toEqual({
BALANCE: "55827093444556",
BROADCASTED_TRANSACTIONS: {},
DELEGATES: [],
EXCHANGE_RATE: 0,
SEQUENCE: "111932",
SIGNED_TRANSACTIONS: {},
});
expect(actual.settings).toBeObject();
expect(actual.settings.AVATAR).toBeString();
});
Expand Down
21 changes: 20 additions & 1 deletion packages/platform-sdk-profiles/src/wallets/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export class Wallet {
this.#dataRepository = new DataRepository();
this.#settingRepository = new SettingRepository(Object.values(WalletSetting));
this.#transactionService = new TransactionService(this);

this.restore();
}

/**
Expand Down Expand Up @@ -189,7 +191,14 @@ export class Wallet {
network: this.network().id,
address: this.address(),
publicKey: this.publicKey(),
data: this.data().all(),
data: {
[WalletData.Balance]: this.data().get(WalletData.Balance, BigNumber.ZERO)?.toFixed(),
[WalletData.BroadcastedTransactions]: this.data().get(WalletData.BroadcastedTransactions, []),
[WalletData.Delegates]: this.data().get(WalletData.Delegates, []),
[WalletData.ExchangeRate]: this.data().get(WalletData.ExchangeRate, 0),
[WalletData.Sequence]: this.data().get(WalletData.Sequence, BigNumber.ZERO)?.toFixed(),
[WalletData.SignedTransactions]: this.data().get(WalletData.SignedTransactions, []),
},
settings: this.settings().all(),
};
}
Expand Down Expand Up @@ -432,4 +441,14 @@ export class Wallet {

return response;
}

private restore(): void {
if (this.data().has(WalletData.Balance)) {
this.data().set(WalletData.Balance, BigNumber.make(this.data().get<string>(WalletData.Balance)!));
}

if (this.data().has(WalletData.Sequence)) {
this.data().set(WalletData.Sequence, BigNumber.make(this.data().get<string>(WalletData.Sequence)!));
}
}
}

0 comments on commit 198083d

Please sign in to comment.