ecdsa

Our implementation comes with two kinds of ecdsa algorithms:

enum class Kind {
    SUBSTRATE,
    ETHEREUM
}

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.ecdsa(kind = Kind.Substrate).publicKey()

val message: ByteArray
val signature: ByteArray = privateKey.ecdsa(kind = Kind.Substrate).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.ecdsa(kind = Kind.Substrate).verify(message, signature)

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

val keyPair: KeyPair = KeyPair.Factory.ecdsa(kind = Kind.Substrate).generate()

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

Word count is 12 by default.

val keyPair: KeyPair = KeyPair.Factory.ecdsa(kind = Kind.Substrate)
    .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.ecdsa(kind = Kind.Substrate)
    .generate(
        phrase = "your mnemonic seed phrase words", 
        passphrase = "your passphrase" // optional parameter, "" by default
    )
    
val keyPair: KeyPair = KeyPair.Factory.ecdsa(kind = Kind.Substrate)
    .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.ecdsa(kind = Kind.Substrate)
    .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