-
Notifications
You must be signed in to change notification settings - Fork 4
/
BTCAddrValidator.java
157 lines (132 loc) · 4.85 KB
/
BTCAddrValidator.java
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
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
public class BTCAddrValidator {
public static void main(String[] args) {
assert validate("1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i") == true;
assert validate("1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62j") == false;
assert validate("1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9") == true;
assert validate("1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62X") == false;
assert validate("1ANNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i") == false;
assert validate("1A Na15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i") == false;
assert validate("BZbvjr") == false;
assert validate("i55j") == false;
assert validate("1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62!") == false;
assert validate("1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62iz") == false;
assert validate("1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62izz") == false;
assert validate("1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nJ9") == false;
assert validate("1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62I") == false;
assert validate("3R2MPpTNQLCNs13qnHz89Rm82jQ27bAwft") == true;
assert validate("34QjytsE8GVRbUBvYNheftqJ5CHfDHvQRD") == true;
assert validate("3GsAUrD4dnCqtaTTUzzsQWoymHNkEFrgGF") == true;
assert validate("3NagLCvw8fLwtoUrK7s2mJPy9k6hoyWvTU") == true;
System.out.println("Test all passed");
}
private static final char[] ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".toCharArray();
private static final int[] INDEXES = new int[128];
private static final MessageDigest digest;
static {
try {
digest = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
for (int i = 0; i < INDEXES.length; i++) {
INDEXES[i] = -1;
}
for (int i = 0; i < ALPHABET.length; i++) {
INDEXES[ALPHABET[i]] = i;
}
}
public static boolean validate(String addr) {
try {
int addressHeader = getAddressHeader(addr);
return (addressHeader == 0 || addressHeader == 5);
} catch (Exception x) {
x.printStackTrace();
}
return false;
}
private static int getAddressHeader(String address) throws IOException {
byte[] tmp = decodeChecked(address);
return tmp[0] & 0xFF;
}
private static byte[] decodeChecked(String input) throws IOException {
byte[] tmp = decode(input);
if (tmp.length < 4)
throw new IOException("BTC AddressFormatException Input too short");
byte[] bytes = copyOfRange(tmp, 0, tmp.length - 4);
byte[] checksum = copyOfRange(tmp, tmp.length - 4, tmp.length);
tmp = doubleDigest(bytes);
byte[] hash = copyOfRange(tmp, 0, 4);
if (!Arrays.equals(checksum, hash))
throw new IOException("BTC AddressFormatException Checksum does not validate");
return bytes;
}
private static byte[] doubleDigest(byte[] input) {
return doubleDigest(input, 0, input.length);
}
private static byte[] doubleDigest(byte[] input, int offset, int length) {
synchronized (digest) {
digest.reset();
digest.update(input, offset, length);
byte[] first = digest.digest();
return digest.digest(first);
}
}
private static byte[] decode(String input) throws IOException {
if (input.length() == 0) {
return new byte[0];
}
byte[] input58 = new byte[input.length()];
// Transform the String to a base58 byte sequence
for (int i = 0; i < input.length(); ++i) {
char c = input.charAt(i);
int digit58 = -1;
if (c >= 0 && c < 128) {
digit58 = INDEXES[c];
}
if (digit58 < 0) {
throw new IOException("Bitcoin AddressFormatException Illegal character " + c + " at " + i);
}
input58[i] = (byte) digit58;
}
// Count leading zeroes
int zeroCount = 0;
while (zeroCount < input58.length && input58[zeroCount] == 0) {
++zeroCount;
}
// The encoding
byte[] temp = new byte[input.length()];
int j = temp.length;
int startAt = zeroCount;
while (startAt < input58.length) {
byte mod = divmod256(input58, startAt);
if (input58[startAt] == 0) {
++startAt;
}
temp[--j] = mod;
}
// Do no add extra leading zeroes, move j to first non null byte.
while (j < temp.length && temp[j] == 0) {
++j;
}
return copyOfRange(temp, j - zeroCount, temp.length);
}
private static byte divmod256(byte[] number58, int startAt) {
int remainder = 0;
for (int i = startAt; i < number58.length; i++) {
int digit58 = (int) number58[i] & 0xFF;
int temp = remainder * 58 + digit58;
number58[i] = (byte) (temp / 256);
remainder = temp % 256;
}
return (byte) remainder;
}
private static byte[] copyOfRange(byte[] source, int from, int to) {
byte[] range = new byte[to - from];
System.arraycopy(source, from, range, 0, range.length);
return range;
}
}