Create js/address.js
Browse files- public/js/address.js +127 -0
public/js/address.js
ADDED
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const to_hex_array = [],
|
2 |
+
to_byte_map = {};
|
3 |
+
for (let ord = 0; ord <= 255; ord++) {
|
4 |
+
let e = ord.toString(16);
|
5 |
+
e.length < 2 && (e = "0" + e), to_hex_array.push(e), to_byte_map[e] = ord
|
6 |
+
}
|
7 |
+
|
8 |
+
function bytesToHex(e) {
|
9 |
+
let t = [];
|
10 |
+
for (let s = 0; s < e.byteLength; s++) t.push(to_hex_array[e[s]]);
|
11 |
+
return t.join("")
|
12 |
+
}
|
13 |
+
|
14 |
+
function hexToBytes(e) {
|
15 |
+
e = e.toLowerCase();
|
16 |
+
let t = e.length;
|
17 |
+
if (t % 2 != 0) throw "hex string must have length a multiple of 2";
|
18 |
+
let s = t / 2,
|
19 |
+
r = new Uint8Array(s);
|
20 |
+
for (let n = 0; n < s; n++) {
|
21 |
+
let i = 2 * n,
|
22 |
+
l = e.substring(i, i + 2);
|
23 |
+
if (!to_byte_map.hasOwnProperty(l)) throw Error("invalid hex character " + l);
|
24 |
+
r[n] = to_byte_map[l]
|
25 |
+
}
|
26 |
+
return r
|
27 |
+
}
|
28 |
+
|
29 |
+
function stringToBytes(e, t = 1) {
|
30 |
+
let s, r;
|
31 |
+
1 === t && (s = new ArrayBuffer(e.length), r = new Uint8Array(s)), 2 === t && (s = new ArrayBuffer(2 * e.length), r = new Uint16Array(s)), 4 === t && (s = new ArrayBuffer(4 * e.length), r = new Uint32Array(s));
|
32 |
+
for (let n = 0, i = e.length; n < i; n++) r[n] = e.charCodeAt(n);
|
33 |
+
return new Uint8Array(r.buffer)
|
34 |
+
}
|
35 |
+
|
36 |
+
function crc16(e) {
|
37 |
+
let t = 0,
|
38 |
+
s = new Uint8Array(e.length + 2);
|
39 |
+
for (let r of (s.set(e), s)) {
|
40 |
+
let n = 128;
|
41 |
+
for (; n > 0;) t <<= 1, r & n && (t += 1), n >>= 1, t > 65535 && (t &= 65535, t ^= 4129)
|
42 |
+
}
|
43 |
+
return new Uint8Array([Math.floor(t / 256), t % 256])
|
44 |
+
}
|
45 |
+
const base64abc = (() => {
|
46 |
+
let e = [];
|
47 |
+
for (let t = 0; t < 26; ++t) e.push(String.fromCharCode(65 + t));
|
48 |
+
for (let s = 0; s < 26; ++s) e.push(String.fromCharCode(97 + s));
|
49 |
+
for (let r = 0; r < 10; ++r) e.push(String.fromCharCode(48 + r));
|
50 |
+
return e.push("+"), e.push("/"), e
|
51 |
+
})();
|
52 |
+
|
53 |
+
function base64toString(e) {
|
54 |
+
return "undefined" == typeof self ? Buffer.from(e, "base64").toString("binary") : atob(e)
|
55 |
+
}
|
56 |
+
|
57 |
+
function stringToBase64(e) {
|
58 |
+
return "undefined" == typeof self ? Buffer.from(e, "binary").toString("base64") : btoa(e)
|
59 |
+
}
|
60 |
+
const bounceable_tag = 17,
|
61 |
+
non_bounceable_tag = 81,
|
62 |
+
test_flag = 128;
|
63 |
+
|
64 |
+
function parseFriendlyAddress(e) {
|
65 |
+
if (48 !== e.length) throw Error("User-friendly address should contain strictly 48 characters");
|
66 |
+
let t = stringToBytes(base64toString(e));
|
67 |
+
if (36 !== t.length) throw "Unknown address type: byte length is not equal to 36";
|
68 |
+
let s = t.slice(0, 34),
|
69 |
+
r = t.slice(34, 36),
|
70 |
+
n = crc16(s);
|
71 |
+
if (!(n[0] === r[0] && n[1] === r[1])) throw "Wrong crc16 hashsum";
|
72 |
+
let i = s[0],
|
73 |
+
l = !1,
|
74 |
+
o = !1;
|
75 |
+
if (128 & i && (l = !0, i ^= 128), 17 !== i && 81 !== i) throw "Unknown address tag";
|
76 |
+
o = 17 === i;
|
77 |
+
let a = null;
|
78 |
+
if (0 !== (a = 255 === s[1] ? -1 : s[1]) && -1 !== a) throw Error("Invalid address wc " + a);
|
79 |
+
let h = s.slice(2, 34);
|
80 |
+
return {
|
81 |
+
isTestOnly: l,
|
82 |
+
isBounceable: o,
|
83 |
+
workchain: a,
|
84 |
+
hashPart: h
|
85 |
+
}
|
86 |
+
}
|
87 |
+
class Address {
|
88 |
+
static isValid(e) {
|
89 |
+
try {
|
90 |
+
return new Address(e), !0
|
91 |
+
} catch (t) {
|
92 |
+
return !1
|
93 |
+
}
|
94 |
+
}
|
95 |
+
constructor(e) {
|
96 |
+
if (null == e) throw "Invalid address";
|
97 |
+
if (e instanceof Address) {
|
98 |
+
this.wc = e.wc, this.hashPart = e.hashPart, this.isTestOnly = e.isTestOnly, this.isUserFriendly = e.isUserFriendly, this.isBounceable = e.isBounceable, this.isUrlSafe = e.isUrlSafe;
|
99 |
+
return
|
100 |
+
}
|
101 |
+
if (this.isUrlSafe = !0, e.search(/\-/) > 0 || e.search(/_/) > 0 ? e = e.replace(/\-/g, "+").replace(/_/g, "/") : this.isUrlSafe = !1, e.indexOf(":") > -1) {
|
102 |
+
let t = e.split(":");
|
103 |
+
if (2 !== t.length) throw Error("Invalid address " + e);
|
104 |
+
let s = parseInt(t[0]);
|
105 |
+
if (0 !== s && -1 !== s) throw Error("Invalid address wc " + e);
|
106 |
+
let r = t[1];
|
107 |
+
if (64 !== r.length) throw Error("Invalid address hex " + e);
|
108 |
+
this.isUserFriendly = !1, this.wc = s, this.hashPart = hexToBytes(r), this.isTestOnly = !1, this.isBounceable = !1
|
109 |
+
} else {
|
110 |
+
this.isUserFriendly = !0;
|
111 |
+
let n = parseFriendlyAddress(e);
|
112 |
+
this.wc = n.workchain, this.hashPart = n.hashPart, this.isTestOnly = n.isTestOnly, this.isBounceable = n.isBounceable
|
113 |
+
}
|
114 |
+
}
|
115 |
+
toString(e, t, s, r) {
|
116 |
+
if (void 0 === e && (e = this.isUserFriendly), void 0 === t && (t = this.isUrlSafe), void 0 === s && (s = this.isBounceable), void 0 === r && (r = this.isTestOnly), !e) return this.wc + ":" + bytesToHex(this.hashPart); {
|
117 |
+
let n = s ? 17 : 81;
|
118 |
+
r && (n |= 128);
|
119 |
+
let i = new Int8Array(34);
|
120 |
+
i[0] = n, i[1] = this.wc, i.set(this.hashPart, 2);
|
121 |
+
let l = new Uint8Array(36);
|
122 |
+
l.set(i), l.set(crc16(i), 34);
|
123 |
+
let o = stringToBase64(String.fromCharCode.apply(null, new Uint8Array(l)));
|
124 |
+
return t && (o = o.replace(/\+/g, "-").replace(/\//g, "_")), o
|
125 |
+
}
|
126 |
+
}
|
127 |
+
}
|