sr25519 / schnorrkel

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

let privateKey: Data
let publicKey = try privateKey.sr25519.publicKey()

let message: Data
let signature = try privateKey.sr25519.sign(message: message)

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

let message: Data
let publicKey: Data
let signature: Data

let isValid = try publicKey.sr25519.verify(message: message, signature: signature)

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

let keyPair = try KeyPairFactory.sr25519.generate()

The same method has two optional parameters: words count and passphrase. Word count is 12 by default.

let keyPair = try KeyPairFactory.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:

let keyPair = try KeyPairFactory.sr25519.generate(
    phrase: "your mnemonic seed phrase words",
    passphrase: "your passphrase" // optional parameter, "" by default

let keyPair = try KeyPairFactory.sr25519.generate(
    words: ["your", "mnemonic", "seed", "phrase", "words"],
    passphrase: "your passphrase" // optional parameter, "" by default

However, there is a Mnemonic protocol available:

public protocol Mnemonic {
    var words: [String] { get }
    var entropy: Data { get }
    func toSeed(passphrase: String) throws -> Data

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

let customMnemonic: Mnemonic
let keyPair = try KeyPairFactory.sr25519.generate(
    from: 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:

let keyPair: KeyPair

let privateKey = keyPair.privateKey
let publicKey = keyPair.publicKey

let message: Data
let signature = try keyPair.sign(message: message)
let isValid = try keyPair.verify(message: message, signature: signature)

Last updated