Sign Your First Document
In this guide you sign a file on disk, save a signature record next to it, and later verify that neither the file nor the record has been tampered with. Everything except the optional on-chain step runs offline.
You need the SDK installed. See Getting Started.
Create or load a signing key
Generate a key once and store the private key securely, for example in a secrets manager or an environment variable that is not committed to source control.
import edexa_signing as sdk
signer = sdk.EdexaSigner.generate()
print("address: ", signer.address())
print("private key:", signer.private_key()) # store this securelyOn later runs, load the same key so your signatures always come from the same address:
import os
import edexa_signing as sdk
signer = sdk.EdexaSigner(os.environ["EDEXA_PRIVATE_KEY"])EDEXA_PRIVATE_KEY is just an example name. The SDK does not read any environment variable itself.
Hash the file
Read the file’s bytes and wrap them in a Document. The bytes stay in memory; only the hash is used from here on.
from pathlib import Path
path = Path("contract.pdf")
doc = sdk.Document(path.name, path.read_bytes())
digest = doc.hash()["hash"]
print("sha256:", digest)Sign the hash
Pass the hash, the algorithm name, your target network and, optionally, metadata as a JSON string.
import json
CHAIN_ID = 11155111 # Sepolia
CONTRACT_ADDRESS = "0x56Bf299f4316483Ee0128b1a8F0473ADD219D475"
signed = signer.sign(
digest,
doc.hash()["algorithm"],
chain_id=CHAIN_ID,
contract_address=CONTRACT_ADDRESS,
metadata_json=json.dumps({"file": doc.name}),
)Only the hash is signed. The metadata is returned with the result but is not protected by the signature.
Save a signature record
The result is plain data. Store it alongside the file, or in your database.
record = {
"file": doc.name,
"document_hash": signed["document_hash"],
"algorithm": signed["algorithm"],
"signature": signed["signature"],
"signer_address": signed["signer_address"],
"chain_id": CHAIN_ID,
"contract_address": CONTRACT_ADDRESS,
}
Path("contract.pdf.sig.json").write_text(json.dumps(record, indent=2))chain_id and contract_address are not included in the result of sign(), so the record adds them.
Verify the file later
Verification needs only the file, the record and the address you trust. It works offline and doesn’t need the private key.
import json
from pathlib import Path
import edexa_signing as sdk
TRUSTED_SIGNER = "0x..." # the address you expect to have signed
record = json.loads(Path("contract.pdf.sig.json").read_text())
current_hash = sdk.hash_document(Path("contract.pdf").read_bytes())
file_unchanged = current_hash == record["document_hash"]
signature_valid = sdk.verify_signature(
record["document_hash"], record["signature"], TRUSTED_SIGNER
)
print("file unchanged: ", file_unchanged)
print("signature valid:", signature_valid)- If the file was modified, its hash changes and
file_unchangedisFalse. - If the record’s hash or signature was altered, or a different key signed it,
signature_validisFalse. A signature that is no longer well-formed (wrong length or invalid hex) raisesValueErrorinstead, so treat that as a failed verification too.
Record the hash on-chain (optional)
sign_document() writes the hash to the blockchain through the eTwin contract’s signDocument(bytes32) function. Only the hash is sent, not the signature from the previous steps. The account whose key you pass sends the transaction and pays for gas, so fund its address with test ETH on Sepolia first.
chain = sdk.EdexaChain(
"https://ethereum-sepolia-rpc.publicnode.com",
CONTRACT_ADDRESS,
CHAIN_ID,
)
receipt = chain.sign_document(signer.private_key(), digest)
print(receipt["status"], receipt["transaction_hash"])sign_document() waits until the transaction is mined, then returns the transaction hash, block number, gas used and status ("success" or "failed").
Complete script
import json
import os
from pathlib import Path
import edexa_signing as sdk
CHAIN_ID = 11155111 # Sepolia
CONTRACT_ADDRESS = "0x56Bf299f4316483Ee0128b1a8F0473ADD219D475"
signer = sdk.EdexaSigner(os.environ["EDEXA_PRIVATE_KEY"])
# Hash and sign
path = Path("contract.pdf")
doc = sdk.Document(path.name, path.read_bytes())
digest = doc.hash()["hash"]
signed = signer.sign(
digest,
doc.hash()["algorithm"],
chain_id=CHAIN_ID,
contract_address=CONTRACT_ADDRESS,
metadata_json=json.dumps({"file": doc.name}),
)
# Save the record
record = {
"file": doc.name,
"document_hash": signed["document_hash"],
"algorithm": signed["algorithm"],
"signature": signed["signature"],
"signer_address": signed["signer_address"],
"chain_id": CHAIN_ID,
"contract_address": CONTRACT_ADDRESS,
}
Path("contract.pdf.sig.json").write_text(json.dumps(record, indent=2))
# Verify
current_hash = sdk.hash_document(path.read_bytes())
assert current_hash == record["document_hash"]
assert sdk.verify_signature(record["document_hash"], record["signature"], signer.address())
print("signed and verified by", signer.address())