Skip to content

Commit

Permalink
fix: changes to fix SonarQube errors
Browse files Browse the repository at this point in the history
  • Loading branch information
satran004 committed Nov 25, 2024
1 parent 5316e90 commit 0f10f3f
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*/
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -211,10 +211,10 @@ public Transaction sign(Transaction txToSign, Set<WalletUtxo> 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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -26,4 +28,5 @@ public static WalletUtxo from(Utxo utxo) {
walletUtxo.setReferenceScriptHash(utxo.getReferenceScriptHash());
return walletUtxo;
}

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

Expand Down Expand Up @@ -111,6 +112,6 @@ public List<WalletUtxo> 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!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -62,37 +62,37 @@ 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));
Assertions.assertEquals(testnetBaseAddress2, wallet.getBaseAddressString(2));
}

@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());
Assertions.assertEquals(entAddress2, wallet.getEntAddress(2).getAddress());
}

@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());
Assertions.assertEquals(testnetEntAddress2, wallet.getEntAddress(2).getAddress());
}

@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);
}
Expand Down

0 comments on commit 0f10f3f

Please sign in to comment.