Commit bedf8a1a authored by zeroleak's avatar zeroleak
Browse files

add HD_WalletFactoryGeneric.newWallet()

parent f5e9d228
......@@ -9,6 +9,8 @@ import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.crypto.MnemonicCode;
import org.bitcoinj.crypto.MnemonicException;
import java.io.IOException;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.List;
......@@ -31,8 +33,34 @@ public class HD_WalletFactoryGeneric {
this.mc = mc;
}
public HD_Wallet newWallet(String passphrase, NetworkParameters params) throws Exception {
return newWallet(12, passphrase, params);
}
public HD_Wallet newWallet(int nbWords, String passphrase, NetworkParameters params) throws IOException, MnemonicException.MnemonicLengthException {
if((nbWords % 3 != 0) || (nbWords < 12 || nbWords > 24)) {
nbWords = 12;
}
// len == 16 (12 words), len == 24 (18 words), len == 32 (24 words)
int len = (nbWords / 3) * 4;
if(passphrase == null) {
passphrase = "";
}
SecureRandom random = new SecureRandom();
byte seed[] = new byte[len];
random.nextBytes(seed);
HD_Wallet hdw = new HD_Wallet(44, mc, params, seed, passphrase);
return hdw;
}
public HD_Wallet restoreWallet(String data, String passphrase, NetworkParameters params)
throws AddressFormatException, DecoderException,
throws AddressFormatException, DecoderException,
MnemonicException.MnemonicLengthException, MnemonicException.MnemonicWordException,
MnemonicException.MnemonicChecksumException {
......
......@@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.samourai.code.wallet</groupId>
<artifactId>extlibj</artifactId>
<version>0.0.24</version>
<version>0.0.25-SNAPSHOT</version>
<name>extlibj</name>
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
......
package com.samourai.wallet.hd;
import java.math.BigInteger;
import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.params.MainNetParams;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.math.BigInteger;
public class HD_WalletFactoryGenericTest {
private static final Logger log = LoggerFactory.getLogger(HD_WalletFactoryGenericTest.class);
private HD_WalletFactoryGeneric hdWalletFactory = HD_WalletFactoryGeneric.getInstance();
@Test
......@@ -40,10 +44,7 @@ public class HD_WalletFactoryGenericTest {
@Test
public void computeSeedFromWords() throws Exception {
NetworkParameters params = MainNetParams.get();
HD_Wallet hdWallet;
byte[] mSeed;
String passphrase = "TREZOR";
// https://github.com/trezor/python-mnemonic/blob/master/vectors.json
......@@ -59,4 +60,27 @@ public class HD_WalletFactoryGenericTest {
mSeed = hdWalletFactory.computeSeedFromWords("void come effort suffer camp survey warrior heavy shoot primary clutch crush open amazing screen patrol group space point ten exist slush involve unfold");
Assertions.assertEquals("f585c11aec520db57dd353c69554b21a89b20fb0650966fa0a9d6f74fd989d8f", org.bouncycastle.util.encoders.Hex.toHexString(mSeed));
}
@Test
public void newWallet() throws Exception {
NetworkParameters params = MainNetParams.get();
String passphrase = "test";
HD_Wallet hdw1 = hdWalletFactory.newWallet(passphrase, params);
verifyNewWallet(hdw1, passphrase);
HD_Wallet hdw2 = hdWalletFactory.newWallet(passphrase, params);
verifyNewWallet(hdw2, passphrase);
Assertions.assertNotEquals(hdw1.getSeedHex(), hdw2.getSeedHex());
Assertions.assertNotEquals(hdw1.getMnemonic(), hdw2.getMnemonic());
}
private void verifyNewWallet(HD_Wallet hdw, String passphrase) {
if (log.isDebugEnabled()) {
log.debug("verifyNewWallet: "+hdw.getMnemonic());
}
Assertions.assertEquals(12, hdw.getMnemonic().split(" ").length);
Assertions.assertEquals(passphrase, hdw.getPassphrase());
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment