Commit 7288971c authored by zeroleak's avatar zeroleak
Browse files

fix hdWallet.getXPUBs()

parent 185965b4
......@@ -12,8 +12,6 @@ import org.bitcoinj.crypto.MnemonicException;
*/
public class BIP47Wallet extends HD_Wallet {
private BIP47Account mAccount = null;
/**
* Constructor for wallet.
*
......@@ -25,11 +23,7 @@ public class BIP47Wallet extends HD_Wallet {
*
*/
public BIP47Wallet(int purpose, MnemonicCode mc, NetworkParameters params, byte[] seed, String passphrase) throws MnemonicException.MnemonicLengthException {
super(purpose, mc, params, seed, passphrase);
mAccount = new BIP47Account(params, mRoot, 0);
}
/**
......@@ -37,22 +31,7 @@ public class BIP47Wallet extends HD_Wallet {
* @param hdWallet
*/
public BIP47Wallet(HD_Wallet hdWallet) {
this(47, hdWallet);
}
/**
* Constructor for wallet.
*
* @param int purpose
* @param HD_Wallet hdWallet to copy from
*
*/
public BIP47Wallet(int purpose, HD_Wallet hdWallet) {
super(purpose, hdWallet);
mAccount = new BIP47Account(mParams, mRoot, 0);
super(47, hdWallet);
}
/**
......@@ -63,8 +42,9 @@ public class BIP47Wallet extends HD_Wallet {
* @return Account
*
*/
@Override
public BIP47Account getAccount(int accountId) {
return mAccount;
return new BIP47Account(mParams, mRoot, 0);
}
}
......@@ -10,7 +10,10 @@ import org.bitcoinj.crypto.*;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.*;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class HD_Wallet {
private byte[] mSeed = null;
......@@ -19,9 +22,11 @@ public class HD_Wallet {
protected DeterministicKey mRoot = null; // null when created from xpub
protected ArrayList<HD_Account> mXpubAccounts = null;
protected Map<Integer,HD_Account> mAccounts = null;
// contains xpub of #account0, or all xpubs from constructor
protected String[] xpubs = null;
protected NetworkParameters mParams = null;
private HD_Wallet() { ; }
......@@ -41,11 +46,13 @@ public class HD_Wallet {
// compute rootKey for accounts
this.mRoot = computeRootKey(purpose, mWordList, strPassphrase, mParams);
this.mXpubAccounts = new ArrayList<>();
// initialize accounts
// initialize mAccounts with account #0
mAccounts = new LinkedHashMap<>();
getAccount(0);
HD_Account hdAccount = getAccount(0);
// xpubs will only contain account #0 (even if mAccounts contains more accounts)
xpubs = new String[]{hdAccount.xpubstr()};
}
public HD_Wallet(int purpose, HD_Wallet inputWallet) {
......@@ -57,12 +64,14 @@ public class HD_Wallet {
*/
public HD_Wallet(NetworkParameters params, String[] xpub) throws AddressFormatException {
mParams = params;
mAccounts = new LinkedHashMap<>();
// initialize xpubAccounts
this.mXpubAccounts = new ArrayList<>();
// initialize mAccounts and xpubs
mAccounts = new LinkedHashMap<>();
xpubs = new String[xpub.length];
for(int i = 0; i < xpub.length; i++) {
mXpubAccounts.add(new HD_Account(mParams, xpub[i], i));
HD_Account account = new HD_Account(mParams, xpub[i], i);
mAccounts.put(i, account);
xpubs[i] = account.xpubstr();
}
}
......@@ -105,11 +114,7 @@ public class HD_Wallet {
}
public String[] getXPUBs() {
String[] ret = new String[mXpubAccounts.size()];
for(int i = 0; i < mXpubAccounts.size(); i++) {
ret[i] = mXpubAccounts.get(i).xpubstr();
}
return ret;
return xpubs;
}
public byte[] getFingerprint() {
......
......@@ -38,7 +38,7 @@ public class CryptoTestUtil {
public BIP47Wallet generateBip47Wallet(NetworkParameters networkParameters) throws Exception {
HD_Wallet bip44Wallet = generateWallet(44, networkParameters);
BIP47Wallet bip47Wallet = new BIP47Wallet(47, bip44Wallet);
BIP47Wallet bip47Wallet = new BIP47Wallet(bip44Wallet);
return bip47Wallet;
}
......
......@@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.samourai.code.wallet</groupId>
<artifactId>extlibj</artifactId>
<version>0.0.23</version>
<version>0.0.24-SNAPSHOT</version>
<name>extlibj</name>
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
......
......@@ -112,8 +112,19 @@ public class HD_WalletTest {
Assertions.assertEquals("bc1qhy6dh6c67q8uwshffrs2rjs85x4wyhx9k45rha", hdWallet2.getAddressAt(0, 0, 0).getAddressString(AddressType.SEGWIT_NATIVE));
}
@Test
public void testHdWalletMainnetXpub() throws Exception {
NetworkParameters params = MainNetParams.get();
HD_Wallet hdWallet1 = new HD_Wallet(params, new String[]{"xpub6By39V6HgpxbtuBVMpGDWPDFaBpMqEewX1KV45eXUZkvoV5TVgr9dvi5MkxtRrdovbngSAJtHR3mau3a2b9hmnTR9G7zjXozwqDBaHFPT5j"});
// verify
verifyWallet1Mainnet(hdWallet1);
}
private void verifyWallet1Mainnet(HD_Wallet hdWallet1) {
Assertions.assertArrayEquals(new String[]{"xpub6By39V6HgpxbtuBVMpGDWPDFaBpMqEewX1KV45eXUZkvoV5TVgr9dvi5MkxtRrdovbngSAJtHR3mau3a2b9hmnTR9G7zjXozwqDBaHFPT5j"}, hdWallet1.getXPUBs());
Assertions.assertEquals("xpub6By39V6HgpxbtuBVMpGDWPDFaBpMqEewX1KV45eXUZkvoV5TVgr9dvi5MkxtRrdovbngSAJtHR3mau3a2b9hmnTR9G7zjXozwqDBaHFPT5j", hdWallet1.getAccount(0).xpubstr());
Assertions.assertEquals("1C36vErfBHdZPnrB5vMh6fRxnZ3RfRr8eW", hdWallet1.getAccount(0).getChain(0).getAddressAt(0).getAddressString());
Assertions.assertEquals("19pAMZjGAy3C4uVREZKK959jhRynUJ6hhD", hdWallet1.getAccount(0).getChain(1).getAddressAt(0).getAddressString());
Assertions.assertEquals("1b1C6KHtjXb5Ln2UFMwxNpuZbuQsmdrGv", hdWallet1.getAccount(0).getChain(1).getAddressAt(1).getAddressString());
......
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