Class SignerKey

java.lang.Object
org.stellar.sdk.SignerKey

public final class SignerKey extends Object
Represents a Stellar signer key used for transaction authorization.

This class supports four types of signers as defined in the Stellar protocol:

  • ED25519 - Standard Ed25519 public key signer
  • PRE_AUTH_TX - Pre-authorized transaction hash signer
  • HASH_X - SHA-256 hash preimage signer
  • ED25519_SIGNED_PAYLOAD - Ed25519 signed payload signer (CAP-40)

The Ed25519 Signed Payload Signer (introduced in CAP-40) is particularly useful for multi-party contracts like payment channels, as it allows all parties to share a set of transactions for signing and guarantees that if one transaction is signed and submitted, information is revealed that allows all other transactions in the set to be authorized.

Example usage:


 // Create an Ed25519 signer
 SignerKey ed25519Signer = SignerKey.fromEd25519PublicKey("GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ");

 // Create a signed payload signer
 byte[] payload = "transaction_hash".getBytes();
 Ed25519SignedPayload signedPayload = new Ed25519SignedPayload(publicKeyBytes, payload);
 SignerKey payloadSigner = SignerKey.fromEd25519SignedPayload(signedPayload);
 
See Also:
  • Field Details

    • SIGNED_PAYLOAD_MAX_PAYLOAD_LENGTH

      public static final int SIGNED_PAYLOAD_MAX_PAYLOAD_LENGTH
      Maximum payload length for Ed25519 signed payload signers. As defined in CAP-40, the payload has a maximum size of 64 bytes.
      See Also:
  • Constructor Details

    • SignerKey

      public SignerKey(byte[] key, SignerKeyType type)
      Creates a new SignerKey instance.
      Parameters:
      key - The raw key bytes for this signer. The format depends on the signer type:
      • ED25519: 32-byte public key
      • PRE_AUTH_TX: 32-byte transaction hash
      • HASH_X: 32-byte SHA-256 hash
      • ED25519_SIGNED_PAYLOAD: Variable length encoded payload with public key
      type - The type of this signer key.
  • Method Details

    • getEncodedSignerKey

      public String getEncodedSignerKey()
      Gets the encoded string representation of this signer key.
      Returns:
      The StrKey-encoded representation of this signer key
      Throws:
      IllegalArgumentException - if the signer key type is unknown
    • fromEncodedSignerKey

      public static SignerKey fromEncodedSignerKey(String encodedSignerKey)
      Creates a SignerKey from an encoded signer key string.

      This method automatically detects the signer key type based on the encoded string format and creates the appropriate SignerKey instance.

      Parameters:
      encodedSignerKey - The StrKey-encoded signer key string
      Returns:
      A new SignerKey instance
      Throws:
      IllegalArgumentException - if the encoded signer key is invalid
    • fromEd25519PublicKey

      public static SignerKey fromEd25519PublicKey(byte[] key)
      Creates a SignerKey from an Ed25519 public key.
      Parameters:
      key - The 32-byte Ed25519 public key
      Returns:
      A new SignerKey instance of type ED25519
    • fromEd25519PublicKey

      public static SignerKey fromEd25519PublicKey(String key)
      Creates a SignerKey from an encoded Ed25519 public key string.
      Parameters:
      key - The StrKey-encoded Ed25519 public key (starts with 'G')
      Returns:
      A new SignerKey instance of type ED25519
      Throws:
      IllegalArgumentException - if the key is not a valid Ed25519 public key
    • fromPreAuthTx

      public static SignerKey fromPreAuthTx(byte[] key)
      Creates a SignerKey from a pre-authorized transaction hash.

      Pre-authorized transaction signers allow a transaction to be authorized by including the hash of a specific transaction as a signer. This is useful for creating transactions that can only be executed if a specific other transaction is also executed.

      Parameters:
      key - The 32-byte transaction hash
      Returns:
      A new SignerKey instance of type PRE_AUTH_TX
    • fromPreAuthTx

      public static SignerKey fromPreAuthTx(String key)
      Creates a SignerKey from an encoded pre-authorized transaction hash string.
      Parameters:
      key - The StrKey-encoded pre-authorized transaction hash (starts with 'T')
      Returns:
      A new SignerKey instance of type PRE_AUTH_TX
      Throws:
      IllegalArgumentException - if the key is not a valid pre-authorized transaction hash
    • fromPreAuthTx

      public static SignerKey fromPreAuthTx(Transaction key)
      Creates a SignerKey from a pre-authorized transaction.
      Parameters:
      key - The pre-authorized transaction
      Returns:
      A new SignerKey instance of type PRE_AUTH_TX
    • fromSha256Hash

      public static SignerKey fromSha256Hash(byte[] key)
      Creates a SignerKey from a SHA-256 hash.

      Hash-X signers allow a transaction to be authorized by revealing the preimage of a specific SHA-256 hash. This is useful for creating hashlocks and other cryptographic puzzles.

      Parameters:
      key - The 32-byte SHA-256 hash
      Returns:
      A new SignerKey instance of type HASH_X
    • fromSha256Hash

      public static SignerKey fromSha256Hash(String key)
      Creates a SignerKey from an encoded SHA-256 hash string.
      Parameters:
      key - The StrKey-encoded SHA-256 hash (starts with 'X')
      Returns:
      A new SignerKey instance of type HASH_X
      Throws:
      IllegalArgumentException - if the key is not a valid SHA-256 hash
    • fromEd25519SignedPayload

      public static SignerKey fromEd25519SignedPayload(byte[] key)
      Creates a SignerKey from an Ed25519 signed payload.

      Ed25519 signed payload signers (CAP-40) allow a transaction to be authorized by providing a signature of a specific payload using an Ed25519 key. This is particularly useful for multi-party contracts where signing one transaction reveals information that allows other transactions to be authorized.

      Parameters:
      key - The encoded payload containing the Ed25519 public key and payload
      Returns:
      A new SignerKey instance of type ED25519_SIGNED_PAYLOAD
    • fromEd25519SignedPayload

      public static SignerKey fromEd25519SignedPayload(String ed25519PublicKey, byte[] payload)
      Creates a SignerKey from an Ed25519 public key and payload.

      This method encodes the Ed25519 public key and payload into the binary format required by the Stellar protocol, including proper padding for the payload.

      Parameters:
      ed25519PublicKey - The StrKey-encoded Ed25519 public key (starts with 'G')
      payload - The payload to be signed (up to 64 bytes)
      Returns:
      A new SignerKey instance of type ED25519_SIGNED_PAYLOAD
      Throws:
      IllegalArgumentException - if the payload length exceeds the maximum allowed
    • fromEd25519SignedPayload

      public static SignerKey fromEd25519SignedPayload(SignerKey.Ed25519SignedPayload ed25519SignedPayload)
      Creates a SignerKey from an Ed25519SignedPayload object.

      This method encodes the Ed25519 public key and payload into the binary format required by the Stellar protocol, including proper padding for the payload.

      Parameters:
      ed25519SignedPayload - The Ed25519SignedPayload containing the public key and payload
      Returns:
      A new SignerKey instance of type ED25519_SIGNED_PAYLOAD
      Throws:
      UnexpectedException - if an I/O error occurs during encoding
    • fromEd25519SignedPayload

      public static SignerKey fromEd25519SignedPayload(String key)
      Creates a SignerKey from an encoded Ed25519 signed payload string.
      Parameters:
      key - The StrKey-encoded Ed25519 signed payload (starts with 'P')
      Returns:
      A new SignerKey instance of type ED25519_SIGNED_PAYLOAD
      Throws:
      IllegalArgumentException - if the key is not a valid Ed25519 signed payload
    • toEd25519SignedPayload

      public SignerKey.Ed25519SignedPayload toEd25519SignedPayload()
      Converts this SignerKey to an Ed25519SignedPayload object.

      This method extracts the Ed25519 public key and payload from the encoded binary format used by Ed25519 signed payload signers.

      Returns:
      An Ed25519SignedPayload object containing the public key and payload
      Throws:
      IllegalArgumentException - if this SignerKey is not of type ED25519_SIGNED_PAYLOAD
    • toXdr

      public SignerKey toXdr()
      Converts this SignerKey to its XDR representation.
      Returns:
      The XDR representation of this signer key
      Throws:
      IllegalArgumentException - if the signer key type is unknown
    • fromXdr

      public static SignerKey fromXdr(SignerKey signerKey)
      Creates a SignerKey from its XDR representation.
      Parameters:
      signerKey - The XDR representation of a signer key
      Returns:
      A new SignerKey instance
      Throws:
      IllegalArgumentException - if the XDR signer key type is unknown
    • getKey

      public byte[] getKey()
      The raw key bytes for this signer. The format depends on the signer type:
      • ED25519: 32-byte public key
      • PRE_AUTH_TX: 32-byte transaction hash
      • HASH_X: 32-byte SHA-256 hash
      • ED25519_SIGNED_PAYLOAD: Variable length encoded payload with public key
      Returns:
      the raw key bytes
    • getType

      public SignerKeyType getType()
      The type of this signer key.
      Returns:
      the signer key type
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object