Skip to content

Commit

Permalink
Simplified the API of Entropy and Account
Browse files Browse the repository at this point in the history
  • Loading branch information
spoto committed Jan 12, 2024
1 parent 0290658 commit 9cf54e9
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,9 @@ public interface Account<R extends Comparable<? super R>> extends Entropy {
*/
R getReference();

/**
* 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 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
*/
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.
* with, as name, 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
Expand All @@ -56,7 +47,7 @@ public interface Account<R extends Comparable<? super R>> extends Entropy {

/**
* Removes the PEM file, in the current directory,
* with the name of the reference of this account, if it exists.
* with the name of the reference of this account, followed by {@code .pem}, if it exists.
*
* @throws IOException if the PEM file cannot be deleted
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public interface Entropy extends Comparable<Entropy> {
*
* @return the entropy (16 bytes)
*/
byte[] getEntropy();
byte[] getEntropyAsBytes();

/**
* Yields the length of the entropy byte array.
Expand All @@ -42,31 +42,12 @@ public interface Entropy extends Comparable<Entropy> {
int length();

/**
* Dumps this entropy into a PEM file.
* Dumps this entropy as a PEM file at the given path.
*
* @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 path of the PEM file ({@code filePrefix} followed by {@code .pem})
* @param path the path of the dumped file
* @throws IOException if the PEM file cannot be created
*/
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 path of the PEM file ({@code filePrefix} followed by {@code .pem})
* @throws IOException if the PEM file cannot be created
*/
Path dump(String filePrefix) throws IOException;

/**
* Deletes the PEM file in the current directory.
*
* @param filePrefix the name of the PEM file, without the trailing {@code .pem}
* @throws IOException if the PEM file cannot be deleted
*/
void delete(String filePrefix) throws IOException;
void dump(Path path) throws IOException;

/**
* Constructs the key pair of this entropy, from the given password.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import io.hotmoka.crypto.api.Account;
import io.hotmoka.crypto.api.BIP39Dictionary;
Expand Down Expand Up @@ -89,19 +91,16 @@ public String toString() {
return reference.toString();
}

@Override
public Path dump(Path where) throws IOException {
return super.dump(where, toString());
}

@Override
public Path dump() throws IOException {
return dump(toString());
var path = Paths.get(this + ".pem");
dump(path);
return path;
}

@Override
public void delete() throws IOException {
delete(toString());
Files.delete(Paths.get(this + ".pem"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class BIP39MnemonicImpl implements BIP39Mnemonic {
public BIP39MnemonicImpl(Account<?> account, BIP39Dictionary dictionary) {
this.dictionary = dictionary;

byte[] entropy = account.getEntropy();
byte[] entropy = account.getEntropyAsBytes();
byte[] reference = account.getReferenceAsBytes();
byte[] merge = new byte[entropy.length + reference.length];
System.arraycopy(entropy, 0, merge, 0, entropy.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public EntropyImpl(Path path) throws IOException {
* @param parent the entropy to clone
*/
public EntropyImpl(Entropy parent) {
this.entropy = parent.getEntropy();
this.entropy = parent.getEntropyAsBytes();
}

/**
Expand All @@ -115,7 +115,7 @@ public EntropyImpl(byte[] entropy) {
}

@Override
public byte[] getEntropy() {
public byte[] getEntropyAsBytes() {
return entropy.clone();
}

Expand All @@ -130,36 +130,10 @@ public String toString() {
}

@Override
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 (var pemWriter = new PemWriter(new OutputStreamWriter(Files.newOutputStream(resolved)))) {
pemWriter.writeObject(pemObject);
public void dump(Path path) throws IOException {
try (var pemWriter = new PemWriter(new OutputStreamWriter(Files.newOutputStream(path)))) {
pemWriter.writeObject(new PemObject("ENTROPY", entropy));
}

return resolved;
}

@Override
public Path dump(String filePrefix) throws IOException {
var pemObject = new PemObject("ENTROPY", entropy);
String fileName = filePrefix + ".pem";
Path resolved = Path.of(fileName);

try (var pemWriter = new PemWriter(new OutputStreamWriter(Files.newOutputStream(resolved)))) {
pemWriter.writeObject(pemObject);
}

return resolved;
}

@Override
public void delete(String filePrefix) throws IOException {
String fileName = filePrefix + ".pem";
Path resolved = Path.of(fileName);
Files.delete(resolved);
}

@Override
Expand All @@ -173,7 +147,7 @@ public int compareTo(Entropy other) {
if (diff != 0)
return diff;
else
return compareBytes(entropy, other.getEntropy());
return compareBytes(entropy, other.getEntropyAsBytes());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
import io.hotmoka.node.remote.internal.http.client.RestClientService;
import io.hotmoka.node.service.NodeServiceConfigBuilders;
import io.hotmoka.node.service.NodeServices;
import io.hotmoka.node.service.api.NodeService;
import io.hotmoka.node.service.api.NodeServiceConfig;

/**
Expand All @@ -67,8 +66,7 @@
class NetworkFromNode extends HotmokaTest {
private static final ConstructorSignature CONSTRUCTOR_INTERNATIONAL_TIME = new ConstructorSignature("io.hotmoka.examples.basicdependency.InternationalTime", INT, INT, INT);

// TODO: change name
private final NodeServiceConfig configNoBanner = NodeServiceConfigBuilders.defaults().setPort(8081).build();
private final NodeServiceConfig config = NodeServiceConfigBuilders.defaults().setPort(8081).build();

/**
* The account that holds all funds.
Expand Down Expand Up @@ -110,7 +108,7 @@ void startNetworkFromNodeAndTestSignatureAlgorithm() {
SignatureAlgorithmResponseModel answer;
RestClientService service = new RestClientService();

try (var nodeRestService = NodeServices.of(configNoBanner, node)) {
try (var nodeRestService = NodeServices.of(config, node)) {
answer = service.get("http://localhost:8081/get/nameOfSignatureAlgorithmForRequests", SignatureAlgorithmResponseModel.class);
}

Expand All @@ -123,7 +121,7 @@ void testGetTakamakaCode() {
TransactionReferenceModel result;
RestClientService service = new RestClientService();

try (NodeService nodeRestService = NodeServices.of(configNoBanner, node)) {
try (var nodeRestService = NodeServices.of(config, node)) {
result = service.get("http://localhost:8081/get/takamakaCode", TransactionReferenceModel.class);
}

Expand All @@ -134,11 +132,11 @@ void testGetTakamakaCode() {
void addJarStoreInitialTransaction() throws IOException {
ErrorModel errorModel = null;

try (NodeService nodeRestService = NodeServices.of(configNoBanner, node)) {
try (var nodeRestService = NodeServices.of(config, node)) {
var request = new JarStoreInitialTransactionRequest(Files.readAllBytes(Paths.get("jars/c13.jar")), node.getTakamakaCode());

try {
RestClientService service = new RestClientService();
var service = new RestClientService();
service.post(
"http://localhost:8081/add/jarStoreInitialTransaction",
new JarStoreInitialTransactionRequestModel(request),
Expand All @@ -158,8 +156,8 @@ void addJarStoreInitialTransaction() throws IOException {
void addJarStoreInitialTransactionWithoutJar() {
ErrorModel errorModel = null;

try (NodeService nodeRestService = NodeServices.of(configNoBanner, node)) {
JsonObject bodyJson = new JsonObject();
try (var nodeRestService = NodeServices.of(config, node)) {
var bodyJson = new JsonObject();
bodyJson.addProperty("jar", (String) null);

try {
Expand All @@ -183,7 +181,7 @@ void addJarStoreInitialTransactionWithoutJar() {
void addConstructorCallTransaction() throws SignatureException, InvalidKeyException {
StorageReferenceModel result;

try (NodeService nodeRestService = NodeServices.of(configNoBanner, node)) {
try (var nodeRestService = NodeServices.of(config, node)) {
var request = new ConstructorCallTransactionRequest(
signature().getSigner(key, SignedTransactionRequest::toByteArrayWithoutSignature),
master,
Expand All @@ -196,7 +194,7 @@ void addConstructorCallTransaction() throws SignatureException, InvalidKeyExcept
new IntValue(1973)
);

RestClientService service = new RestClientService();
var service = new RestClientService();
result = service.post(
"http://localhost:8081/add/constructorCallTransaction",
new ConstructorCallTransactionRequestModel(request),
Expand All @@ -211,7 +209,7 @@ void addConstructorCallTransaction() throws SignatureException, InvalidKeyExcept
void testGetState() throws InvalidKeyException, SignatureException {
StateModel state;

try (var nodeRestService = NodeServices.of(configNoBanner, node)) {
try (var nodeRestService = NodeServices.of(config, node)) {
var request = new ConstructorCallTransactionRequest(
signature().getSigner(key, SignedTransactionRequest::toByteArrayWithoutSignature),
master,
Expand Down Expand Up @@ -244,7 +242,7 @@ void testGetState() throws InvalidKeyException, SignatureException {
void testGetClassTag() throws InvalidKeyException, SignatureException {
ClassTagModel classTag;

try (var nodeRestService = NodeServices.of(configNoBanner, node)) {
try (var nodeRestService = NodeServices.of(config, node)) {
var request = new ConstructorCallTransactionRequest(
signature().getSigner(key, SignedTransactionRequest::toByteArrayWithoutSignature),
master,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package io.hotmoka.tools.internal.moka;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64;

Expand Down Expand Up @@ -54,15 +53,14 @@ protected void execute() throws Exception {
StorageReference storageReference;
if ("anonymous".equals(reference))
storageReference = getReferenceFromAccountLedger();
else {
else {
checkStorageReference(reference);
storageReference = new StorageReference(reference);
}

var account = Accounts.of(Entropies.load(Paths.get(key+ ".pem")), storageReference);
System.out.println("A new account " + account + " has been created.");
Path fileName = account.dump();
System.out.println("Its entropy has been saved into the file \"" + fileName + "\".");
System.out.println("Its entropy has been saved into the file \"" + account.dump() + "\".");
}

private StorageReference getReferenceFromAccountLedger() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package io.hotmoka.tools.internal.moka;

import java.math.BigInteger;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyPair;
import java.security.PublicKey;
Expand Down Expand Up @@ -116,8 +115,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.");
Path fileName = account.dump();
System.out.println("Its entropy has been saved into the file \"" + fileName + "\".");
System.out.println("Its entropy has been saved into the file \"" + account.dump() + "\".");
printPassphrase(account);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

package io.hotmoka.tools.internal.moka;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyPair;
import java.util.Base64;
import java.util.function.Function;

import io.hotmoka.crypto.Base58;
import io.hotmoka.crypto.Base64;
import io.hotmoka.crypto.Entropies;
import io.hotmoka.crypto.HashingAlgorithms;
import io.hotmoka.crypto.Hex;
Expand Down Expand Up @@ -59,23 +59,24 @@ private Run() throws Exception {
var publicKeyBase58 = Base58.encode(publicKeyBytes);
System.out.println("A new key has been created.");
System.out.println("Public key Base58: " + publicKeyBase58);
System.out.println("Public key Base64: " + Base64.getEncoder().encodeToString(publicKeyBytes));
System.out.println("Public key Base64: " + Base64.toBase64String(publicKeyBytes));

if (privateKey) {
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));
System.out.println("Private key Base64: " + Base64.toBase64String(privateKey));
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));
System.out.println("Concatenated private+public key Base64: " + Base64.toBase64String(concatenated));
}

byte[] hashedKey = HashingAlgorithms.sha256().getHasher(Function.identity()).hash(publicKeyBytes);
String hex = Hex.toHexString(hashedKey, 0, 20).toUpperCase();
System.out.println("Tendermint-like address: " + hex);

Path fileName = entropy.dump(publicKeyBase58);
var fileName = Paths.get(publicKeyBase58 + ".pem");
entropy.dump(fileName);
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,8 +16,6 @@

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 @@ -43,7 +41,6 @@ protected void execute() throws Exception {

var account = BIP39Mnemonics.of(words).toAccount(Accounts::of);
System.out.println("The account " + account + " has been imported.");
Path fileName = account.dump();
System.out.println("Its entropy has been saved into the file \"" + fileName + "\".");
System.out.println("Its entropy has been saved into the file \"" + account.dump() + "\".");
}
}
Loading

0 comments on commit 9cf54e9

Please sign in to comment.