Add EthAddress method for Ethereum-compatible address derivation

Adds EthAddress() methods to both PrivateKey and PublicKey types
to compute Ethereum addresses from secp256k1 keys using Keccak256.
This commit is contained in:
Zach Kelling
2025-12-13 03:05:25 +00:00
parent 78e922fa7a
commit d3c8fb2fe2
2 changed files with 21 additions and 1 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
module github.com/luxfi/crypto
go 1.25.5
go 1.25.4
require (
filippo.io/age v1.2.1
+20
View File
@@ -191,6 +191,11 @@ func (k *PrivateKey) Address() ids.ShortID {
return k.PublicKey().Address()
}
// EthAddress returns the Ethereum address derived from the private key
func (k *PrivateKey) EthAddress() [20]byte {
return k.PublicKey().EthAddress()
}
// Address returns the address of the public key as an ids.ShortID
func (k *PublicKey) Address() ids.ShortID {
// Use traditional Lux address format (SHA256 + RIPEMD160)
@@ -201,6 +206,21 @@ func (k *PublicKey) Address() ids.ShortID {
return addr
}
// EthAddress returns the Ethereum address derived from the public key
// This is computed as the last 20 bytes of the Keccak256 hash of the uncompressed public key
func (k *PublicKey) EthAddress() [20]byte {
// Get uncompressed public key bytes (excluding the 0x04 prefix)
pkBytes := k.Bytes()
// Compute Keccak256 hash
hash := Keccak256(pkBytes)
// Take the last 20 bytes as the address
var addr [20]byte
copy(addr[:], hash[12:])
return addr
}
// Bytes returns the public key bytes
func (k *PublicKey) Bytes() []byte {
return k.bytes