Skip to content

Commit 21761ba

Browse files
committed
Fix Sonarqube warnings
1 parent 2bfa053 commit 21761ba

File tree

5 files changed

+32
-10
lines changed

5 files changed

+32
-10
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public Account(Network network, byte[] accountKey, int account, int index) {
184184
if (accountKey.length == 96)
185185
this.accountKey = accountKey;
186186
else
187-
throw new RuntimeException("Invalid length (Account Private Key): " + accountKey.length);
187+
throw new AccountException("Invalid length (Account Private Key): " + accountKey.length);
188188

189189
this.derivationPath = DerivationPath.createExternalAddressDerivationPathForAccount(account);
190190
this.derivationPath.getIndex().setValue(index);
@@ -207,13 +207,13 @@ private Account(Network network, String mnemonic, byte[] rootKey, byte[] account
207207
if (rootKey.length == 96)
208208
this.rootKey = rootKey;
209209
else
210-
throw new RuntimeException("Invalid length (Root Pvt Key): " + rootKey.length);
210+
throw new AccountException("Invalid length (Root Pvt Key): " + rootKey.length);
211211
} else if (accountKey != null && accountKey.length > 0) {
212212
this.mnemonic = null;
213213
if (accountKey.length == 96)
214214
this.accountKey = accountKey;
215215
else
216-
throw new RuntimeException("Invalid length (Account Private Key): " + accountKey.length);
216+
throw new AccountException("Invalid length (Account Private Key): " + accountKey.length);
217217
}
218218

219219
baseAddress();
@@ -706,7 +706,7 @@ private HdKeyPair getHdKeyPairFromDerivationPath(DerivationPath derivationPath)
706706
} else if (rootKey != null && rootKey.length > 0) {
707707
hdKeyPair = new CIP1852().getKeyPairFromRootKey(this.rootKey, derivationPath);
708708
} else {
709-
throw new RuntimeException("HDKeyPair derivation failed. Only one of mnemonic, rootKey, or accountKey should be set.");
709+
throw new AccountException("HDKeyPair derivation failed. Only one of mnemonic, rootKey, or accountKey should be set.");
710710
}
711711

712712
return hdKeyPair;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.bloxbean.cardano.client.account;
2+
3+
public class AccountException extends RuntimeException {
4+
public AccountException() {
5+
}
6+
7+
public AccountException(String msg) {
8+
super(msg);
9+
}
10+
11+
public AccountException(Throwable cause) {
12+
super(cause);
13+
}
14+
15+
public AccountException(String msg, Throwable cause) {
16+
super(msg, cause);
17+
}
18+
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -614,8 +614,9 @@ void testAccountFromMnemonic_15words_acc4_index3() {
614614

615615
@Test
616616
void testAccountFromRootKey_15words_acc4_index3() {
617+
617618
//Original mnemonic
618-
//String mnemonic = "top exact spice seed cloud birth orient bracket happy cat section girl such outside elder";
619+
//top exact spice seed cloud birth orient bracket happy cat section girl such outside elder
619620

620621
String rootKey = "root_xsk1zza6z52v8gelnaqdhuny3ywlccud5dtm8rvvyem4utnfwzcaa9pspsmdm99qfpy2qz7sw9sts59mrkegmdqyjen5ykm4z3ccyrkn8g5mm0qw35arvwxclfh6tj3s4x7t2q85wenvppjpxckcxgnf8vd80ug0l6rw";
621622

@@ -632,7 +633,7 @@ void testAccountFromRootKey_15words_acc4_index3() {
632633
@Test
633634
void testAccountFromAccountKey_15words_acc4_index3() {
634635
//Original mnemonic
635-
//String mnemonic = "top exact spice seed cloud birth orient bracket happy cat section girl such outside elder";
636+
//top exact spice seed cloud birth orient bracket happy cat section girl such outside elder
636637

637638
String accountKey = "acct_xsk1azc6gn5zkdprp4gkapmhdckykphjl62rm9224699ut5z6xcaa9p4hv5hmjfgcrzk72tnsqh6dw0njekdjpsv8nv5h5hk6lpd4ag62zenwhzqs205kfurd7kgs8fm5gx4l4j8htutwj060kyp5y5kgw55qc8lsltd";
638639

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ public Optional<HdKeyPair> getRootKeyPair() {
321321

322322
public Optional<byte[]> getRootPvtKey() {
323323
return getRootKeyPair()
324-
.map(rootKeyPair -> rootKeyPair.getPrivateKey().getBytes());
324+
.map(rkp -> rkp.getPrivateKey().getBytes());
325325
}
326326

327327
/**

hd-wallet/src/test/java/com/bloxbean/cardano/hdwallet/WalletTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ void testAccountWhenFromRootKey_account_2() {
188188
@Test
189189
void testAccountWhenFromAccountKey_15words_acc4_index3() {
190190
//Original mnemonic
191-
//String mnemonic = "top exact spice seed cloud birth orient bracket happy cat section girl such outside elder";
191+
//top exact spice seed cloud birth orient bracket happy cat section girl such outside elder
192192

193193
String accountKey = "acct_xsk1azc6gn5zkdprp4gkapmhdckykphjl62rm9224699ut5z6xcaa9p4hv5hmjfgcrzk72tnsqh6dw0njekdjpsv8nv5h5hk6lpd4ag62zenwhzqs205kfurd7kgs8fm5gx4l4j8htutwj060kyp5y5kgw55qc8lsltd";
194194

@@ -207,8 +207,10 @@ void testAccountFromAccountKey_128bytes_throwsException() {
207207
//Added random 32 bytes at the end to test with a 128 bytes key
208208
String accountKey = "e8b1a44e82b34230d516e87776e2c4b06f2fe943d954aae8a5e2e82d1b1de9435bb297dc928c0c56f2973802fa6b9f3966cd9060c3cd94bd2f6d7c2daf51a50b3375c40829f4b27836fac881d3ba20d5fd647baf8b749fa7d881a129643a9406c333ef7429361bdb1414e15e054f6654bce419d26057d0e38d76993f9c3ab71f";
209209

210+
var network = Networks.testnet();
211+
var acctKeyBytes = HexUtil.decodeHexString(accountKey);
210212
assertThrows(WalletException.class, () -> {
211-
Wallet.createFromAccountKey(Networks.testnet(), HexUtil.decodeHexString(accountKey));
213+
Wallet.createFromAccountKey(network, acctKeyBytes);
212214
});
213215
}
214216

@@ -218,8 +220,9 @@ void testAccountFromRootKey_128Bytes_throwsException() {
218220
String rootKey128Bytes = "48c0062f7d36bb4f8194beb2762d4e323fd84dd947e7d09db8f8b454b74a105560631379e7b976f41e165133978ecd56fe65114f5ba6aaafaa0d482a48e71edec6377bc291444b063f64d59b955447c9fb3a3026e43f9052d82a792d304fd644c333ef7429361bdb1414e15e054f6654bce419d26057d0e38d76993f9c3ab71f";
219221
byte[] rootKey = HexUtil.decodeHexString(rootKey128Bytes);
220222

223+
var network = Networks.testnet();
221224
assertThrows(WalletException.class, () -> {
222-
Wallet.createFromRootKey(Networks.testnet(), rootKey);
225+
Wallet.createFromRootKey(network, rootKey);
223226
});
224227
}
225228
}

0 commit comments

Comments
 (0)