API Reference
Everything is imported from the edexa_signing module:
import edexa_signing as sdkConventions
- Hashes, signatures and keys are passed and returned as hex strings. Inputs are accepted with or without a
0xprefix. - Document contents are passed as
bytes.strandbytearrayraiseTypeError. - Addresses are returned in lowercase.
verify_signature()compares addresses case-insensitively.
The examples on this page use the key 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 so that their outputs are reproducible. Never use this key for real signing.
Functions
hash_document(data)
Computes the SHA-256 hash of data.
| Parameter | Type | Description |
|---|---|---|
data | bytes | The document contents. Empty bytes are allowed. |
Returns str: the 32-byte hash as 64 lowercase hex characters, without a 0x prefix.
Raises TypeError if data is not bytes.
sdk.hash_document(b"hello world")
# 'b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9'verify_signature(digest_hex, signature_hex, expected_address)
Recovers the address that signed digest_hex and compares it with expected_address. Works offline.
| Parameter | Type | Description |
|---|---|---|
digest_hex | str | The hash that was signed |
signature_hex | str | The 65-byte signature (130 hex characters) returned by sign() |
expected_address | str | The address you expect to have signed. Case-insensitive. |
Returns bool: True if the signature was made by expected_address over exactly this hash. False if it was made by a different key or over a different hash.
Raises ValueError if an argument is not valid hex, the signature is not 65 bytes, or its last byte (v) is not 27 or 28.
sdk.verify_signature(digest, signed["signature"], "0x6370ef2f4db3611d657b90667de398a2cc2a370c")
# TrueDocument
A named document held in memory. Its contents are never serialized or sent anywhere by the SDK.
Document(name, data)
| Parameter | Type | Description |
|---|---|---|
name | str | A label for the document, such as a file name |
data | bytes | The document contents. Must not be empty. |
Raises ValueError (document error: document data is empty) if data is empty, and TypeError if data is not bytes.
| Member | Returns | Description |
|---|---|---|
name | str | Property: the document name |
data() | bytes | The document contents |
hash() | dict | {"algorithm": "sha256", "hash": "<64 hex characters>"}. hash is the same value hash_document() returns for these bytes. |
doc = sdk.Document("invoice.pdf", b"hello world")
doc.name # 'invoice.pdf'
doc.hash() # {'algorithm': 'sha256', 'hash': 'b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9'}EdexaSigner
A secp256k1 key pair that signs document hashes locally.
EdexaSigner(private_key_hex)
Loads an existing key.
| Parameter | Type | Description |
|---|---|---|
private_key_hex | str | A 32-byte secp256k1 private key as 64 hex characters, with or without 0x |
Raises ValueError:
invalid private key: private key is not valid hexif the key is not valid hexinvalid private key: invalid secp256k1 private key (must be 32 bytes)if the key is the wrong length or not a valid secp256k1 key (for example all zeros)
EdexaSigner.generate()
Static method. Returns a new EdexaSigner with a random key from the operating system’s secure random number generator.
Key and address methods
| Method | Returns | Description |
|---|---|---|
address() | str | The signer’s address: 0x + 40 lowercase hex characters |
private_key() | str | The private key: 0x + 64 hex characters |
public_key() | str | The uncompressed public key without the 04 prefix: 0x + 128 hex characters |
signer = sdk.EdexaSigner("0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20")
signer.address() # '0x6370ef2f4db3611d657b90667de398a2cc2a370c'sign(document_hash_hex, algorithm, chain_id, contract_address, metadata_json=None)
Signs a document hash.
| Parameter | Type | Description |
|---|---|---|
document_hash_hex | str | The hash to sign: the 64-character SHA-256 hash from hash_document() or Document.hash()["hash"] |
algorithm | str | Name of the hash algorithm, returned unchanged in the result. The SDK hashes with SHA-256, so pass "sha256". |
chain_id | int | EIP-155 chain ID of the target network. Not part of the signature. |
contract_address | str | eTwin contract address. Not part of the signature. |
metadata_json | str or None | Optional. A JSON string returned in the result. An empty string is treated as None. Not part of the signature. |
Returns dict:
| Key | Type | Description |
|---|---|---|
signature | str | 65-byte signature r ‖ s ‖ v as 130 hex characters, without 0x |
document_hash | str | The signed hash, as 64 hex characters without 0x |
algorithm | str | The algorithm you passed in |
signer_address | str | The signer’s address |
metadata | str or None | metadata_json re-serialized as compact JSON (for example {"invoice":42}), or None |
Only the hash is signed, and signing is deterministic: the same key and hash always give the same signature. chain_id and contract_address are not returned.
Raises
ValueErrorifdocument_hash_hexis not valid hex, ormetadata_jsonis not valid JSON (serialization error: …)RuntimeError(signing failed: …) if the hash cannot be signed, for example because it is shorter than 16 bytesTypeErrorif a required argument is missing
digest = sdk.hash_document(b"hello world")
signed = signer.sign(
digest,
"sha256",
chain_id=11155111,
contract_address="0x56Bf299f4316483Ee0128b1a8F0473ADD219D475",
metadata_json='{"invoice": 42}',
)
# {
# 'signature': '663224e10787790f0d98db42034f0ec75abdd51d6d48a6e33590d6316ce4076f355f856cd41af1ac5559b26a22b2c38dd0da9bcc4cf623edc0155366a8a4eaba1c',
# 'document_hash': 'b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9',
# 'algorithm': 'sha256',
# 'signer_address': '0x6370ef2f4db3611d657b90667de398a2cc2a370c',
# 'metadata': '{"invoice":42}'
# }verify(document_hash_hex, signature_hex, expected_address)
Identical to verify_signature(): it takes the same arguments, returns the same result and raises the same errors. It does not use the signer’s own key, so it checks expected_address, not the address of the signer you call it on.
EdexaChain
A connection to an EVM chain for recording document hashes on-chain.
EdexaChain(rpc_url, contract_address, chain_id)
| Parameter | Type | Description |
|---|---|---|
rpc_url | str | JSON-RPC endpoint. Not validated or contacted until sign_document() is called. |
contract_address | str | The eTwin contract address |
chain_id | int | EIP-155 chain ID, for example 11155111 for Sepolia |
Raises ValueError (configuration error: invalid contract address) if contract_address is not a valid address.
| Property | Type | Description |
|---|---|---|
rpc_url | str | The configured RPC endpoint |
contract_address | str | The contract address in EIP-55 checksum form (mixed case) |
chain_id | int | The configured chain ID |
See Configuration for the Sepolia values used in the SDK’s examples.
sign_document(private_key_hex, document_hash_hex)
Records a document hash on-chain. It sends a transaction that calls signDocument(bytes32) on the contract with the hash, then waits until the transaction is mined. The call blocks while waiting.
The transaction carries only the 32-byte hash. It does not include the signature from EdexaSigner.sign(). On-chain, the account that sends the transaction is the one calling signDocument.
| Parameter | Type | Description |
|---|---|---|
private_key_hex | str | The 32-byte private key of the account that sends the transaction and pays for gas, for example signer.private_key() |
document_hash_hex | str | The 32-byte hash to record (64 hex characters) |
Returns dict:
| Key | Type | Description |
|---|---|---|
transaction_hash | str | Transaction hash, 0x-prefixed |
block_number | int | Block in which the transaction was mined |
gas_used | int | Gas used by the transaction |
status | str | "success" or "failed" |
Raises
ValueErrorif an argument is not valid hexRuntimeError(invalid private key: …) if the key is 32 bytes but not a valid secp256k1 keyRuntimeError(blockchain RPC error: …) if connecting, sending the transaction or fetching the receipt fails, for example when the RPC endpoint is unreachable or the account cannot pay for gasRuntimeError(document error: document hash must be 32 bytes (bytes32)) if the hash is not 32 bytespyo3_runtime.PanicExceptionif the private key is not exactly 32 bytes. This exception does not inherit fromException, soexcept Exceptionwon’t catch it. Pass a full 32-byte key.
chain = sdk.EdexaChain(
"https://ethereum-sepolia-rpc.publicnode.com",
"0x56Bf299f4316483Ee0128b1a8F0473ADD219D475",
11155111,
)
receipt = chain.sign_document(signer.private_key(), digest)
# {'transaction_hash': '0x...', 'block_number': ..., 'gas_used': ..., 'status': 'success'}Errors
The SDK raises standard Python exceptions. Messages start with a category prefix:
| Exception | Message prefix | Raised when |
|---|---|---|
ValueError | invalid private key: | A key is not valid hex or not a valid 32-byte secp256k1 key. Also used when any hex argument (hash, signature) is malformed: invalid hex characters or invalid hex length. |
ValueError | document error: | Document is created with empty data |
ValueError | serialization error: | metadata_json is not valid JSON |
ValueError | signing failed: | verify_signature() / verify() gets a signature that is not 65 bytes or has an invalid v byte |
ValueError | configuration error: | EdexaChain gets an invalid contract address |
RuntimeError | signing failed: | sign() cannot sign the hash |
RuntimeError | invalid private key: | sign_document() gets a 32-byte key that is not a valid secp256k1 key |
RuntimeError | blockchain RPC error: | sign_document() fails to connect, send the transaction or fetch the receipt |
RuntimeError | document error: | sign_document() gets a hash that is not 32 bytes |
TypeError | An argument has the wrong type (for example str instead of bytes), or a required argument is missing | |
pyo3_runtime.PanicException | sign_document() gets a private key that is not exactly 32 bytes |