Skip to content

Commit ad9cf4a

Browse files
committed
Fix Sonarqube warnings
1 parent 21761ba commit ad9cf4a

File tree

4 files changed

+41
-52
lines changed
  • core/src
  • crypto/src/main/java/com/bloxbean/cardano/client/crypto/cip1852
  • hd-wallet/src/main/java/com/bloxbean/cardano/hdwallet

4 files changed

+41
-52
lines changed

core/src/main/java/com/bloxbean/cardano/client/account/Account.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ private Account(Network network, String mnemonic, byte[] rootKey, byte[] account
196196
this.network = network;
197197
this.derivationPath = derivationPath;
198198

199-
if (mnemonic != null && mnemonic.length() > 0) {
199+
if (mnemonic != null && !mnemonic.isEmpty()) {
200200
this.mnemonic = mnemonic;
201201
this.accountKey = null;
202202
MnemonicUtil.validateMnemonic(this.mnemonic);
@@ -656,7 +656,7 @@ public Transaction signWithCommitteeHotKey(Transaction transaction) {
656656
}
657657

658658
public Optional<HdKeyPair> getRootKeyPair() {
659-
if (mnemonic != null && mnemonic.length() > 0) {
659+
if (mnemonic != null && !mnemonic.isEmpty()) {
660660
return Optional.of(new CIP1852().getRootKeyPairFromMnemonic(mnemonic));
661661
} else if (rootKey != null && rootKey.length > 0) {
662662
return Optional.of(new CIP1852().getRootKeyPairFromRootKey(rootKey));
@@ -699,7 +699,7 @@ private HdKeyPair getCommitteeHotKeyPair() {
699699

700700
private HdKeyPair getHdKeyPairFromDerivationPath(DerivationPath derivationPath) {
701701
HdKeyPair hdKeyPair;
702-
if (mnemonic != null && mnemonic.trim().length() > 0) {
702+
if (mnemonic != null && !mnemonic.isEmpty()) {
703703
hdKeyPair = new CIP1852().getKeyPairFromMnemonic(mnemonic, derivationPath);
704704
} else if (accountKey != null && accountKey.length > 0) {
705705
hdKeyPair = new CIP1852().getKeyPairFromAccountKey(this.accountKey, derivationPath);

core/src/test/java/com/bloxbean/cardano/client/account/AccountTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -442,8 +442,7 @@ void testAccountFromRootKey() {
442442
Account account = Account.createFromRootKey(Networks.testnet(), rootKeyBytes);
443443

444444
//expected
445-
//var seedPhrase =
446-
// "tragic movie pulp rely quick damage spoil case bubble forget banana bomb pilot fresh trumpet learn basic melt curtain defy erode soccer race oil";
445+
//tragic movie pulp rely quick damage spoil case bubble forget banana bomb pilot fresh trumpet learn basic melt curtain defy erode soccer race oil
447446

448447
assertThat(account.baseAddress()).isEqualTo("addr_test1qzm0439fe55aynh58qcn4jnh4mwuqwr5n5fez7j0hck9ds8j3nmg5pkqfur4gyupppuu82r83s5eheewzmf6fwlzfz7qzsp6rc");
449448
assertThat(account.changeAddress()).isEqualTo("addr_test1qqkqwker9785sna30vmjggynjxzce6sdg2th7w3w0sgfvr8j3nmg5pkqfur4gyupppuu82r83s5eheewzmf6fwlzfz7q5r7hzu");
@@ -474,7 +473,6 @@ void testAccountFromRootKey_128Bytes_throwsException() {
474473

475474
assertThrows(Exception.class, () -> {
476475
Account.createFromRootKey(Networks.testnet(), rootKey);
477-
;
478476
});
479477
}
480478

crypto/src/main/java/com/bloxbean/cardano/client/crypto/cip1852/CIP1852.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ public HdKeyPair getKeyPairFromEntropy(byte[] entropy, DerivationPath derivation
6161
HdKeyPair accountKey = hdKeyGenerator.getChildKeyPair(coinTypeKey, derivationPath.getAccount().getValue(), derivationPath.getAccount().isHarden());
6262
HdKeyPair roleKey = hdKeyGenerator.getChildKeyPair(accountKey, derivationPath.getRole().getValue(), derivationPath.getRole().isHarden());
6363

64-
HdKeyPair indexKey = hdKeyGenerator.getChildKeyPair(roleKey, derivationPath.getIndex().getValue(), derivationPath.getIndex().isHarden());
65-
return indexKey;
64+
return hdKeyGenerator.getChildKeyPair(roleKey, derivationPath.getIndex().getValue(), derivationPath.getIndex().isHarden());
6665
}
6766

6867
/**
@@ -77,8 +76,7 @@ public HdKeyPair getKeyPairFromAccountKey(byte[] accountKey, DerivationPath deri
7776
HdKeyPair accountKeyPair = hdKeyGenerator.getAccountKeyPairFromSecretKey(accountKey, derivationPath);
7877
HdKeyPair roleKey = hdKeyGenerator.getChildKeyPair(accountKeyPair, derivationPath.getRole().getValue(), derivationPath.getRole().isHarden());
7978

80-
HdKeyPair indexKey = hdKeyGenerator.getChildKeyPair(roleKey, derivationPath.getIndex().getValue(), derivationPath.getIndex().isHarden());
81-
return indexKey;
79+
return hdKeyGenerator.getChildKeyPair(roleKey, derivationPath.getIndex().getValue(), derivationPath.getIndex().isHarden());
8280
}
8381

8482
/**
@@ -141,8 +139,7 @@ public HdKeyPair getKeyPairFromRootKey(byte[] rootKey, DerivationPath derivation
141139
HdKeyPair accountKey = hdKeyGenerator.getChildKeyPair(coinTypeKey, derivationPath.getAccount().getValue(), derivationPath.getAccount().isHarden());
142140
HdKeyPair roleKey = hdKeyGenerator.getChildKeyPair(accountKey, derivationPath.getRole().getValue(), derivationPath.getRole().isHarden());
143141

144-
HdKeyPair indexKey = hdKeyGenerator.getChildKeyPair(roleKey, derivationPath.getIndex().getValue(), derivationPath.getIndex().isHarden());
145-
return indexKey;
142+
return hdKeyGenerator.getChildKeyPair(roleKey, derivationPath.getIndex().getValue(), derivationPath.getIndex().isHarden());
146143
}
147144

148145
/**

hd-wallet/src/main/java/com/bloxbean/cardano/hdwallet/Wallet.java

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public class Wallet {
4949
private HdKeyPair rootKeyPair;
5050
private HdKeyPair stakeKeys;
5151

52+
@Getter
53+
@Setter
5254
private boolean searchUtxoByAddrVkh;
5355

5456
@Getter
@@ -151,15 +153,15 @@ public static Wallet createFromAccountKey(Network network, byte[] accountKey) {
151153
/**
152154
* Create a Wallet object from given mnemonic or rootKey or accountKey
153155
* Only one of these value should be set : mnemonic or rootKey or accountKey
154-
* @param network
155-
* @param mnemonic
156-
* @param rootKey
157-
* @param accountKey
158-
* @param account
156+
* @param network network
157+
* @param mnemonic mnemonic
158+
* @param rootKey root key
159+
* @param accountKey account level key
160+
* @param account account number
159161
*/
160162
private Wallet(Network network, String mnemonic, byte[] rootKey, byte[] accountKey, int account) {
161163
//check if more than one value set and throw exception
162-
if ((mnemonic != null && mnemonic.trim().length() > 0 ? 1 : 0) +
164+
if ((mnemonic != null && !mnemonic.isEmpty() ? 1 : 0) +
163165
(rootKey != null && rootKey.length > 0 ? 1 : 0) +
164166
(accountKey != null && accountKey.length > 0 ? 1 : 0) > 1) {
165167
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
168170
this.network = network;
169171
this.cache = new HashMap<>();
170172

171-
if (mnemonic != null && mnemonic.trim().length() > 0) {
173+
if (mnemonic != null && !mnemonic.isEmpty()) {
172174
this.mnemonic = mnemonic;
173175
this.accountNo = account;
174176
MnemonicUtil.validateMnemonic(this.mnemonic);
@@ -192,65 +194,65 @@ private Wallet(Network network, String mnemonic, byte[] rootKey, byte[] accountK
192194

193195
/**
194196
* Get Enterprise address for current account. Account can be changed via the setter.
195-
* @param index
196-
* @return
197+
* @param index address index
198+
* @return Address object with enterprise address
197199
*/
198200
public Address getEntAddress(int index) {
199201
return getEntAddress(this.accountNo, index);
200202
}
201203

202204
/**
203205
* Get Enterprise address for derivation path m/1852'/1815'/{account}'/0/{index}
204-
* @param account
205-
* @param index
206-
* @return
206+
* @param account account no
207+
* @param index address index
208+
* @return Address object with Enterprise address
207209
*/
208210
private Address getEntAddress(int account, int index) {
209211
return getAccountNo(account, index).getEnterpriseAddress();
210212
}
211213

212214
/**
213215
* Get Baseaddress for current account. Account can be changed via the setter.
214-
* @param index
215-
* @return
216+
* @param index address index
217+
* @return Address object for Base address
216218
*/
217219
public Address getBaseAddress(int index) {
218220
return getBaseAddress(this.accountNo, index);
219221
}
220222

221223
/**
222224
* Get Baseaddress for current account as String. Account can be changed via the setter.
223-
* @param index
224-
* @return
225+
* @param index address index
226+
* @return Base address as string
225227
*/
226228
public String getBaseAddressString(int index) {
227229
return getBaseAddress(index).getAddress();
228230
}
229231

230232
/**
231233
* Get Baseaddress for derivationpath m/1852'/1815'/{account}'/0/{index}
232-
* @param account
233-
* @param index
234-
* @return
234+
* @param account account number
235+
* @param index address index
236+
* @return Address object for Base address
235237
*/
236238
public Address getBaseAddress(int account, int index) {
237239
return getAccountNo(account,index).getBaseAddress();
238240
}
239241

240242
/**
241243
* Returns the Account object for the index and current account. Account can be changed via the setter.
242-
* @param index
243-
* @return
244+
* @param index address index
245+
* @return Account object
244246
*/
245247
public Account getAccountAtIndex(int index) {
246248
return getAccountNo(this.accountNo, index);
247249
}
248250

249251
/**
250252
* Returns the Account object for the index and account.
251-
* @param account
252-
* @param index
253-
* @return
253+
* @param account account number
254+
* @param index address index
255+
* @return Account object
254256
*/
255257
public Account getAccountNo(int account, int index) {
256258
if(account != this.accountNo) {
@@ -272,7 +274,7 @@ private Account deriveAccount(int account, int index) {
272274
DerivationPath derivationPath = DerivationPath.createExternalAddressDerivationPathForAccount(account);
273275
derivationPath.getIndex().setValue(index);
274276

275-
if (mnemonic != null && mnemonic.trim().length() > 0) {
277+
if (mnemonic != null && !mnemonic.isEmpty()) {
276278
return Account.createFromMnemonic(this.network, this.mnemonic, derivationPath);
277279
} else if (rootKey != null && rootKey.length > 0) {
278280
return Account.createFromRootKey(this.network, this.rootKey, derivationPath);
@@ -286,7 +288,7 @@ private Account deriveAccount(int account, int index) {
286288
/**
287289
* Setting the current account for derivation path.
288290
* Setting the account will reset the cache.
289-
* @param account
291+
* @param account account number which will be set in the wallet
290292
*/
291293
public void setAccountNo(int account) {
292294
this.accountNo = account;
@@ -296,12 +298,12 @@ public void setAccountNo(int account) {
296298

297299
/**
298300
* Returns the RootkeyPair
299-
* @return
301+
* @return Root key as HdKeyPair if non-empty else empty optional
300302
*/
301303
@JsonIgnore
302304
public Optional<HdKeyPair> getRootKeyPair() {
303305
if(rootKeyPair == null) {
304-
if (mnemonic != null && mnemonic.trim().length() > 0) {
306+
if (mnemonic != null && !mnemonic.isEmpty()) {
305307
HdKeyGenerator hdKeyGenerator = new HdKeyGenerator();
306308
try {
307309
byte[] entropy = MnemonicCode.INSTANCE.toEntropy(this.mnemonic);
@@ -326,7 +328,7 @@ public Optional<byte[]> getRootPvtKey() {
326328

327329
/**
328330
* Finds needed signers within wallet and signs the transaction with each one
329-
* @param txToSign
331+
* @param txToSign transaction
330332
* @return signed Transaction
331333
*/
332334
public Transaction sign(Transaction txToSign, Set<WalletUtxo> utxos) {
@@ -400,7 +402,7 @@ public Transaction sign(Transaction txToSign, Set<WalletUtxo> utxos) {
400402

401403
/**
402404
* Returns the stake address of the wallet.
403-
* @return
405+
* @return Stake address as string
404406
*/
405407
public String getStakeAddress() {
406408
if (stakeAddress == null || stakeAddress.isEmpty()) {
@@ -413,21 +415,13 @@ public String getStakeAddress() {
413415

414416
/**
415417
* Signs the transaction with stake key from wallet.
416-
* @param transaction
417-
* @return
418+
* @param transaction transaction object to sign
419+
* @return Signed transaction object
418420
*/
419421
public Transaction signWithStakeKey(Transaction transaction) {
420422
return TransactionSigner.INSTANCE.sign(transaction, getStakeKeyPair());
421423
}
422424

423-
public void setSearchUtxoByAddrVkh(boolean flag) {
424-
searchUtxoByAddrVkh = flag;
425-
}
426-
427-
public boolean isSearchUtxoByAddrVkh() {
428-
return searchUtxoByAddrVkh;
429-
}
430-
431425
private HdKeyPair getStakeKeyPair() {
432426
if(stakeKeys == null) {
433427
DerivationPath stakeDerivationPath = DerivationPath.createStakeAddressDerivationPathForAccount(this.accountNo);

0 commit comments

Comments
 (0)