mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-02-12 10:15:52 +00:00
Add a default c'tor to MnemonicCode and simplify the unit tests.
This commit is contained in:
parent
95fc6d7245
commit
8769773717
@ -16,11 +16,15 @@
|
||||
|
||||
package com.google.bitcoin.crypto;
|
||||
|
||||
import com.google.bitcoin.core.Sha256Hash;
|
||||
import org.spongycastle.crypto.engines.RijndaelEngine;
|
||||
import org.spongycastle.crypto.params.KeyParameter;
|
||||
import org.spongycastle.util.encoders.Hex;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.ArrayList;
|
||||
@ -28,12 +32,6 @@ import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.spongycastle.crypto.engines.RijndaelEngine;
|
||||
import org.spongycastle.crypto.params.KeyParameter;
|
||||
import org.spongycastle.util.encoders.Hex;
|
||||
|
||||
import com.google.bitcoin.core.Sha256Hash;
|
||||
|
||||
/**
|
||||
* A MnemonicCode object may be used to convert between binary seed values and
|
||||
* lists of words per <a href="https://en.bitcoin.it/wiki/BIP_0039">the BIP 39
|
||||
@ -45,19 +43,19 @@ import com.google.bitcoin.core.Sha256Hash;
|
||||
*/
|
||||
|
||||
public class MnemonicCode {
|
||||
|
||||
private ArrayList<String> wordList;
|
||||
|
||||
public static String BIP0039_ENGLISH_SHA256 =
|
||||
"ad90bf3beb7b0eb7e5acd74727dc0da96e0a280a258354e7293fb7e211ac03db";
|
||||
public static String BIP39_ENGLISH_SHA256 = "ad90bf3beb7b0eb7e5acd74727dc0da96e0a280a258354e7293fb7e211ac03db";
|
||||
|
||||
public MnemonicCode() throws IOException {
|
||||
this(MnemonicCode.class.getResourceAsStream("mnemonic/wordlist/english.txt"), BIP39_ENGLISH_SHA256);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an MnemonicCode object, initializing with words read
|
||||
* from the supplied input stream. If a wordListDigest is
|
||||
* supplied the digest of the words will be checked.
|
||||
* Creates an MnemonicCode object, initializing with words read from the supplied input stream. If a wordListDigest
|
||||
* is supplied the digest of the words will be checked.
|
||||
*/
|
||||
public MnemonicCode(InputStream wordstream, String wordListDigest)
|
||||
throws IOException, IllegalArgumentException {
|
||||
public MnemonicCode(InputStream wordstream, String wordListDigest) throws IOException, IllegalArgumentException {
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(wordstream, "UTF-8"));
|
||||
String word;
|
||||
this.wordList = new ArrayList<String>();
|
||||
|
@ -16,19 +16,19 @@
|
||||
|
||||
package com.google.bitcoin.crypto;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.spongycastle.util.encoders.Hex;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.spongycastle.util.encoders.Hex;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class MnemonicCodeTest {
|
||||
|
||||
// These vectors are from https://raw.github.com/trezor/python-mnemonic/master/vectors.json
|
||||
String vectors[] = {
|
||||
|
||||
@ -104,71 +104,39 @@ public class MnemonicCodeTest {
|
||||
"c87c135433c16f1ecbf9919dc53dd9f30f85824dc7264d4e1bd644826c902be2",
|
||||
"upper will wisdom term once bean blur inquiry used bamboo frequent hamster amazing cake attack any author mimic leopard day token joy install company",
|
||||
};
|
||||
private MnemonicCode mc;
|
||||
|
||||
@Before
|
||||
public void setup() throws IOException {
|
||||
mc = new MnemonicCode();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEncodeVectors() throws Exception {
|
||||
InputStream wordstream = getClass().getResourceAsStream("mnemonic/wordlist/english.txt");
|
||||
MnemonicCode mc = new MnemonicCode(wordstream, MnemonicCode.BIP0039_ENGLISH_SHA256);
|
||||
|
||||
for (int ii = 0; ii < vectors.length; ii += 2) {
|
||||
List<String> words = mc.encode(Hex.decode(vectors[ii]));
|
||||
assertEquals(vectors[ii+1], join(words));
|
||||
assertEquals(vectors[ii + 1], Joiner.on(' ').join(words));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecodeVectors() throws Exception {
|
||||
InputStream wordstream = getClass().getResourceAsStream("mnemonic/wordlist/english.txt");
|
||||
MnemonicCode mc = new MnemonicCode(wordstream, MnemonicCode.BIP0039_ENGLISH_SHA256);
|
||||
|
||||
for (int ii = 0; ii < vectors.length; ii += 2) {
|
||||
byte[] seed = mc.decode(split(vectors[ii+1]));
|
||||
assertEquals(vectors[ii], new String(Hex.encode(seed)));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testBadSeedLength() throws Exception {
|
||||
InputStream wordstream = getClass().getResourceAsStream("mnemonic/wordlist/english.txt");
|
||||
MnemonicCode mc = new MnemonicCode(wordstream, MnemonicCode.BIP0039_ENGLISH_SHA256);
|
||||
|
||||
boolean sawException = false;
|
||||
try {
|
||||
byte[] seed = Hex.decode("7f7f7f7f7f7f7f7f7f7f7f7f7f7f");
|
||||
List<String> words = mc.encode(seed);
|
||||
} catch (IllegalArgumentException ex) {
|
||||
sawException = true;
|
||||
}
|
||||
assertEquals(true, sawException);
|
||||
mc.encode(seed);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testBadWordsLength() throws Exception {
|
||||
InputStream wordstream = getClass().getResourceAsStream("mnemonic/wordlist/english.txt");
|
||||
MnemonicCode mc = new MnemonicCode(wordstream, MnemonicCode.BIP0039_ENGLISH_SHA256);
|
||||
|
||||
boolean sawException = false;
|
||||
try {
|
||||
List<String> words = split("risk tiger venture dinner age assume float denial penalty");
|
||||
byte[] seed = mc.decode(words);
|
||||
} catch (IllegalArgumentException ex) {
|
||||
sawException = true;
|
||||
}
|
||||
assertEquals(true, sawException);
|
||||
}
|
||||
|
||||
static public String join(List<String> list) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
boolean first = true;
|
||||
for (String item : list)
|
||||
{
|
||||
if (first)
|
||||
first = false;
|
||||
else
|
||||
sb.append(" ");
|
||||
sb.append(item);
|
||||
}
|
||||
return sb.toString();
|
||||
mc.decode(words);
|
||||
}
|
||||
|
||||
static public List<String> split(String words) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user