Skip to content

Commit

Permalink
[BTC] avoid serializing promise instead of string for addresses (#6483)
Browse files Browse the repository at this point in the history
fix(btc): serialize functions do not return promise

Signed-off-by: Stéphane Prohaszka <stephane.prohaszka@ledger.fr>
  • Loading branch information
sprohaszka-ledger authored Mar 20, 2024
1 parent e1eac16 commit f50bb75
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions libs/coin-modules/coin-bitcoin/src/wallet-btc/storage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class BitcoinLikeStorage implements IStorage {
// accounting
unspentUtxos: { [key: string]: Output[] } = {};

addressCache: Record<string, Promise<string>> = {};
addressCache: Record<string, string> = {};

// only needed to handle the case when the input
// is seen before the output (typically explorer
Expand Down Expand Up @@ -213,17 +213,17 @@ class BitcoinLikeStorage implements IStorage {
if (!this.addressCache) {
this.addressCache = {};
}
this.addressCache[key] = Promise.resolve(address);
this.addressCache[key] = address;
}

exportSync(): { txs: TX[]; addressCache: Record<string, Promise<string>> } {
exportSync(): { txs: TX[]; addressCache: Record<string, string> } {
return {
txs: this.txs,
addressCache: this.addressCache,
};
}

loadSync(data: { txs: TX[]; addressCache: Record<string, Promise<string>> }) {
loadSync(data: { txs: TX[]; addressCache: Record<string, string> }) {
this.txs = [];
this.primaryIndex = {};
this.accountIndex = {};
Expand All @@ -237,23 +237,35 @@ class BitcoinLikeStorage implements IStorage {
});
this.appendTxs(data.txs);
this.addressCache = data.addressCache;
Base.addressCache = { ...Base.addressCache, ...this.addressCache };
const addressCacheConverted: Record<string, Promise<string>> = Object.keys(
this.addressCache,
).reduce(
(acc, key) => ({
...acc,
[key]: Promise.resolve(this.addressCache[key]),
}),
{},
);
Base.addressCache = {
...Base.addressCache,
...addressCacheConverted,
};
}

/**
* export the storage data(txs, UTXOs, index...) to app.json
*/
async export(): Promise<{
txs: TX[];
addressCache: Record<string, Promise<string>>;
addressCache: Record<string, string>;
}> {
return this.exportSync();
}

/**
* load account data(txs, UTXOs, index...) from app.json
*/
async load(data: { txs: TX[]; addressCache: Record<string, Promise<string>> }): Promise<void> {
async load(data: { txs: TX[]; addressCache: Record<string, string> }): Promise<void> {
return this.loadSync(data);
}

Expand Down

0 comments on commit f50bb75

Please sign in to comment.