Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demo version fix backup problems #1192

Open
wants to merge 5 commits into
base: upgrade-ui
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion html/Web.bundle/site/fallback-2cbe8504a1de420e3a98.js

This file was deleted.

1 change: 1 addition & 0 deletions html/Web.bundle/site/fallback-9c912c3ba3e1bf2f7289.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion html/Web.bundle/site/index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!doctype html><html lang="en"><head><meta charset="utf-8"><title>SubWallet</title><script inline inline-asset="fallback.js$" inline-asset-delete></script><script inline inline-asset="web-runner.js$" inline-asset-delete></script><script defer="defer" src="fallback-2cbe8504a1de420e3a98.js"></script><script defer="defer" src="web-runner-18f419ea5dc631a81afe.js"></script></head><body><div id="root">SubWallet</div></body></html>
<!doctype html><html lang="en"><head><meta charset="utf-8"><title>SubWallet</title><script inline inline-asset="fallback.js$" inline-asset-delete></script><script inline inline-asset="web-runner.js$" inline-asset-delete></script><script defer="defer" src="fallback-9c912c3ba3e1bf2f7289.js"></script><script defer="defer" src="web-runner-aeb1d5362fd7b5ee4d09.js"></script></head><body><div id="root">SubWallet</div></body></html>

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions src/messaging/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ import {
import { _ChainAsset, _ChainInfo } from '@subwallet/chain-list/types';
import { AuthUrls } from '@subwallet/extension-base/services/request-service/types';
import { _getKnownHashes } from 'utils/defaultChains';
import { backupStorageData, needBackup } from 'utils/storage';
import { addLazy } from '@subwallet/extension-base/utils/lazy';
import { Alert } from 'react-native';

interface Handler {
resolve: (data: any) => void;
Expand Down Expand Up @@ -308,6 +311,19 @@ export const postMessage = ({ id, message, request, origin }, supportRestart = f
const _post = () => {
const injection = 'window.postMessage(' + JSON.stringify({ id, message, request, origin }) + ')';
webviewRef.current?.injectJavaScript(injection);

if (needBackup(message)) {
addLazy(
'backupStorageData',
() => {
Alert.alert(`*** Backup storage after ${message}`);
backupStorageData();
},
3000,
9000,
false,
);
}
};

if (!webviewRef || !webviewEvents) {
Expand Down
21 changes: 6 additions & 15 deletions src/providers/WebRunnerProvider/WebRunner.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Create web view with solution suggested in https://medium0.com/@caphun/react-native-load-local-static-site-inside-webview-2b93eb1c4225
import { Alert, AppState, Linking, NativeSyntheticEvent, Platform, View } from 'react-native';
import EventEmitter from 'eventemitter3';
import React, { useEffect, useReducer } from 'react';
import React, { useReducer } from 'react';
import WebView from 'react-native-webview';
import { WebViewMessage } from 'react-native-webview/lib/WebViewTypes';
import { WebRunnerState, WebRunnerStatus } from 'providers/contexts';
Expand All @@ -12,9 +12,12 @@ import RNFS from 'react-native-fs';
import i18n from 'utils/i18n/i18n';
import VersionNumber from 'react-native-version-number';
import { getId } from '@subwallet/extension-base/utils/getId';
import { backupStorageData, mmkvStore, restoreStorageData } from 'utils/storage';
import { mmkvStore, restoreStorageData } from 'utils/storage';

const WEB_SERVER_PORT = 9135;
const WEB_SERVER_PORT = mmkvStore.getNumber('web-port') || 9135;
mmkvStore.set('web-port', WEB_SERVER_PORT + 1);

console.log('### Web Server Port:', WEB_SERVER_PORT);
const LONG_TIMEOUT = 300000; //5*60*1000
const ACCEPTABLE_RESPONSE_TIME = 30000;

Expand Down Expand Up @@ -209,8 +212,6 @@ class WebRunnerHandler {
}
this.shouldReloadHandler = true;
this.startPing();
// BACKUP-003: Back up local storage for reload web runner after 10 seconds
setTimeout(() => backupStorageData(), 10000);
} else {
this.stopPing();
}
Expand Down Expand Up @@ -368,14 +369,6 @@ export const WebRunner = React.memo(({ webRunnerRef, webRunnerStateRef, webRunne
eventEmitter: webRunnerEventEmitter,
});

useEffect(() => {
// BACKUP-003: Back up local storage every 30 seconds
const interval = setInterval(() => backupStorageData(), 30000);
return () => {
clearInterval(interval);
};
}, []);

webRunnerHandler.update(runnerGlobalState, dispatchRunnerGlobalState);
webRunnerHandler.active();

Expand Down Expand Up @@ -403,8 +396,6 @@ export const WebRunner = React.memo(({ webRunnerRef, webRunnerStateRef, webRunne
'window.ReactNativeWebView.postMessage(JSON.stringify({backupStorage: window.localStorage || ""}));',
);
}
// BACKUP-003: Back up local storage for reload web runner purpose
backupStorageData();
};

return (
Expand Down
88 changes: 88 additions & 0 deletions src/utils/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,94 @@ export const mmkvReduxStore: Storage = {
},
};

export const BACKUP_BLACKLIST: string[] = ['mobile(storage.backup)'];
export const BACKUP_WHITELIST: string[] = [
// Chain events
'pri(chainService.upsertChain)',
'pri(chainService.enableChains)',
'pri(chainService.enableChain)',
'pri(chainService.disableChains)',
'pri(chainService.disableChain)',
'pri(chainService.removeChain)',
'pri(chainService.deleteCustomAsset)',
'pri(chainService.upsertCustomAsset)',
'pri(chainService.resetDefaultChains)',
'pri(chainService.disableAllChains)',

// Assets events
'pri(assetSetting.update)',
// 'pri(mantaPay.enable)',
// 'pri(mantaPay.disable)',
// 'pri(mantaPay.initSyncMantaPay)',

// Account events
'pri(authorize.approveV2)',
'pri(authorize.changeSiteAll)',
'pri(authorize.changeSite)',
'pri(authorize.changeSitePerAccount)',
'pri(authorize.changeSitePerSite)',
'pri(authorize.changeSiteBlock)',
'pri(authorize.forgetSite)',
'pri(authorize.forgetAllSite)',
'pri(authorize.rejectV2)',
'pri(authorize.toggle)',
'pri(accounts.create.suriV2)',
'pri(accounts.create.externalV2)',
'pri(accounts.create.hardwareV2)',
'pri(accounts.create.hardwareMultiple)',
'pri(accounts.create.withSecret)',
'pri(accounts.forget)',
// 'pri(accounts.inject.add)',
// 'pri(accounts.inject.remove)',
'pri(derivation.createV2)',
'pri(json.restoreV2)',
'pri(json.batchRestoreV2)',
'pri(accounts.updateCurrentAddress)',
'pri(currentAccount.saveAddress)',
'pri(accounts.saveRecent)',
'pri(accounts.editContact)',
'pri(accounts.deleteContact)',

// Settings events
'pri(settings.changeBalancesVisibility)',
'pri(settings.saveAccountAllLogo)',
'pri(settings.saveTheme)',
'pri(settings.saveBrowserConfirmationType)',
'pri(settings.saveCamera)',
'pri(settings.saveAutoLockTime)',
'pri(settings.saveUnlockType)',
'pri(settings.saveEnableChainPatrol)',
'pri(settings.saveLanguage)',
'pri(settings.saveShowZeroBalance)',
'pri(settings.saveShowBalance)',

// Confirmation events
'pri(confirmations.complete)',

// Keyring events
'pri(keyring.change)',
'pri(keyring.migrate)',
'pri(keyring.reset)',
'pri(signing.approve.passwordV2)',
'pri(derivation.create.multiple)',
'pri(derivation.createV3)',
// 'pri(transactions.subscribe)',
// 'pri(notifications.subscribe)',
'pub(token.add)',

// WalletConnect events
'pri(walletConnect.session.approve)',
'pri(walletConnect.session.disconnect)',

// Others
'pri(campaign.banner.complete)',
];

export const needBackup = (message: string): boolean => {
console.debug('** needBackup:', message);
return BACKUP_WHITELIST.includes(message);
};

// Backup and restore data
export const backupStorageData = (forceBackup?: boolean) => {
mobileBackup()
Expand Down
Loading