Python Bindings for Schnorrkel (sr25519)

0.2.3 · active · verified Wed Apr 15

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

Install

Imports

Quickstart

This quickstart demonstrates how to generate a key pair from a BIP-39 mnemonic phrase (requiring the optional `bip39` library), sign a message, and verify the signature using the `sr25519` module. It illustrates the core functionality for cryptographic operations.

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')

view raw JSON →