BlueWallet/components/WatchConnectivity.ios.js

194 lines
7.7 KiB
JavaScript
Raw Normal View History

2024-11-08 21:33:35 -04:00
import { useCallback, useEffect, useRef } from 'react';
import {
transferCurrentComplicationUserInfo,
2023-10-30 12:38:12 -04:00
transferUserInfo,
2024-05-20 10:54:13 +01:00
updateApplicationContext,
useInstalled,
2024-11-08 21:33:35 -04:00
usePaired,
2024-05-20 10:54:13 +01:00
useReachability,
watchEvents,
} from 'react-native-watch-connectivity';
2024-05-18 11:36:16 -04:00
import Notifications from '../blue_modules/notifications';
import { MultisigHDWallet } from '../class';
2024-05-20 10:54:13 +01:00
import loc, { formatBalance, transactionTimeToReadable } from '../loc';
import { Chain } from '../models/bitcoinUnits';
import { FiatUnit } from '../models/fiatUnit';
2024-05-31 13:18:01 -04:00
import { useSettings } from '../hooks/context/useSettings';
import { useStorage } from '../hooks/context/useStorage';
2020-07-18 20:33:43 +01:00
2020-10-12 12:36:35 -04:00
function WatchConnectivity() {
const { walletsInitialized, wallets, fetchWalletTransactions, saveToDisk, txMetadata } = useStorage();
2024-04-17 21:05:48 -04:00
const { preferredFiatCurrency } = useSettings();
const isReachable = useReachability();
2024-11-08 21:33:35 -04:00
const isInstalled = useInstalled();
const isPaired = usePaired();
const messagesListenerActive = useRef(false);
const lastPreferredCurrency = useRef(FiatUnit.USD.endPointKey);
useEffect(() => {
2024-11-08 21:33:35 -04:00
if (!isInstalled || !isPaired || !walletsInitialized || !isReachable) {
console.debug('Apple Watch not installed, not paired, or other conditions not met. Exiting message listener setup.');
return;
}
2024-11-08 21:33:35 -04:00
const messagesListener = watchEvents.addListener('message', handleMessages);
messagesListenerActive.current = true;
return () => {
messagesListener();
messagesListenerActive.current = false;
};
// eslint-disable-next-line react-hooks/exhaustive-deps
2024-11-08 21:33:35 -04:00
}, [walletsInitialized, isReachable, isInstalled, isPaired]);
2021-02-24 19:52:55 -05:00
useEffect(() => {
2024-11-08 21:33:35 -04:00
if (!isInstalled || !isPaired || !walletsInitialized) {
console.debug('Apple Watch not installed, not paired, or wallets not initialized. Skipping wallet data transfer.');
return;
}
(async () => {
try {
const walletsToProcess = await constructWalletsToSendToWatch();
2023-10-30 12:38:12 -04:00
if (walletsToProcess) {
if (isReachable) {
transferUserInfo(walletsToProcess);
2024-11-08 21:33:35 -04:00
console.debug('Apple Watch: sent info to watch transferUserInfo');
2023-10-30 12:38:12 -04:00
} else {
updateApplicationContext(walletsToProcess);
2024-11-08 21:33:35 -04:00
console.debug('Apple Watch: sent info to watch context');
2023-10-30 12:38:12 -04:00
}
}
2024-11-08 21:33:35 -04:00
} catch (error) {
console.debug('Failed to send wallets to watch:', error);
}
})();
}, [walletsInitialized, wallets, isReachable, isInstalled, isPaired, constructWalletsToSendToWatch]);
useEffect(() => {
2024-11-08 21:33:35 -04:00
if (!isInstalled || !isPaired || !walletsInitialized || !isReachable) {
console.debug('Apple Watch not installed, not paired, or other conditions not met. Skipping application context update.');
return;
}
2024-11-08 21:33:35 -04:00
updateApplicationContext({ isWalletsInitialized: walletsInitialized, randomID: Math.floor(Math.random() * 11) });
}, [isReachable, walletsInitialized, isInstalled, isPaired]);
useEffect(() => {
2024-11-08 21:33:35 -04:00
if (!isInstalled || !isPaired || !walletsInitialized || !isReachable || !preferredFiatCurrency) {
console.debug('Apple Watch not installed, not paired, or other conditions not met. Skipping preferred fiat currency update.');
return;
}
const preferredFiatCurrencyParsed = preferredFiatCurrency ?? FiatUnit.USD;
if (lastPreferredCurrency.current !== preferredFiatCurrencyParsed.endPointKey) {
try {
2024-11-08 21:33:35 -04:00
transferCurrentComplicationUserInfo(
{
preferredFiatCurrency: preferredFiatCurrencyParsed.endPointKey,
2024-11-08 21:33:35 -04:00
},
[wallets, walletsInitialized, txMetadata],
);
lastPreferredCurrency.current = preferredFiatCurrencyParsed.endPointKey;
console.debug('Apple Watch: updated preferred fiat currency');
} catch (error) {
console.debug('Error updating preferredFiatCurrency on watch:', error);
}
2024-11-08 21:33:35 -04:00
} else {
console.debug('WatchConnectivity lastPreferredCurrency has not changed');
}
2024-11-08 21:33:35 -04:00
}, [preferredFiatCurrency, walletsInitialized, isReachable, isInstalled, isPaired, txMetadata, wallets]);
const handleMessages = async (message, reply) => {
try {
if (message.request === 'createInvoice') {
const createInvoiceRequest = await handleLightningInvoiceCreateRequest(message.walletIndex, message.amount, message.description);
reply({ invoicePaymentRequest: createInvoiceRequest });
} else if (message.message === 'sendApplicationContext') {
const walletsToProcess = await constructWalletsToSendToWatch();
if (walletsToProcess) updateApplicationContext(walletsToProcess);
} else if (message.message === 'fetchTransactions') {
await fetchWalletTransactions();
await saveToDisk();
reply({});
} else if (message.message === 'hideBalance') {
wallets[message.walletIndex].hideBalance = message.hideBalance;
await saveToDisk();
reply({});
}
} catch (error) {
console.debug('Error handling message:', error);
reply({});
}
};
2019-05-02 16:33:03 -04:00
const handleLightningInvoiceCreateRequest = async (walletIndex, amount, description = loc.lnd.placeholder) => {
const wallet = wallets[walletIndex];
if (wallet.allowReceive() && amount > 0) {
2019-05-02 16:33:03 -04:00
try {
const invoiceRequest = await wallet.addInvoice(amount, description);
2020-07-18 20:33:43 +01:00
try {
if (await Notifications.isNotificationsEnabled()) {
const decoded = await wallet.decodeInvoice(invoiceRequest);
Notifications.majorTomToGroundControl([], [decoded.payment_hash], []);
}
2024-11-08 21:33:35 -04:00
} catch (notificationError) {
console.debug('WatchConnectivity - Notification error or running in Simulator');
console.debug(notificationError);
}
2019-05-02 16:33:03 -04:00
return invoiceRequest;
2024-11-08 21:33:35 -04:00
} catch (invoiceError) {
console.debug('Error creating invoice:', invoiceError);
2019-05-02 16:33:03 -04:00
}
}
};
2019-05-02 16:33:03 -04:00
2024-11-08 21:33:35 -04:00
const constructWalletsToSendToWatch = useCallback(async () => {
if (!Array.isArray(wallets) || !walletsInitialized) return;
2024-11-08 21:33:35 -04:00
const walletsToProcess = await Promise.all(
wallets.map(async wallet => {
let receiveAddress;
try {
2024-11-08 21:33:35 -04:00
receiveAddress = wallet.chain === Chain.ONCHAIN ? await wallet.getAddressAsync() : wallet.getAddress();
} catch {
receiveAddress =
wallet.chain === Chain.ONCHAIN ? wallet._getExternalAddressByIndex(wallet.next_free_address_index) : wallet.getAddress();
}
2024-11-08 21:33:35 -04:00
const transactions = wallet.getTransactions(10).map(transaction => {
const type = transaction.confirmations ? 'pendingConfirmation' : 'received';
const amount = formatBalance(transaction.value, wallet.getPreferredBalanceUnit(), true).toString();
const memo = txMetadata[transaction.hash]?.memo || transaction.memo || '';
const time = transactionTimeToReadable(transaction.received);
return { type, amount, memo, time };
});
return {
label: wallet.getLabel(),
balance: formatBalance(Number(wallet.getBalance()), wallet.getPreferredBalanceUnit(), true),
type: wallet.type,
preferredBalanceUnit: wallet.getPreferredBalanceUnit(),
receiveAddress,
transactions,
hideBalance: wallet.hideBalance,
...(wallet.chain === Chain.ONCHAIN &&
wallet.type !== MultisigHDWallet.type && {
xpub: wallet.getXpub() || wallet.getSecret(),
}),
...(wallet.allowBIP47() && wallet.isBIP47Enabled() && { paymentCode: wallet.getBIP47PaymentCode() }),
};
}),
);
console.debug('Constructed wallets to process for Apple Watch');
2023-10-30 12:38:12 -04:00
return { wallets: walletsToProcess, randomID: Math.floor(Math.random() * 11) };
2024-11-08 21:33:35 -04:00
}, [wallets, walletsInitialized, txMetadata]);
2024-11-08 21:33:35 -04:00
return null;
}
2020-10-12 12:36:35 -04:00
export default WatchConnectivity;