FIX: Only send wallets to watch app if installed

This commit is contained in:
Marcos Rodriguez Vélez 2020-09-14 14:10:25 -04:00 committed by Overtorment
parent 84b78da934
commit ce0002a0f9

View file

@ -51,110 +51,113 @@ const WatchConnectivity = () => {
}; };
WatchConnectivity.sendWalletsToWatch = () => { WatchConnectivity.sendWalletsToWatch = () => {
const BlueApp = require('./BlueApp'); getIsWatchAppInstalled().then(installed => {
const allWallets = BlueApp.getWallets(); if (!installed) return;
if (!Array.isArray(allWallets)) {
console.log('No Wallets set to sync with Watch app. Exiting...');
return;
} else if (allWallets.length === 0) {
console.log('Wallets array is set. No Wallets set to sync with Watch app. Exiting...');
return;
}
return InteractionManager.runAfterInteractions(async () => {
const BlueApp = require('./BlueApp'); const BlueApp = require('./BlueApp');
const wallets = []; const allWallets = BlueApp.getWallets();
if (!Array.isArray(allWallets)) {
console.log('No Wallets set to sync with Watch app. Exiting...');
return;
} else if (allWallets.length === 0) {
console.log('Wallets array is set. No Wallets set to sync with Watch app. Exiting...');
return;
}
for (const wallet of allWallets) { return InteractionManager.runAfterInteractions(async () => {
let receiveAddress; const BlueApp = require('./BlueApp');
if (wallet.getAddressAsync) { const wallets = [];
if (wallet.chain === Chain.ONCHAIN) {
try {
receiveAddress = await wallet.getAddressAsync();
} catch (_) {}
if (!receiveAddress) {
// either sleep expired or getAddressAsync threw an exception
receiveAddress = wallet._getExternalAddressByIndex(wallet.next_free_address_index);
}
} else if (wallet.chain === Chain.OFFCHAIN) {
try {
await wallet.getAddressAsync();
receiveAddress = wallet.getAddress();
} catch (_) {}
if (!receiveAddress) {
// either sleep expired or getAddressAsync threw an exception
receiveAddress = wallet.getAddress();
}
}
}
const transactions = wallet.getTransactions(10);
const watchTransactions = [];
for (const transaction of transactions) {
let type = 'pendingConfirmation';
let memo = '';
let amount = 0;
if ('confirmations' in transaction && !(transaction.confirmations > 0)) { for (const wallet of allWallets) {
type = 'pendingConfirmation'; let receiveAddress;
} else if (transaction.type === 'user_invoice' || transaction.type === 'payment_request') { if (wallet.getAddressAsync) {
const currentDate = new Date(); if (wallet.chain === Chain.ONCHAIN) {
const now = (currentDate.getTime() / 1000) | 0; try {
const invoiceExpiration = transaction.timestamp + transaction.expire_time; receiveAddress = await wallet.getAddressAsync();
} catch (_) {}
if (invoiceExpiration > now) { if (!receiveAddress) {
type = 'pendingConfirmation'; // either sleep expired or getAddressAsync threw an exception
} else if (invoiceExpiration < now) { receiveAddress = wallet._getExternalAddressByIndex(wallet.next_free_address_index);
if (transaction.ispaid) { }
type = 'received'; } else if (wallet.chain === Chain.OFFCHAIN) {
} else { try {
type = 'sent'; await wallet.getAddressAsync();
receiveAddress = wallet.getAddress();
} catch (_) {}
if (!receiveAddress) {
// either sleep expired or getAddressAsync threw an exception
receiveAddress = wallet.getAddress();
} }
} }
} else if (transaction.value / 100000000 < 0) {
type = 'sent';
} else {
type = 'received';
} }
if (transaction.type === 'user_invoice' || transaction.type === 'payment_request') { const transactions = wallet.getTransactions(10);
amount = isNaN(transaction.value) ? '0' : amount; const watchTransactions = [];
const currentDate = new Date(); for (const transaction of transactions) {
const now = (currentDate.getTime() / 1000) | 0; let type = 'pendingConfirmation';
const invoiceExpiration = transaction.timestamp + transaction.expire_time; let memo = '';
let amount = 0;
if (invoiceExpiration > now) { if ('confirmations' in transaction && !(transaction.confirmations > 0)) {
amount = formatBalance(transaction.value, wallet.getPreferredBalanceUnit(), true).toString(); type = 'pendingConfirmation';
} else if (invoiceExpiration < now) { } else if (transaction.type === 'user_invoice' || transaction.type === 'payment_request') {
if (transaction.ispaid) { const currentDate = new Date();
const now = (currentDate.getTime() / 1000) | 0;
const invoiceExpiration = transaction.timestamp + transaction.expire_time;
if (invoiceExpiration > now) {
type = 'pendingConfirmation';
} else if (invoiceExpiration < now) {
if (transaction.ispaid) {
type = 'received';
} else {
type = 'sent';
}
}
} else if (transaction.value / 100000000 < 0) {
type = 'sent';
} else {
type = 'received';
}
if (transaction.type === 'user_invoice' || transaction.type === 'payment_request') {
amount = isNaN(transaction.value) ? '0' : amount;
const currentDate = new Date();
const now = (currentDate.getTime() / 1000) | 0;
const invoiceExpiration = transaction.timestamp + transaction.expire_time;
if (invoiceExpiration > now) {
amount = formatBalance(transaction.value, wallet.getPreferredBalanceUnit(), true).toString(); amount = formatBalance(transaction.value, wallet.getPreferredBalanceUnit(), true).toString();
} else if (invoiceExpiration < now) {
if (transaction.ispaid) {
amount = formatBalance(transaction.value, wallet.getPreferredBalanceUnit(), true).toString();
} else {
amount = loc.lnd.expired;
}
} else { } else {
amount = loc.lnd.expired; amount = formatBalance(transaction.value, wallet.getPreferredBalanceUnit(), true).toString();
} }
} else { } else {
amount = formatBalance(transaction.value, wallet.getPreferredBalanceUnit(), true).toString(); amount = formatBalance(transaction.value, wallet.getPreferredBalanceUnit(), true).toString();
} }
} else { if (BlueApp.tx_metadata[transaction.hash] && BlueApp.tx_metadata[transaction.hash].memo) {
amount = formatBalance(transaction.value, wallet.getPreferredBalanceUnit(), true).toString(); memo = BlueApp.tx_metadata[transaction.hash].memo;
} else if (transaction.memo) {
memo = transaction.memo;
}
const watchTX = { type, amount, memo, time: transactionTimeToReadable(transaction.received) };
watchTransactions.push(watchTX);
} }
if (BlueApp.tx_metadata[transaction.hash] && BlueApp.tx_metadata[transaction.hash].memo) { wallets.push({
memo = BlueApp.tx_metadata[transaction.hash].memo; label: wallet.getLabel(),
} else if (transaction.memo) { balance: formatBalance(Number(wallet.getBalance()), wallet.getPreferredBalanceUnit(), true),
memo = transaction.memo; type: wallet.type,
} preferredBalanceUnit: wallet.getPreferredBalanceUnit(),
const watchTX = { type, amount, memo, time: transactionTimeToReadable(transaction.received) }; receiveAddress: receiveAddress,
watchTransactions.push(watchTX); transactions: watchTransactions,
xpub: wallet.getXpub() ? wallet.getXpub() : wallet.getSecret(),
});
} }
wallets.push({ updateApplicationContext({ wallets, randomID: Math.floor(Math.random() * 11) });
label: wallet.getLabel(), return { wallets };
balance: formatBalance(Number(wallet.getBalance()), wallet.getPreferredBalanceUnit(), true), });
type: wallet.type,
preferredBalanceUnit: wallet.getPreferredBalanceUnit(),
receiveAddress: receiveAddress,
transactions: watchTransactions,
xpub: wallet.getXpub() ? wallet.getXpub() : wallet.getSecret(),
});
}
updateApplicationContext({ wallets, randomID: Math.floor(Math.random() * 11) });
return { wallets };
}); });
}; };
export default WatchConnectivity; export default WatchConnectivity;