mirror of
https://github.com/luxfi/crypto.git
synced 2026-07-27 01:54:50 +00:00
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:
@@ -1,6 +1,6 @@
|
||||
module github.com/luxfi/crypto
|
||||
|
||||
go 1.25.5
|
||||
go 1.25.4
|
||||
|
||||
require (
|
||||
filippo.io/age v1.2.1
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user