Skip to content

Commit

Permalink
Code clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
spoto committed Sep 5, 2023
1 parent 3b39f21 commit f7670b4
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ public interface Account<R extends Comparable<? super R>> 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,19 @@ public interface Entropy extends Comparable<Entropy> {
*
* @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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static <T> HashingAlgorithm<T> of(String name, Function<? super T, byte[]
return (HashingAlgorithm<T>) 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);
}
}

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 + "\".");
}

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

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

Expand All @@ -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);
Expand All @@ -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 + "\".");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 + "\".");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 + "\".");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit f7670b4

Please sign in to comment.