This part of our library wraps our own sr25519 implementation.
All extension entry points are functions which a label parameter, which is defaulted to "substrate" for use in a Substrate ecosystem:
val privateKey: ByteArray
val publicKeyDefault: ByteArray = privateKey.sr25519()
val publicKeySubstrate: ByteArray = privateKey.sr25519(label = "substrate")
// publicKeyDefault == publicKeySubstrate
But you can use any other label for your own purpose.
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.sr25519().publicKey()
val message: ByteArray
val signature: ByteArray = privateKey.sr25519().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.sr25519().verify(message, signature)
If you want to generate the key pair, there is also a extension available:
val keyPair: KeyPair = KeyPair.Factory.sr25519().generate()
The same method has two optional parameters: words count and passphrase.
Word count is 12 by default.
val keyPair: KeyPair = KeyPair.Factory.sr25519()
.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.sr25519
.generate(
phrase = "your mnemonic seed phrase words",
passphrase = "your passphrase" // optional parameter, "" by default
)
val keyPair: KeyPair = KeyPair.Factory.sr25519()
.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.sr25519()
.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