Skip to content

Commit

Permalink
move eth address to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
ceyonur committed Dec 24, 2024
1 parent 0a2b7bc commit 482c913
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
8 changes: 8 additions & 0 deletions utils/crypto/secp256k1/secp256k1.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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()
Expand Down
7 changes: 1 addition & 6 deletions vms/secp256k1fx/keychain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()))
}
2 changes: 1 addition & 1 deletion wallet/subnet/primary/examples/c-chain-import/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 482c913

Please sign in to comment.