-
Notifications
You must be signed in to change notification settings - Fork 0
/
hoprNode.js
120 lines (101 loc) · 3.05 KB
/
hoprNode.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
import { NetworkError } from '../errors.js';
import config from '../config/config.js';
import { getHeaders } from './getHeaders.js';
function hoprNodeHttpUrl() {
let url = config.restURL;
if(url && url.length > 1) {
if(url[url.length - 1] == '/')
url = url.substring(0, url.length - 1);
return url;
} else return false;
}
function callAPI(path, body) {
const hopr_node_http_url = hoprNodeHttpUrl();
const authToken = config.authToken;
let method = 'GET';
if(body)
method = 'POST';
const headers = getHeaders(authToken, method == 'POST');
const fetchConfig = {
headers,
method,
...body && {body: JSON.stringify(body)}
};
return fetch(`${hopr_node_http_url}/api/v2/${path}`, fetchConfig)
.then(res => {
return res.json()
.catch(e => {
return {};
})
.then(jsonRes => {
if(res.ok)
return jsonRes;
else {
if(res && typeof res == 'object')
return res;
let errorMessage = res.statusText;
if(jsonRes.error)
errorMessage += " - " + jsonRes.error;
else
console.error(jsonRes);
throw new NetworkError(errorMessage);
}
});
})
}
export function getAddress() {
if(hoprNodeHttpUrl()) {
return callAPI('account/addresses')
.then(res => {
return res.hoprAddress;
});
}
else return Promise.resolve(null);
}
export function establishChannel(hoprAddr, amount='1000000000000000000') {
const body = {
peerId: hoprAddr,
amount
}
return callAPI('channels', body)
.catch(e => {
if(e.status)
throw e.status;
else {
throw e;
}
});
}
export function getChannel(hoprAddr, direction='incoming') {
if(!hoprAddr || !/^16Uiu/.test(hoprAddr))
return Promise.reject(new Error("Please supply peer id"));
return callAPI('channels/' + hoprAddr + '/' + direction)
.then(res => {
// console.log("channesl:", res);
return res;
});
}
export function getChannels(direction='outgoing') {
return callAPI('channels')
}
export function sendHoprMessage(hoprAddr, message, path) {
if(!path)
path = [];
try {
message = JSON.stringify(message);
} catch(e) {
console.log("cannot stringify message");
}
if(hoprNodeHttpUrl()) {
const body = {
recipient: hoprAddr,
body: message,
path,
};
return callAPI('messages', body)
.then(res => {
console.log("send hopr message", res);
return res;
});
} else return Promise.reject("Node url not set. Update config");
}