BlueWallet/WatchConnectivity.ios.js

157 lines
6.1 KiB
JavaScript
Raw Normal View History

2019-10-21 17:13:16 +02:00
import * as Watch from 'react-native-watch-connectivity';
import { InteractionManager } from 'react-native';
2019-05-02 22:33:03 +02:00
const loc = require('./loc');
export default class WatchConnectivity {
isAppInstalled = false;
static shared = new WatchConnectivity();
wallets;
fetchTransactionsFunction = () => {};
2019-05-02 22:33:03 +02:00
constructor() {
this.getIsWatchAppInstalled();
2019-05-02 22:33:03 +02:00
}
getIsWatchAppInstalled() {
2019-10-21 17:13:16 +02:00
Watch.getIsWatchAppInstalled((err, isAppInstalled) => {
2019-05-02 22:33:03 +02:00
if (!err) {
WatchConnectivity.shared.isAppInstalled = isAppInstalled;
Watch.subscribeToWatchState((err, watchState) => {
if (!err) {
if (watchState === 'Activated') {
WatchConnectivity.shared.sendWalletsToWatch();
}
}
});
Watch.subscribeToMessages(async (err, message, reply) => {
if (!err) {
if (message.request === 'createInvoice') {
const createInvoiceRequest = await this.handleLightningInvoiceCreateRequest(
message.walletIndex,
message.amount,
message.description,
);
reply({ invoicePaymentRequest: createInvoiceRequest });
} else if (message.message === 'sendApplicationContext') {
await WatchConnectivity.shared.sendWalletsToWatch(WatchConnectivity.shared.wallets);
} else if (message.message === 'fetchTransactions') {
await WatchConnectivity.shared.fetchTransactionsFunction();
}
} else {
reply(err);
}
});
2019-05-02 22:33:03 +02:00
}
});
}
async handleLightningInvoiceCreateRequest(walletIndex, amount, description) {
const wallet = WatchConnectivity.shared.wallets[walletIndex];
2019-05-02 22:33:03 +02:00
if (wallet.allowReceive() && amount > 0 && description.trim().length > 0) {
try {
const invoiceRequest = await wallet.addInvoice(amount, description);
return invoiceRequest;
} catch (error) {
return error;
}
}
}
async sendWalletsToWatch(allWallets) {
if (allWallets === undefined && WatchConnectivity.shared.wallets !== undefined) {
allWallets = WatchConnectivity.shared.wallets;
}
if (allWallets && allWallets.length === 0) {
return;
}
return InteractionManager.runAfterInteractions(async () => {
if (WatchConnectivity.shared.isAppInstalled) {
2019-05-02 22:33:03 +02:00
let wallets = [];
2019-05-02 22:33:03 +02:00
for (const wallet of allWallets) {
let receiveAddress = '';
if (wallet.allowReceive()) {
if (wallet.getAddressAsync) {
try {
await wallet.getAddressAsync();
receiveAddress = wallet.getAddress();
} catch (error) {
console.log(error);
receiveAddress = wallet.getAddress();
}
2019-05-02 22:33:03 +02:00
} else {
receiveAddress = wallet.getAddress();
}
}
let transactions = wallet.getTransactions(10);
let watchTransactions = [];
for (const transaction of transactions) {
let type = 'pendingConfirmation';
let memo = '';
let amount = 0;
if (transaction.hasOwnProperty('confirmations') && !(transaction.confirmations > 0)) {
2019-05-02 22:33:03 +02:00
type = 'pendingConfirmation';
} else if (transaction.type === 'user_invoice' || transaction.type === 'payment_request') {
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;
2019-05-02 22:33:03 +02:00
const currentDate = new Date();
const now = (currentDate.getTime() / 1000) | 0;
const invoiceExpiration = transaction.timestamp + transaction.expire_time;
if (invoiceExpiration > now) {
amount = loc.formatBalance(transaction.value, wallet.getPreferredBalanceUnit(), true).toString();
} else if (invoiceExpiration < now) {
if (transaction.ispaid) {
amount = loc.formatBalance(transaction.value, wallet.getPreferredBalanceUnit(), true).toString();
} else {
amount = loc.lnd.expired;
}
} else {
amount = loc.formatBalance(transaction.value, wallet.getPreferredBalanceUnit(), true).toString();
}
} else {
amount = loc.formatBalance(transaction.value, wallet.getPreferredBalanceUnit(), true).toString();
}
if (WatchConnectivity.shared.tx_metadata[transaction.hash] && WatchConnectivity.shared.tx_metadata[transaction.hash]['memo']) {
memo = WatchConnectivity.shared.tx_metadata[transaction.hash]['memo'];
2019-05-02 22:33:03 +02:00
} else if (transaction.memo) {
memo = transaction.memo;
}
2019-08-19 22:17:24 +02:00
const watchTX = { type, amount, memo, time: loc.transactionTimeToReadable(transaction.received) };
2019-05-02 22:33:03 +02:00
watchTransactions.push(watchTX);
}
wallets.push({
label: wallet.getLabel(),
balance: loc.formatBalance(Number(wallet.getBalance()), wallet.getPreferredBalanceUnit(), true),
type: wallet.type,
preferredBalanceUnit: wallet.getPreferredBalanceUnit(),
receiveAddress: receiveAddress,
transactions: watchTransactions,
});
}
Watch.updateApplicationContext({ wallets, randomID: Math.floor(Math.random() * 11) });
return { wallets };
2019-05-02 22:33:03 +02:00
}
});
}
}