Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

fix secp256k1 public key formatting #501

Merged
merged 3 commits into from
Sep 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions crypto/secp256k1.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func GenerateKey() (PrivKeySecp256k1, error) {
// PubKey returns the ECDSA private key's public key.
func (privkey PrivKeySecp256k1) PubKey() tmcrypto.PubKey {
ecdsaPKey := privkey.ToECDSA()
return PubKeySecp256k1(ethcrypto.FromECDSAPub(&ecdsaPKey.PublicKey))
return PubKeySecp256k1(ethcrypto.CompressPubkey(&ecdsaPKey.PublicKey))
}

// Bytes returns the raw ECDSA private key bytes.
Expand All @@ -59,7 +59,10 @@ func (privkey PrivKeySecp256k1) Equals(other tmcrypto.PrivKey) bool {

// ToECDSA returns the ECDSA private key as a reference to ecdsa.PrivateKey type.
func (privkey PrivKeySecp256k1) ToECDSA() *ecdsa.PrivateKey {
key, _ := ethcrypto.ToECDSA(privkey)
key, err := ethcrypto.ToECDSA(privkey)
if err != nil {
panic(err)
}
return key
}

Expand All @@ -74,7 +77,11 @@ type PubKeySecp256k1 []byte

// Address returns the address of the ECDSA public key.
func (key PubKeySecp256k1) Address() tmcrypto.Address {
pubk, _ := ethcrypto.UnmarshalPubkey(key)
pubk, err := ethcrypto.DecompressPubkey(key)
if err != nil {
panic(err)
}

return tmcrypto.Address(ethcrypto.PubkeyToAddress(*pubk).Bytes())
}

Expand Down
2 changes: 1 addition & 1 deletion tests/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ func TestEth_EstimateGas(t *testing.T) {
err := json.Unmarshal(rpcRes.Result, &gas)
require.NoError(t, err, string(rpcRes.Result))

require.Equal(t, "0x1051d", gas)
require.Equal(t, "0xfd40", gas)
}

func TestEth_EstimateGas_ContractDeployment(t *testing.T) {
Expand Down
16 changes: 16 additions & 0 deletions types/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,19 @@ func TestEthermintAccount_String(t *testing.T) {
require.Contains(t, accountStr, addr.String())
require.Contains(t, accountStr, bech32pubkey)
}

func TestEthermintAccount_MarshalJSON(t *testing.T) {
pubkey := secp256k1.GenPrivKey().PubKey()
addr := sdk.AccAddress(pubkey.Address())
balance := sdk.NewCoins(sdk.NewCoin(DenomDefault, sdk.OneInt()))
baseAcc := auth.NewBaseAccount(addr, balance, pubkey, 10, 50)
ethAcc := &EthAccount{BaseAccount: baseAcc, CodeHash: []byte{1, 2}}

bz, err := ethAcc.MarshalJSON()
require.NoError(t, err)

res := new(EthAccount)
err = res.UnmarshalJSON(bz)
require.NoError(t, err)
require.Equal(t, ethAcc, res)
}