Simple test
Ensure your device works with this simple test.
examples/miniethers_simpletest.py
1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
2# SPDX-FileCopyrightText: Copyright (c) 2025 Shamba Chowdhury
3#
4# SPDX-License-Identifier: Unlicense
5
6from circuitpython_miniethers import Signature, Wallet
7
8# The private key is used for both signing methods
9# IMPORTANT: In a real app, never hardcode private keys.
10# Use environment variables or a secure wallet connection.
11privateKey = "0x022b99092266a16a949e6a450f0e88a8288d39d5f1d75c00575a35a0ba270dbc"
12
13# Create a wallet instance from the private key
14wallet = Wallet(privateKey)
15
16
17def generateFlatSignature():
18 """
19 Signs a simple string message (ERC-191).
20 """
21 print("--- Signing a Flat String (ERC-191) ---")
22
23 # The message to sign
24 message = "hello"
25
26 print(f'Signing message: "{message}"')
27 print(f"Signer Address: {wallet.address}")
28 print("---")
29
30 flatSignature = wallet.signMessage(message)
31
32 print("Full Flat Signature:", flatSignature)
33
34 # For comparison, let's split the signature into its components
35 signature = getattr(Signature, "from")(flatSignature)
36 print("Signature Components:")
37 print(" r:", signature.r)
38 print(" s:", signature.s)
39 print(" v:", signature.v)
40 print("-----------------------------------------\n")
41
42
43def signTypedDataMail():
44 """
45 Signs structured typed data (EIP-712).
46 This provides more readable and secure signing prompts in wallets like MetaMask.
47 """
48 print("--- Signing Typed Data (EIP-712) ---")
49
50 # 1. The Domain Separator: Defines the context of the signature.
51 # This prevents a signature from being valid in a different application.
52 domain = {
53 # The user-friendly name of the signing domain
54 "name": "Ether Mail",
55 # The current version of the signing domain
56 "version": "1",
57 # The chain ID of the intended network (1 for Ethereum Mainnet)
58 "chainId": 1,
59 # The address of the contract that will verify the signature
60 "verifyingContract": "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
61 }
62
63 # 2. The Types: Defines the structure of the data being signed.
64 # You define your primary type ("Mail") and any custom nested types ("Person").
65 types = {
66 "Person": [
67 {"name": "name", "type": "string"},
68 {"name": "wallet", "type": "address"},
69 ],
70 "Mail": [
71 {"name": "from", "type": "Person"}, # Nested custom type
72 {"name": "to", "type": "Person"}, # Nested custom type
73 {"name": "contents", "type": "string"},
74 ],
75 }
76
77 # 3. The Value: The actual data object to be signed.
78 # This object must match the structure defined in `types`.
79 value = {
80 "from": {
81 "name": "Cow",
82 "wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826",
83 },
84 "to": {
85 "name": "Bob",
86 "wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB",
87 },
88 "contents": "Hello, Bob! This is a typed message.",
89 }
90
91 print("Signing EIP-712 data for address:", wallet.address)
92 print("Domain:", domain)
93 print("Value:", value)
94 print("---")
95
96 # The `_signTypedData` method handles hashing the structured data according to the EIP-712 spec.
97 signature = wallet._signTypedData(domain, types, value)
98
99 print("EIP-712 Signature:", signature)
100 print("------------------------------------\n")
101
102
103# Run both signing functions
104def main():
105 print("CircuitPython MiniEthers - Ethers.js Compatibility Test\n")
106 print(f"Wallet Address: {wallet.address}")
107 print(f"Private Key: {wallet.privateKey}")
108 print(f"Public Key: {wallet.publicKey}\n")
109
110 generateFlatSignature()
111 signTypedDataMail()
112
113 print("\n✅ All tests completed successfully!")
114
115
116if __name__ == "__main__":
117 main()