This repository has been archived by the owner on Jun 22, 2023. It is now read-only.
forked from shawntabrizi/substrate-js-utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.js
245 lines (212 loc) · 6.47 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
/* 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 = util.bnToHex(b2h.balance.value, { isLe: true });
} catch (e) {
b2h.hex.value = "Error";
console.error(e);
}
}
function hex2bn() {
try {
b2h.balance.value = util.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 = util.u8aToHex(keyring.decodeAddress(a2h.account.value));
} catch (e) {
a2h.hex.value = "Error";
console.error(e);
}
}
function hex2account() {
try {
a2h.account.value = keyring.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 = util_crypto.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 = util_crypto.blake2AsHex(blake2concat.input.value, blake2concat.bits.value);
if (util.isHex(blake2concat.input.value)) {
hash += blake2concat.input.value.substr(2);
} else {
hash += util.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 = util_crypto.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 = util_crypto.xxhashAsHex(xxhashconcat.input.value, xxhashconcat.bits.value);
if (util.isHex(xxhashconcat.input.value)) {
hash += xxhashconcat.input.value.substr(2);
} else {
hash += util.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.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 = util_crypto.decodeAddress(address);
let prefix = cap.prefix.value;
if (prefix) {
cap.result.innerText = util_crypto.encodeAddress(decoded, prefix);
} else {
cap.result.innerText = util_crypto.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 = util.stringToU8a(("modl" + moduleId).padEnd(32, '\0'));
modid.address.innerText = util_crypto.encodeAddress(address);
} catch (e) {
modid.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 = util.stringToU8a("modlpy/utilisuba");
let whoBytes = util_crypto.decodeAddress(address);
if (isNaN(parseInt(index))) {
subid.subid.innerText = "Bad Index";
return;
}
let indexBytes = util.bnToU8a(parseInt(index), 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 = util_crypto.blake2AsU8a(combinedBytes, 256);
subid.subid.innerText = util_crypto.encodeAddress(entropy);
} catch (e) {
subid.subid.innerText = "Error";
console.error(e);
}
}