Skip to Content
DocumentationPythonAPI Reference

API Reference

Everything is imported from the edexa_signing module:

import edexa_signing as sdk

Conventions

  • Hashes, signatures and keys are passed and returned as hex strings. Inputs are accepted with or without a 0x prefix.
  • Document contents are passed as bytes. str and bytearray raise TypeError.
  • 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.

ParameterTypeDescription
databytesThe 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.

ParameterTypeDescription
digest_hexstrThe hash that was signed
signature_hexstrThe 65-byte signature (130 hex characters) returned by sign()
expected_addressstrThe 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") # True

Document

A named document held in memory. Its contents are never serialized or sent anywhere by the SDK.

Document(name, data)

ParameterTypeDescription
namestrA label for the document, such as a file name
databytesThe 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.

MemberReturnsDescription
namestrProperty: the document name
data()bytesThe 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.

ParameterTypeDescription
private_key_hexstrA 32-byte secp256k1 private key as 64 hex characters, with or without 0x

Raises ValueError:

  • invalid private key: private key is not valid hex if the key is not valid hex
  • invalid 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

MethodReturnsDescription
address()strThe signer’s address: 0x + 40 lowercase hex characters
private_key()strThe private key: 0x + 64 hex characters
public_key()strThe 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.

ParameterTypeDescription
document_hash_hexstrThe hash to sign: the 64-character SHA-256 hash from hash_document() or Document.hash()["hash"]
algorithmstrName of the hash algorithm, returned unchanged in the result. The SDK hashes with SHA-256, so pass "sha256".
chain_idintEIP-155 chain ID of the target network. Not part of the signature.
contract_addressstreTwin contract address. Not part of the signature.
metadata_jsonstr or NoneOptional. A JSON string returned in the result. An empty string is treated as None. Not part of the signature.

Returns dict:

KeyTypeDescription
signaturestr65-byte signature r ‖ s ‖ v as 130 hex characters, without 0x
document_hashstrThe signed hash, as 64 hex characters without 0x
algorithmstrThe algorithm you passed in
signer_addressstrThe signer’s address
metadatastr or Nonemetadata_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

  • ValueError if document_hash_hex is not valid hex, or metadata_json is not valid JSON (serialization error: …)
  • RuntimeError (signing failed: …) if the hash cannot be signed, for example because it is shorter than 16 bytes
  • TypeError if 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)

ParameterTypeDescription
rpc_urlstrJSON-RPC endpoint. Not validated or contacted until sign_document() is called.
contract_addressstrThe eTwin contract address
chain_idintEIP-155 chain ID, for example 11155111 for Sepolia

Raises ValueError (configuration error: invalid contract address) if contract_address is not a valid address.

PropertyTypeDescription
rpc_urlstrThe configured RPC endpoint
contract_addressstrThe contract address in EIP-55 checksum form (mixed case)
chain_idintThe 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.

ParameterTypeDescription
private_key_hexstrThe 32-byte private key of the account that sends the transaction and pays for gas, for example signer.private_key()
document_hash_hexstrThe 32-byte hash to record (64 hex characters)

Returns dict:

KeyTypeDescription
transaction_hashstrTransaction hash, 0x-prefixed
block_numberintBlock in which the transaction was mined
gas_usedintGas used by the transaction
statusstr"success" or "failed"

Raises

  • ValueError if an argument is not valid hex
  • RuntimeError (invalid private key: …) if the key is 32 bytes but not a valid secp256k1 key
  • RuntimeError (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 gas
  • RuntimeError (document error: document hash must be 32 bytes (bytes32)) if the hash is not 32 bytes
  • pyo3_runtime.PanicException if the private key is not exactly 32 bytes. This exception does not inherit from Exception, so except Exception won’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:

ExceptionMessage prefixRaised when
ValueErrorinvalid 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.
ValueErrordocument error:Document is created with empty data
ValueErrorserialization error:metadata_json is not valid JSON
ValueErrorsigning failed:verify_signature() / verify() gets a signature that is not 65 bytes or has an invalid v byte
ValueErrorconfiguration error:EdexaChain gets an invalid contract address
RuntimeErrorsigning failed:sign() cannot sign the hash
RuntimeErrorinvalid private key:sign_document() gets a 32-byte key that is not a valid secp256k1 key
RuntimeErrorblockchain RPC error:sign_document() fails to connect, send the transaction or fetch the receipt
RuntimeErrordocument error:sign_document() gets a hash that is not 32 bytes
TypeErrorAn argument has the wrong type (for example str instead of bytes), or a required argument is missing
pyo3_runtime.PanicExceptionsign_document() gets a private key that is not exactly 32 bytes