-
Notifications
You must be signed in to change notification settings - Fork 1
/
bgtask.js
118 lines (98 loc) · 3.93 KB
/
bgtask.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
import BackgroundTimer from "react-native-background-timer";
import { test_call_provider_newprice } from "./xonefi-api-client/call_provider_newprice";
import {
read_default_config,
write_default_config,
starter_config,
} from "./xonefi-api-client/config";
import AsyncStorage from "@react-native-async-storage/async-storage";
import DeviceInfo from "react-native-device-info";
import {decrypt_aes256ctr} from "./xonefi-api-client/symcrypto";
import Web3 from "web3";
import { test_call_provider_update } from "./xonefi-api-client/call_provider_update";
const worker = require("./client-daemon/worker");
module.exports = async (taskData) => {
console.log("XLOG: startClientDaemon 1");
let global_counter = 0;
const user_password = "seitlab123!@";
let decrypted_private_key = "";
// Check local config file, if it's empty, create a new one
try {
AsyncStorage.getItem("config").then((value) => {
if (value === null) {
console.log("XLOG: startClientDaemon 2");
let config_json = starter_config();
config_json.client_on = false;
config_json.pft = true;
config_json.cft = true;
config_json.loop_started = false;
config_json.account_set = false;
// test_call_provider_newprice((ret) => {
// console.log(`Successfully called test_call_provider_newprice: ${ret}`);
// });
// test_call_provider_update((ret) => {
// console.log(`Successfully called test_call_provider_update: ${JSON.stringify(ret)}`);
// });
console.log("XLOG: startClientDaemon 3");
write_default_config(config_json, () => {
console.log("XLOG: Config is successfully initialized.");
read_default_config((config_json1) => {
console.log(`config_json1: ${JSON.stringify(config_json1)}`);
const value = AsyncStorage.getItem("privateKey").then(value1 => {
console.log(`VALUE: ${value1}`);
if (value1 !== null) {
let uniqueId = DeviceInfo.getDeviceId();
decrypted_private_key = decrypt_aes256ctr(value1, uniqueId);
let Web3 = require("web3");
let web3 = new Web3();
let account = web3.eth.accounts.privateKeyToAccount(decrypted_private_key);
config_json1.account.address = account.address;
config_json1.account_set = true;
write_default_config(config_json1, () => {
console.log("XLOG: Config is successfully written.");
});
}
});
});
});
}
});
} catch (e) {
console.log(`AsyncStorage get initial config: ERROR: ${e}`);
}
console.log("XLOG: startClientDaemon 4");
let timer_started = false;
while (true) {
//await setTimeout(() => {
//BackgroundTimer.runBackgroundTimer( () => {
console.log("XLOG: startClientDaemon 5");
read_default_config((config_json1) => {
console.log(`config_json1: ${JSON.stringify(config_json1)}`);
decrypted_private_key = config_json1.account.dpk;
if(config_json1.client_on === true) {
console.log("XLOG: startClientDaemon 7");
console.log(
"XLOG: config_json.client_session.status pre: " +
config_json1.client_session.status
);
worker.client_worker(
config_json1,
user_password,
decrypted_private_key,
() => {
console.log(`${global_counter}: Client is on`);
global_counter++;
console.log(
"XLOG: config_json.client_session.status post: " +
config_json1.client_session.status
);
}
);
} else {
console.log(`${global_counter}: Client is off`);
global_counter++;
}
});
await new Promise((resolve) => setTimeout(resolve, 5000));
}
};