ed25519

If you know a private key byte array, you can directly use this via extension to sign messages or convert it to public array:

val privateKey: ByteArray
val publicKey: ByteArray = privateKey.ed25519.publicKey()

val message: ByteArray
val signature: ByteArray = privateKey.ed25519.sign(message)

And same, if you know a public key, you can verify messages signatures:

val publicKey: ByteArray
val message: ByteArray
val signature: ByteArray

val verified: Boolean = publicKey.ed25519.verify(message, signature)

If you want to generate the key pair, there is also a extension available:

val keyPair: KeyPair = KeyPair.Factory.ed25519.generate()

The same method has two optional parameters: words count and passphrase.

Word count is 12 by default.

val keyPair: KeyPair = KeyPair.Factory.ed25519
    .generate(wordCount = 12, passphrase = "your passphrase")

Also, if you managed to generate your own mnemonic phrase, there are two different methods for your convenience. Both have optional passphrase:

val keyPair: KeyPair = KeyPair.Factory.ed25519
    .generate(
        phrase = "your mnemonic seed phrase words", 
        passphrase = "your passphrase" // optional parameter, "" by default
    )
    
val keyPair: KeyPair = KeyPair.Factory.ed25519
    .generate(
        words = listOf("your", "mnemonic", "seed", "phrase", "words"), 
        passphrase = "your passphrase" // optional parameter, "" by default
    )

However, there is a Mnemonic interface available:

interface Mnemonic {
    val wordCount: Int
    val words: List<String>
    val entropy: ByteArray
    fun toSeed(passphrase: String = ""): ByteArray
}

You can implement this interface above, and use this to generate a key pair using next method:

val customMnemonic: Mnemonic
val keyPair: KeyPair = KeyPair.Factory.ed25519
    .generate(
        mnemonic: customMnemonic, 
        passphrase = "your passphrase" // optional parameter, "" by default
    )

In the end, you can operate with a key pair to either sign or verify messages, and retreive private and public key from it:

val keyPair: KeyPair

val privateKey: ByteArray = keyPair.privateKey
val publicKey: ByteArray = keyPair.publicKey

val message: ByteArray
val signature: ByteArray = keyPair.sign(message)
val verified: Boolean = keyPair.verify(message, signature)
// verified == true

Last updated