forked from hashgraph/hedera-hcs-chat-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
84 lines (79 loc) · 2.03 KB
/
utils.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
var initQuestions = [
{
type: "list",
name: "status",
message: "What mode do you want to run in?",
choices: ["Default", "Minimal", "Debug"],
filter: function(val) {
return val.toLowerCase();
}
},
{
type: "input",
name: "account",
message:
"What's your account ID? [empty will default to the value at process.env.ACCOUNT_ID]\n"
},
{
type: "password",
name: "key",
message:
"What's your private key? \n[empty will default to the value at process.env.PRIVATE_KEY]\n"
},
{
type: "list",
name: "topic",
message: "Should we create a new topic, or connect to an existing one?",
choices: ["Create a new topic", "Connect to an existing topic"],
filter: function(val) {
return val.toLowerCase();
}
},
{
type: "input",
name: "existingTopicId",
message: "What's the topic ID?\n[empty will default to the value at process.env.TOPIC_ID]\n",
when: (answers) => !answers.topic.includes("create")
}
];
function UInt8ToString(array) {
var str = "";
for (var i = 0; i < array.length; i++) {
str += array[i];
}
return str;
}
function secondsToDate(time) {
var date = new Date(1970, 0, 1);
date.setSeconds(time.seconds);
return date;
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function handleLog(event, log, status) {
if (status === "default") {
console.log(event + " " + log);
} else if (status === "minimal") {
console.log(event);
} else {
// debug mode. destructure mirror receipts or print a usual log
if (log.toString() !== log && log["runningHash"] !== undefined) {
console.log(event);
console.log("\t message: " + log.toString());
console.log("\t runningHash: " + UInt8ToString(log["runningHash"]));
console.log(
"\t consensusTimestamp: " + secondsToDate(log["consensusTimestamp"])
);
} else {
console.log(event + " " + log);
}
}
}
module.exports = {
initQuestions,
UInt8ToString,
secondsToDate,
sleep,
handleLog
};