-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddress-lib.js
302 lines (259 loc) · 10.3 KB
/
address-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
document.getElementById('convertToHex').addEventListener('click', () => {
const textAddress = document.getElementById('textAddress').value.trim();
const error = document.getElementById('error');
try {
const hexAddress = AddressLib.textAddressToHex(textAddress);
document.getElementById('hexAddress').value = "0x" + hexAddress;
const paddedEthereumAddress = "0x" + hexAddress.padStart(40, '0');
document.getElementById('ethereumAddress').value = toChecksumAddress(paddedEthereumAddress);
error.style.display = 'none';
} catch (err) {
error.style.display = 'block';
error.textContent = 'Invalid Text Address';
}
});
document.getElementById('convertToText').addEventListener('click', () => {
const hexAddress = document.getElementById('hexAddress').value.trim();
const error = document.getElementById('error');
try {
const strippedHex = hexAddress.startsWith('0x') ? hexAddress.slice(2) : hexAddress;
const textAddress = AddressLib.hexToTextAddress(strippedHex);
document.getElementById('textAddress').value = textAddress;
const paddedEthereumAddress = "0x" + strippedHex.padStart(40, '0');
document.getElementById('ethereumAddress').value = toChecksumAddress(paddedEthereumAddress);
error.style.display = 'none';
} catch (err) {
error.style.display = 'block';
error.textContent = 'Invalid Hex Address';
}
});
/*
document.getElementById('convertToEthereum').addEventListener('click', () => {
const hexAddress = document.getElementById('hexAddress').value.trim();
const error = document.getElementById('error');
try {
const strippedHex = hexAddress.startsWith('0x') ? hexAddress.slice(2) : hexAddress;
// Convert to Ethereum format by padding the 64-bit address to 160 bits
const paddedEthereumAddress = "0x" + strippedHex.padStart(40, '0');
document.getElementById('ethereumAddress').value = paddedEthereumAddress;
error.style.display = 'none';
} catch (err) {
error.style.display = 'block';
error.textContent = 'Invalid Hex Address';
}
});
*/
// Decode Ethereum address back to the application's hex address
/*
document.getElementById('ethereumAddress').addEventListener('change', () => {
const ethereumAddress = document.getElementById('ethereumAddress').value.trim();
const error = document.getElementById('error');
try {
const strippedEthereum = ethereumAddress.startsWith('0x') ? ethereumAddress.slice(2) : ethereumAddress;
if (strippedEthereum.length !== 40) {
throw new Error('Invalid Ethereum Address');
}
// Extract the 64-bit original hex address
const originalHexAddress = strippedEthereum.slice(-16);
document.getElementById('hexAddress').value = "0x" + originalHexAddress;
error.style.display = 'none';
} catch (err) {
error.style.display = 'block';
error.textContent = 'Invalid Ethereum Address';
}
});
*/
/*
document.getElementById('convertToHex').addEventListener('click', () => {
const textAddress = document.getElementById('textAddress').value;
const error = document.getElementById('error');
try {
const hexAddress = AddressLib.textAddressToHex(textAddress);
document.getElementById('hexAddress').value = hexAddress;
error.style.display = 'none';
} catch (err) {
error.style.display = 'block';
error.textContent = 'Invalid Text Address';
}
});
document.getElementById('convertToText').addEventListener('click', () => {
const hexAddress = document.getElementById('hexAddress').value;
const error = document.getElementById('error');
try {
const textAddress = AddressLib.hexToTextAddress(hexAddress);
document.getElementById('textAddress').value = textAddress;
error.style.display = 'none';
} catch (err) {
error.style.display = 'block';
error.textContent = 'Invalid Hex Address';
}
});
*/
let globalCrcTable = null;
function makeCRCTable() {
let crcTable = globalCrcTable;
if (!crcTable) {
crcTable = [];
for (let i = 0; i < 256; i++) {
let value = i;
for (let k = 0; k < 8; k++) {
value = ((value & 1) ? (0xEDB88320 ^ (value >>> 1)) : (value >>> 1));
}
crcTable[i] = value;
}
globalCrcTable = crcTable;
}
return crcTable;
}
function crc32(address) {
let crcTable = makeCRCTable();
let crc = 0 ^ (-1);
for (let i = 0; i < 8; i++) {
crc = (crc >>> 8) ^ crcTable[(crc ^ address[i]) & 0xFF];
}
return (crc ^ (-1)) >>> 0;
}
function decodeAddress(address) {
let binaryAddress = new Uint8Array(8);
binaryAddress[0] = 0x80;
if (address.scope === 'private') {
binaryAddress[0] |= 0x20;
binaryAddress[0] |= Math.floor(address.block / 4294967296) & 0x1F;
binaryAddress[1] |= address.block >>> 24;
binaryAddress[2] |= address.block >>> 16;
binaryAddress[3] |= address.block >>> 8;
binaryAddress[4] |= address.block;
binaryAddress[5] |= address.wallet >>> 16;
binaryAddress[6] |= address.wallet >>> 8;
binaryAddress[7] |= address.wallet;
} else {
let group = (address.group << 5) >>> 0;
binaryAddress[0] |= (group >>> 16) & 0x1F;
binaryAddress[1] |= group >>> 8;
binaryAddress[2] |= group;
binaryAddress[2] |= (address.block >>> 16) & 0x1F;
binaryAddress[3] |= address.block >>> 8;
binaryAddress[4] |= address.block;
binaryAddress[5] |= address.wallet >>> 16;
binaryAddress[6] |= address.wallet >>> 8;
binaryAddress[7] |= address.wallet;
}
return binaryAddress;
}
function pad(base, num, size) {
let S;
if (base === 26) {
let A = "A".charCodeAt(0),
B0 = num % 26,
B1 = Math.floor(num / 26);
S = String.fromCharCode(B1 + A) + String.fromCharCode(B0 + A);
} else {
S = num.toString(base).toUpperCase();
while (S.length < size) {
S = "0" + S;
}
}
return S;
}
const AddressLib = {
encodeAddress(address) {
// 100GGGGG GGGGGGGG GGGBBBBB BBBBBBBB BBBBBBBB AAAAAAAA AAAAAAAA AAAAAAAA
// 101BBBBB BBBBBBBB BBBBBBBB BBBBBBBB BBBBBBBB AAAAAAAA AAAAAAAA AAAAAAAA
if (address[0] < 128 || address[0] > 191) {
throw new Error("Bad address");
}
const wallet = address[7] | (address[6] << 8) | (address[5] << 16);
const checksum = crc32(address);
let result;
if (address[0] & 0x20) { //private
const block = ((address[4] | address[3] << 8 | address[2] << 16 | address[1] << 24) >>> 0) + ((0x1f & address[0]) * 4294967296);
const txt = pad(16, block, 10) + pad(16, wallet, 6) + pad(16, checksum % 256, 2);
result = {
scope: "private",
checksum: checksum % 256,
block,
txt,
wallet
}
} else { //public
const block = address[4] | (address[3] << 8) | ((0x1f & address[2]) << 16);
const group = (address[2] | (address[1] << 8) | ((0x1f & address[0]) << 16)) >>> 5;
const IntPart = (wallet + (block * 16777216));
const txt = pad(26, parseInt(group / 100), 2) + pad(10, group % 100, 2) + pad(10, IntPart, 14) + pad(10, checksum % 100, 2);
result = {
scope: "public",
checksum: checksum % 100,
wallet,
block,
txt,
group
}
}
return result;
},
parseTextAddress(textAddress) {
//BBBB BBBB BBAA AAAA CC
//GGgg AAAA AAAA AAAA AACC
let binaryAddress, checksum, addrChecksum;
if (textAddress.length === 20) { //public
const intPart = parseInt(textAddress.slice(4, 18));
const groupRemainder = parseInt(textAddress.slice(2, 4));
const A = "A".charCodeAt(0);
const B0 = textAddress.charCodeAt(1) - A;
const B1 = textAddress.charCodeAt(0) - A;
const groupQuotient = (B1 * 26) + B0;
const group = groupQuotient * 100 + groupRemainder;
const block = Math.floor(intPart / 16777216);
const wallet = intPart % 16777216;
checksum = parseInt(textAddress.slice(18, 20));
binaryAddress = decodeAddress({block, wallet, group, scope: 'public'});
addrChecksum = crc32(binaryAddress) % 100;
} else {
const block = parseInt(textAddress.slice(0, 10), 16);
const wallet = parseInt(textAddress.slice(10, 16), 16);
checksum = parseInt(textAddress.slice(16, 18), 16);
binaryAddress = decodeAddress({block, wallet, scope: 'private'});
addrChecksum = crc32(binaryAddress) % 256;
}
if (checksum !== addrChecksum) {
throw new Error('Invalid address checksum')
}
return binaryAddress;
},
hexToAddress(hex) {
const array = hex.match(/(.{1,2})/g);
return array.map(item => parseInt(item, 16));
},
textAddressToHex(address) {
const binaryAddress = AddressLib.parseTextAddress(address);
let result = '';
for (let i = 0; i < binaryAddress.length; i++) {
result += pad(16, binaryAddress[i], 2);
}
return result;
},
isTextAddressValid(textAddress) {
try {
AddressLib.parseTextAddress(textAddress)
} catch (e) {
return false;
}
return true;
},
hexToTextAddress(hexAddress) {
return AddressLib.encodeAddress(AddressLib.hexToAddress(hexAddress)).txt
}
};
module.exports = AddressLib;
function toChecksumAddress(address) {
// return address.toLowerCase();
// Ensure address is lowercase
const lowerAddress = address.toLowerCase().replace('0x', '');
//const hash = web3.utils.keccak256(lowerAddress);
const hash = CryptoJS.SHA3(lowerAddress, { outputLength: 256 }).toString();
let checksumAddress = '0x';
for (let i = 0; i < lowerAddress.length; i++) {
checksumAddress += parseInt(hash[i], 16) > 7 ? lowerAddress[i].toUpperCase() : lowerAddress[i];
}
return checksumAddress;
}