From f7670b421bc1a1aa75d51555399901e83f5ad5f4 Mon Sep 17 00:00:00 2001 From: Fausto Spoto Date: Tue, 5 Sep 2023 19:12:24 +0200 Subject: [PATCH] Code clean-up --- .../java/io/hotmoka/crypto/api/Account.java | 8 ++++---- .../java/io/hotmoka/crypto/api/Entropy.java | 8 ++++---- .../io/hotmoka/crypto/HashingAlgorithms.java | 2 +- .../crypto/internal/AbstractAccountImpl.java | 4 ++-- .../hotmoka/crypto/internal/EntropyImpl.java | 20 +++++++++---------- .../internal/UnmarshallingContextImpl.java | 2 +- .../hotmoka/tools/internal/moka/BindKey.java | 3 ++- .../tools/internal/moka/CreateAccount.java | 3 ++- .../tools/internal/moka/CreateKey.java | 15 +++++++------- .../tools/internal/moka/ImportAccount.java | 4 +++- .../tools/internal/moka/InitMemory.java | 2 +- .../tools/internal/moka/InitTendermint.java | 5 ++--- .../tools/internal/moka/RotateKey.java | 3 ++- 13 files changed, 42 insertions(+), 37 deletions(-) diff --git a/io-hotmoka-crypto-api/src/main/java/io/hotmoka/crypto/api/Account.java b/io-hotmoka-crypto-api/src/main/java/io/hotmoka/crypto/api/Account.java index 0f85e240e..1e33a08c0 100644 --- a/io-hotmoka-crypto-api/src/main/java/io/hotmoka/crypto/api/Account.java +++ b/io-hotmoka-crypto-api/src/main/java/io/hotmoka/crypto/api/Account.java @@ -40,19 +40,19 @@ public interface Account> extends Entropy { * Dumps the entropy of this account into a PEM file with the name of the reference of this account. * * @param where the directory where the file must be dumped - * @return the full name of the PEM file (name of the reference of this account followed by {@code .pem}) + * @return the full path of the PEM file (name of the reference of this account followed by {@code .pem}) * @throws IOException if the PEM file cannot be created */ - String dump(Path where) throws IOException; + Path dump(Path where) throws IOException; /** * Dumps the entropy of this account into a PEM file, in the current directory, * with the name of the reference of this account. * - * @return the full name of the PEM file (name of the reference of this account followed by {@code .pem}) + * @return the full path of the PEM file (name of the reference of this account followed by {@code .pem}) * @throws IOException if the PEM file cannot be created */ - String dump() throws IOException; + Path dump() throws IOException; /** * Removes the PEM file, in the current directory, diff --git a/io-hotmoka-crypto-api/src/main/java/io/hotmoka/crypto/api/Entropy.java b/io-hotmoka-crypto-api/src/main/java/io/hotmoka/crypto/api/Entropy.java index 24d6c4300..020794bc6 100644 --- a/io-hotmoka-crypto-api/src/main/java/io/hotmoka/crypto/api/Entropy.java +++ b/io-hotmoka-crypto-api/src/main/java/io/hotmoka/crypto/api/Entropy.java @@ -46,19 +46,19 @@ public interface Entropy extends Comparable { * * @param where the directory where the file must be dumped * @param filePrefix the name of the PEM file, without the trailing {@code .pem} - * @return the full name of the PEM file ({@code filePrefix} followed by {@code .pem}) + * @return the full path of the PEM file ({@code filePrefix} followed by {@code .pem}) * @throws IOException if the PEM file cannot be created */ - String dump(Path where, String filePrefix) throws IOException; + Path dump(Path where, String filePrefix) throws IOException; /** * Dumps this entropy into a PEM file in the current directory. * * @param filePrefix the name of the PEM file, without the trailing {@code .pem} - * @return the full name of the PEM file ({@code filePrefix} followed by {@code .pem}) + * @return the full path of the PEM file ({@code filePrefix} followed by {@code .pem}) * @throws IOException if the PEM file cannot be created */ - String dump(String filePrefix) throws IOException; + Path dump(String filePrefix) throws IOException; /** * Deletes the PEM file in the current directory. diff --git a/io-hotmoka-crypto/src/main/java/io/hotmoka/crypto/HashingAlgorithms.java b/io-hotmoka-crypto/src/main/java/io/hotmoka/crypto/HashingAlgorithms.java index d7439952d..befd9cbe3 100644 --- a/io-hotmoka-crypto/src/main/java/io/hotmoka/crypto/HashingAlgorithms.java +++ b/io-hotmoka-crypto/src/main/java/io/hotmoka/crypto/HashingAlgorithms.java @@ -75,7 +75,7 @@ public static HashingAlgorithm of(String name, Function) method.invoke(null, supplier); } catch (NoSuchMethodException | SecurityException | InvocationTargetException | IllegalAccessException | IllegalArgumentException e) { - throw new NoSuchAlgorithmException("unknown hashing algorithm named " + name, e); + throw new NoSuchAlgorithmException("Unknown hashing algorithm named " + name, e); } } diff --git a/io-hotmoka-crypto/src/main/java/io/hotmoka/crypto/internal/AbstractAccountImpl.java b/io-hotmoka-crypto/src/main/java/io/hotmoka/crypto/internal/AbstractAccountImpl.java index b7dcaae5b..f9b951e04 100644 --- a/io-hotmoka-crypto/src/main/java/io/hotmoka/crypto/internal/AbstractAccountImpl.java +++ b/io-hotmoka-crypto/src/main/java/io/hotmoka/crypto/internal/AbstractAccountImpl.java @@ -90,12 +90,12 @@ public String toString() { } @Override - public String dump(Path where) throws IOException { + public Path dump(Path where) throws IOException { return super.dump(where, toString()); } @Override - public String dump() throws IOException { + public Path dump() throws IOException { return dump(toString()); } diff --git a/io-hotmoka-crypto/src/main/java/io/hotmoka/crypto/internal/EntropyImpl.java b/io-hotmoka-crypto/src/main/java/io/hotmoka/crypto/internal/EntropyImpl.java index e494d0af9..da1b65557 100644 --- a/io-hotmoka-crypto/src/main/java/io/hotmoka/crypto/internal/EntropyImpl.java +++ b/io-hotmoka-crypto/src/main/java/io/hotmoka/crypto/internal/EntropyImpl.java @@ -69,7 +69,7 @@ public EntropyImpl() { * @throws IOException if the PEM file cannot be read */ public EntropyImpl(String filePrefix) throws IOException { - try (PemReader reader = new PemReader(new FileReader(filePrefix + ".pem"))) { + try (var reader = new PemReader(new FileReader(filePrefix + ".pem"))) { entropy = reader.readPemObject().getContent(); } @@ -114,29 +114,29 @@ public String toString() { } @Override - public String dump(Path where, String filePrefix) throws IOException { - PemObject pemObject = new PemObject("ENTROPY", entropy); + public Path dump(Path where, String filePrefix) throws IOException { + var pemObject = new PemObject("ENTROPY", entropy); String fileName = filePrefix + ".pem"; Path resolved = where.resolve(fileName); - try (PemWriter pemWriter = new PemWriter(new OutputStreamWriter(Files.newOutputStream(resolved)))) { + try (var pemWriter = new PemWriter(new OutputStreamWriter(Files.newOutputStream(resolved)))) { pemWriter.writeObject(pemObject); } - return resolved.toString(); + return resolved; } @Override - public String dump(String filePrefix) throws IOException { - PemObject pemObject = new PemObject("ENTROPY", entropy); + public Path dump(String filePrefix) throws IOException { + var pemObject = new PemObject("ENTROPY", entropy); String fileName = filePrefix + ".pem"; Path resolved = Path.of(fileName); - try (PemWriter pemWriter = new PemWriter(new OutputStreamWriter(Files.newOutputStream(resolved)))) { + try (var pemWriter = new PemWriter(new OutputStreamWriter(Files.newOutputStream(resolved)))) { pemWriter.writeObject(pemObject); } - return resolved.toString(); + return resolved; } @Override @@ -152,7 +152,7 @@ public KeyPair keys(String password, SignatureAlgorithm algorithm) { } @Override - public int compareTo(io.hotmoka.crypto.api.Entropy other) { + public int compareTo(Entropy other) { int diff = getClass().getName().compareTo(other.getClass().getName()); if (diff != 0) return diff; diff --git a/io-hotmoka-marshalling/src/main/java/io/hotmoka/marshalling/internal/UnmarshallingContextImpl.java b/io-hotmoka-marshalling/src/main/java/io/hotmoka/marshalling/internal/UnmarshallingContextImpl.java index b0a0839ee..958bbeed9 100644 --- a/io-hotmoka-marshalling/src/main/java/io/hotmoka/marshalling/internal/UnmarshallingContextImpl.java +++ b/io-hotmoka-marshalling/src/main/java/io/hotmoka/marshalling/internal/UnmarshallingContextImpl.java @@ -144,7 +144,7 @@ public String readStringUnshared() throws IOException { @Override public byte[] readBytes(int length, String errorMessage) throws IOException { - byte[] bytes = new byte[length]; + var bytes = new byte[length]; if (length != ois.readNBytes(bytes, 0, length)) throw new IOException(errorMessage); diff --git a/io-hotmoka-tools/src/main/java/io/hotmoka/tools/internal/moka/BindKey.java b/io-hotmoka-tools/src/main/java/io/hotmoka/tools/internal/moka/BindKey.java index ffcb5550d..8438f5df0 100644 --- a/io-hotmoka-tools/src/main/java/io/hotmoka/tools/internal/moka/BindKey.java +++ b/io-hotmoka-tools/src/main/java/io/hotmoka/tools/internal/moka/BindKey.java @@ -16,6 +16,7 @@ package io.hotmoka.tools.internal.moka; +import java.nio.file.Path; import java.util.Base64; import io.hotmoka.beans.requests.InstanceMethodCallTransactionRequest; @@ -60,7 +61,7 @@ protected void execute() throws Exception { var account = Accounts.of(Entropies.load(key), storageReference); System.out.println("A new account " + account + " has been created."); - String fileName = account.dump(); + Path fileName = account.dump(); System.out.println("Its entropy has been saved into the file \"" + fileName + "\"."); } diff --git a/io-hotmoka-tools/src/main/java/io/hotmoka/tools/internal/moka/CreateAccount.java b/io-hotmoka-tools/src/main/java/io/hotmoka/tools/internal/moka/CreateAccount.java index 67ce5f292..41be03c93 100644 --- a/io-hotmoka-tools/src/main/java/io/hotmoka/tools/internal/moka/CreateAccount.java +++ b/io-hotmoka-tools/src/main/java/io/hotmoka/tools/internal/moka/CreateAccount.java @@ -17,6 +17,7 @@ package io.hotmoka.tools.internal.moka; import java.math.BigInteger; +import java.nio.file.Path; import java.security.KeyPair; import java.security.PublicKey; @@ -115,7 +116,7 @@ private Run() throws Exception { StorageReference accountReference = "faucet".equals(payer) ? createAccountFromFaucet() : createAccountFromPayer(); var account = Accounts.of(entropy, accountReference); System.out.println("A new account " + account + " has been created."); - String fileName = account.dump(); + Path fileName = account.dump(); System.out.println("Its entropy has been saved into the file \"" + fileName + "\"."); printPassphrase(account); } diff --git a/io-hotmoka-tools/src/main/java/io/hotmoka/tools/internal/moka/CreateKey.java b/io-hotmoka-tools/src/main/java/io/hotmoka/tools/internal/moka/CreateKey.java index 19e105ff6..ffc42a853 100644 --- a/io-hotmoka-tools/src/main/java/io/hotmoka/tools/internal/moka/CreateKey.java +++ b/io-hotmoka-tools/src/main/java/io/hotmoka/tools/internal/moka/CreateKey.java @@ -16,15 +16,16 @@ package io.hotmoka.tools.internal.moka; +import java.nio.file.Path; import java.security.KeyPair; import java.util.Base64; +import java.util.function.Function; import io.hotmoka.crypto.Base58; import io.hotmoka.crypto.Entropies; import io.hotmoka.crypto.HashingAlgorithms; import io.hotmoka.crypto.Hex; -import io.hotmoka.crypto.api.Entropy; -import io.hotmoka.node.SignatureAlgorithmForTransactionRequests; +import io.hotmoka.crypto.SignatureAlgorithms; import picocli.CommandLine.Command; import picocli.CommandLine.Option; @@ -51,8 +52,8 @@ private class Run { private Run() throws Exception { passwordOfNewKey = ensurePassword(passwordOfNewKey, "the new key", interactive, false); - var signatureAlgorithmOfNewAccount = SignatureAlgorithmForTransactionRequests.ed25519(); - Entropy entropy = Entropies.random(); + var signatureAlgorithmOfNewAccount = SignatureAlgorithms.ed25519(Function.identity()); + var entropy = Entropies.random(); KeyPair keys = entropy.keys(passwordOfNewKey, signatureAlgorithmOfNewAccount); byte[] publicKeyBytes = signatureAlgorithmOfNewAccount.encodingOf(keys.getPublic()); var publicKeyBase58 = Base58.encode(publicKeyBytes); @@ -64,17 +65,17 @@ private Run() throws Exception { byte[] privateKey = signatureAlgorithmOfNewAccount.encodingOf(keys.getPrivate()); System.out.println("Private key Base58: " + Base58.encode(privateKey)); System.out.println("Private key Base64: " + Base64.getEncoder().encodeToString(privateKey)); - byte[] concatenated = new byte[privateKey.length + publicKeyBytes.length]; + var concatenated = new byte[privateKey.length + publicKeyBytes.length]; System.arraycopy(privateKey, 0, concatenated, 0, privateKey.length); System.arraycopy(publicKeyBytes, 0, concatenated, privateKey.length, publicKeyBytes.length); System.out.println("Concatenated private+public key Base64: " + Base64.getEncoder().encodeToString(concatenated)); } - byte[] hashedKey = HashingAlgorithms.sha256((byte[] bytes) -> bytes).hash(publicKeyBytes); + byte[] hashedKey = HashingAlgorithms.sha256(Function.identity()).hash(publicKeyBytes); String hex = Hex.toHexString(hashedKey, 0, 20).toUpperCase(); System.out.println("Tendermint-like address: " + hex); - String fileName = entropy.dump(publicKeyBase58); + Path fileName = entropy.dump(publicKeyBase58); System.out.println("Its entropy has been saved into the file \"" + fileName + "\"."); } } diff --git a/io-hotmoka-tools/src/main/java/io/hotmoka/tools/internal/moka/ImportAccount.java b/io-hotmoka-tools/src/main/java/io/hotmoka/tools/internal/moka/ImportAccount.java index 3b97baee2..730146e12 100644 --- a/io-hotmoka-tools/src/main/java/io/hotmoka/tools/internal/moka/ImportAccount.java +++ b/io-hotmoka-tools/src/main/java/io/hotmoka/tools/internal/moka/ImportAccount.java @@ -16,6 +16,8 @@ package io.hotmoka.tools.internal.moka; +import java.nio.file.Path; + import io.hotmoka.crypto.BIP39Dictionaries; import io.hotmoka.crypto.BIP39Mnemonics; import io.hotmoka.node.Accounts; @@ -41,7 +43,7 @@ protected void execute() throws Exception { var account = BIP39Mnemonics.of(words).toAccount(Accounts::of); System.out.println("The account " + account + " has been imported."); - String fileName = account.dump(); + Path fileName = account.dump(); System.out.println("Its entropy has been saved into the file \"" + fileName + "\"."); } } \ No newline at end of file diff --git a/io-hotmoka-tools/src/main/java/io/hotmoka/tools/internal/moka/InitMemory.java b/io-hotmoka-tools/src/main/java/io/hotmoka/tools/internal/moka/InitMemory.java index 68c6efd4c..1dac8715f 100644 --- a/io-hotmoka-tools/src/main/java/io/hotmoka/tools/internal/moka/InitMemory.java +++ b/io-hotmoka-tools/src/main/java/io/hotmoka/tools/internal/moka/InitMemory.java @@ -105,7 +105,7 @@ private Run() throws Exception { checkPublicKey(keyOfGamete); askForConfirmation(); - MemoryBlockchainConfig nodeConfig = new MemoryBlockchainConfig.Builder() + var nodeConfig = new MemoryBlockchainConfig.Builder() .setMaxGasPerViewTransaction(maxGasPerView) .setDir(dir) .build(); diff --git a/io-hotmoka-tools/src/main/java/io/hotmoka/tools/internal/moka/InitTendermint.java b/io-hotmoka-tools/src/main/java/io/hotmoka/tools/internal/moka/InitTendermint.java index 5c65ec9b4..a79b79068 100644 --- a/io-hotmoka-tools/src/main/java/io/hotmoka/tools/internal/moka/InitTendermint.java +++ b/io-hotmoka-tools/src/main/java/io/hotmoka/tools/internal/moka/InitTendermint.java @@ -40,7 +40,6 @@ import io.hotmoka.constants.Constants; import io.hotmoka.crypto.Base58; import io.hotmoka.crypto.Entropies; -import io.hotmoka.crypto.api.Entropy; import io.hotmoka.helpers.ManifestHelpers; import io.hotmoka.helpers.api.InitializedNode; import io.hotmoka.node.Accounts; @@ -205,8 +204,8 @@ private void bindValidators() throws Exception { String publicKeyBase58 = Base58.encode(Base64.getDecoder().decode(publicKeyBase64)); // the pem file, if it exists, is named with the public key, base58 try { - Entropy entropy = Entropies.load(publicKeyBase58); - String fileName = Accounts.of(entropy, validator).dump(dir); + var entropy = Entropies.load(publicKeyBase58); + Path fileName = Accounts.of(entropy, validator).dump(dir); entropy.delete(publicKeyBase58); System.out.println("The entropy of the validator #" + num + " has been saved into the file \"" + fileName + "\"."); } diff --git a/io-hotmoka-tools/src/main/java/io/hotmoka/tools/internal/moka/RotateKey.java b/io-hotmoka-tools/src/main/java/io/hotmoka/tools/internal/moka/RotateKey.java index 523534335..27a504d27 100644 --- a/io-hotmoka-tools/src/main/java/io/hotmoka/tools/internal/moka/RotateKey.java +++ b/io-hotmoka-tools/src/main/java/io/hotmoka/tools/internal/moka/RotateKey.java @@ -17,6 +17,7 @@ package io.hotmoka.tools.internal.moka; import java.math.BigInteger; +import java.nio.file.Path; import java.security.KeyPair; import java.security.PublicKey; import java.util.Base64; @@ -106,7 +107,7 @@ private Run() throws Exception { rotateKey(); var rotatedAccount = Accounts.of(entropy, account); System.out.println("The key of the account " + rotatedAccount + " has been rotated."); - String fileName = rotatedAccount.dump(); + Path fileName = rotatedAccount.dump(); System.out.println("Its new entropy has been saved into the file \"" + fileName + "\"."); printPassphrase(rotatedAccount); }