This repository has been archived by the owner on Mar 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlib.js
390 lines (372 loc) · 13.6 KB
/
lib.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// The MIT License (MIT)
// Copyright (c) 2013 Artem S Vybornov
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// The MIT License (MIT)
// Copyright base-x contributors (c) 2016
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// https://github.com/cryptocoinjs/base-x/blob/master/index.js
// base-x encoding
// Forked from https://github.com/cryptocoinjs/bs58
// Originally written by Mike Hearn for BitcoinJ
// Copyright (c) 2011 Google Inc
// Ported to JavaScript by Stefan Thomas
// Merged Buffer refactorings from base58-native by Stephen Pair
// Copyright (c) 2013 BitPay Inc
// Copyright (c) 2017 Pieter Wuille
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// ISC License
// Copyright (c) 2013-2016 The btcsuite developers
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
const CHARSET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"
const CHARSET_MAP = {"q": 0, "p": 1, "z": 2, "r": 3, "y": 4, "9": 5, "x": 6, "8": 7, "g": 8, "f": 9, "2": 10, "t": 11,
"v": 12, "d": 13, "w": 14, "0": 15, "s": 16, "3": 17, "j": 18, "n": 19, "5": 20, "4": 21, "k": 22, "h": 23,
"c": 24, "e": 25, "6": 26, "m": 27, "u": 28, "a": 29, "7": 30, "l": 31}
const ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
const ALPHABET_MAP = {"1": 0, "2": 1, "3": 2, "4": 3, "5": 4, "6": 5, "7": 6,
"8": 7, "9": 8, "A": 9, "B": 10, "C": 11, "D": 12, "E": 13, "F": 14, "G": 15,
"H": 16, "J": 17, "K": 18, "L": 19, "M": 20, "N": 21, "P": 22, "Q": 23, "R": 24,
"S": 25, "T": 26, "U": 27, "V": 28, "W": 29, "X": 30, "Y": 31, "Z": 32, "a": 33,
"b": 34, "c": 35, "d": 36, "e": 37, "f": 38, "g": 39, "h": 40, "i": 41, "j": 42,
"k": 43, "m": 44, "n": 45, "o": 46, "p": 47, "q": 48, "r": 49, "s": 50, "t": 51,
"u": 52, "v": 53, "w": 54, "x": 55, "y": 56, "z": 57}
function parseAndConvertCashAddress(prefix, payloadString) {
var payloadUnparsed = new Array(payloadString.length);
for (var i = 0; i < payloadString.length; i++) {
if (CHARSET_MAP[payloadString[i]] === undefined) {
throw "Unexpected character!";
}
payloadUnparsed[i] = CHARSET_MAP[payloadString[i]];
}
var polymodConstant_0 = 0;
var polymodConstant_1 = 0;
if (prefix == "bitcoincash") {
// 1058337025301
polymodConstant_0 = 246;
polymodConstant_1 = 1775070485;
var netType = true;
} else {
// if (prefix == "bchtest")
// 584719417569
polymodConstant_0 = 136;
polymodConstant_1 = 603865313;
var netType = false;
}
var polymodResult = polyMod(payloadUnparsed, polymodConstant_0, polymodConstant_1);
if (polymodResult[0] !== 0 || polymodResult[1] !== 0) {
var syndromes = {};
for (var p = 0; p < polymodInput.length; p++) {
for (var e = 1; e < 32; e++) {
polymodInput[p] ^= e;
var c = polyMod(polymodInput);
if (c[0] === 0 && c[1] === 0) {
correctedAddress = rebuildAddress(polymodInput);
document.getElementById("correctedButton").style = "";
return "";
}
//syndromes[[polymodResult[0] ^ c[0], polymodResult[1] ^ c[1]]] = p * 32 + e
polymodInput[p] ^= e;
}
}
/*for (var s0 in syndromes) {
if ([s0[0] ^ polymodResult[0], s0[1] ^ polymodResult[1]] in syndromes) {
polymodInput[syndromes[s0]>>>5] ^= syndromes[s0] % 32
polymodInput[syndromes[[s0[0] ^ polymodResult[0], s0[1] ^ polymodResult[1]]]>>5] ^= syndromes[[s0[0] ^ polymodResult[0], s0[1] ^ polymodResult[1]]] % 32
//if (syndromes[s0]>>5 >= polymodInput.length || syndromes[simplify(xor(s0, polymodResult))]>>5 >= polymodInput.length) {
// alert("er")
//}
correctedAddress = rebuildAddress(polymodInput)
console.log(correctedAddress)
document.getElementById('correctedButton').style = ""
}
}*/
throw "Can't correct errors!";
}
var payload = convertBits(payloadUnparsed.slice(0, -8), 5, 8, false);
// payload[0] >> 3: 0 (P2PKH) or 1 (P2SH)
return craftOldAddress(payload[0] >> 3, payload.slice(1, 21), netType);
}
function craftOldAddress(kind, addressHash, netType) {
if (netType) {
if (kind == 0) {
return CheckEncodeBase58(addressHash, 0x00);
} else {
return CheckEncodeBase58(addressHash, 0x05);
}
} else {
if (kind == 0) {
return CheckEncodeBase58(addressHash, 0x6f);
} else {
return CheckEncodeBase58(addressHash, 0xc4);
}
}
}
function CheckEncodeBase58(input, version) {
var b = [version].concat(input);
var h = sha256(sha256(Uint8Array.from(b)));
b = b.concat(Array.from(h).slice(0, 4));
return EncodeBase58Simplified(b);
}
function EncodeBase58Simplified(b) {
var digits = [0];
for (var i = 0; i < b.length; i++) {
for (var j = 0, carry = b[i]; j < digits.length; j++) {
carry += digits[j] << 8;
digits[j] = carry % 58;
// https://jsperf.com/different-ways-to-truncate
carry = (carry / 58) | 0;
}
while (carry > 0) {
digits.push(carry % 58);
carry = (carry / 58) | 0;
}
}
var answer = "";
// leading zero bytes
for (var i = 0; i < b.length && b[i] === 0; i++) {
answer = answer.concat("1");
}
// reverse
for (var t = digits.length - 1; t >= 0; t--) {
answer = answer.concat(ALPHABET[digits[t]]);
}
return answer;
}
function parseAndConvertOldAddress(oldAddress) {
var bytes = [0];
for (var i = 0; i < oldAddress.length; i++) {
var value = ALPHABET_MAP[oldAddress[i]];
if (value === undefined) {
throw "Unexpected character!"
}
for (var j = 0; j < bytes.length; j++) {
value += bytes[j] * 58;
bytes[j] = value & 0xff;
value >>= 8;
}
while (value > 0) {
bytes.push(value & 0xff);
value >>= 8;
}
}
for (var i = 0; i < oldAddress.length && oldAddress[i] === "1"; i++) {
bytes.push(0);
}
bytes = bytes.reverse();
var version = bytes[0];
var h = sha256(Uint8Array.from(bytes.slice(0, -4)));
var h2 = sha256(h);
if (
h2[0] != bytes[bytes.length - 4] ||
h2[1] != bytes[bytes.length - 3] ||
h2[2] != bytes[bytes.length - 2] ||
h2[3] != bytes[bytes.length - 1]
) {
throw "Address checksum doesn't match! Did you type it wrong?"
}
var payload = bytes.slice(1, bytes.length - 4);
if (version == 0x00) {
return craftCashAddress(0, payload, true);
} else if (version == 0x05) {
return craftCashAddress(1, payload, true);
} else if (version == 0x6f) {
return craftCashAddress(0, payload, false);
} else if (version == 0xc4) {
return craftCashAddress(1, payload, false);
} else if (version == 0x1c) {
return craftCashAddress(0, payload, true);
} else if (version == 0x28) {
return craftCashAddress(1, payload, true);
} else {
throw "Unknown version byte"
}
}
function convertBits(data, fromBits, tobits, pad) {
// General power-of-2 base conversion.
var acc = 0;
var bits = 0;
var ret = [];
var maxv = (1 << tobits) - 1;
var maxAcc = (1 << (fromBits + tobits - 1)) - 1;
for (var i = 0; i < data.length; i++) {
var value = data[i];
if (value < 0 || value >>> fromBits !== 0) {
throw "convertBits error!";
}
acc = ((acc << fromBits) | value) & maxAcc;
bits += fromBits;
while (bits >= tobits) {
bits -= tobits;
ret.push((acc >>> bits) & maxv);
}
}
if (pad) {
if (bits > 0) {
ret.push((acc << (tobits - bits)) & maxv);
}
} else if (bits >= fromBits || ((acc << (tobits - bits)) & maxv) != 0) {
throw "convertBits error!";
}
return ret;
}
function craftCashAddress(kind, addressHash, netType) {
// Those addresses are not in use!
/*var encodedSize = (addressHash.length - 20) / 4
if ((addressHash.length-20)%4 != 0) {
throw ""
}
if (encodedSize < 0 || encodedSize > 8) {
throw ""
}
versionByte |= encodedSize*/
var payload = convertBits([kind << 3].concat(addressHash), 8, 5, true);
var polymodConstant_0 = 0;
var polymodConstant_1 = 0;
if (netType == true) {
// 1058337025301
polymodConstant_0 = 246;
polymodConstant_1 = 1775070485;
} else {
// 584719417569
polymodConstant_0 = 136;
polymodConstant_1 = 603865313;
}
var mod = polyMod(payload.concat([0, 0, 0, 0, 0, 0, 0, 0]), polymodConstant_0, polymodConstant_1);
var mod_0 = mod[0];
var mod_1 = mod[1];
var retChecksum = new Array(8);
for (var i = 7; i > 5; i--) {
// Convert the 5-bit groups in mod to checksum values.
// retChecksum[i] = (mod >> uint(5*(7-i))) & 0x1f
retChecksum[i] = mod_1 & 31;
mod_1 >>>= 5;
mod_1 |= (mod_0 & 31) << 27;
mod_0 >>>= 5;
}
for (; i > 0; i--) {
// Convert the 5-bit groups in mod to checksum values.
// retChecksum[i] = (mod >> uint(5*(7-i))) & 0x1f
retChecksum[i] = mod_1 & 31;
mod_1 >>>= 5;
}
retChecksum[0] = mod_1;
var combined = payload.concat(retChecksum);
if (netType == true) {
var ret = "bitcoincash:";
} else {
var ret = "bchtest:";
}
for (var i = 0; i < combined.length; i++) {
ret = ret.concat(CHARSET[combined[i]]);
}
if (ret.length == 54 || ret.length == 50) {
return ret;
} else {
throw "Unexpected converted address length!";
}
}
// If you append the prefix, c_0 = 0 and c_1 = 1
function polyMod(v, c_0, c_1) {
var c0 = 0;
for (var i = 0; i < v.length; i++) {
c0 = c_0 >>> 3;
c_0 = c_0 & 7;
c_0 = (c_0 << 5) | (c_1 >>> 27);
c_1 &= 0x07ffffff;
c_1 <<= 5;
c_1 ^= v[i];
if (c0 & 1) {
c_0 ^= 0x98;
c_1 ^= 0xf2bc8e61;
}
if (c0 & 2) {
c_0 ^= 0x79;
c_1 ^= 0xb76d99e2;
}
if (c0 & 4) {
c_0 ^= 0xf3;
c_1 ^= 0x3e5fb3c4;
}
if (c0 & 8) {
c_0 ^= 0xae;
c_1 ^= 0x2eabe2a8;
}
if (c0 & 16) {
c_0 ^= 0x1e;
c_1 ^= 0x4f43e470;
}
}
return [c_0, c_1 ^ 1];
}
function rebuildAddress(bytes) {
var ret = "";
var i = 0;
while (bytes[i] != 0 && i < bytes.length) {
ret = ret.concat(String.fromCharCode(96 + bytes[i]));
i++;
}
ret = ret.concat(":");
for (i++; i < bytes.length; i++) {
ret = ret.concat(CHARSET[bytes[i]]);
}
return ret;
}