Skip to content

Commit

Permalink
Removed duplictaed Signer class
Browse files Browse the repository at this point in the history
  • Loading branch information
spoto committed Aug 23, 2023
1 parent e563c36 commit c45f1b2
Show file tree
Hide file tree
Showing 42 changed files with 220 additions and 208 deletions.
5 changes: 5 additions & 0 deletions io-hotmoka-beans/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
<groupId>io.hotmoka</groupId>
<artifactId>io-hotmoka-constants</artifactId>
<version>${hotmoka.version}</version>
</dependency>
<dependency>
<groupId>io.hotmoka</groupId>
<artifactId>io-hotmoka-crypto-api</artifactId>
<version>${hotmoka.version}</version>
</dependency>
<dependency>
<groupId>io.hotmoka</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io.hotmoka.beans.signatures.ConstructorSignature;
import io.hotmoka.beans.values.StorageReference;
import io.hotmoka.beans.values.StorageValue;
import io.hotmoka.crypto.api.Signer;
import io.hotmoka.marshalling.api.MarshallingContext;
import io.hotmoka.marshalling.api.UnmarshallingContext;

Expand Down Expand Up @@ -69,7 +70,7 @@ public class ConstructorCallTransactionRequest extends CodeExecutionTransactionR
* @throws SignatureException if the signer cannot sign the request
* @throws InvalidKeyException if the signer uses an invalid private key
*/
public ConstructorCallTransactionRequest(Signer signer, StorageReference caller, BigInteger nonce, String chainId, BigInteger gasLimit, BigInteger gasPrice, TransactionReference classpath, ConstructorSignature constructor, StorageValue... actuals) throws InvalidKeyException, SignatureException {
public ConstructorCallTransactionRequest(Signer<? super ConstructorCallTransactionRequest> signer, StorageReference caller, BigInteger nonce, String chainId, BigInteger gasLimit, BigInteger gasPrice, TransactionReference classpath, ConstructorSignature constructor, StorageValue... actuals) throws InvalidKeyException, SignatureException {
super(caller, nonce, gasLimit, gasPrice, classpath, actuals);

if (constructor == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import io.hotmoka.beans.values.LongValue;
import io.hotmoka.beans.values.StorageReference;
import io.hotmoka.beans.values.StorageValue;
import io.hotmoka.crypto.api.Signer;
import io.hotmoka.marshalling.api.MarshallingContext;
import io.hotmoka.marshalling.api.UnmarshallingContext;

Expand Down Expand Up @@ -74,7 +75,7 @@ public class InstanceMethodCallTransactionRequest extends AbstractInstanceMethod
* @throws SignatureException if the signer cannot sign the request
* @throws InvalidKeyException if the signer uses an invalid private key
*/
public InstanceMethodCallTransactionRequest(Signer signer, StorageReference caller, BigInteger nonce, String chainId, BigInteger gasLimit, BigInteger gasPrice, TransactionReference classpath, MethodSignature method, StorageReference receiver, StorageValue... actuals) throws InvalidKeyException, SignatureException {
public InstanceMethodCallTransactionRequest(Signer<? super InstanceMethodCallTransactionRequest> signer, StorageReference caller, BigInteger nonce, String chainId, BigInteger gasLimit, BigInteger gasPrice, TransactionReference classpath, MethodSignature method, StorageReference receiver, StorageValue... actuals) throws InvalidKeyException, SignatureException {
super(caller, nonce, gasLimit, gasPrice, classpath, method, receiver, actuals);

if (chainId == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.hotmoka.beans.references.TransactionReference;
import io.hotmoka.beans.responses.JarStoreNonInitialTransactionResponse;
import io.hotmoka.beans.values.StorageReference;
import io.hotmoka.crypto.api.Signer;
import io.hotmoka.marshalling.api.MarshallingContext;
import io.hotmoka.marshalling.api.UnmarshallingContext;

Expand Down Expand Up @@ -72,7 +73,7 @@ public class JarStoreTransactionRequest extends NonInitialTransactionRequest<Jar
* @throws SignatureException if the signer cannot sign the request
* @throws InvalidKeyException if the signer uses an invalid private key
*/
public JarStoreTransactionRequest(Signer signer, StorageReference caller, BigInteger nonce, String chainId, BigInteger gasLimit, BigInteger gasPrice, TransactionReference classpath, byte[] jar, TransactionReference... dependencies) throws InvalidKeyException, SignatureException {
public JarStoreTransactionRequest(Signer<? super JarStoreTransactionRequest> signer, StorageReference caller, BigInteger nonce, String chainId, BigInteger gasLimit, BigInteger gasPrice, TransactionReference classpath, byte[] jar, TransactionReference... dependencies) throws InvalidKeyException, SignatureException {
super(caller, nonce, gasLimit, gasPrice, classpath);

if (jar == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.SignatureException;

import io.hotmoka.beans.marshalling.BeanMarshallingContext;
import io.hotmoka.beans.values.StorageReference;
Expand Down Expand Up @@ -85,20 +83,4 @@ default byte[] toByteArrayWithoutSignature() {
throw new RuntimeException("unexpected exception", e);
}
}

/**
* An object that provides the signature of a request.
*/
interface Signer {

/**
* Computes the signature of the given request.
*
* @param what the request to sign
* @return the signature of the request
* @throws InvalidKeyException if the private key used for signing is invalid
* @throws SignatureException if the request cannot be signed
*/
byte[] sign(SignedTransactionRequest what) throws InvalidKeyException, SignatureException;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io.hotmoka.beans.signatures.MethodSignature;
import io.hotmoka.beans.values.StorageReference;
import io.hotmoka.beans.values.StorageValue;
import io.hotmoka.crypto.api.Signer;
import io.hotmoka.marshalling.api.MarshallingContext;
import io.hotmoka.marshalling.api.UnmarshallingContext;

Expand Down Expand Up @@ -65,7 +66,7 @@ public class StaticMethodCallTransactionRequest extends MethodCallTransactionReq
* @throws SignatureException if the signer cannot sign the request
* @throws InvalidKeyException if the signer uses an invalid private key
*/
public StaticMethodCallTransactionRequest(Signer signer, StorageReference caller, BigInteger nonce, String chainId, BigInteger gasLimit, BigInteger gasPrice, TransactionReference classpath, MethodSignature method, StorageValue... actuals) throws InvalidKeyException, SignatureException {
public StaticMethodCallTransactionRequest(Signer<? super StaticMethodCallTransactionRequest> signer, StorageReference caller, BigInteger nonce, String chainId, BigInteger gasLimit, BigInteger gasPrice, TransactionReference classpath, MethodSignature method, StorageValue... actuals) throws InvalidKeyException, SignatureException {
super(caller, nonce, gasLimit, gasPrice, classpath, method, actuals);

if (chainId == null)
Expand Down
1 change: 1 addition & 0 deletions io-hotmoka-beans/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
exports io.hotmoka.beans;
requires transitive io.hotmoka.marshalling;
requires io.hotmoka.constants;
requires transitive io.hotmoka.crypto.api;
requires io.hotmoka.annotations;
requires io.hotmoka.exceptions;
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public interface SignatureAlgorithm<T> {
* @return the name of the algorithm
*/
String getName();

/**
* Creates a key pair from the given entropy and password.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright 2023 Fausto Spoto
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package io.hotmoka.crypto.api;

import java.security.InvalidKeyException;
import java.security.SignatureException;

/**
* An object that computes the signature of a value with a private key.
*
* @param <T> the type of values that get signed
*/
public interface Signer<T> {

/**
* Computes the signature of the given value with the given key.
*
* @param what the value to sign
* @return the signature of the value
* @throws InvalidKeyException if the private key used for signing is invalid
* @throws SignatureException if the value cannot be signed
*/
byte[] sign(T what) throws InvalidKeyException, SignatureException;
}
37 changes: 37 additions & 0 deletions io-hotmoka-crypto/src/main/java/io/hotmoka/crypto/Signers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.hotmoka.crypto;

import java.security.KeyPair;
import java.security.PrivateKey;

import io.hotmoka.crypto.api.SignatureAlgorithm;
import io.hotmoka.crypto.api.Signer;

/**
* Providers of objects that sign values with a private key.
*/
public final class Signers<T> {

private Signers() {}

/**
* Yields a signer for the given algorithm with the given key pair.
*
* @param signature the signing algorithm
* @param keys the key pair
* @return the signer
*/
public static <T> Signer<T> with(SignatureAlgorithm<? super T> signature, KeyPair keys) {
return with(signature, keys.getPrivate());
}

/**
* Yields a signer for the given algorithm with the given private key.
*
* @param signature the signing algorithm
* @param key the private key
* @return the signer
*/
public static <T> Signer<T> with(SignatureAlgorithm<? super T> signature, PrivateKey key) {
return what -> signature.sign(what, key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import io.hotmoka.beans.values.BigIntegerValue;
import io.hotmoka.beans.values.StorageReference;
import io.hotmoka.beans.values.StringValue;
import io.hotmoka.crypto.SignatureAlgorithms;
import io.hotmoka.crypto.Signers;
import io.hotmoka.crypto.api.SignatureAlgorithm;
import io.hotmoka.helpers.GasHelpers;
import io.hotmoka.helpers.NonceHelpers;
Expand All @@ -49,7 +51,6 @@
import io.hotmoka.helpers.api.NonceHelper;
import io.hotmoka.nodes.Node;
import io.hotmoka.nodes.SignatureAlgorithmForTransactionRequests;
import io.hotmoka.nodes.Signer;

/**
* An object that helps with the creation of new accounts.
Expand Down Expand Up @@ -115,7 +116,7 @@ public StorageReference paidByFaucet(SignatureAlgorithm<SignedTransactionRequest
// we use an empty signature algorithm and an arbitrary key, since the faucet is unsigned
SignatureAlgorithm<SignedTransactionRequest> signatureForFaucet = SignatureAlgorithmForTransactionRequests.empty();
KeyPair keyPair = signatureForFaucet.getKeyPair();
var signer = Signer.with(signatureForFaucet, keyPair);
var signer = Signers.with(signatureForFaucet, keyPair);
String publicKeyEncoded = Base64.getEncoder().encodeToString(signatureAlgorithm.encodingOf(publicKey));
var request = new InstanceMethodCallTransactionRequest
(signer, gamete, nonceHelper.getNonceOf(gamete),
Expand Down Expand Up @@ -162,7 +163,7 @@ public StorageReference paidBy(StorageReference payer, KeyPair keysOfPayer,

gasHandler.accept(totalGas);

var signer = Signer.with(signatureForPayer, keysOfPayer);
var signer = Signers.with(signatureForPayer, keysOfPayer);
String publicKeyEncoded = Base64.getEncoder().encodeToString(signatureAlgorithm.encodingOf(publicKey));
StorageReference account;
TransactionRequest<?> request1;
Expand Down Expand Up @@ -212,12 +213,12 @@ public StorageReference tendermintValidatorPaidByFaucet(PublicKey publicKey,
StorageReference gamete = (StorageReference) node.runInstanceMethodCallTransaction(new InstanceMethodCallTransactionRequest
(manifest, _100_000, takamakaCode, CodeSignature.GET_GAMETE, manifest));

BigInteger gas = gasForCreatingAccountWithSignature("ed25519");
BigInteger gas = gasForCreatingAccountWithSignature(SignatureAlgorithms.TYPES.ED25519.name());

// we use an empty signature algorithm and an arbitrary key, since the faucet is unsigned
SignatureAlgorithm<SignedTransactionRequest> signatureForFaucet = SignatureAlgorithmForTransactionRequests.empty();
KeyPair keyPair = signatureForFaucet.getKeyPair();
var signer = Signer.with(signatureForFaucet, keyPair);
var signer = Signers.with(signatureForFaucet, keyPair);
String publicKeyEncoded = Base64.getEncoder().encodeToString(SignatureAlgorithmForTransactionRequests.ed25519().encodingOf(publicKey));
var request = new InstanceMethodCallTransactionRequest
(signer, gamete, nonceHelper.getNonceOf(gamete),
Expand All @@ -237,13 +238,13 @@ public StorageReference tendermintValidatorPaidBy(StorageReference payer, KeyPai

SignatureAlgorithm<SignedTransactionRequest> signatureForPayer = SignatureHelpers.of(node).signatureAlgorithmFor(payer);

BigInteger gas1 = gasForCreatingAccountWithSignature("ed25519");
BigInteger gas1 = gasForCreatingAccountWithSignature(SignatureAlgorithms.TYPES.ED25519.name());
BigInteger gas2 = gasForTransactionWhosePayerHasSignature(signatureForPayer.getName());
BigInteger totalGas = balanceRed.signum() > 0 ? gas1.add(gas2).add(gas2) : gas1.add(gas2);

gasHandler.accept(totalGas);

var signer = Signer.with(signatureForPayer, keysOfPayer);
var signer = Signers.with(signatureForPayer, keysOfPayer);
String publicKeyEncoded = Base64.getEncoder().encodeToString(SignatureAlgorithmForTransactionRequests.ed25519().encodingOf(publicKey));
var request1 = new ConstructorCallTransactionRequest
(signer, payer, nonceHelper.getNonceOf(payer),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@
import io.hotmoka.beans.values.StorageReference;
import io.hotmoka.beans.values.StorageValue;
import io.hotmoka.beans.values.StringValue;
import io.hotmoka.crypto.Signers;
import io.hotmoka.crypto.api.SignatureAlgorithm;
import io.hotmoka.crypto.api.Signer;
import io.hotmoka.helpers.GasHelpers;
import io.hotmoka.helpers.SignatureHelpers;
import io.hotmoka.helpers.api.AccountsNode;
import io.hotmoka.nodes.Node;
import io.hotmoka.nodes.Signer;

/**
* A decorator of a node, that creates some initial accounts in it.
Expand Down Expand Up @@ -121,7 +122,7 @@ public AccountsNodeImpl(Node parent, StorageReference payer, PrivateKey privateK

StorageReference manifest = getManifest();
SignatureAlgorithm<SignedTransactionRequest> signature = SignatureHelpers.of(this).signatureAlgorithmFor(payer);
var signerOnBehalfOfPayer = Signer.with(signature, privateKeyOfPayer);
Signer<SignedTransactionRequest> signerOnBehalfOfPayer = Signers.with(signature, privateKeyOfPayer);
var _100_000 = BigInteger.valueOf(100_000L);
var _200_000 = BigInteger.valueOf(200_000L);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@
import io.hotmoka.beans.values.StorageReference;
import io.hotmoka.beans.values.StorageValue;
import io.hotmoka.beans.values.StringValue;
import io.hotmoka.crypto.Signers;
import io.hotmoka.crypto.api.SignatureAlgorithm;
import io.hotmoka.crypto.api.Signer;
import io.hotmoka.helpers.GasHelpers;
import io.hotmoka.helpers.SignatureHelpers;
import io.hotmoka.helpers.api.JarsNode;
import io.hotmoka.nodes.Node;
import io.hotmoka.nodes.Signer;

/**
* A decorator of a node, that installs some jars in the node.
Expand Down Expand Up @@ -100,7 +101,7 @@ public JarsNodeImpl(Node parent, StorageReference payer, PrivateKey privateKeyOf

TransactionReference takamakaCode = getTakamakaCode();
SignatureAlgorithm<SignedTransactionRequest> signature = SignatureHelpers.of(this).signatureAlgorithmFor(payer);
Signer signerOnBehalfOfPayer = Signer.with(signature, privateKeyOfPayer);
Signer<SignedTransactionRequest> signerOnBehalfOfPayer = Signers.with(signature, privateKeyOfPayer);
var _50_000 = BigInteger.valueOf(50_000);

// we get the nonce of the payer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
import io.hotmoka.beans.values.BigIntegerValue;
import io.hotmoka.beans.values.StorageReference;
import io.hotmoka.beans.values.StringValue;
import io.hotmoka.crypto.Signers;
import io.hotmoka.crypto.api.SignatureAlgorithm;
import io.hotmoka.helpers.NonceHelpers;
import io.hotmoka.helpers.api.MintBurnHelper;
import io.hotmoka.helpers.api.NonceHelper;
import io.hotmoka.nodes.Node;
import io.hotmoka.nodes.Signer;

/**
* Implementation of a helper for minting and burning coins of an account in the accounts ledger
Expand Down Expand Up @@ -75,7 +75,7 @@ public MintBurnHelperImpl(Node node) throws TransactionRejectedException, Transa

@Override
public StorageReference mint(KeyPair keysOfGamete, SignatureAlgorithm<SignedTransactionRequest> signatureAlgorithm, String publicKey, BigInteger amount) throws InvalidKeyException, SignatureException, TransactionRejectedException, TransactionException, CodeExecutionException {
var signer = Signer.with(signatureAlgorithm, keysOfGamete);
var signer = Signers.with(signatureAlgorithm, keysOfGamete);

// we look up the account in the accounts ledger; if it is not there, it will be created
// we use 0 as gas price, so that the gamete will not pay for that (the add method of the accounts ledger
Expand All @@ -93,7 +93,7 @@ public StorageReference mint(KeyPair keysOfGamete, SignatureAlgorithm<SignedTran

@Override
public StorageReference burn(KeyPair keysOfGamete, SignatureAlgorithm<SignedTransactionRequest> signatureAlgorithm, String publicKey, BigInteger amount) throws InvalidKeyException, SignatureException, TransactionRejectedException, TransactionException, CodeExecutionException {
var signer = Signer.with(signatureAlgorithm, keysOfGamete);
var signer = Signers.with(signatureAlgorithm, keysOfGamete);

// we look up the account in the accounts ledger
var account = (StorageReference) node.runInstanceMethodCallTransaction(new InstanceMethodCallTransactionRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import io.hotmoka.beans.values.BigIntegerValue;
import io.hotmoka.beans.values.StorageReference;
import io.hotmoka.beans.values.StringValue;
import io.hotmoka.crypto.Signers;
import io.hotmoka.crypto.api.SignatureAlgorithm;
import io.hotmoka.helpers.GasHelpers;
import io.hotmoka.helpers.NonceHelpers;
Expand All @@ -48,7 +49,6 @@
import io.hotmoka.helpers.api.SendCoinsHelper;
import io.hotmoka.nodes.Node;
import io.hotmoka.nodes.SignatureAlgorithmForTransactionRequests;
import io.hotmoka.nodes.Signer;

/**
* Implementation of an object that helps with sending coins to accounts.
Expand Down Expand Up @@ -87,7 +87,7 @@ public void sendFromPayer(StorageReference payer, KeyPair keysOfPayer,
throws TransactionRejectedException, TransactionException, CodeExecutionException, InvalidKeyException, SignatureException, NoSuchAlgorithmException, ClassNotFoundException {

SignatureAlgorithm<SignedTransactionRequest> signature = SignatureHelpers.of(node).signatureAlgorithmFor(payer);
var signer = Signer.with(signature, keysOfPayer);
var signer = Signers.with(signature, keysOfPayer);
BigInteger gas = gasForTransactionWhosePayerHasSignature(signature.getName(), node);
BigInteger totalGas = amountRed.signum() > 0 ? gas.add(gas) : gas;
gasHandler.accept(totalGas);
Expand Down Expand Up @@ -130,7 +130,7 @@ public void sendFromFaucet(StorageReference destination, BigInteger amount, BigI
// we use the empty signature algorithm, since the faucet is unsigned
SignatureAlgorithm<SignedTransactionRequest> signature = SignatureAlgorithmForTransactionRequests.empty();
var request = new InstanceMethodCallTransactionRequest
(Signer.with(signature, signature.getKeyPair()),
(Signers.with(signature, signature.getKeyPair()),
gamete, nonceHelper.getNonceOf(gamete),
chainId, _100_000, gasHelper.getGasPrice(), takamakaCode,
new VoidMethodSignature(GAMETE, "faucet", PAYABLE_CONTRACT, BIG_INTEGER, BIG_INTEGER),
Expand Down
Loading

0 comments on commit c45f1b2

Please sign in to comment.