Understanding Ethereum Derivation Paths in Electrum
As a developer, you’re probably interested in understanding how Electrum 2.0 and later generate Ethereum addresses using the Brain-Computer Interface (BIP) 0032 specification. In this article, we’ll dive into the derivation paths that Electrum uses to produce these addresses.
What is BIP32?
Before we continue, let’s quickly go over what BIP32 is. Brain-Computer Interface (BIP) 0032 is a specification that defines a method for generating Ethereum addresses based on a set of rules and algorithms. It is an alternative to the traditional Merkle-based address generation approach that Electrum uses.
BIP0032 Derivation Paths
To generate Ethereum addresses using BIP0032, Electrum uses several derivation paths. These paths are defined in the BIP 0032 specification and involve a combination of public keys, cryptographic hashes, and other parameters. Here are some common derivation paths used by Electrum:
p2sh
(PrivateKey to Script Hash)
spv
(Script Public Version)
scriptSig
scriptPubkey
Electrum Derivation Paths
Specifically, Electrum uses the following BIP0032 derivation paths to generate Ethereum addresses:
- p2sh/spv
: This is a common combination used by Electrum to generate addresses from private keys or public keys using the
p2sh
andspv
functions.
- scriptSig/scriptPubkey: This path is used to generate addresses based on the signature of a transaction, which includes the sender’s public key, the transaction hash, and other information.
Sample Code
To generate an Ethereum address using BIP0032 derivation paths with Electrum 2.0 or later, you can use the following code as a starting point:
import hashlib
def derive_address(private_key):
Extract the sender's public key from private_key (assuming 32 bytes)public_key = private_key[:32]
Derive the script hash using the p2sh and spv functionsscript_hash = derive_script_hash(public_key)
Combine the script hash with the transaction signature to generate the addressaddress = f"{script_hash.hex()}{private_key[32:]}"
return address
def derive_script_hash(public_key):
Generate a random session key (assuming 16 bytes)session_key = b'\x00' * 16
Derive the script hash from the public key and session keyscript_hash = hashlib.sha256(session_key + public_key).digest()
return script_hash.hex()
Load the private key from file or generate a new oneprivate_key = b'\x01\x02\x03\x04\x05\x06\x07\x08'
Replace with your own private key
Generate an Ethereum address using BIP0032 derivation pathsaddress = derive_address(private_key)
print(address)
Output: A unique Ethereum address (e.g. 0x1234567890abcdef)
Note that this code is just a starting point and you will need to modify it to suit your specific use case. Also, keep in mind that generating Ethereum addresses using BIP0032 derivation paths can be complex and may require additional cryptographic expertise.
Conclusion
In conclusion, Electrum 2.0 and later versions use a combination of derivation paths from the BIP0032 specification (p2sh/spv, scriptSig/scriptPubkey) to generate Ethereum addresses internally. While the code may appear to be proprietary, understanding how these derivation paths work can help you develop your own applications that use the underlying Ethereum protocol.