Skip to content

Commit

Permalink
Mark existing wallets as synced
Browse files Browse the repository at this point in the history
  • Loading branch information
neuodev committed Oct 15, 2021
1 parent 254fd52 commit 56ae3cd
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 24 deletions.
3 changes: 1 addition & 2 deletions packages/yoroi-extension/app/containers/wallet/Wallet.js
Expand Up @@ -30,7 +30,6 @@ import NavBarTitle from '../../components/topbar/NavBarTitle';
import SubMenu from '../../components/topbar/SubMenu';
import type { GeneratedData as NavBarContainerRevampData } from '../NavBarContainerRevamp';
import WalletSyncingOverlay from '../../components/wallet/syncingOverlay/WalletSyncingOverlay';
import { isErgo } from '../../api/ada/lib/storage/database/prepackaged/networks';

export type GeneratedData = typeof Wallet.prototype.generated;

Expand Down Expand Up @@ -183,7 +182,7 @@ class Wallet extends Component<AllProps> {
}))}
>
{this.props.children}
{isErgo(selectedWallet.getParent().getNetworkInfo()) && this.renderOverlay()}
{this.renderOverlay()}
</WalletWithNavigation>
</TopBarLayout>
);
Expand Down
Expand Up @@ -29,6 +29,7 @@ import {
} from './WalletStore';
import type { ActionsMap } from '../../actions/index';
import type { StoresMap } from '../index';
import { removeWalletFromLS } from '../../utils/localStorage';

export type PublicDeriverSettingsCache = {|
publicDeriver: PublicDeriver<>,
Expand Down Expand Up @@ -228,6 +229,7 @@ export default class WalletSettingsStore extends Store<StoresMap, ActionsMap> {
if (group == null) {
throw new Error(`${nameof(this._removeWallet)} wallet doesn't belong to group`);
}
await removeWalletFromLS(request.publicDeriver)
await this.removeWalletRequest.execute({
publicDeriver: request.publicDeriver,
conceptualWallet: group.publicDerivers.length === 1
Expand Down
10 changes: 3 additions & 7 deletions packages/yoroi-extension/app/stores/toplevel/WalletStore.js
Expand Up @@ -35,7 +35,7 @@ import { getApiForNetwork } from '../../api/common/utils';
import { Bip44Wallet } from '../../api/ada/lib/storage/models/Bip44Wallet/wrapper';
import type { ActionsMap } from '../../actions/index';
import type { StoresMap } from '../index';
import { getCurrentWalletFromLS, updateSyncedWallets } from '../../utils/localStorage';
import { getCurrentWalletFromLS, markExistingWalletsAsSynced, updateSyncedWallets } from '../../utils/localStorage';

type GroupedWallets = {|
publicDerivers: Array<PublicDeriver<>>,
Expand Down Expand Up @@ -215,12 +215,8 @@ export default class WalletStore extends Store<StoresMap, ActionsMap> {

refreshWalletFromRemote: (PublicDeriver<>) => Promise<void> = async publicDeriver => {
try {
/**
* Check if the wallet is syncing for the first time then mark it as synced
* if the wallet was not stored or was stored and is not done syncing
* -> show the overlay that pervent user from performing any transaction
*/
const wallet = await getCurrentWalletFromLS(publicDeriver)
await markExistingWalletsAsSynced(this.stores.wallets.publicDerivers)
const wallet = await getCurrentWalletFromLS(publicDeriver);
if (!wallet || !wallet.isSynced) {
runInAction(() => {
this.firstSync = true
Expand Down
70 changes: 55 additions & 15 deletions packages/yoroi-extension/app/utils/localStorage.js
Expand Up @@ -5,19 +5,23 @@ import { asGetPublicKey } from '../../app/api/ada/lib/storage/models/PublicDeriv
type SyncedWallet = {|
publicKey: string,
isSynced: boolean,
publicDeriverId: number,
|}

type StoredWallets = {|
wallets: SyncedWallet[],
isFirstTime: boolean,
|}

export const SYNCED_WALLETS = 'wallets/synced'
const getSyncedWallets = (): SyncedWallet[] => {
return JSON.parse(localStorage.getItem(SYNCED_WALLETS) || '[]' )
}
const SYNCED_WALLETS = 'wallets/synced'
const SYNCED_WALLETS_DEFAULT = JSON.stringify({
wallets: [],
// When we push this into production we want to take existing wallets, store them and mark them as synced.
isFirstTime: true,
});

export const getCurrentWalletFromLS = async (publicDeriver: PublicDeriver<>): Promise<void | SyncedWallet> => {
const withPubKey = asGetPublicKey(publicDeriver);
if (withPubKey == null) return
const publicKey = await withPubKey.getPublicKey();
console.log({publicKey: publicKey.Hash})
return getSyncedWallets().find(wallet => wallet.publicKey === publicKey.Hash)
const getSyncedWallets = (): StoredWallets => {
return JSON.parse(localStorage.getItem(SYNCED_WALLETS) || SYNCED_WALLETS_DEFAULT)
}

const createSyncedWalletObj = async (publicDeriver: PublicDeriver<>): Promise<SyncedWallet> => {
Expand All @@ -29,18 +33,54 @@ const createSyncedWalletObj = async (publicDeriver: PublicDeriver<>): Promise<Sy

return {
publicKey: publicKey.Hash,
isSynced: true
isSynced: true,
publicDeriverId: publicDeriver.publicDeriverId
}
}

export const getCurrentWalletFromLS = async (publicDeriver: PublicDeriver<>): Promise<void | SyncedWallet> => {
const syncedWallet = await createSyncedWalletObj(publicDeriver)
return getSyncedWallets().wallets.find(wallet => compareWallets(wallet, syncedWallet))
}


export const removeWalletFromLS = async (publicDeriver: PublicDeriver<>): Promise<void> => {
const syncedWallet = await createSyncedWalletObj(publicDeriver)
const syncedWallets = getSyncedWallets()
const filteredWallets = syncedWallets.wallets.filter(wallet => !compareWallets(wallet, syncedWallet))
localStorage.setItem(SYNCED_WALLETS, JSON.stringify({ isFirstTime: syncedWallets.isFirstTime, wallets: filteredWallets }))
}

export const updateSyncedWallets = async (publicDeriver: PublicDeriver<>): Promise<void> => {
const syncedWallet = await createSyncedWalletObj(publicDeriver);
const wallets = getSyncedWallets()
const wallet = wallets.find(wallet => wallet.publicKey === syncedWallet.publicKey)
const syncedWallets = getSyncedWallets()
const wallet = syncedWallets.wallets.find(wallet => compareWallets(wallet, syncedWallet))
if (wallet) {
wallet.isSynced = true
} else {
wallets.push(syncedWallet)
syncedWallets.wallets.push(syncedWallet)
}
localStorage.setItem(SYNCED_WALLETS, JSON.stringify(syncedWallets))
}

/**
* Take all the exsiting wallets, store them and mark them as synced.
* This is nessary as when we push this into production every wallet will see the syncing overlay
* Due to empty localstorage.
* to avoid this we are using the `isFirstTime` flage to store these wallet.
*/
export const markExistingWalletsAsSynced = async (publicDerivers: PublicDeriver<>[]): Promise<void> => {
const syncedWallets = getSyncedWallets()
if (!syncedWallets.isFirstTime) return
for (let publicDeriver of publicDerivers) {
const syncedWallet = await createSyncedWalletObj(publicDeriver)
syncedWallets.wallets.push(syncedWallet)
}
localStorage.setItem(SYNCED_WALLETS, JSON.stringify(wallets))
syncedWallets.isFirstTime = false
localStorage.setItem(SYNCED_WALLETS, JSON.stringify(syncedWallets))
}

// Utils
function compareWallets (first: SyncedWallet, second: SyncedWallet) {
return (first.publicKey === second.publicKey) && (first.publicDeriverId === second.publicDeriverId)
}

0 comments on commit 56ae3cd

Please sign in to comment.