diff --git a/utils/crypto/secp256k1/secp256k1.go b/utils/crypto/secp256k1/secp256k1.go index 7dbbf9f84f7d..a72735860337 100644 --- a/utils/crypto/secp256k1/secp256k1.go +++ b/utils/crypto/secp256k1/secp256k1.go @@ -10,12 +10,16 @@ import ( "github.com/decred/dcrd/dcrec/secp256k1/v4/ecdsa" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ava-labs/avalanchego/cache" "github.com/ava-labs/avalanchego/ids" "github.com/ava-labs/avalanchego/utils/cb58" "github.com/ava-labs/avalanchego/utils/hashing" stdecdsa "crypto/ecdsa" + secp256k1 "github.com/decred/dcrd/dcrec/secp256k1/v4" ) @@ -166,6 +170,10 @@ func (k *PublicKey) Address() ids.ShortID { return k.addr } +func (k *PublicKey) EthAddress() common.Address { + return crypto.PubkeyToAddress(*(k.ToECDSA())) +} + func (k *PublicKey) Bytes() []byte { if k.bytes == nil { k.bytes = k.pk.SerializeCompressed() diff --git a/vms/secp256k1fx/keychain.go b/vms/secp256k1fx/keychain.go index aceacb26ac5d..1d2849655154 100644 --- a/vms/secp256k1fx/keychain.go +++ b/vms/secp256k1fx/keychain.go @@ -9,7 +9,6 @@ import ( "strings" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/crypto" "github.com/ava-labs/avalanchego/ids" "github.com/ava-labs/avalanchego/utils/crypto/keychain" @@ -55,7 +54,7 @@ func (kc *Keychain) Add(key *secp256k1.PrivateKey) { avaxAddr := pk.Address() if _, ok := kc.avaxAddrToKeyIndex[avaxAddr]; !ok { kc.avaxAddrToKeyIndex[avaxAddr] = len(kc.Keys) - ethAddr := PublicKeyToEthAddress(pk) + ethAddr := pk.EthAddress() kc.ethAddrToKeyIndex[ethAddr] = len(kc.Keys) kc.Keys = append(kc.Keys, key) kc.Addrs.Add(avaxAddr) @@ -169,7 +168,3 @@ func (kc Keychain) get(id ids.ShortID) (*secp256k1.PrivateKey, bool) { } return nil, false } - -func PublicKeyToEthAddress(pk *secp256k1.PublicKey) common.Address { - return crypto.PubkeyToAddress(*(pk.ToECDSA())) -} diff --git a/wallet/subnet/primary/examples/c-chain-import/main.go b/wallet/subnet/primary/examples/c-chain-import/main.go index 1b8274c3cd06..98f747f2223d 100644 --- a/wallet/subnet/primary/examples/c-chain-import/main.go +++ b/wallet/subnet/primary/examples/c-chain-import/main.go @@ -22,7 +22,7 @@ func main() { uri := primary.LocalAPIURI kc := secp256k1fx.NewKeychain(key) avaxAddr := key.Address() - ethAddr := secp256k1fx.PublicKeyToEthAddress(key.PublicKey()) + ethAddr := key.PublicKey().EthAddress() ctx := context.Background()