Swift wrapper for C bindings of shnorrkel Rust library.
Sr25519 deploys to macOS 10.12, iOS 11 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.
Sr25519 uses only random number generator plafrom specific API, so it should be easy to port it to other operating systems.
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.1.0")
CocoaPods: Put this in your Podfile
:
pod 'Sr25519', '~> 0.1'
CocoaPods with Rust sources build:
If you want to build Rust part from sources add this in your Podfile
:
pod 'Sr25519/Build', '~> 0.1'
And install Rust targets:
rustup target add aarch64-apple-ios x86_64-apple-ios aarch64-apple-darwin x86_64-apple-darwin
Linux:
Linux supported through SPM
. For build you have to build Rust library manually. You can build it using scripts/build_libray_linux.sh
script from this repository.
./scripts/build_libray_linux.sh "SOME_INSTALL_PATH"
And provide path to it as parameters to SPM for build.
swift build -Xlinker -L"SOME_INSTALL_PATH/lib" -Xcc -I"SOME_INSTALL_PATH/include"
Following are some examples to demonstrate usage of the library.
import Sr25519
// Creating new KeyPair from random seed
let keypair = KeyPair(seed: Seed())
// 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! PublicKey(data: Data(repeating: 0, count: PublicKey.size))
// Our message Data
let message = "Hello, World!".data(using: .utf8)!
// Signature
let signature = try! Signature(data: Data(repeating: 0, count: Signature.size))
// Validating
let valid = pkey.verify(message: message, signature: signature)
print("Is valid:", valid)
import Sr25519
// Creating new KeyPair from random seed
let keypair = KeyPair(seed: Seed())
// It's PublicKey
let pkey = keypair.publicKey
// Creating ChainCode for derivation from Data
let chaincode = try! ChainCode(code: Data(repeating: 0, count: ChainCode.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 = KeyPair(seed: Seed())
// Our message Data
let message = "Hello, World!".data(using: .utf8)!
// Default 0xFF filled threshold
let limit = VrfThreshold()
// 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.
link |
Stars: 0 |
Last commit: 2 weeks ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco