Python Bindings for Schnorrkel (sr25519)
py-sr25519-bindings provides Python bindings for the `schnorrkel` RUST cryptographic crate, which implements the sr25519 signature scheme commonly used in blockchain ecosystems like Polkadot. It enables Python applications to leverage the efficient and secure cryptographic primitives from the underlying Rust library. The current version is 0.2.3, with releases occurring as needed for new Python version compatibility and feature additions.
Warnings
- gotcha The quickstart examples and many practical use cases rely on the `bip39` Python library for generating sr25519 key pairs from mnemonic phrases. This library is not a direct dependency of `py-sr25519-bindings` and must be installed separately.
- gotcha Explicit compatibility with newer Python versions (e.g., 3.10, 3.12, 3.13, 3.14) is frequently added in specific releases. Users on newer or less common Python versions should verify the latest `py-sr25519-bindings` release supports their interpreter version to avoid compatibility issues.
Install
-
pip install py-sr25519-bindings
Imports
- sr25519
import sr25519
Quickstart
import sr25519
import bip39 # This library is a suggested optional dependency for key generation.
message = b"test_message"
# Get private and public key from a BIP-39 seed
# Note: bip39 is an external dependency not automatically installed with py-sr25519-bindings
seed_phrase = 'daughter song common combine misery cotton audit morning stuff weasel flee field'
seed = bip39.bip39_to_mini_secret(seed_phrase, '')
public_key, private_key = sr25519.pair_from_seed(bytes(seed))
print(f"Public Key: {public_key.hex()}")
print(f"Private Key (seed): {private_key.hex()}")
# Generate signature
signature = sr25519.sign((public_key, private_key), message)
print(f"Signature: {signature.hex()}")
# Verify message with signature
is_verified = sr25519.verify(signature, message, public_key)
if is_verified:
print('Signature Verified: True')
else:
print('Signature Verified: False')