Sitelet https://github.com/schildbach/bitcoinj/commit/771835e97a5b93a40c2d94ff1b15061a142c3655
Skip to content

Commit 771835e

Browse files
committed
Base58: protect alphabet from mutation
Make the constant `ALPHABET` private and provide an accessor `alphabet()` to use instead. Add tests to verify the alphabet cannot be mutated any more.
1 parent 19d4d17 commit 771835e

2 files changed

Lines changed: 55 additions & 3 deletions

File tree

‎base/src/main/java/org/bitcoinj/base/Base58.java‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/*
2-
* Copyright 2011 Google Inc.
3-
* Copyright 2018 Andreas Schildbach
2+
* Copyright by the original author or authors.
43
*
54
* Licensed under the Apache License, Version 2.0 (the "License");
65
* you may not use this file except in compliance with the License.
@@ -21,7 +20,10 @@
2120
import org.bitcoinj.base.internal.ByteUtils;
2221

2322
import java.math.BigInteger;
23+
import java.nio.CharBuffer;
2424
import java.util.Arrays;
25+
import java.util.List;
26+
import java.util.stream.Collectors;
2527

2628
/**
2729
* Base58 is a way to encode Bitcoin addresses (or arbitrary data) as alphanumeric strings.
@@ -48,7 +50,7 @@
4850
* numbers), and finally represent the resulting base-58 digits as alphanumeric ASCII characters.
4951
*/
5052
public class Base58 {
51-
public static final char[] ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".toCharArray();
53+
private static final char[] ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".toCharArray();
5254
private static final char ENCODED_ZERO = ALPHABET[0];
5355
private static final int[] INDEXES = new int[128];
5456
static {
@@ -58,6 +60,18 @@ public class Base58 {
5860
}
5961
}
6062

63+
/**
64+
* Get the Base58 alphabet.
65+
*
66+
* @return alphabet as a list of characters
67+
*/
68+
public static List<Character> alphabet() {
69+
return CharBuffer.wrap(ALPHABET)
70+
.chars()
71+
.mapToObj(c -> (char) c)
72+
.collect(Collectors.toList());
73+
}
74+
6175
/**
6276
* Encodes the given bytes as a base58 string (no checksum is appended).
6377
*

‎base/src/test/java/Base58Test.java‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright by the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import org.bitcoinj.base.Base58;
18+
import org.junit.Test;
19+
20+
import java.util.List;
21+
22+
import static org.junit.Assert.assertEquals;
23+
24+
public class Base58Test {
25+
@Test
26+
public void alphabetHas58Chars() {
27+
assertEquals(58, Base58.alphabet().size());
28+
}
29+
30+
@Test
31+
public void alphabetIsImmutable() {
32+
List<Character> alphabet1 = Base58.alphabet();
33+
char previousChar = alphabet1.remove(0);
34+
alphabet1.add(0, '₿');
35+
List<Character> alphabet2 = Base58.alphabet();
36+
assertEquals(previousChar, alphabet2.get(0).charValue());
37+
}
38+
}

0 commit comments

Comments
 (0)