com.hedera.hapi.node.transaction.SignedTransaction Maven / Gradle / Ivy
package com.hedera.hapi.node.transaction;
import com.hedera.hapi.node.base.*;
import com.hedera.pbj.runtime.*;
import com.hedera.pbj.runtime.io.*;
import com.hedera.pbj.runtime.io.buffer.*;
import com.hedera.pbj.runtime.io.stream.*;
import edu.umd.cs.findbugs.annotations.*;
import com.hedera.pbj.runtime.Codec;
import java.util.function.Consumer;
import edu.umd.cs.findbugs.annotations.Nullable;
import edu.umd.cs.findbugs.annotations.NonNull;
import static java.util.Objects.requireNonNull;
/**
* SignedTransaction
*
* @param bodyBytes (1) TransactionBody serialized into bytes, which needs to be signed
* @param sigMap (2) The signatures on the body with the new format, to authorize the transaction
*/
public record SignedTransaction(
@NonNull Bytes bodyBytes,
@Nullable SignatureMap sigMap
) {
/** Protobuf codec for reading and writing in protobuf format */
public static final Codec PROTOBUF = new com.hedera.hapi.node.transaction.codec.SignedTransactionProtoCodec();
/** JSON codec for reading and writing in JSON format */
public static final JsonCodec JSON = new com.hedera.hapi.node.transaction.codec.SignedTransactionJsonCodec();
/** Default instance with all fields set to default values */
public static final SignedTransaction DEFAULT = newBuilder().build();
/**
* Create a pre-populated SignedTransaction.
*
* @param bodyBytes (1) TransactionBody serialized into bytes, which needs to be signed,
* @param sigMap (2) The signatures on the body with the new format, to authorize the transaction
*/
public SignedTransaction(Bytes bodyBytes, SignatureMap sigMap) {
this.bodyBytes = bodyBytes != null ? bodyBytes : Bytes.EMPTY;
this.sigMap = sigMap;
}
/**
* Override the default hashCode method for
* all other objects to make hashCode
*/
@Override
public int hashCode() {
int result = 1;
if (bodyBytes != null && !bodyBytes.equals(DEFAULT.bodyBytes)) {
result = 31 * result + bodyBytes.hashCode();
}
if (sigMap != null && !sigMap.equals(DEFAULT.sigMap)) {
result = 31 * result + sigMap.hashCode();
}
long hashCode = result;
// Shifts: 30, 27, 16, 20, 5, 18, 10, 24, 30
hashCode += hashCode << 30;
hashCode ^= hashCode >>> 27;
hashCode += hashCode << 16;
hashCode ^= hashCode >>> 20;
hashCode += hashCode << 5;
hashCode ^= hashCode >>> 18;
hashCode += hashCode << 10;
hashCode ^= hashCode >>> 24;
hashCode += hashCode << 30;
return (int)hashCode;
}
/**
* Override the default equals method for
*/
@Override
public boolean equals(Object that) {
if (that == null || this.getClass() != that.getClass()) {
return false;
}
SignedTransaction thatObj = (SignedTransaction)that;
if (bodyBytes == null && thatObj.bodyBytes != null) {
return false;
}
if (bodyBytes != null && !bodyBytes.equals(thatObj.bodyBytes)) {
return false;
}
if (sigMap == null && thatObj.sigMap != null) {
return false;
}
if (sigMap != null && !sigMap.equals(thatObj.sigMap)) {
return false;
}
return true;
}
/**
* Convenience method to check if the sigMap has a value
*
* @return true of the sigMap has a value
*/
public boolean hasSigMap() {
return sigMap != null;
}
/**
* Gets the value for sigMap if it has a value, or else returns the default
* value for the type.
*
* @param defaultValue the default value to return if sigMap is null
* @return the value for sigMap if it has a value, or else returns the default value
*/
public SignatureMap sigMapOrElse(@NonNull final SignatureMap defaultValue) {
return hasSigMap() ? sigMap : defaultValue;
}
/**
* Gets the value for sigMap if it has a value, or else throws an NPE.
* value for the type.
*
* @return the value for sigMap if it has a value
* @throws NullPointerException if sigMap is null
*/
public @NonNull SignatureMap sigMapOrThrow() {
return requireNonNull(sigMap, "Field sigMap is null");
}
/**
* Executes the supplied {@link Consumer} if, and only if, the sigMap has a value
*
* @param ifPresent the {@link Consumer} to execute
*/
public void ifSigMap(@NonNull final Consumer ifPresent) {
if (hasSigMap()) {
ifPresent.accept(sigMap);
}
}
/**
* Return a builder for building a copy of this model object. It will be pre-populated with all the data from this
* model object.
*
* @return a pre-populated builder
*/
public Builder copyBuilder() {
return new Builder(bodyBytes, sigMap);
}
/**
* Return a new builder for building a model object. This is just a shortcut for new Model.Builder()
.
*
* @return a new builder
*/
public static Builder newBuilder() {
return new Builder();
}
/**
* Builder class for easy creation, ideal for clean code where performance is not critical. In critical performance
* paths use the constructor directly.
*/
public static final class Builder {
@NonNull private Bytes bodyBytes = Bytes.EMPTY;
@Nullable private SignatureMap sigMap = null;
/**
* Create an empty builder
*/
public Builder() {}
/**
* Create a pre-populated Builder.
*
* @param bodyBytes (1) TransactionBody serialized into bytes, which needs to be signed,
* @param sigMap (2) The signatures on the body with the new format, to authorize the transaction
*/
public Builder(Bytes bodyBytes, SignatureMap sigMap) {
this.bodyBytes = bodyBytes != null ? bodyBytes : Bytes.EMPTY;
this.sigMap = sigMap;
}
/**
* Build a new model record with data set on builder
*
* @return new model record with data set
*/
public SignedTransaction build() {
return new SignedTransaction(bodyBytes, sigMap);
}
/**
* (1) TransactionBody serialized into bytes, which needs to be signed
*
* @param bodyBytes value to set
* @return builder to continue building with
*/
public Builder bodyBytes(@NonNull Bytes bodyBytes) {
this.bodyBytes = bodyBytes != null ? bodyBytes : Bytes.EMPTY;
return this;
}
/**
* (2) The signatures on the body with the new format, to authorize the transaction
*
* @param sigMap value to set
* @return builder to continue building with
*/
public Builder sigMap(@Nullable SignatureMap sigMap) {
this.sigMap = sigMap;
return this;
}
/**
* (2) The signatures on the body with the new format, to authorize the transaction
*
* @param builder A pre-populated builder
* @return builder to continue building with
*/
public Builder sigMap(SignatureMap.Builder builder) {
this.sigMap = builder.build() ;
return this;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy