Skip to content

Commit 5f034ed

Browse files
committed
删除多余的工具类代码
1 parent ff23ab0 commit 5f034ed

File tree

6 files changed

+66
-380
lines changed

6 files changed

+66
-380
lines changed

src/main/java/one/wangwei/blockchain/block/Block.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import one.wangwei.blockchain.pow.ProofOfWork;
99
import one.wangwei.blockchain.transaction.MerkleTree;
1010
import one.wangwei.blockchain.transaction.Transaction;
11-
import org.apache.commons.codec.binary.Hex;
11+
import one.wangwei.blockchain.util.ByteUtils;
1212

1313
import java.time.Instant;
1414

@@ -24,8 +24,6 @@
2424
@ToString
2525
public class Block {
2626

27-
private static final String ZERO_HASH = Hex.encodeHexString(new byte[32]);
28-
2927
/**
3028
* 区块hash值
3129
*/
@@ -47,15 +45,14 @@ public class Block {
4745
*/
4846
private long nonce;
4947

50-
5148
/**
5249
* <p> 创建创世区块 </p>
5350
*
5451
* @param coinbase
5552
* @return
5653
*/
5754
public static Block newGenesisBlock(Transaction coinbase) {
58-
return Block.newBlock(ZERO_HASH, new Transaction[]{coinbase});
55+
return Block.newBlock(ByteUtils.ZERO_HASH, new Transaction[]{coinbase});
5956
}
6057

6158
/**

src/main/java/one/wangwei/blockchain/util/Base58Check.java

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,34 @@
55
* https://www.nayuki.io/page/bitcoin-cryptography-library
66
* https://github.com/nayuki/Bitcoin-Cryptography-Library
77
*/
8-
98
package one.wangwei.blockchain.util;
109

1110
import java.io.ByteArrayOutputStream;
1211
import java.io.IOException;
1312
import java.math.BigInteger;
1413
import java.util.Arrays;
1514

16-
1715
/**
18-
* Converts between an array of bytes and a Base58Check string. Not instantiable.
16+
* Base58 转化工具
1917
*/
2018
public final class Base58Check {
2119

22-
/*---- Static functions ----*/
23-
24-
// Adds the checksum and converts to Base58Check. Note that the caller needs to prepend the version byte(s).
20+
/**
21+
* 添加校验码并转化为 Base58 字符串
22+
*
23+
* @param data
24+
* @return
25+
*/
2526
public static String bytesToBase58(byte[] data) {
2627
return rawBytesToBase58(addCheckHash(data));
2728
}
2829

29-
// Directly converts to Base58Check without adding a checksum.
30+
/**
31+
* 转化为 Base58 字符串
32+
*
33+
* @param data
34+
* @return
35+
*/
3036
public static String rawBytesToBase58(byte[] data) {
3137
// Convert to base-58 string
3238
StringBuilder sb = new StringBuilder();
@@ -38,16 +44,22 @@ public static String rawBytesToBase58(byte[] data) {
3844
}
3945

4046
// Add '1' characters for leading 0-value bytes
41-
for (int i = 0; i < data.length && data[i] == 0; i++)
47+
for (int i = 0; i < data.length && data[i] == 0; i++) {
4248
sb.append(ALPHABET.charAt(0));
49+
}
4350
return sb.reverse().toString();
4451
}
4552

4653

47-
// Returns a new byte array by concatenating the given array with its checksum.
54+
/**
55+
* 添加校验码并返回待有校验码的原生数据
56+
*
57+
* @param data
58+
* @return
59+
*/
4860
static byte[] addCheckHash(byte[] data) {
4961
try {
50-
byte[] hash = Arrays.copyOf(Sha256.getDoubleHash(data).toBytes(), 4);
62+
byte[] hash = Arrays.copyOf(BtcAddressUtils.doubleHash(data), 4);
5163
ByteArrayOutputStream buf = new ByteArrayOutputStream();
5264
buf.write(data);
5365
buf.write(hash);
@@ -58,41 +70,53 @@ static byte[] addCheckHash(byte[] data) {
5870
}
5971

6072

61-
// Converts the given Base58Check string to a byte array, verifies the checksum, and removes the checksum to return the payload.
62-
// The caller is responsible for handling the version byte(s).
73+
/**
74+
* 将 Base58Check 字符串转化为 byte 数组,并校验其校验码
75+
* 返回的byte数组带有版本号,但不带有校验码
76+
*
77+
* @param s
78+
* @return
79+
*/
6380
public static byte[] base58ToBytes(String s) {
6481
byte[] concat = base58ToRawBytes(s);
6582
byte[] data = Arrays.copyOf(concat, concat.length - 4);
6683
byte[] hash = Arrays.copyOfRange(concat, concat.length - 4, concat.length);
67-
byte[] rehash = Arrays.copyOf(Sha256.getDoubleHash(data).toBytes(), 4);
68-
if (!Arrays.equals(rehash, hash))
84+
byte[] rehash = Arrays.copyOf(BtcAddressUtils.doubleHash(data), 4);
85+
if (!Arrays.equals(rehash, hash)) {
6986
throw new IllegalArgumentException("Checksum mismatch");
87+
}
7088
return data;
7189
}
7290

7391

74-
// Converts the given Base58Check string to a byte array, without checking or removing the trailing 4-byte checksum.
92+
/**
93+
* 将 Base58Check 字符串反转为 byte 数组
94+
*
95+
* @param s
96+
* @return
97+
*/
7598
static byte[] base58ToRawBytes(String s) {
7699
// Parse base-58 string
77100
BigInteger num = BigInteger.ZERO;
78101
for (int i = 0; i < s.length(); i++) {
79102
num = num.multiply(ALPHABET_SIZE);
80103
int digit = ALPHABET.indexOf(s.charAt(i));
81-
if (digit == -1)
104+
if (digit == -1) {
82105
throw new IllegalArgumentException("Invalid character for Base58Check");
106+
}
83107
num = num.add(BigInteger.valueOf(digit));
84108
}
85-
86109
// Strip possible leading zero due to mandatory sign bit
87110
byte[] b = num.toByteArray();
88-
if (b[0] == 0)
111+
if (b[0] == 0) {
89112
b = Arrays.copyOfRange(b, 1, b.length);
90-
113+
}
91114
try {
92115
// Convert leading '1' characters to leading 0-value bytes
93116
ByteArrayOutputStream buf = new ByteArrayOutputStream();
94-
for (int i = 0; i < s.length() && s.charAt(i) == ALPHABET.charAt(0); i++)
117+
for (int i = 0; i < s.length() && s.charAt(i) == ALPHABET.charAt(0); i++) {
95118
buf.write(0);
119+
}
96120
buf.write(b);
97121
return buf.toByteArray();
98122
} catch (IOException e) {

src/main/java/one/wangwei/blockchain/util/BtcAddressUtils.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@
1111
* @date 2018/03/21
1212
*/
1313
public class BtcAddressUtils {
14+
15+
/**
16+
* 双重Hash
17+
*
18+
* @param data
19+
* @return
20+
*/
21+
public static byte[] doubleHash(byte[] data) {
22+
return DigestUtils.sha256(DigestUtils.sha256(data));
23+
}
1424

1525
/**
1626
* 计算公钥的 RIPEMD160 Hash值
@@ -28,16 +38,14 @@ public static byte[] ripeMD160Hash(byte[] pubKey) {
2838
return output;
2939
}
3040

31-
3241
/**
3342
* 生成公钥的校验码
3443
*
3544
* @param payload
3645
* @return
3746
*/
3847
public static byte[] checksum(byte[] payload) {
39-
byte[] firstSHA = DigestUtils.sha256(payload);
40-
byte[] secondSHA = DigestUtils.sha256(firstSHA);
41-
return Arrays.copyOfRange(secondSHA, 0, 4);
48+
return Arrays.copyOfRange(doubleHash(payload), 0, 4);
4249
}
50+
4351
}

src/main/java/one/wangwei/blockchain/util/ByteUtils.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package one.wangwei.blockchain.util;
22

3+
import org.apache.commons.codec.binary.Hex;
34
import org.apache.commons.lang3.ArrayUtils;
45

56
import java.nio.ByteBuffer;
@@ -14,6 +15,12 @@
1415
*/
1516
public class ByteUtils {
1617

18+
public static final byte[] EMPTY_ARRAY = new byte[0];
19+
20+
public static final byte[] EMPTY_BYTES = new byte[32];
21+
22+
public static final String ZERO_HASH = Hex.encodeHexString(EMPTY_BYTES);
23+
1724
/**
1825
* 将多个字节数组合并成一个字节数组
1926
*
@@ -22,7 +29,7 @@ public class ByteUtils {
2229
*/
2330
public static byte[] merge(byte[]... bytes) {
2431
Stream<Byte> stream = Stream.of();
25-
for (byte[] b : bytes) {
32+
for (byte[] b: bytes) {
2633
stream = Stream.concat(stream, Arrays.stream(ArrayUtils.toObject(b)));
2734
}
2835
return ArrayUtils.toPrimitive(stream.toArray(Byte[]::new));

0 commit comments

Comments
 (0)