This repository has been archived by the owner on Oct 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
140 lines (113 loc) · 3.98 KB
/
util.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
import { Base64 } from "https://deno.land/x/bb64/mod.ts";
import Kia from "https://deno.land/x/[email protected]/mod.ts";
import { createHash } from "https://deno.land/[email protected]/hash/mod.ts";
import { cryptoRandomString } from "https://deno.land/x/[email protected]/mod.ts"
import { decodeString } from "https://deno.land/std/encoding/hex.ts"
const rpcAddress = Deno.env.get("RPC_ADDRESS");
const rpcPort = Deno.env.get("RPC_PORT");
const rpcUser = Deno.env.get("RPC_USER");
const rpcPassword = Deno.env.get("RPC_PASSWORD");
export const BOT_VERSION = "1.0.4";
export function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
export const rpcMethod = async (method, params, hideSpinner, ignoreError) => {
const kia = hideSpinner || new Kia(`Performing: ${method}`);
if(!hideSpinner) {
kia.start();
}
let rpcData = {'jsonrpc': '1.0', 'id': 'rpctest', 'method': method, 'params': params || [] };
let res = await fetch(`http://${rpcAddress}:${rpcPort}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Authorization': 'Basic ' +
Base64.fromString(`${rpcUser}:${rpcPassword}`).toString()},
body: JSON.stringify(rpcData)
}
);
const resJson = (await res.json());
if(resJson.error && !ignoreError) {
console.error('<<< Error >>>');
console.error(resJson.error);
if(resJson.error && resJson.error.message) {
console.error(resJson.error.message);
}
}
if(!hideSpinner) {
kia.succeed(`Completed: ${method}`);
}
return resJson;
}
export const waitConfirmation = async (txResult, waitUntil, hideSpinner) => {
if (txResult.error != null) {
return txResult;
}
const result = txResult.result ? txResult.result : txResult;
const txHash = result.txid ? result.txid : result;
const kia = hideSpinner || new Kia(`Confirming tx id: ${txHash}`);
if(!hideSpinner) {
kia.start();
}
let unconfirmed = true;
while(unconfirmed) {
const txInfo = await rpcMethod('gettransaction', [txHash], true);
if (txInfo.result == null) {
continue;
}
const confirmations = txInfo.result.confirmations;
if(!hideSpinner) {
await kia.set({ text: `Confirming tx id: ${txHash} Confirmations: ${confirmations}` });
}
unconfirmed = confirmations <= (waitUntil ? waitUntil : 0);
unconfirmed && await sleep(5000);
}
if(!hideSpinner) {
kia.succeed(`Tx id: ${txHash} confirmed`);
}
return txHash;
}
export const waitSPVConnected = async (callback, hideSpinner) => {
const kia = hideSpinner || new Kia(`Ensuring SPV sync`);
if(!hideSpinner) {
kia.start();
}
let response;
while(!response) {
if(!hideSpinner) {
const syncStatus = (await rpcMethod('spv_syncstatus'));
await kia.set({ text: `Waiting for SPV, sync status: ${JSON.stringify(syncStatus)}` });
}
response = await callback();
if(response.error && response.error.code === -1) {
response = null;
}
!response && await sleep(1000);
}
if(!hideSpinner) {
kia.succeed(`SPV tx submitted`);
}
return response;
}
export const waitEvent = async (callback, hideSpinner) => {
const kia = hideSpinner || new Kia(`Waiting for event`);
if(!hideSpinner) {
kia.start();
}
let unconfirmed = true;
while(unconfirmed) {
if(!hideSpinner) {
await kia.set({ text: `Waiting for event` });
}
unconfirmed = callback();
unconfirmed && await sleep(1000);
}
if(!hideSpinner) {
kia.succeed(`Event found`);
}
return;
}
export const createSeedHashPair = (useSeed) => {
const seed = useSeed || cryptoRandomString({length: 64});
let hash = createHash('sha256');
hash.update(decodeString(seed));
return { seed: seed, hash: hash.toString('hex')};
}