Swift wrapper for shnorrkel C library.
Sr25519.swift deploys to macOS, iOS, tvOS, watchOS and Linux. It has been tested on the latest OS releases only however, as the module uses very few platform-provided APIs, there should be very few issues with earlier versions.
Setup instructions:
-
Swift Package Manager: Add this to the dependency section of your
Package.swift
manifest:.package(url: "https://github.com/tesseract-one/Sr25519.swift.git", from: "0.2.0")
-
CocoaPods: Put this in your
Podfile
:pod 'Sr25519', '~> 0.2'
-
CocoaPods with Ed25519:
If you want to build Ed25519 part from sources add this in your
Podfile
:pod 'Sr25519/Sr25519', '~> 0.2' pod 'Sr25519/Ed25519', '~> 0.2'
Following are some examples to demonstrate usage of the library.
import Sr25519
// Creating new KeyPair from random seed
let keypair = Sr25519KeyPair(seed: Sr25519Seed())
// Our message Data
let message = "Hello, World!".data(using: .utf8)!
// Signing
let signature = keypair.sign(message: message)
print("Signature:" signature)
// Validating signature
let valid = keypair.validate(message: message, signature: signature)
print("Is valid:", valid)
import Sr25519
// Creating PublicKey from Data
let pkey = try! Sr25519PublicKey(data: Data(repeating: 0, count: Sr25519PublicKey.size))
// Our message Data
let message = "Hello, World!".data(using: .utf8)!
// Signature
let signature = try! Sr25519Signature(data: Data(repeating: 0, count: Sr25519Signature.size))
// Validating
let valid = pkey.verify(message: message, signature: signature)
print("Is valid:", valid)
import Sr25519
// Creating new KeyPair from random seed
let keypair = Sr25519KeyPair(seed: Sr25519Seed())
// It's PublicKey
let pkey = keypair.publicKey
// Creating ChainCode for derivation from Data
let chaincode = try! Sr25519ChainCode(code: Data(repeating: 0, count: Sr25519ChainCode.size))
// Derive
let derived = keypair.derive(chainCode: chaincode, hard: true)
print("Hard derived PublicKey", derived.publicKey)
// Also soft derivation can be performed on PrivateKey directly
let pderived = pkey.derive(chainCode: chaincode)
print("Soft derived PublicKey", pderived)
import Sr25519
// Creating new KeyPair from random seed
let keypair = Sr25519KeyPair(seed: Sr25519Seed())
// Our message Data
let message = "Hello, World!".data(using: .utf8)!
// Default 0xFF filled threshold
let limit = Sr25519VrfThreshold()
// Signing
let (signature, isLess) = try! keypair.vrfSign(message: message, ifLessThan: limit)
print("Signature:", signature, "is less:", isLess)
// Verification
let valid = keypair.vrfVerify(message: message, signature: signature, threshold: limit)
print("Is valid:", valid)
Sr25519.swift can be used, distributed and modified under the Apache 2.0 license.