A Python implementation of Sign in with Ethereum (EIP-4361).
Project description
Sign in with Ethereum
This package provides a Python implementation of EIP-4361: Sign in With Ethereum.
Installation
SIWE can be easily installed in any Python project with pip:
pip install signinwithethereum
The distribution is published as signinwithethereum, but the import name remains siwe:
from siwe import SiweMessage
Usage
SIWE provides a SiweMessage class which implements EIP-4361.
Parsing a SIWE Message
Parsing is done by initializing a SiweMessage object with an EIP-4361 formatted string:
from siwe import SiweMessage
message = SiweMessage.from_message(message=eip_4361_string)
Or to initialize a SiweMessage as a pydantic.BaseModel right away:
message = SiweMessage(domain="login.xyz", address="0x1234...", ...)
Verifying and Authenticating a SIWE Message
Verification and authentication is performed via EIP-191, using the address field of the SiweMessage as the expected signer. The validate method checks message structural integrity, signature address validity, and time-based validity attributes.
try:
message.verify(signature="0x...")
# You can also specify other checks (e.g. the nonce or domain expected).
except siwe.VerificationError:
# Invalid
Serialization of a SIWE Message
SiweMessage instances can also be serialized as their EIP-4361 string representations via the prepare_message method:
print(message.prepare_message())
Example
Parsing and verifying a SiweMessage is easy:
try:
message = SiweMessage.from_message(eip_4361_string)
message.verify(signature, nonce="abcdef", domain="example.com")
except ValueError:
# Invalid message format
print("Authentication attempt rejected.")
except siwe.ExpiredMessage:
print("Authentication attempt rejected.")
except siwe.DomainMismatch:
print("Authentication attempt rejected.")
except siwe.NonceMismatch:
print("Authentication attempt rejected.")
except siwe.InvalidSignature:
print("Authentication attempt rejected.")
# Message has been verified. Authentication complete. Continue with authorization/other.
Testing
git submodule update --init
uv sync
uv run pytest
See Also
- Sign in with Ethereum: TypeScript
- SIWE website: siwe.xyz
- EIP-4361 Specification Draft
- EIP-191 Specification
Disclaimer
Our Python library for Sign in with Ethereum has not yet undergone a formal security audit. We welcome continued feedback on the usability, architecture, and security of this implementation.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file signinwithethereum-5.0.0.tar.gz.
File metadata
- Download URL: signinwithethereum-5.0.0.tar.gz
- Upload date:
- Size: 178.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1401a450ebf3d98d36d1bbe30738ebc84ca768ac4628478f5d27f755e595024c
|
|
| MD5 |
a382d5b5214bbf16afdb01a900bdba8a
|
|
| BLAKE2b-256 |
46dd451afab4a2712e3d7f9b3caa3512c5b201e8ea92a8bf392f2b15826842f2
|
File details
Details for the file signinwithethereum-5.0.0-py3-none-any.whl.
File metadata
- Download URL: signinwithethereum-5.0.0-py3-none-any.whl
- Upload date:
- Size: 18.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3d712b953eaf36eeb3666b9e3859610577ee933acf4370737db44be26432a4e3
|
|
| MD5 |
34b132fb584a6b792e7b50a04283984e
|
|
| BLAKE2b-256 |
5eb91c03297eff6aa26275d496be3fcb8a529d452064e470334a902b3a52be25
|