This repository has been archived by the owner on Jan 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddress-lib.js
186 lines (156 loc) · 5.78 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
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;