2020-10-24 19:20:59 +02:00
|
|
|
import React, { createContext, useEffect, useState } from 'react';
|
2021-09-23 15:05:10 +02:00
|
|
|
import { Alert } from 'react-native';
|
|
|
|
import { useAsyncStorage } from '@react-native-async-storage/async-storage';
|
2021-01-30 04:45:24 +01:00
|
|
|
import { FiatUnit } from '../models/fiatUnit';
|
2021-09-23 15:05:10 +02:00
|
|
|
import Notifications from '../blue_modules/notifications';
|
2022-06-19 15:05:30 +02:00
|
|
|
import loc, { STORAGE_KEY as LOC_STORAGE_KEY } from '../loc';
|
2022-10-08 03:02:05 +02:00
|
|
|
import { LegacyWallet, WatchOnlyWallet } from '../class';
|
2021-10-04 08:02:33 +02:00
|
|
|
import alert from '../components/Alert';
|
2023-12-29 12:52:12 +01:00
|
|
|
import triggerHapticFeedback, { HapticFeedbackTypes } from './hapticFeedback';
|
2024-01-28 16:11:08 +01:00
|
|
|
import { PREFERRED_CURRENCY_STORAGE_KEY } from './currency';
|
2020-10-24 19:20:59 +02:00
|
|
|
const BlueApp = require('../BlueApp');
|
|
|
|
const BlueElectrum = require('./BlueElectrum');
|
2021-09-23 15:05:10 +02:00
|
|
|
const A = require('../blue_modules/analytics');
|
2020-10-24 19:20:59 +02:00
|
|
|
|
2021-01-01 20:15:40 +01:00
|
|
|
const _lastTimeTriedToRefetchWallet = {}; // hashmap of timestamps we _started_ refetching some wallet
|
|
|
|
|
2021-01-28 01:30:42 +01:00
|
|
|
export const WalletTransactionsStatus = { NONE: false, ALL: true };
|
2020-10-24 19:20:59 +02:00
|
|
|
export const BlueStorageContext = createContext();
|
|
|
|
export const BlueStorageProvider = ({ children }) => {
|
|
|
|
const [wallets, setWallets] = useState([]);
|
2024-01-05 09:03:47 +01:00
|
|
|
const [selectedWalletID, setSelectedWalletID] = useState();
|
2021-01-28 01:30:42 +01:00
|
|
|
const [walletTransactionUpdateStatus, setWalletTransactionUpdateStatus] = useState(WalletTransactionsStatus.NONE);
|
2020-10-24 19:20:59 +02:00
|
|
|
const [walletsInitialized, setWalletsInitialized] = useState(false);
|
2021-01-30 04:45:24 +01:00
|
|
|
const [preferredFiatCurrency, _setPreferredFiatCurrency] = useState(FiatUnit.USD);
|
2020-12-13 10:52:54 +01:00
|
|
|
const [language, _setLanguage] = useState();
|
2024-01-28 16:11:08 +01:00
|
|
|
const getPreferredCurrencyAsyncStorage = useAsyncStorage(PREFERRED_CURRENCY_STORAGE_KEY).getItem;
|
2022-06-19 15:05:30 +02:00
|
|
|
const getLanguageAsyncStorage = useAsyncStorage(LOC_STORAGE_KEY).getItem;
|
2021-01-19 04:40:11 +01:00
|
|
|
const [isHandOffUseEnabled, setIsHandOffUseEnabled] = useState(false);
|
2021-08-24 07:00:57 +02:00
|
|
|
const [isElectrumDisabled, setIsElectrumDisabled] = useState(true);
|
2021-09-25 17:04:45 +02:00
|
|
|
const [isPrivacyBlurEnabled, setIsPrivacyBlurEnabled] = useState(true);
|
2023-09-17 18:28:54 +02:00
|
|
|
const [currentSharedCosigner, setCurrentSharedCosigner] = useState('');
|
2021-08-24 07:00:57 +02:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
BlueElectrum.isDisabled().then(setIsElectrumDisabled);
|
|
|
|
}, []);
|
2021-01-31 06:02:48 +01:00
|
|
|
|
2021-09-25 17:04:45 +02:00
|
|
|
useEffect(() => {
|
|
|
|
console.log(`Privacy blur: ${isPrivacyBlurEnabled}`);
|
2021-09-25 19:09:34 +02:00
|
|
|
if (!isPrivacyBlurEnabled) {
|
2021-10-04 08:02:33 +02:00
|
|
|
alert('Privacy blur has been disabled.');
|
2021-09-25 19:09:34 +02:00
|
|
|
}
|
2021-09-25 17:04:45 +02:00
|
|
|
}, [isPrivacyBlurEnabled]);
|
|
|
|
|
2021-01-19 04:40:11 +01:00
|
|
|
const setIsHandOffUseEnabledAsyncStorage = value => {
|
|
|
|
setIsHandOffUseEnabled(value);
|
2021-05-18 22:38:18 +02:00
|
|
|
return BlueApp.setIsHandoffEnabled(value);
|
2021-01-19 04:40:11 +01:00
|
|
|
};
|
|
|
|
|
2021-09-07 21:30:07 +02:00
|
|
|
const saveToDisk = async (force = false) => {
|
|
|
|
if (BlueApp.getWallets().length === 0 && !force) {
|
|
|
|
console.log('not saving empty wallets array');
|
|
|
|
return;
|
|
|
|
}
|
2020-10-24 19:20:59 +02:00
|
|
|
BlueApp.tx_metadata = txMetadata;
|
|
|
|
await BlueApp.saveToDisk();
|
|
|
|
setWallets([...BlueApp.getWallets()]);
|
|
|
|
txMetadata = BlueApp.tx_metadata;
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setWallets(BlueApp.getWallets());
|
|
|
|
}, []);
|
|
|
|
|
2021-01-19 04:40:11 +01:00
|
|
|
useEffect(() => {
|
|
|
|
(async () => {
|
|
|
|
try {
|
2021-05-18 22:38:18 +02:00
|
|
|
const enabledHandoff = await BlueApp.isHandoffEnabled();
|
2021-01-19 04:40:11 +01:00
|
|
|
setIsHandOffUseEnabled(!!enabledHandoff);
|
|
|
|
} catch (_e) {
|
|
|
|
setIsHandOffUseEnabledAsyncStorage(false);
|
|
|
|
setIsHandOffUseEnabled(false);
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
}, []);
|
|
|
|
|
2020-12-13 02:33:42 +01:00
|
|
|
const getPreferredCurrency = async () => {
|
2024-01-27 14:05:16 +01:00
|
|
|
const item = JSON.parse(await getPreferredCurrencyAsyncStorage()) ?? FiatUnit.USD;
|
2020-12-13 02:33:42 +01:00
|
|
|
_setPreferredFiatCurrency(item);
|
2023-11-13 01:53:43 +01:00
|
|
|
return item;
|
2020-12-13 02:33:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const setPreferredFiatCurrency = () => {
|
|
|
|
getPreferredCurrency();
|
|
|
|
};
|
|
|
|
|
2020-12-13 10:52:54 +01:00
|
|
|
const getLanguage = async () => {
|
|
|
|
const item = await getLanguageAsyncStorage();
|
|
|
|
_setLanguage(item);
|
|
|
|
};
|
|
|
|
|
|
|
|
const setLanguage = () => {
|
|
|
|
getLanguage();
|
|
|
|
};
|
|
|
|
|
2020-12-13 02:33:42 +01:00
|
|
|
useEffect(() => {
|
|
|
|
getPreferredCurrency();
|
2020-12-13 10:52:54 +01:00
|
|
|
getLanguageAsyncStorage();
|
2020-12-13 02:33:42 +01:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, []);
|
|
|
|
|
2020-10-24 19:20:59 +02:00
|
|
|
const resetWallets = () => {
|
|
|
|
setWallets(BlueApp.getWallets());
|
|
|
|
};
|
|
|
|
|
2023-07-25 15:50:04 +02:00
|
|
|
const setWalletsWithNewOrder = wlts => {
|
|
|
|
BlueApp.wallets = wlts;
|
2020-10-24 19:20:59 +02:00
|
|
|
saveToDisk();
|
|
|
|
};
|
|
|
|
|
2021-01-28 01:30:42 +01:00
|
|
|
const refreshAllWalletTransactions = async (lastSnappedTo, showUpdateStatusIndicator = true) => {
|
2020-10-24 19:20:59 +02:00
|
|
|
let noErr = true;
|
|
|
|
try {
|
2024-01-20 23:16:36 +01:00
|
|
|
await BlueElectrum.waitTillConnected();
|
2021-01-28 01:30:42 +01:00
|
|
|
if (showUpdateStatusIndicator) {
|
|
|
|
setWalletTransactionUpdateStatus(WalletTransactionsStatus.ALL);
|
|
|
|
}
|
2022-12-12 03:34:50 +01:00
|
|
|
const paymentCodesStart = Date.now();
|
|
|
|
await fetchSenderPaymentCodes(lastSnappedTo);
|
|
|
|
const paymentCodesEnd = Date.now();
|
|
|
|
console.log('fetch payment codes took', (paymentCodesEnd - paymentCodesStart) / 1000, 'sec');
|
2020-10-24 19:20:59 +02:00
|
|
|
const balanceStart = +new Date();
|
2020-11-13 16:14:49 +01:00
|
|
|
await fetchWalletBalances(lastSnappedTo);
|
2020-10-24 19:20:59 +02:00
|
|
|
const balanceEnd = +new Date();
|
|
|
|
console.log('fetch balance took', (balanceEnd - balanceStart) / 1000, 'sec');
|
|
|
|
const start = +new Date();
|
2020-11-13 16:14:49 +01:00
|
|
|
await fetchWalletTransactions(lastSnappedTo);
|
2020-10-24 19:20:59 +02:00
|
|
|
const end = +new Date();
|
|
|
|
console.log('fetch tx took', (end - start) / 1000, 'sec');
|
|
|
|
} catch (err) {
|
|
|
|
noErr = false;
|
|
|
|
console.warn(err);
|
2021-01-28 01:30:42 +01:00
|
|
|
} finally {
|
|
|
|
setWalletTransactionUpdateStatus(WalletTransactionsStatus.NONE);
|
2020-10-24 19:20:59 +02:00
|
|
|
}
|
|
|
|
if (noErr) await saveToDisk(); // caching
|
|
|
|
};
|
|
|
|
|
|
|
|
const fetchAndSaveWalletTransactions = async walletID => {
|
|
|
|
const index = wallets.findIndex(wallet => wallet.getID() === walletID);
|
|
|
|
let noErr = true;
|
|
|
|
try {
|
2021-01-01 20:15:40 +01:00
|
|
|
// 5sec debounce:
|
|
|
|
if (+new Date() - _lastTimeTriedToRefetchWallet[walletID] < 5000) {
|
|
|
|
console.log('re-fetch wallet happens too fast; NOP');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_lastTimeTriedToRefetchWallet[walletID] = +new Date();
|
|
|
|
|
2020-10-24 19:20:59 +02:00
|
|
|
await BlueElectrum.waitTillConnected();
|
2024-01-20 23:16:36 +01:00
|
|
|
setWalletTransactionUpdateStatus(walletID);
|
2020-10-24 19:20:59 +02:00
|
|
|
const balanceStart = +new Date();
|
|
|
|
await fetchWalletBalances(index);
|
|
|
|
const balanceEnd = +new Date();
|
|
|
|
console.log('fetch balance took', (balanceEnd - balanceStart) / 1000, 'sec');
|
|
|
|
const start = +new Date();
|
|
|
|
await fetchWalletTransactions(index);
|
|
|
|
const end = +new Date();
|
|
|
|
console.log('fetch tx took', (end - start) / 1000, 'sec');
|
|
|
|
} catch (err) {
|
|
|
|
noErr = false;
|
|
|
|
console.warn(err);
|
2021-01-28 01:30:42 +01:00
|
|
|
} finally {
|
|
|
|
setWalletTransactionUpdateStatus(WalletTransactionsStatus.NONE);
|
2020-10-24 19:20:59 +02:00
|
|
|
}
|
|
|
|
if (noErr) await saveToDisk(); // caching
|
|
|
|
};
|
|
|
|
|
|
|
|
const addWallet = wallet => {
|
|
|
|
BlueApp.wallets.push(wallet);
|
|
|
|
setWallets([...BlueApp.getWallets()]);
|
|
|
|
};
|
|
|
|
|
|
|
|
const deleteWallet = wallet => {
|
|
|
|
BlueApp.deleteWallet(wallet);
|
|
|
|
setWallets([...BlueApp.getWallets()]);
|
|
|
|
};
|
|
|
|
|
2021-09-23 15:05:10 +02:00
|
|
|
const addAndSaveWallet = async w => {
|
|
|
|
if (wallets.some(i => i.getID() === w.getID())) {
|
2023-12-29 12:52:12 +01:00
|
|
|
triggerHapticFeedback(HapticFeedbackTypes.NotificationError);
|
2021-09-23 15:05:10 +02:00
|
|
|
Alert.alert('', 'This wallet has been previously imported.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const emptyWalletLabel = new LegacyWallet().getLabel();
|
2023-12-29 12:52:12 +01:00
|
|
|
triggerHapticFeedback(HapticFeedbackTypes.NotificationSuccess);
|
2021-09-23 15:05:10 +02:00
|
|
|
if (w.getLabel() === emptyWalletLabel) w.setLabel(loc.wallets.import_imported + ' ' + w.typeReadable);
|
|
|
|
w.setUserHasSavedExport(true);
|
|
|
|
addWallet(w);
|
|
|
|
await saveToDisk();
|
|
|
|
A(A.ENUM.CREATED_WALLET);
|
2022-10-08 03:02:05 +02:00
|
|
|
Alert.alert('', w.type === WatchOnlyWallet.type ? loc.wallets.import_success_watchonly : loc.wallets.import_success);
|
2021-09-23 15:05:10 +02:00
|
|
|
Notifications.majorTomToGroundControl(w.getAllExternalAddresses(), [], []);
|
|
|
|
// start balance fetching at the background
|
|
|
|
await w.fetchBalance();
|
|
|
|
setWallets([...BlueApp.getWallets()]);
|
|
|
|
};
|
|
|
|
|
2023-09-17 18:28:54 +02:00
|
|
|
const setSharedCosigner = cosigner => {
|
|
|
|
setCurrentSharedCosigner(cosigner);
|
|
|
|
};
|
|
|
|
|
2020-10-24 19:20:59 +02:00
|
|
|
let txMetadata = BlueApp.tx_metadata || {};
|
|
|
|
const getTransactions = BlueApp.getTransactions;
|
2023-03-04 16:30:51 +01:00
|
|
|
const isAdvancedModeEnabled = BlueApp.isAdvancedModeEnabled;
|
2020-10-24 19:20:59 +02:00
|
|
|
|
2022-12-12 03:34:50 +01:00
|
|
|
const fetchSenderPaymentCodes = BlueApp.fetchSenderPaymentCodes;
|
2020-10-24 19:20:59 +02:00
|
|
|
const fetchWalletBalances = BlueApp.fetchWalletBalances;
|
|
|
|
const fetchWalletTransactions = BlueApp.fetchWalletTransactions;
|
|
|
|
const getBalance = BlueApp.getBalance;
|
|
|
|
const isStorageEncrypted = BlueApp.storageIsEncrypted;
|
|
|
|
const startAndDecrypt = BlueApp.startAndDecrypt;
|
|
|
|
const encryptStorage = BlueApp.encryptStorage;
|
|
|
|
const sleep = BlueApp.sleep;
|
|
|
|
const setHodlHodlApiKey = BlueApp.setHodlHodlApiKey;
|
|
|
|
const getHodlHodlApiKey = BlueApp.getHodlHodlApiKey;
|
|
|
|
const createFakeStorage = BlueApp.createFakeStorage;
|
|
|
|
const decryptStorage = BlueApp.decryptStorage;
|
|
|
|
const isPasswordInUse = BlueApp.isPasswordInUse;
|
|
|
|
const cachedPassword = BlueApp.cachedPassword;
|
2023-03-18 13:31:27 +01:00
|
|
|
const setIsAdvancedModeEnabled = BlueApp.setIsAdvancedModeEnabled;
|
2020-10-24 19:20:59 +02:00
|
|
|
const getHodlHodlSignatureKey = BlueApp.getHodlHodlSignatureKey;
|
|
|
|
const addHodlHodlContract = BlueApp.addHodlHodlContract;
|
2021-05-07 17:55:17 +02:00
|
|
|
const setDoNotTrack = BlueApp.setDoNotTrack;
|
|
|
|
const isDoNotTrackEnabled = BlueApp.isDoNotTrackEnabled;
|
2020-10-24 19:20:59 +02:00
|
|
|
const getItem = BlueApp.getItem;
|
|
|
|
const setItem = BlueApp.setItem;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<BlueStorageContext.Provider
|
|
|
|
value={{
|
|
|
|
wallets,
|
|
|
|
setWalletsWithNewOrder,
|
|
|
|
txMetadata,
|
|
|
|
saveToDisk,
|
|
|
|
getTransactions,
|
2023-11-20 21:02:47 +01:00
|
|
|
selectedWalletID,
|
|
|
|
setSelectedWalletID,
|
2020-10-24 19:20:59 +02:00
|
|
|
addWallet,
|
|
|
|
deleteWallet,
|
2023-09-17 18:28:54 +02:00
|
|
|
currentSharedCosigner,
|
|
|
|
setSharedCosigner,
|
2021-09-23 15:05:10 +02:00
|
|
|
addAndSaveWallet,
|
2020-10-24 19:20:59 +02:00
|
|
|
setItem,
|
|
|
|
getItem,
|
2023-03-04 16:30:51 +01:00
|
|
|
isAdvancedModeEnabled,
|
2020-10-24 19:20:59 +02:00
|
|
|
fetchWalletBalances,
|
|
|
|
fetchWalletTransactions,
|
|
|
|
fetchAndSaveWalletTransactions,
|
|
|
|
isStorageEncrypted,
|
|
|
|
getHodlHodlSignatureKey,
|
|
|
|
encryptStorage,
|
|
|
|
startAndDecrypt,
|
|
|
|
cachedPassword,
|
|
|
|
addHodlHodlContract,
|
|
|
|
getBalance,
|
|
|
|
walletsInitialized,
|
|
|
|
setWalletsInitialized,
|
|
|
|
refreshAllWalletTransactions,
|
|
|
|
sleep,
|
|
|
|
setHodlHodlApiKey,
|
|
|
|
createFakeStorage,
|
|
|
|
resetWallets,
|
|
|
|
getHodlHodlApiKey,
|
|
|
|
decryptStorage,
|
|
|
|
isPasswordInUse,
|
2023-03-18 13:31:27 +01:00
|
|
|
setIsAdvancedModeEnabled,
|
2020-12-13 02:33:42 +01:00
|
|
|
setPreferredFiatCurrency,
|
|
|
|
preferredFiatCurrency,
|
2020-12-13 10:52:54 +01:00
|
|
|
setLanguage,
|
|
|
|
language,
|
2021-01-19 04:40:11 +01:00
|
|
|
isHandOffUseEnabled,
|
|
|
|
setIsHandOffUseEnabledAsyncStorage,
|
2021-01-28 01:30:42 +01:00
|
|
|
walletTransactionUpdateStatus,
|
|
|
|
setWalletTransactionUpdateStatus,
|
2021-05-07 17:55:17 +02:00
|
|
|
setDoNotTrack,
|
|
|
|
isDoNotTrackEnabled,
|
2021-08-24 07:00:57 +02:00
|
|
|
isElectrumDisabled,
|
|
|
|
setIsElectrumDisabled,
|
2021-09-25 17:04:45 +02:00
|
|
|
isPrivacyBlurEnabled,
|
|
|
|
setIsPrivacyBlurEnabled,
|
2020-10-24 19:20:59 +02:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</BlueStorageContext.Provider>
|
|
|
|
);
|
|
|
|
};
|