Class KeyPair

java.lang.Object
org.stellar.sdk.KeyPair

public class KeyPair extends Object
Holds a Stellar keypair.
  • Method Details

    • canSign

      public boolean canSign()
      Returns true if this Keypair is capable of signing.
      Returns:
      true if this keypair has a private key
    • fromSecretSeed

      public static KeyPair fromSecretSeed(char[] seed)
      Creates a new Stellar KeyPair from a strkey encoded Stellar secret seed.
      Parameters:
      seed - Char array containing strkey encoded Stellar secret seed.
      Returns:
      KeyPair
      Throws:
      IllegalArgumentException - if the provided seed is invalid
    • fromSecretSeed

      public static KeyPair fromSecretSeed(String seed)
      Insecure Creates a new Stellar KeyPair from a strkey encoded Stellar secret seed. This method is insecure. Use only if you are aware of security implications.
      Parameters:
      seed - The strkey encoded Stellar secret seed.
      Returns:
      KeyPair
      Throws:
      IllegalArgumentException - if the provided seed is invalid
      See Also:
    • fromSecretSeed

      public static KeyPair fromSecretSeed(byte[] seed)
      Creates a new Stellar keypair from a raw 32 byte secret seed.
      Parameters:
      seed - The 32 byte secret seed.
      Returns:
      KeyPair
      Throws:
      IllegalArgumentException - if the provided seed is invalid
    • fromAccountId

      public static KeyPair fromAccountId(String accountId)
      Creates a new Stellar KeyPair from a strkey encoded Stellar account ID.
      Parameters:
      accountId - The strkey encoded Stellar account ID.
      Returns:
      KeyPair
      Throws:
      IllegalArgumentException - if the provided account ID is invalid
    • fromPublicKey

      public static KeyPair fromPublicKey(byte[] publicKey)
      Creates a new Stellar keypair from a 32 byte address.

      Note: This method accepts any 32-byte array as a public key, even if it is not a valid Ed25519 public key point (e.g., all zeros). Such keypairs can still be used for address representation but will throw an exception when attempting to verify signatures.

      Parameters:
      publicKey - The 32 byte public key.
      Returns:
      KeyPair
      Throws:
      IllegalArgumentException - if the provided public key is not 32 bytes
    • fromBip39Seed

      public static KeyPair fromBip39Seed(byte[] bip39Seed, int accountNumber)
      Finds the KeyPair for the path m/44'/148'/accountNumber' using the method described in SEP-0005.

      You can generate a BIP39 seed using a library like mnemonic4j.

      Parameters:
      bip39Seed - The output of BIP0039
      accountNumber - The number of the account
      Returns:
      KeyPair with secret
      Throws:
      IllegalArgumentException - if the provided bip39Seed is invalid
    • random

      public static KeyPair random()
      Generates a random Stellar keypair.
      Returns:
      a random Stellar keypair.
    • getAccountId

      public String getAccountId()
      Returns the human-readable account ID encoded in strkey.
      Returns:
      the account ID starting with G
    • getSecretSeed

      public char[] getSecretSeed()
      Returns the human-readable secret seed encoded in strkey.

      WARNING: This method returns the secret seed of the keypair. The secret seed should be handled with care and not be exposed to anyone else. Exposing the secret seed can lead to the theft of the account.

      Returns:
      char[] The secret seed of the keypair. If the keypair was created without a secret seed, this method will return null.
    • getPublicKey

      public byte[] getPublicKey()
      Returns the raw 32 byte public key.
      Returns:
      a copy of the 32-byte public key
    • getSignatureHint

      public SignatureHint getSignatureHint()
      Returns the signature hint for this keypair.
      Returns:
      the last 4 bytes of the XDR-encoded public key as a SignatureHint
    • getXdrPublicKey

      public PublicKey getXdrPublicKey()
      Returns the XDR PublicKey for this keypair.
      Returns:
      the XDR public key
    • getXdrAccountId

      public AccountID getXdrAccountId()
      Returns the XDR AccountID for this keypair.
      Returns:
      the XDR account ID
    • fromXdrPublicKey

      public static KeyPair fromXdrPublicKey(PublicKey key)
      Creates a new KeyPair from an XDR PublicKey.
      Parameters:
      key - The XDR PublicKey object.
      Returns:
      KeyPair
    • sign

      public byte[] sign(byte[] data)
      Sign the provided data with the keypair's private key.
      Parameters:
      data - The data to sign.
      Returns:
      signed bytes, null if the private key for this keypair is null.
      Throws:
      IllegalStateException - if the private key for this keypair is null.
    • signDecorated

      public DecoratedSignature signDecorated(byte[] data)
      Sign the provided data with the keypair's private key and returns DecoratedSignature.
      Parameters:
      data - the data to sign
      Returns:
      DecoratedSignature
    • signPayloadDecorated

      public DecoratedSignature signPayloadDecorated(byte[] signerPayload)
      Sign the provided payload data for payload signer where the input is the data being signed. Per the CAP-40 Signature spec DecoratedSignature.
      Parameters:
      signerPayload - the payload signers raw data to sign
      Returns:
      DecoratedSignature
    • verify

      public boolean verify(byte[] data, byte[] signature)
      Verify the provided data and signature match this keypair's public key.
      Parameters:
      data - The data that was signed.
      signature - The signature.
      Returns:
      True if they match, false otherwise.
      Throws:
      IllegalStateException - if the public key is not a valid Ed25519 public key (e.g., all zeros)
    • equals

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

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

      public byte[] signMessage(String message)
      Sign a message according to SEP-53.
      Parameters:
      message - The message to sign.
      Returns:
      The signature bytes.
    • signMessage

      public byte[] signMessage(byte[] message)
      Sign a message according to SEP-53.
      Parameters:
      message - The message to sign.
      Returns:
      The signature bytes.
    • verifyMessage

      public boolean verifyMessage(byte[] message, byte[] signature)
      Verify a SEP-53 signed message.
      Parameters:
      message - The original message.
      signature - The signature to verify.
      Returns:
      True if the signature is valid for the given message, false otherwise.
    • verifyMessage

      public boolean verifyMessage(String message, byte[] signature)
      Verify a SEP-53 signed message.
      Parameters:
      message - The original message.
      signature - The signature to verify.
      Returns:
      True if the signature is valid for the given message, false otherwise.