From 0f10f3f1066e73a81f258d58b8dc8239e9a72dba Mon Sep 17 00:00:00 2001 From: Satya Date: Mon, 25 Nov 2024 12:43:56 +0800 Subject: [PATCH] fix: changes to fix SonarQube errors --- .../cardano/client/account/Account.java | 32 ----------- .../cardano/client/crypto/MnemonicUtil.java | 6 +- .../function/helper/SignerProviders.java | 3 +- .../com/bloxbean/cardano/hdwallet/Wallet.java | 8 +-- .../cardano/hdwallet/WalletException.java | 19 +++++++ .../cardano/hdwallet/WalletInterface.java | 55 ------------------- .../cardano/hdwallet/model/WalletUtxo.java | 7 ++- .../supplier/DefaultWalletUtxoSupplier.java | 3 +- .../bloxbean/cardano/hdwallet/WalletTest.java | 12 ++-- 9 files changed, 42 insertions(+), 103 deletions(-) create mode 100644 hd-wallet/src/main/java/com/bloxbean/cardano/hdwallet/WalletException.java delete mode 100644 hd-wallet/src/main/java/com/bloxbean/cardano/hdwallet/WalletInterface.java 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 e6d954fd..f6ed7c7a 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 @@ -7,12 +7,9 @@ import com.bloxbean.cardano.client.common.model.Network; import com.bloxbean.cardano.client.common.model.Networks; import com.bloxbean.cardano.client.crypto.bip32.HdKeyPair; -import com.bloxbean.cardano.client.crypto.bip39.MnemonicCode; -import com.bloxbean.cardano.client.crypto.bip39.MnemonicException; import com.bloxbean.cardano.client.crypto.bip39.Words; import com.bloxbean.cardano.client.crypto.cip1852.CIP1852; import com.bloxbean.cardano.client.crypto.cip1852.DerivationPath; -import com.bloxbean.cardano.client.exception.AddressRuntimeException; import com.bloxbean.cardano.client.exception.CborDeserializationException; import com.bloxbean.cardano.client.exception.CborSerializationException; import com.bloxbean.cardano.client.governance.keys.CommitteeColdKey; @@ -23,9 +20,6 @@ import com.bloxbean.cardano.client.util.HexUtil; import com.fasterxml.jackson.annotation.JsonIgnore; -import java.util.Arrays; -import java.util.stream.Collectors; - /** * Create and manage secrets, and perform account-based work such as signing transactions. */ @@ -487,32 +481,6 @@ public Transaction signWithCommitteeHotKey(Transaction transaction) { return TransactionSigner.INSTANCE.sign(transaction, getCommitteeHotKeyPair()); } - private void generateNew(Words noOfWords) { - String mnemonic = null; - try { - mnemonic = MnemonicCode.INSTANCE.createMnemonic(noOfWords).stream().collect(Collectors.joining(" ")); - } catch (MnemonicException.MnemonicLengthException e) { - throw new RuntimeException("Mnemonic generation failed", e); - } - this.mnemonic = mnemonic; - baseAddress(); - } - - private void validateMnemonic() { - if (mnemonic == null) { - throw new AddressRuntimeException("Mnemonic cannot be null"); - } - - mnemonic = mnemonic.replaceAll("\\s+", " "); - String[] words = mnemonic.split("\\s+"); - - try { - MnemonicCode.INSTANCE.check(Arrays.asList(words)); - } catch (MnemonicException e) { - throw new AddressRuntimeException("Invalid mnemonic phrase", e); - } - } - private HdKeyPair getHdKeyPair() { HdKeyPair hdKeyPair; if (mnemonic == null || mnemonic.trim().length() == 0) { diff --git a/crypto/src/main/java/com/bloxbean/cardano/client/crypto/MnemonicUtil.java b/crypto/src/main/java/com/bloxbean/cardano/client/crypto/MnemonicUtil.java index 1fbbe4f5..c13cad99 100644 --- a/crypto/src/main/java/com/bloxbean/cardano/client/crypto/MnemonicUtil.java +++ b/crypto/src/main/java/com/bloxbean/cardano/client/crypto/MnemonicUtil.java @@ -10,6 +10,10 @@ public class MnemonicUtil { + private MnemonicUtil() { + + } + public static void validateMnemonic(String mnemonic) { if (mnemonic == null) { throw new AddressRuntimeException("Mnemonic cannot be null"); @@ -30,7 +34,7 @@ public static String generateNew(Words noOfWords) { try { mnemonic = MnemonicCode.INSTANCE.createMnemonic(noOfWords).stream().collect(Collectors.joining(" ")); } catch (MnemonicException.MnemonicLengthException e) { - throw new RuntimeException("Mnemonic generation failed", e); + throw new AddressRuntimeException("Mnemonic generation failed", e); } return mnemonic; } diff --git a/function/src/main/java/com/bloxbean/cardano/client/function/helper/SignerProviders.java b/function/src/main/java/com/bloxbean/cardano/client/function/helper/SignerProviders.java index 3eeed8f5..cc242cb1 100644 --- a/function/src/main/java/com/bloxbean/cardano/client/function/helper/SignerProviders.java +++ b/function/src/main/java/com/bloxbean/cardano/client/function/helper/SignerProviders.java @@ -46,8 +46,7 @@ public static TxSigner signerFrom(Wallet wallet) { .stream().filter(utxo -> utxo instanceof WalletUtxo) .map(utxo -> (WalletUtxo) utxo) .collect(Collectors.toSet()); - Transaction outputTxn = wallet.sign(transaction, utxos); - return outputTxn; + return wallet.sign(transaction, utxos); }; } 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 1ec01c77..be02a7b9 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 @@ -185,7 +185,7 @@ public HdKeyPair getRootKeyPair() { rootKeys = hdKeyGenerator.getRootKeyPairFromEntropy(entropy); } catch (MnemonicException.MnemonicLengthException | MnemonicException.MnemonicWordException | MnemonicException.MnemonicChecksumException e) { - throw new RuntimeException(e); + throw new WalletException("Unable to derive root key pair", e); } } return rootKeys; @@ -211,10 +211,10 @@ public Transaction sign(Transaction txToSign, Set utxos) { var accounts = accountMap.values(); if(accounts.isEmpty()) - throw new RuntimeException("No signers found!"); + throw new WalletException("No signers found!"); - for (Account account : accounts) - txToSign = account.sign(txToSign); + for (Account signerAcc : accounts) + txToSign = signerAcc.sign(txToSign); return txToSign; } diff --git a/hd-wallet/src/main/java/com/bloxbean/cardano/hdwallet/WalletException.java b/hd-wallet/src/main/java/com/bloxbean/cardano/hdwallet/WalletException.java new file mode 100644 index 00000000..4b5c1884 --- /dev/null +++ b/hd-wallet/src/main/java/com/bloxbean/cardano/hdwallet/WalletException.java @@ -0,0 +1,19 @@ +package com.bloxbean.cardano.hdwallet; + +public class WalletException extends RuntimeException { + + public WalletException() { + } + + public WalletException(String msg) { + super(msg); + } + + public WalletException(Throwable cause) { + super(cause); + } + + public WalletException(String msg, Throwable cause) { + super(msg, cause); + } +} diff --git a/hd-wallet/src/main/java/com/bloxbean/cardano/hdwallet/WalletInterface.java b/hd-wallet/src/main/java/com/bloxbean/cardano/hdwallet/WalletInterface.java deleted file mode 100644 index 19a38e83..00000000 --- a/hd-wallet/src/main/java/com/bloxbean/cardano/hdwallet/WalletInterface.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.bloxbean.cardano.hdwallet; - -import com.bloxbean.cardano.client.account.Account; -import com.bloxbean.cardano.client.api.model.Amount; - -import java.util.List; - -public interface WalletInterface { - -// private String mnemonic = ""; -// public HDWalletInterface(Network network, String mnemonic); -// public HDWalletInterface(Network network); - - // sum up all amounts within this wallet - // scanning strategy is as described in specification.md - public List getWalletBalance(); - - /** - * Returns the account for the given index - * @param index - * @return - */ - public Account getAccount(int index); - - /** - * Creates a new Account for the first empty index. - * @return - */ - public Account newAccount(); - - /** - * Returns the stake address - * @return - */ - public String getStakeAddress(); - - /** - * returns the master private key - * @return - */ - public byte[] getMasterPrivateKey(); - - /** - * Returns the master public key - * @return - */ - public String getMasterPublicKey(); - - /** - * returns the master adress from where other addresses can be derived - * @return - */ - public String getMasterAddress(); // prio 2 - -} diff --git a/hd-wallet/src/main/java/com/bloxbean/cardano/hdwallet/model/WalletUtxo.java b/hd-wallet/src/main/java/com/bloxbean/cardano/hdwallet/model/WalletUtxo.java index 94a7962f..8c670063 100644 --- a/hd-wallet/src/main/java/com/bloxbean/cardano/hdwallet/model/WalletUtxo.java +++ b/hd-wallet/src/main/java/com/bloxbean/cardano/hdwallet/model/WalletUtxo.java @@ -5,10 +5,12 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.databind.PropertyNamingStrategies; import com.fasterxml.jackson.databind.annotation.JsonNaming; -import lombok.Data; +import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.Setter; -@Data +@Getter +@Setter @NoArgsConstructor @JsonIgnoreProperties(ignoreUnknown = true) @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) @@ -26,4 +28,5 @@ public static WalletUtxo from(Utxo utxo) { walletUtxo.setReferenceScriptHash(utxo.getReferenceScriptHash()); return walletUtxo; } + } diff --git a/hd-wallet/src/main/java/com/bloxbean/cardano/hdwallet/supplier/DefaultWalletUtxoSupplier.java b/hd-wallet/src/main/java/com/bloxbean/cardano/hdwallet/supplier/DefaultWalletUtxoSupplier.java index 515bf76c..095573bc 100644 --- a/hd-wallet/src/main/java/com/bloxbean/cardano/hdwallet/supplier/DefaultWalletUtxoSupplier.java +++ b/hd-wallet/src/main/java/com/bloxbean/cardano/hdwallet/supplier/DefaultWalletUtxoSupplier.java @@ -10,6 +10,7 @@ import com.bloxbean.cardano.client.crypto.cip1852.DerivationPath; import com.bloxbean.cardano.client.crypto.cip1852.Segment; import com.bloxbean.cardano.hdwallet.Wallet; +import com.bloxbean.cardano.hdwallet.WalletException; import com.bloxbean.cardano.hdwallet.model.WalletUtxo; import lombok.Setter; @@ -111,6 +112,6 @@ public List getUtxosForAccountAndIndex(int account, int index) { private void checkIfWalletIsSet() { if(this.wallet == null) - throw new RuntimeException("Wallet has to be provided!"); + throw new WalletException("Wallet has to be provided!"); } } diff --git a/hd-wallet/src/test/java/com/bloxbean/cardano/hdwallet/WalletTest.java b/hd-wallet/src/test/java/com/bloxbean/cardano/hdwallet/WalletTest.java index 10183505..d123692e 100644 --- a/hd-wallet/src/test/java/com/bloxbean/cardano/hdwallet/WalletTest.java +++ b/hd-wallet/src/test/java/com/bloxbean/cardano/hdwallet/WalletTest.java @@ -53,7 +53,7 @@ void WalletAddressToAccountAddressTest() { } @Test - public void testGetBaseAddressFromMnemonicIndex_0() { + void testGetBaseAddressFromMnemonicIndex_0() { Wallet wallet = new Wallet(Networks.mainnet(), phrase24W); Assertions.assertEquals(baseAddress0, wallet.getBaseAddressString(0)); Assertions.assertEquals(baseAddress1, wallet.getBaseAddressString(1)); @@ -62,7 +62,7 @@ public void testGetBaseAddressFromMnemonicIndex_0() { } @Test - public void testGetBaseAddressFromMnemonicByNetworkInfoTestnet() { + void testGetBaseAddressFromMnemonicByNetworkInfoTestnet() { Wallet wallet = new Wallet(Networks.testnet(), phrase24W); Assertions.assertEquals(testnetBaseAddress0, wallet.getBaseAddressString(0)); Assertions.assertEquals(testnetBaseAddress1, wallet.getBaseAddressString(1)); @@ -70,7 +70,7 @@ public void testGetBaseAddressFromMnemonicByNetworkInfoTestnet() { } @Test - public void testGetEnterpriseAddressFromMnemonicIndex() { + void testGetEnterpriseAddressFromMnemonicIndex() { Wallet wallet = new Wallet(Networks.mainnet(), phrase24W); Assertions.assertEquals(entAddress0, wallet.getEntAddress(0).getAddress()); Assertions.assertEquals(entAddress1, wallet.getEntAddress(1).getAddress()); @@ -78,7 +78,7 @@ public void testGetEnterpriseAddressFromMnemonicIndex() { } @Test - public void testGetEnterpriseAddressFromMnemonicIndexByNetwork() { + void testGetEnterpriseAddressFromMnemonicIndexByNetwork() { Wallet wallet = new Wallet(Networks.testnet(), phrase24W); Assertions.assertEquals(testnetEntAddress0, wallet.getEntAddress(0).getAddress()); Assertions.assertEquals(testnetEntAddress1, wallet.getEntAddress(1).getAddress()); @@ -86,13 +86,13 @@ public void testGetEnterpriseAddressFromMnemonicIndexByNetwork() { } @Test - public void testGetPublicKeyBytesFromMnemonic() { + void testGetPublicKeyBytesFromMnemonic() { byte[] pubKey = new Wallet(phrase24W).getRootKeyPair().getPublicKey().getKeyData(); Assertions.assertEquals(32, pubKey.length); } @Test - public void testGetPrivateKeyBytesFromMnemonic() { + void testGetPrivateKeyBytesFromMnemonic() { byte[] pvtKey = new Wallet(phrase24W).getRootKeyPair().getPrivateKey().getBytes(); Assertions.assertEquals(96, pvtKey.length); }