Class SignedData<T extends ACell>

Type Parameters:
T - The type of the signed object
All Implemented Interfaces:
IValidated, IWriteable

public class SignedData<T extends ACell> extends ACell
Node representing a signed data object. A signed data object encapsulates:
  • An Address that identifies the signer
  • A digital signature
  • An underlying Cell that has been signed.
The SignedData instance is considered valid if the signature can be successfully validated for the given Address and data value, and if so can be taken as a cryptographic proof that the signature was created by someone in possession of the corresponding private key. Note we currently go via a Ref here for a few reasons: - It guarantees we have a hash for signing - It makes the SignedData object implementation/representation independent of the value type - It creates a possibility of structural sharing for transaction values excluding signatures Binary representation:
  1. 1 byte - Tag.SIGNED_DATA tag
  2. 32 bytes - Public Key of signer
  3. 64 bytes - raw Signature data
  4. 1+ bytes - Data Value Ref (may be embedded)
SECURITY: signing requires presence of a local keypair TODO: SECURITY: any persistence forces validation of Signature??
  • Method Details

    • createWithRef

      public static <T extends ACell> SignedData<T> createWithRef(AKeyPair keyPair, Ref<T> ref)
      Signs a data value Ref with the given keypair. SECURITY: Marks as already validated, since we just signed it.
      Parameters:
      keyPair - The public/private key pair of the signer.
      ref - Ref to the data to sign
      Returns:
      SignedData object signed with the given key-pair
    • create

      public static <T extends ACell> SignedData<T> create(AKeyPair keyPair, T value2)
    • create

      public static <T extends ACell> SignedData<T> create(AccountKey address, ASignature sig, Ref<T> ref)
      Creates a SignedData object with the given parameters. SECURITY: Not assumed to be valid.
      Parameters:
      address - Public Address of the signer
      sig - Signature of the supplied data
      ref - Ref to the data that has been signed
      Returns:
      A new SignedData object
    • create

      public static SignedData<ATransaction> create(AKeyPair kp, ASignature sig, Ref<ATransaction> ref)
    • getValue

      public T getValue()
      Gets the signed value object encapsulated by this SignedData object. Does not check Signature.
      Returns:
      Data value that has been signed
    • getAccountKey

      public AccountKey getAccountKey()
      Gets the public key of the signer. If the signature is valid, this represents a cryptographic proof that the signer was in possession of the private key of this address.
      Returns:
      Public Key of signer.
    • getSignature

      public ASignature getSignature()
      Gets the Signature that formed part of this SignedData object
      Returns:
      Signature instance
    • encode

      public int encode(byte[] bs, int pos)
      Description copied from class: ACell
      Writes this Cell's encoding to a byte array, including a tag byte which will be written first. Cell must be canonical, or else an error may occur.
      Specified by:
      encode in interface IWriteable
      Specified by:
      encode in class ACell
      Parameters:
      bs - A byte array to which to write the encoding
      pos - The offset into the byte array
      Returns:
      New position after writing
    • encodeRaw

      public int encodeRaw(byte[] bs, int pos)
      Description copied from class: ACell
      Writes this Cell's encoding to a byte array, excluding the tag byte.
      Specified by:
      encodeRaw in class ACell
      Parameters:
      bs - A byte array to which to write the encoding
      pos - The offset into the byte array
      Returns:
      New position after writing
    • estimatedEncodingSize

      public int estimatedEncodingSize()
      Description copied from interface: IWriteable
      Estimate the encoded data size for this Cell. Used for quickly sizing buffers. Implementations should try to return a size that is likely to contain the entire object when represented in binary format, including the tag byte.
      Returns:
      The estimated size for the binary representation of this object.
    • read

      public static <T extends ACell> SignedData<T> read(ByteBuffer data) throws BadFormatException
      Reads a SignedData instance from the given ByteBuffer
      Parameters:
      data - A ByteBuffer containing
      Returns:
      A SignedData object
      Throws:
      BadFormatException - If encoding is invalid
    • checkSignature

      public boolean checkSignature()
      Validates the signature in this SignedData instance. Caches result
      Returns:
      true if valid, false otherwise
    • isSignatureChecked

      public boolean isSignatureChecked()
      Checks if the signature has already gone through verification. MAy or may not be a valid signature.
      Returns:
      true if valid, false otherwise
    • validateSignature

      public void validateSignature() throws BadSignatureException
      Throws:
      BadSignatureException
    • isCanonical

      public boolean isCanonical()
      Description copied from class: ACell
      Returns true if this Cell is in a canonical representation for message writing. Non-canonical objects may be used on a temporary internal basis, they must always be converted to canonical representations for external use (e.g. Encoding).
      Specified by:
      isCanonical in class ACell
      Returns:
      true if the object is in canonical format, false otherwise
    • isCVMValue

      public final boolean isCVMValue()
      Description copied from class: ACell
      Returns true if this Cell represents a first class CVM Value. Sub-structural cells that are not themselves first class values should return false, pretty much everything else should return true. Note: CVM values might not be in a canonical format, e.g. temporary data structures
      Specified by:
      isCVMValue in class ACell
      Returns:
      true if the object is a CVM Value, false otherwise
    • getRefCount

      public int getRefCount()
      Description copied from class: ACell
      Gets the number of Refs contained within this Cell. This number is final / immutable for any given instance and is defined by the Cell encoding rules. Contained Refs may be either external or embedded.
      Specified by:
      getRefCount in class ACell
      Returns:
      The number of Refs in this Cell
    • getRef

      public <R extends ACell> Ref<R> getRef(int i)
      Description copied from class: ACell
      Gets a numbered child Ref from within this Cell.
      Overrides:
      getRef in class ACell
      Type Parameters:
      R - Type of referenced Cell
      Parameters:
      i - Index of ref to get
      Returns:
      The Ref at the specified index
    • updateRefs

      public SignedData<T> updateRefs(IRefFunction func)
      Description copied from class: ACell
      Updates all Refs in this object using the given function. The function *must not* change the hash value of Refs, in order to ensure structural integrity of modified data structures. This is a building block for a very sneaky trick that enables use to do a lot of efficient operations on large trees of smart references. Must return the same object if no Refs are altered.
      Overrides:
      updateRefs in class ACell
      Parameters:
      func - Ref update function
      Returns:
      Cell with updated Refs
    • print

      public boolean print(BlobBuilder bb, long limit)
      Description copied from class: AObject
      Prints this Object to a readable String Representation. SECURITY: Must halt and return false in O(1) time if limit of printing is exceeded otherwise DoS attacks may be possible.
      Specified by:
      print in class AObject
      Parameters:
      bb - BlobBuilder to append to
      limit - Limit of printing in string bytes
      Returns:
      True if fully printed within limit, false otherwise
    • validate

      public void validate() throws InvalidDataException
      Description copied from interface: IValidated
      Validates the complete structure of this object. It is necessary to ensure all child Refs are validated, so the general contract for validate is:
      1. Call super.validate() - which will indirectly call validateCell()
      2. Call validate() on any contained cells in this class
      Specified by:
      validate in interface IValidated
      Overrides:
      validate in class ACell
      Throws:
      InvalidDataException - If the data Value is invalid in any way
    • validateCell

      public void validateCell() throws InvalidDataException
      Description copied from class: ACell
      Validates the local structure and invariants of this cell. Called by validate() super implementation. Should validate directly contained data, but should not validate all other structure of this cell. In particular, should not traverse potentially missing child Refs.
      Specified by:
      validateCell in class ACell
      Throws:
      InvalidDataException - If the Cell is invalid
    • getDataRef

      public Ref<T> getDataRef()
    • isEmbedded

      public boolean isEmbedded()
      SignedData is not embedded. We want to persist in store always to cache verification status
      Overrides:
      isEmbedded in class ACell
      Returns:
      Always false
    • toString

      public String toString()
      Description copied from class: ACell
      Returns the Java String representation of this Cell. The String representation is intended to be a easy-to-read textual representation of the Cell's data content.
      Overrides:
      toString in class ACell
    • getTag

      public byte getTag()
      Description copied from class: ACell
      Gets the tag byte for this cell. The tag byte is always written as the first byte of the Cell's Encoding
      Specified by:
      getTag in class ACell
      Returns:
      Tag byte for this Cell
    • toCanonical

      public ACell toCanonical()
      Description copied from class: ACell
      Converts this Cell to its canonical version. Returns this Cell if already canonical, may be O(n) in size of value otherwise.
      Specified by:
      toCanonical in class ACell
      Returns:
      Canonical version of Cell