3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-14 19:25:51 +00:00

Add a default c'tor to MnemonicCode and simplify the unit tests.

This commit is contained in:
Mike Hearn 2013-11-24 23:59:25 +01:00
parent 95fc6d7245
commit 8769773717
2 changed files with 30 additions and 64 deletions

View File

@ -16,11 +16,15 @@
package com.google.bitcoin.crypto; 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.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.ArrayList; import java.util.ArrayList;
@ -28,12 +32,6 @@ import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; 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 * 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 * 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 { public class MnemonicCode {
private ArrayList<String> wordList;
private ArrayList<String> wordList; public static String BIP39_ENGLISH_SHA256 = "ad90bf3beb7b0eb7e5acd74727dc0da96e0a280a258354e7293fb7e211ac03db";
public static String BIP0039_ENGLISH_SHA256 = public MnemonicCode() throws IOException {
"ad90bf3beb7b0eb7e5acd74727dc0da96e0a280a258354e7293fb7e211ac03db"; this(MnemonicCode.class.getResourceAsStream("mnemonic/wordlist/english.txt"), BIP39_ENGLISH_SHA256);
}
/** /**
* Creates an MnemonicCode object, initializing with words read * Creates an MnemonicCode object, initializing with words read from the supplied input stream. If a wordListDigest
* from the supplied input stream. If a wordListDigest is * is supplied the digest of the words will be checked.
* supplied the digest of the words will be checked.
*/ */
public MnemonicCode(InputStream wordstream, String wordListDigest) public MnemonicCode(InputStream wordstream, String wordListDigest) throws IOException, IllegalArgumentException {
throws IOException, IllegalArgumentException {
BufferedReader br = new BufferedReader(new InputStreamReader(wordstream, "UTF-8")); BufferedReader br = new BufferedReader(new InputStreamReader(wordstream, "UTF-8"));
String word; String word;
this.wordList = new ArrayList<String>(); this.wordList = new ArrayList<String>();

View File

@ -16,19 +16,19 @@
package com.google.bitcoin.crypto; package com.google.bitcoin.crypto;
import com.google.common.base.Joiner;
import org.junit.Before;
import org.junit.Test; 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.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import org.spongycastle.util.encoders.Hex;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
public class MnemonicCodeTest { public class MnemonicCodeTest {
// These vectors are from https://raw.github.com/trezor/python-mnemonic/master/vectors.json // These vectors are from https://raw.github.com/trezor/python-mnemonic/master/vectors.json
String vectors[] = { String vectors[] = {
@ -104,71 +104,39 @@ public class MnemonicCodeTest {
"c87c135433c16f1ecbf9919dc53dd9f30f85824dc7264d4e1bd644826c902be2", "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", "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 @Test
public void testEncodeVectors() throws Exception { 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) { for (int ii = 0; ii < vectors.length; ii += 2) {
List<String> words = mc.encode(Hex.decode(vectors[ii])); List<String> words = mc.encode(Hex.decode(vectors[ii]));
assertEquals(vectors[ii+1], join(words)); assertEquals(vectors[ii + 1], Joiner.on(' ').join(words));
} }
} }
@Test @Test
public void testDecodeVectors() throws Exception { 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) { for (int ii = 0; ii < vectors.length; ii += 2) {
byte[] seed = mc.decode(split(vectors[ii+1])); byte[] seed = mc.decode(split(vectors[ii+1]));
assertEquals(vectors[ii], new String(Hex.encode(seed))); assertEquals(vectors[ii], new String(Hex.encode(seed)));
} }
} }
@Test @Test(expected = IllegalArgumentException.class)
public void testBadSeedLength() throws Exception { public void testBadSeedLength() throws Exception {
InputStream wordstream = getClass().getResourceAsStream("mnemonic/wordlist/english.txt"); byte[] seed = Hex.decode("7f7f7f7f7f7f7f7f7f7f7f7f7f7f");
MnemonicCode mc = new MnemonicCode(wordstream, MnemonicCode.BIP0039_ENGLISH_SHA256); mc.encode(seed);
boolean sawException = false;
try {
byte[] seed = Hex.decode("7f7f7f7f7f7f7f7f7f7f7f7f7f7f");
List<String> words = mc.encode(seed);
} catch (IllegalArgumentException ex) {
sawException = true;
}
assertEquals(true, sawException);
} }
@Test @Test(expected = IllegalArgumentException.class)
public void testBadWordsLength() throws Exception { public void testBadWordsLength() throws Exception {
InputStream wordstream = getClass().getResourceAsStream("mnemonic/wordlist/english.txt"); List<String> words = split("risk tiger venture dinner age assume float denial penalty");
MnemonicCode mc = new MnemonicCode(wordstream, MnemonicCode.BIP0039_ENGLISH_SHA256); mc.decode(words);
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();
} }
static public List<String> split(String words) { static public List<String> split(String words) {