forked from BlueWallet/BlueWallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ldkViewLogs.js
151 lines (136 loc) · 4.37 KB
/
ldkViewLogs.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
import React, { useEffect, useState, useContext, useRef } from 'react';
import { StyleSheet, View, ScrollView, TouchableOpacity } from 'react-native';
import { useNavigation, useRoute, useTheme } from '@react-navigation/native';
import { BlueLoading, BlueSpacing20, BlueText, SafeBlueArea } from '../../BlueComponents';
import navigationStyle from '../../components/navigationStyle';
import { BlueStorageContext } from '../../blue_modules/storage-context';
import loc from '../../loc';
import { Icon } from 'react-native-elements';
import { LightningLdkWallet } from '../../class';
import alert from '../../components/Alert';
const fs = require('../../blue_modules/fs');
const LdkViewLogs = () => {
const { colors } = useTheme();
const { wallets } = useContext(BlueStorageContext);
const { walletID } = useRoute().params;
/** @type {LightningLdkWallet} */
const wallet = wallets.find(w => w.getID() === walletID);
const [isLoading, setIsLoading] = useState(false);
const [logs, setLogs] = useState('');
const [info, setInfo] = useState('');
const [getInfo, setGetInfo] = useState({});
const { setOptions } = useNavigation();
const refreshDataInterval = useRef();
const stylesHooks = StyleSheet.create({
root: {
backgroundColor: colors.elevated,
},
});
useEffect(() => {
setIsLoading(true);
refetchData()
.then(() => {
refreshDataInterval.current = setInterval(() => {
refetchData();
}, 5000);
})
.finally(() => {
setOptions({
headerRight: () => (
<TouchableOpacity accessibilityRole="button" accessibilityLabel={loc.wallets.list_tryagain} style={styles.reloadLogs} onPress={getLogs}>
<Icon name="redo" type="font-awesome-5" size={22} color={colors.foregroundColor} />
</TouchableOpacity>
),
});
});
return () => {
clearInterval(refreshDataInterval.current);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const getLogs = () => {
wallet.getLogs().then(setLogs);
};
const syncBlockchain = () => {
wallet.checkBlockchain();
};
const exportLogs = async () => {
return fs.writeFileAndExport('rn-ldk.log', info + '\n' + (await wallet.getLogsWithTs()));
};
const selfTest = async () => {
try {
await wallet.selftest();
alert('ok');
} catch (error) {
alert(error.message);
}
};
const refetchData = async () => {
getLogs();
await wallet
.getInfo()
.then(async info => {
setGetInfo(info);
const peers = await wallet.listPeers();
const listChannels = await wallet.listChannels();
const version = await LightningLdkWallet.getVersion();
let nfo = 'num peers: ' + peers.length;
nfo += '\nnum channels: ' + listChannels.length;
nfo += '\nldk binary version: ' + version;
nfo += '\nstorage namespace: ' + wallet.getStorageNamespace();
setInfo(nfo);
})
.finally(() => {
setIsLoading(false);
});
};
if (isLoading) {
return (
<View style={[styles.root, stylesHooks.root]}>
<BlueLoading />
</View>
);
}
return (
<SafeBlueArea>
<ScrollView style={styles.root}>
<TouchableOpacity accessibilityRole="button" onPress={selfTest} style={styles.button}>
<BlueText>self test</BlueText>
</TouchableOpacity>
<TouchableOpacity accessibilityRole="button" onPress={exportLogs} style={styles.button}>
<BlueText>export logs to a file</BlueText>
</TouchableOpacity>
<TouchableOpacity accessibilityRole="button" onPress={syncBlockchain} style={styles.button}>
<BlueText>sync blockchain</BlueText>
</TouchableOpacity>
<BlueText>Identity pubkey: {getInfo.identityPubkey}</BlueText>
<BlueText>{info}</BlueText>
<BlueSpacing20 />
<BlueText>{logs}</BlueText>
</ScrollView>
</SafeBlueArea>
);
};
LdkViewLogs.navigationOptions = navigationStyle({}, opts => ({
...opts,
title: loc.lnd.view_logs,
}));
export default LdkViewLogs;
const styles = StyleSheet.create({
root: {
flex: 1,
},
button: {
alignItems: 'center',
padding: 10,
borderRadius: 15,
margin: 5,
borderWidth: 1,
},
reloadLogs: {
marginHorizontal: 16,
minWidth: 150,
justifyContent: 'center',
alignItems: 'flex-end',
},
});