-
Notifications
You must be signed in to change notification settings - Fork 20
/
utilities.js
336 lines (292 loc) · 8.67 KB
/
utilities.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
const { stringToHex, isHex, bnToHex, hexToBn, hexToString, u8aToHex, stringToU8a, bnToU8a } = polkadotUtil;
const { blake2AsHex, xxhashAsHex, encodeAddress, decodeAddress, blake2AsU8a } = polkadotUtilCrypto;
const { Keyring } = polkadotKeyring;
/* String to Hex */
let s2h = {
"string": document.getElementById("string-s2h"),
"hex": document.getElementById("hex-s2h")
};
s2h.string.addEventListener("input", string2hex);
s2h.hex.addEventListener("input", hex2string);
function string2hex() {
try {
s2h.hex.value = stringToHex(s2h.string.value);
} catch (e) {
s2h.hex.value = "Error";
console.error(e);
}
}
function hex2string() {
try {
s2h.string.value = hexToString(s2h.hex.value);
} catch (e) {
s2h.string.value = "Error";
console.error(e);
}
}
/* Balance to Hex (Little Endian) */
let b2h = {
"balance": document.getElementById("balance-b2h"),
"hex": document.getElementById("hex-b2h")
};
b2h.balance.addEventListener("input", bn2hex);
b2h.hex.addEventListener("input", hex2bn);
function bn2hex() {
try {
b2h.hex.value = bnToHex(b2h.balance.value, { isLe: true });
} catch (e) {
b2h.hex.value = "Error";
console.error(e);
}
}
function hex2bn() {
try {
b2h.balance.value = hexToBn(b2h.hex.value, { isLe: true });
} catch (e) {
b2h.balance.value = "Error";
console.error(e);
}
}
/* AccountId to Hex */
let a2h = {
"account": document.getElementById("account-a2h"),
"hex": document.getElementById("hex-a2h")
};
a2h.account.addEventListener("input", account2hex);
a2h.hex.addEventListener("input", hex2account);
function account2hex() {
try {
a2h.hex.value = u8aToHex(decodeAddress(a2h.account.value));
} catch (e) {
a2h.hex.value = "Error";
console.error(e);
}
}
function hex2account() {
try {
a2h.account.value = encodeAddress(a2h.hex.value);
} catch (e) {
a2h.account.value = "Error";
console.error(e);
}
}
/* Blake-256 a String */
let blake2 = {
"input": document.getElementById("input-blake2"),
"bits": document.getElementById("bits-blake2"),
"hash": document.getElementById("hash-blake2")
};
blake2.input.addEventListener("input", blake2string);
blake2.bits.addEventListener("input", blake2string);
function blake2string() {
try {
blake2.hash.innerText = blake2AsHex(blake2.input.value, blake2.bits.value);
} catch (e) {
blake2.hash.innerText = "Error";
console.error(e);
}
}
/* Blake-256 Concat a String */
let blake2concat = {
"input": document.getElementById("input-blake2-concat"),
"bits": document.getElementById("bits-blake2-concat"),
"hash": document.getElementById("hash-blake2-concat")
};
blake2concat.input.addEventListener("input", blake2ConcatString);
blake2concat.bits.addEventListener("input", blake2ConcatString);
function blake2ConcatString() {
try {
let hash = blake2AsHex(blake2concat.input.value, blake2concat.bits.value);
if (isHex(blake2concat.input.value)) {
hash += blake2concat.input.value.substr(2);
} else {
hash += stringToHex(blake2concat.input.value).substr(2);
}
blake2concat.hash.innerText = hash;
} catch (e) {
blake2concat.hash.innerText = "Error";
console.error(e);
}
}
/* XXHash a String */
let xxhash = {
"input": document.getElementById("input-xxhash"),
"bits": document.getElementById("bits-xxhash"),
"hash": document.getElementById("hash-xxhash")
};
xxhash.input.addEventListener("input", xxhashString);
xxhash.bits.addEventListener("input", xxhashString);
function xxhashString() {
try {
xxhash.hash.innerText = xxhashAsHex(xxhash.input.value, xxhash.bits.value);
} catch (e) {
xxhash.hash.innerText = "Error";
console.error(e);
}
}
/* XXHash Concat a String */
let xxhashconcat = {
"input": document.getElementById("input-xxhash-concat"),
"bits": document.getElementById("bits-xxhash-concat"),
"hash": document.getElementById("hash-xxhash-concat")
};
xxhashconcat.input.addEventListener("input", xxhashConcatString);
xxhashconcat.bits.addEventListener("input", xxhashConcatString);
function xxhashConcatString() {
try {
let hash = xxhashAsHex(xxhashconcat.input.value, xxhashconcat.bits.value);
if (isHex(xxhashconcat.input.value)) {
hash += xxhashconcat.input.value.substr(2);
} else {
hash += stringToHex(xxhashconcat.input.value).substr(2);
}
xxhashconcat.hash.innerText = hash;
} catch (e) {
xxhashconcat.hash.innerText = "Error";
console.error(e);
}
}
/* Seed to Address */
let s2a = {
"address": document.getElementById("address-s2a"),
"seed": document.getElementById("seed-s2a")
};
s2a.seed.addEventListener("input", seed2address);
function seed2address() {
try {
let k = new Keyring({ type: "sr25519" });
let user = k.addFromUri(s2a.seed.value);
s2a.address.innerText = user.address;
} catch (e) {
s2a.address.innerText = "Error";
console.error(e);
}
}
/* Change Address Prefix */
let cap = {
"address": document.getElementById("address-cap"),
"prefix": document.getElementById("prefix-cap"),
"result": document.getElementById("result-cap")
};
cap.prefix.addEventListener("input", changeAddressPrefix);
cap.address.addEventListener("input", changeAddressPrefix);
function changeAddressPrefix() {
try {
let address = cap.address.value;
let decoded = decodeAddress(address);
let prefix = cap.prefix.value;
if (prefix) {
cap.result.innerText = encodeAddress(decoded, prefix);
} else {
cap.result.innerText = encodeAddress(decoded);
}
} catch (e) {
cap.result.innerText = "Error";
console.error(e);
}
}
/* Module ID to Address */
let modid = {
"moduleId": document.getElementById("moduleId-modid"),
"address": document.getElementById("address-modid")
};
modid.moduleId.addEventListener("input", moduleId2Address);
modid.address.addEventListener("input", moduleId2Address);
function moduleId2Address() {
try {
let moduleId = modid.moduleId.value;
if (moduleId.length != 8) {
modid.address.innerText = "Module Id must be 8 characters (i.e. `py/trsry`)";
return
}
let address = stringToU8a(("modl" + moduleId).padEnd(32, '\0'));
modid.address.innerText = encodeAddress(address);
} catch (e) {
modid.address.innerText = "Error";
console.error(e);
}
}
/* Para ID to Address */
let paraid = {
"paraId": document.getElementById("paraId-paraid"),
"address": document.getElementById("address-paraid")
};
let paraType = document.getElementById("type-paraid");
paraid.paraId.addEventListener("input", paraId2Address);
paraid.address.addEventListener("input", paraId2Address);
paraType.addEventListener("input", paraId2Address);
function paraId2Address() {
try {
let paraId = paraid.paraId.value;
if (!parseInt(paraId)) {
paraid.address.innerText = "Para Id should be a number";
return
}
let type = paraType.value;
let typeEncoded = stringToU8a(type);
let paraIdEncoded = bnToU8a(parseInt(paraId), 16);
let zeroPadding = new Uint8Array(32 - typeEncoded.length - paraIdEncoded.length).fill(0);
let address = new Uint8Array([...typeEncoded, ...paraIdEncoded, ...zeroPadding]);
paraid.address.innerText = encodeAddress(address);
} catch (e) {
paraid.address.innerText = "Error";
console.error(e);
}
}
/* Sub Account Generator */
let subid = {
"address": document.getElementById("address-subid"),
"index": document.getElementById("index-subid"),
"subid": document.getElementById("subid-subid")
};
subid.address.addEventListener("input", subAccountId);
subid.index.addEventListener("input", subAccountId);
function subAccountId() {
try {
let address = subid.address.value;
let index = subid.index.value;
let seedBytes = stringToU8a("modlpy/utilisuba");
let whoBytes = decodeAddress(address);
if (isNaN(parseInt(index))) {
subid.subid.innerText = "Bad Index";
return;
}
let indexBytes = bnToU8a(parseInt(index), { bitLength: 16 });
let combinedBytes = new Uint8Array(seedBytes.length + whoBytes.length + indexBytes.length);
combinedBytes.set(seedBytes);
combinedBytes.set(whoBytes, seedBytes.length);
combinedBytes.set(indexBytes, seedBytes.length + whoBytes.length);
let entropy = blake2AsU8a(combinedBytes, 256);
subid.subid.innerText = encodeAddress(entropy);
} catch (e) {
subid.subid.innerText = "Error";
console.error(e);
}
}
/* u8a to Hex */
let u82h = {
"u8a": document.getElementById("u8a-u82h"),
"hex": document.getElementById("hex-u82h")
};
u82h.u8a.addEventListener("input", u8a2hex);
u82h.hex.addEventListener("input", hex2u8a);
function u8a2hex() {
try {
let array = u82h.u8a.value.replace(/ /g, "").split(",").filter(e => e);
let u8array = new Uint8Array(array);
u82h.hex.value = u8aToHex(u8array);
} catch (e) {
u82h.hex.value = "Error";
console.error(e);
}
}
function hex2u8a() {
try {
let array = u82h.hex.value.match(/.{1,2}/g).map(byte => parseInt(byte, 16));
let u8a = new Uint8Array(array);
u82h.u8a.value = u8a;
} catch (e) {
u82h.hex.value = "Error";
console.error(e);
}
}