-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtonCenter.js
130 lines (120 loc) · 3.55 KB
/
tonCenter.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
service_name = "ValeriTon";
function request_promise(method, params) {
api_url = "https://toncenter.com/api/test/v1";
var request = { id: 1, jsonrpc: "2.0", method: method, params: params };
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open("POST", api_url, true);
xhr.onload = function() {
if (this.status == 200) {
r = JSON.parse(this.responseText);
if ("error" in r) {
reject(r["error"]);
}
resolve(r["result"]);
} else {
var error = new Error(this.statusText);
error.code = this.status;
reject(error);
}
};
xhr.onerror = function() {
reject(new Error("Network Error"));
};
xhr.setRequestHeader("content-type", "application/json");
xhr.send(JSON.stringify(request));
});
}
function request(method, params, callback, error_callback = false) {
var xrq = new XMLHttpRequest();
xrq.onreadystatechange = function() {
if (xrq.readyState == 4 && xrq.status == 200) {
r = JSON.parse(xrq.responseText);
if ("error" in r) {
error_callback();
}
callback(r["result"]);
} else {
if (xrq.readyState == 0 && xrq.status == 4) {
console.log("error");
} else {
if (xrq.status == 400 && error_callback) {
error_callback();
}
}
}
};
api = "https://toncenter.com/api/test/v1";
xrq.open("POST", api, true);
var request = { id: 1, jsonrpc: "2.0", method: method, params: params };
xrq.setRequestHeader("content-type", "application/json");
xrq.send(JSON.stringify(request));
}
const base64abc = (() => {
let abc = [],
A = "A".charCodeAt(0),
a = "a".charCodeAt(0),
n = "0".charCodeAt(0);
for (let i = 0; i < 26; ++i) {
abc.push(String.fromCharCode(A + i));
}
for (let i = 0; i < 26; ++i) {
abc.push(String.fromCharCode(a + i));
}
for (let i = 0; i < 10; ++i) {
abc.push(String.fromCharCode(n + i));
}
abc.push("+");
abc.push("/");
return abc;
})();
function bytesToBase64(bytes) {
let result = "",
i,
l = bytes.length;
for (i = 2; i < l; i += 3) {
result += base64abc[bytes[i - 2] >> 2];
result += base64abc[((bytes[i - 2] & 0x03) << 4) | (bytes[i - 1] >> 4)];
result += base64abc[((bytes[i - 1] & 0x0f) << 2) | (bytes[i] >> 6)];
result += base64abc[bytes[i] & 0x3f];
}
if (i === l + 1) {
// 1 octet missing
result += base64abc[bytes[i - 2] >> 2];
result += base64abc[(bytes[i - 2] & 0x03) << 4];
result += "==";
}
if (i === l) {
// 2 octets missing
result += base64abc[bytes[i - 2] >> 2];
result += base64abc[((bytes[i - 2] & 0x03) << 4) | (bytes[i - 1] >> 4)];
result += base64abc[(bytes[i - 1] & 0x0f) << 2];
result += "=";
}
return result;
}
function sendboc(ui8arr) {
return request_promise("sendboc", [bytesToBase64(ui8arr)]);
}
function give_non_bouncable(address) {
return request_promise("getaccountforms", [address]).then(address_forms => {
return address_forms["non-bounceable"]["b64"];
});
}
function parse_addr(address) {
return request_promise("getaccountforms", [address]).then(address_forms => {
var bounce = true; //default regime
if (address_forms["given_type"].search("non_bounceable") >= 0) {
bounce = false;
}
var wc = address_forms["raw_form"].split(":")[0];
var hex_addr = address_forms["raw_form"].split(":")[1];
return [wc, hex_addr, bounce];
});
}
function getaccount(address) {
return request_promise("getaccount", [address]);
}
function checkBigInt() {
return true;
}