diff --git a/core/src/main/java/com/bloxbean/cardano/client/account/Account.java b/core/src/main/java/com/bloxbean/cardano/client/account/Account.java index cf70341b..5299817b 100644 --- a/core/src/main/java/com/bloxbean/cardano/client/account/Account.java +++ b/core/src/main/java/com/bloxbean/cardano/client/account/Account.java @@ -196,7 +196,7 @@ private Account(Network network, String mnemonic, byte[] rootKey, byte[] account this.network = network; this.derivationPath = derivationPath; - if (mnemonic != null && mnemonic.length() > 0) { + if (mnemonic != null && !mnemonic.isEmpty()) { this.mnemonic = mnemonic; this.accountKey = null; MnemonicUtil.validateMnemonic(this.mnemonic); @@ -656,7 +656,7 @@ public Transaction signWithCommitteeHotKey(Transaction transaction) { } public Optional getRootKeyPair() { - if (mnemonic != null && mnemonic.length() > 0) { + if (mnemonic != null && !mnemonic.isEmpty()) { return Optional.of(new CIP1852().getRootKeyPairFromMnemonic(mnemonic)); } else if (rootKey != null && rootKey.length > 0) { return Optional.of(new CIP1852().getRootKeyPairFromRootKey(rootKey)); @@ -699,7 +699,7 @@ private HdKeyPair getCommitteeHotKeyPair() { private HdKeyPair getHdKeyPairFromDerivationPath(DerivationPath derivationPath) { HdKeyPair hdKeyPair; - if (mnemonic != null && mnemonic.trim().length() > 0) { + if (mnemonic != null && !mnemonic.isEmpty()) { hdKeyPair = new CIP1852().getKeyPairFromMnemonic(mnemonic, derivationPath); } else if (accountKey != null && accountKey.length > 0) { hdKeyPair = new CIP1852().getKeyPairFromAccountKey(this.accountKey, derivationPath); diff --git a/core/src/test/java/com/bloxbean/cardano/client/account/AccountTest.java b/core/src/test/java/com/bloxbean/cardano/client/account/AccountTest.java index 23a320eb..ec78c762 100644 --- a/core/src/test/java/com/bloxbean/cardano/client/account/AccountTest.java +++ b/core/src/test/java/com/bloxbean/cardano/client/account/AccountTest.java @@ -442,8 +442,7 @@ void testAccountFromRootKey() { Account account = Account.createFromRootKey(Networks.testnet(), rootKeyBytes); //expected - //var seedPhrase = - // "tragic movie pulp rely quick damage spoil case bubble forget banana bomb pilot fresh trumpet learn basic melt curtain defy erode soccer race oil"; + //tragic movie pulp rely quick damage spoil case bubble forget banana bomb pilot fresh trumpet learn basic melt curtain defy erode soccer race oil assertThat(account.baseAddress()).isEqualTo("addr_test1qzm0439fe55aynh58qcn4jnh4mwuqwr5n5fez7j0hck9ds8j3nmg5pkqfur4gyupppuu82r83s5eheewzmf6fwlzfz7qzsp6rc"); assertThat(account.changeAddress()).isEqualTo("addr_test1qqkqwker9785sna30vmjggynjxzce6sdg2th7w3w0sgfvr8j3nmg5pkqfur4gyupppuu82r83s5eheewzmf6fwlzfz7q5r7hzu"); @@ -474,7 +473,6 @@ void testAccountFromRootKey_128Bytes_throwsException() { assertThrows(Exception.class, () -> { Account.createFromRootKey(Networks.testnet(), rootKey); - ; }); } diff --git a/crypto/src/main/java/com/bloxbean/cardano/client/crypto/cip1852/CIP1852.java b/crypto/src/main/java/com/bloxbean/cardano/client/crypto/cip1852/CIP1852.java index 41624863..52e5942b 100644 --- a/crypto/src/main/java/com/bloxbean/cardano/client/crypto/cip1852/CIP1852.java +++ b/crypto/src/main/java/com/bloxbean/cardano/client/crypto/cip1852/CIP1852.java @@ -61,8 +61,7 @@ public HdKeyPair getKeyPairFromEntropy(byte[] entropy, DerivationPath derivation HdKeyPair accountKey = hdKeyGenerator.getChildKeyPair(coinTypeKey, derivationPath.getAccount().getValue(), derivationPath.getAccount().isHarden()); HdKeyPair roleKey = hdKeyGenerator.getChildKeyPair(accountKey, derivationPath.getRole().getValue(), derivationPath.getRole().isHarden()); - HdKeyPair indexKey = hdKeyGenerator.getChildKeyPair(roleKey, derivationPath.getIndex().getValue(), derivationPath.getIndex().isHarden()); - return indexKey; + return hdKeyGenerator.getChildKeyPair(roleKey, derivationPath.getIndex().getValue(), derivationPath.getIndex().isHarden()); } /** @@ -77,8 +76,7 @@ public HdKeyPair getKeyPairFromAccountKey(byte[] accountKey, DerivationPath deri HdKeyPair accountKeyPair = hdKeyGenerator.getAccountKeyPairFromSecretKey(accountKey, derivationPath); HdKeyPair roleKey = hdKeyGenerator.getChildKeyPair(accountKeyPair, derivationPath.getRole().getValue(), derivationPath.getRole().isHarden()); - HdKeyPair indexKey = hdKeyGenerator.getChildKeyPair(roleKey, derivationPath.getIndex().getValue(), derivationPath.getIndex().isHarden()); - return indexKey; + return hdKeyGenerator.getChildKeyPair(roleKey, derivationPath.getIndex().getValue(), derivationPath.getIndex().isHarden()); } /** @@ -141,8 +139,7 @@ public HdKeyPair getKeyPairFromRootKey(byte[] rootKey, DerivationPath derivation HdKeyPair accountKey = hdKeyGenerator.getChildKeyPair(coinTypeKey, derivationPath.getAccount().getValue(), derivationPath.getAccount().isHarden()); HdKeyPair roleKey = hdKeyGenerator.getChildKeyPair(accountKey, derivationPath.getRole().getValue(), derivationPath.getRole().isHarden()); - HdKeyPair indexKey = hdKeyGenerator.getChildKeyPair(roleKey, derivationPath.getIndex().getValue(), derivationPath.getIndex().isHarden()); - return indexKey; + return hdKeyGenerator.getChildKeyPair(roleKey, derivationPath.getIndex().getValue(), derivationPath.getIndex().isHarden()); } /** diff --git a/hd-wallet/src/main/java/com/bloxbean/cardano/hdwallet/Wallet.java b/hd-wallet/src/main/java/com/bloxbean/cardano/hdwallet/Wallet.java index c8377c7c..6386f6b2 100644 --- a/hd-wallet/src/main/java/com/bloxbean/cardano/hdwallet/Wallet.java +++ b/hd-wallet/src/main/java/com/bloxbean/cardano/hdwallet/Wallet.java @@ -49,6 +49,8 @@ public class Wallet { private HdKeyPair rootKeyPair; private HdKeyPair stakeKeys; + @Getter + @Setter private boolean searchUtxoByAddrVkh; @Getter @@ -151,15 +153,15 @@ public static Wallet createFromAccountKey(Network network, byte[] accountKey) { /** * Create a Wallet object from given mnemonic or rootKey or accountKey * Only one of these value should be set : mnemonic or rootKey or accountKey - * @param network - * @param mnemonic - * @param rootKey - * @param accountKey - * @param account + * @param network network + * @param mnemonic mnemonic + * @param rootKey root key + * @param accountKey account level key + * @param account account number */ private Wallet(Network network, String mnemonic, byte[] rootKey, byte[] accountKey, int account) { //check if more than one value set and throw exception - if ((mnemonic != null && mnemonic.trim().length() > 0 ? 1 : 0) + + if ((mnemonic != null && !mnemonic.isEmpty() ? 1 : 0) + (rootKey != null && rootKey.length > 0 ? 1 : 0) + (accountKey != null && accountKey.length > 0 ? 1 : 0) > 1) { throw new WalletException("Only one of mnemonic, rootKey, or accountKey should be set."); @@ -168,7 +170,7 @@ private Wallet(Network network, String mnemonic, byte[] rootKey, byte[] accountK this.network = network; this.cache = new HashMap<>(); - if (mnemonic != null && mnemonic.trim().length() > 0) { + if (mnemonic != null && !mnemonic.isEmpty()) { this.mnemonic = mnemonic; this.accountNo = account; MnemonicUtil.validateMnemonic(this.mnemonic); @@ -192,8 +194,8 @@ private Wallet(Network network, String mnemonic, byte[] rootKey, byte[] accountK /** * Get Enterprise address for current account. Account can be changed via the setter. - * @param index - * @return + * @param index address index + * @return Address object with enterprise address */ public Address getEntAddress(int index) { return getEntAddress(this.accountNo, index); @@ -201,9 +203,9 @@ public Address getEntAddress(int index) { /** * Get Enterprise address for derivation path m/1852'/1815'/{account}'/0/{index} - * @param account - * @param index - * @return + * @param account account no + * @param index address index + * @return Address object with Enterprise address */ private Address getEntAddress(int account, int index) { return getAccountNo(account, index).getEnterpriseAddress(); @@ -211,8 +213,8 @@ private Address getEntAddress(int account, int index) { /** * Get Baseaddress for current account. Account can be changed via the setter. - * @param index - * @return + * @param index address index + * @return Address object for Base address */ public Address getBaseAddress(int index) { return getBaseAddress(this.accountNo, index); @@ -220,8 +222,8 @@ public Address getBaseAddress(int index) { /** * Get Baseaddress for current account as String. Account can be changed via the setter. - * @param index - * @return + * @param index address index + * @return Base address as string */ public String getBaseAddressString(int index) { return getBaseAddress(index).getAddress(); @@ -229,9 +231,9 @@ public String getBaseAddressString(int index) { /** * Get Baseaddress for derivationpath m/1852'/1815'/{account}'/0/{index} - * @param account - * @param index - * @return + * @param account account number + * @param index address index + * @return Address object for Base address */ public Address getBaseAddress(int account, int index) { return getAccountNo(account,index).getBaseAddress(); @@ -239,8 +241,8 @@ public Address getBaseAddress(int account, int index) { /** * Returns the Account object for the index and current account. Account can be changed via the setter. - * @param index - * @return + * @param index address index + * @return Account object */ public Account getAccountAtIndex(int index) { return getAccountNo(this.accountNo, index); @@ -248,9 +250,9 @@ public Account getAccountAtIndex(int index) { /** * Returns the Account object for the index and account. - * @param account - * @param index - * @return + * @param account account number + * @param index address index + * @return Account object */ public Account getAccountNo(int account, int index) { if(account != this.accountNo) { @@ -272,7 +274,7 @@ private Account deriveAccount(int account, int index) { DerivationPath derivationPath = DerivationPath.createExternalAddressDerivationPathForAccount(account); derivationPath.getIndex().setValue(index); - if (mnemonic != null && mnemonic.trim().length() > 0) { + if (mnemonic != null && !mnemonic.isEmpty()) { return Account.createFromMnemonic(this.network, this.mnemonic, derivationPath); } else if (rootKey != null && rootKey.length > 0) { return Account.createFromRootKey(this.network, this.rootKey, derivationPath); @@ -286,7 +288,7 @@ private Account deriveAccount(int account, int index) { /** * Setting the current account for derivation path. * Setting the account will reset the cache. - * @param account + * @param account account number which will be set in the wallet */ public void setAccountNo(int account) { this.accountNo = account; @@ -296,12 +298,12 @@ public void setAccountNo(int account) { /** * Returns the RootkeyPair - * @return + * @return Root key as HdKeyPair if non-empty else empty optional */ @JsonIgnore public Optional getRootKeyPair() { if(rootKeyPair == null) { - if (mnemonic != null && mnemonic.trim().length() > 0) { + if (mnemonic != null && !mnemonic.isEmpty()) { HdKeyGenerator hdKeyGenerator = new HdKeyGenerator(); try { byte[] entropy = MnemonicCode.INSTANCE.toEntropy(this.mnemonic); @@ -326,7 +328,7 @@ public Optional getRootPvtKey() { /** * Finds needed signers within wallet and signs the transaction with each one - * @param txToSign + * @param txToSign transaction * @return signed Transaction */ public Transaction sign(Transaction txToSign, Set utxos) { @@ -400,7 +402,7 @@ public Transaction sign(Transaction txToSign, Set utxos) { /** * Returns the stake address of the wallet. - * @return + * @return Stake address as string */ public String getStakeAddress() { if (stakeAddress == null || stakeAddress.isEmpty()) { @@ -413,21 +415,13 @@ public String getStakeAddress() { /** * Signs the transaction with stake key from wallet. - * @param transaction - * @return + * @param transaction transaction object to sign + * @return Signed transaction object */ public Transaction signWithStakeKey(Transaction transaction) { return TransactionSigner.INSTANCE.sign(transaction, getStakeKeyPair()); } - public void setSearchUtxoByAddrVkh(boolean flag) { - searchUtxoByAddrVkh = flag; - } - - public boolean isSearchUtxoByAddrVkh() { - return searchUtxoByAddrVkh; - } - private HdKeyPair getStakeKeyPair() { if(stakeKeys == null) { DerivationPath stakeDerivationPath = DerivationPath.createStakeAddressDerivationPathForAccount(this.accountNo);