Skip to content

Commit

Permalink
Fix Sonarqube warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
satran004 committed Jan 11, 2025
1 parent 21761ba commit ad9cf4a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -656,7 +656,7 @@ public Transaction signWithCommitteeHotKey(Transaction transaction) {
}

public Optional<HdKeyPair> 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));
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -474,7 +473,6 @@ void testAccountFromRootKey_128Bytes_throwsException() {

assertThrows(Exception.class, () -> {
Account.createFromRootKey(Networks.testnet(), rootKey);
;
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand Down Expand Up @@ -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());
}

/**
Expand Down
74 changes: 34 additions & 40 deletions hd-wallet/src/main/java/com/bloxbean/cardano/hdwallet/Wallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public class Wallet {
private HdKeyPair rootKeyPair;
private HdKeyPair stakeKeys;

@Getter
@Setter
private boolean searchUtxoByAddrVkh;

@Getter
Expand Down Expand Up @@ -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.");
Expand All @@ -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);
Expand All @@ -192,65 +194,65 @@ 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);
}

/**
* 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();
}

/**
* 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);
}

/**
* 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();
}

/**
* 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();
}

/**
* 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);
}

/**
* 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) {
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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<HdKeyPair> 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);
Expand All @@ -326,7 +328,7 @@ public Optional<byte[]> 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<WalletUtxo> utxos) {
Expand Down Expand Up @@ -400,7 +402,7 @@ public Transaction sign(Transaction txToSign, Set<WalletUtxo> utxos) {

/**
* Returns the stake address of the wallet.
* @return
* @return Stake address as string
*/
public String getStakeAddress() {
if (stakeAddress == null || stakeAddress.isEmpty()) {
Expand All @@ -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);
Expand Down

0 comments on commit ad9cf4a

Please sign in to comment.