forked from BlueWallet/BlueWallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WatchConnectivity.ios.js
158 lines (150 loc) · 6.23 KB
/
WatchConnectivity.ios.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import * as Watch from 'react-native-watch-connectivity';
import { InteractionManager } from 'react-native';
const loc = require('./loc');
export default class WatchConnectivity {
isAppInstalled = false;
static shared = new WatchConnectivity();
wallets;
fetchTransactionsFunction = () => {};
constructor() {
this.getIsWatchAppInstalled();
}
getIsWatchAppInstalled() {
Watch.getIsWatchAppInstalled((err, isAppInstalled) => {
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();
} else if (message.message === 'fetchTransactions') {
await WatchConnectivity.shared.fetchTransactionsFunction();
}
} else {
reply(err);
}
});
}
});
}
async handleLightningInvoiceCreateRequest(walletIndex, amount, description) {
const wallet = WatchConnectivity.shared.wallets[walletIndex];
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() {
const allWallets = WatchConnectivity.shared.wallets;
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;
}
console.log('Wallets set to sync with Watch app. Continuing...');
return InteractionManager.runAfterInteractions(async () => {
if (WatchConnectivity.shared.isAppInstalled) {
let wallets = [];
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();
}
} 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)) {
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;
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'];
} else if (transaction.memo) {
memo = transaction.memo;
}
const watchTX = { type, amount, memo, time: loc.transactionTimeToReadable(transaction.received) };
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 };
}
});
}
}