WalletToolsBTC implements IWalletTools { @Override public String getWalletAddress(IMasterPrivateKey master, String cryptoCurrency, int accountIndex, int chainIndex, int index) { byte[] serializedKey = Base58.decodeChecked(master.getPRV()); ByteBuffer buffer = ByteBuffer.wrap(serializedKey); int header = buffer.getInt(); int standard = -1; switch (header) { case XPRV: standard = STANDARD_BIP44; break; case YPRV: standard = STANDARD_BIP49; break; case ZPRV: standard = STANDARD_BIP84; break; } if (standard == -1) { return null; } DeterministicKey masterKey = MasterPrivateKeyBTC.getDeterministicKey(master.getPRV(),standard); if (standard == STANDARD_BIP44) { final DeterministicKey purposeKey = HDKeyDerivation.deriveChildKey(masterKey, new ChildNumber(STANDARD_BIP44, true)); final DeterministicKey coinKey = HDKeyDerivation.deriveChildKey(purposeKey, new ChildNumber(WalletTools.getCoinTypeByCryptoCurrency(cryptoCurrency), true)); final DeterministicKey accountKey = HDKeyDerivation.deriveChildKey(coinKey, new ChildNumber(accountIndex, true)); final DeterministicKey chainKey = HDKeyDerivation.deriveChildKey(accountKey, new ChildNumber(chainIndex, false)); final DeterministicKey walletKey = HDKeyDerivation.deriveChildKey(chainKey, new ChildNumber(index, false)); return Address.fromKey(MainNetParams.get(), walletKey, Script.ScriptType.P2PKH).toString(); } else if (standard == STANDARD_BIP49) { final DeterministicKey purposeKey = HDKeyDerivation.deriveChildKey(masterKey, new ChildNumber(STANDARD_BIP49, true)); final DeterministicKey coinKey = HDKeyDerivation.deriveChildKey(purposeKey, new ChildNumber(WalletTools.getCoinTypeByCryptoCurrency(cryptoCurrency), true)); final DeterministicKey accountKey = HDKeyDerivation.deriveChildKey(coinKey, new ChildNumber(accountIndex, true)); final DeterministicKey chainKey = HDKeyDerivation.deriveChildKey(accountKey, new ChildNumber(chainIndex, false)); final DeterministicKey walletKey = HDKeyDerivation.deriveChildKey(chainKey, new ChildNumber(index, false)); ByteBuffer bb = ByteBuffer.allocate(2+walletKey.getPubKeyHash().length); bb.put(new byte[]{0x00,0x14}); bb.put(walletKey.getPubKeyHash()); byte[] scriptSig = bb.array(); byte[] addressBytes = Utils.sha256hash160(scriptSig); return LegacyAddress.fromScriptHash(MainNetParams.get(),addressBytes).toString(); } else if (standard == STANDARD_BIP84) { final DeterministicKey purposeKey = HDKeyDerivation.deriveChildKey(masterKey, new ChildNumber(STANDARD_BIP84, true)); final DeterministicKey coinKey = HDKeyDerivation.deriveChildKey(purposeKey, new ChildNumber(WalletTools.getCoinTypeByCryptoCurrency(cryptoCurrency), true)); final DeterministicKey accountKey = HDKeyDerivation.deriveChildKey(coinKey, new ChildNumber(accountIndex, true)); final DeterministicKey chainKey = HDKeyDerivation.deriveChildKey(accountKey, new ChildNumber(chainIndex, false)); final DeterministicKey walletKey = HDKeyDerivation.deriveChildKey(chainKey, new ChildNumber(index, false)); return "bechadress"; } return null; }  @Override String generateSeedMnemonicSeparatedBySpaces(); MasterPrivateKeyBTC getMasterPrivateKey(String seedMnemonicSeparatedBySpaces, String password, String cryptoCurrency, int standard); @Override MasterPrivateKeyBTC getMasterPrivateKey(String prv, String cryptoCurrency, int standard); @Override String getWalletAddress(IMasterPrivateKey master, String cryptoCurrency, int accountIndex, int chainIndex, int index); @Override String getWalletAddressFromAccountPUB(String accountPUB, String cryptoCurrency, int chainIndex, int index); @Override String getWalletPrivateKey(IMasterPrivateKey master, String cryptoCurrency, int accountIndex, int chainIndex, int index); @Override String getAccountPUB(IMasterPrivateKey master, String cryptoCurrency, int accountIndex); @Override String getWalletAddressFromPrivateKey(String privateKey, String cryptoCurrency); boolean isAddressValid(String address, String cryptoCurrency); @Override ISignature sign(String privateKey, byte[] hashToSign, String cryptoCurrency); @Override Classification classify(String input, String cryptoCurrencyHint); @Override Classification classify(String input); @Override Set<String> supportedCryptoCurrencies(); String generateWalletPrivateKeyWithPrefix(String prefix, String cryptoCurrency); static final int XPUB; static final int XPRV; static final int YPUB; static final int YPRV; static final int ZPUB; static final int ZPRV; }
@Test public void getWalletAddress() { Assert.assertEquals("1CPrAD3jcUsfmFRedto6m9sG53zvCBLQS8", walletToolsBTC.getWalletAddress( walletToolsBTC.getMasterPrivateKey("have nothing", "password", "BTC", IWalletTools.STANDARD_BIP44), "BTC", 0, 2, 3)); Assert.assertEquals("38tMYJ2LtiikEp4MWwyctUK1kCMFk2W7R6", walletToolsBTC.getWalletAddress( walletToolsBTC.getMasterPrivateKey("have nothing", "password", "BTC", IWalletTools.STANDARD_BIP49), "BTC", 0, 2, 3)); Assert.assertEquals("bechadress", walletToolsBTC.getWalletAddress( walletToolsBTC.getMasterPrivateKey("have nothing", "password", "BTC", IWalletTools.STANDARD_BIP84), "BTC", 0, 2, 3)); }