move EthAddressFromPubKey to chain/types/ethtypes.

This commit is contained in:
Raúl Kripalani 2023-01-11 12:29:44 +00:00 committed by raulk
parent 4aafd42462
commit 73d6c7b28a
6 changed files with 22 additions and 34 deletions

View File

@ -20,7 +20,6 @@ import (
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/lib/sigs/delegated"
)
const Eip1559TxType = 2
@ -333,7 +332,7 @@ func (tx *EthTxArgs) Sender() (address.Address, error) {
return address.Undef, err
}
ethAddr, err := delegated.EthAddressFromPubKey(pubk)
ethAddr, err := EthAddressFromPubKey(pubk)
if err != nil {
return address.Undef, err
}

View File

@ -20,7 +20,6 @@ import (
"github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/lib/sigs"
"github.com/filecoin-project/lotus/lib/sigs/delegated"
_ "github.com/filecoin-project/lotus/lib/sigs/delegated"
)
@ -200,7 +199,7 @@ func TestDelegatedSigner(t *testing.T) {
r := mustDecodeHex(rHex)
s := mustDecodeHex(sHex)
addrHash, err := delegated.EthAddressFromPubKey(pubk)
addrHash, err := EthAddressFromPubKey(pubk)
require.NoError(t, err)
from, err := address.NewDelegatedAddress(builtintypes.EthereumAddressManagerActorID, addrHash[12:])

View File

@ -621,3 +621,20 @@ func GetContractEthAddressFromCode(sender EthAddress, salt [32]byte, initcode []
return ethAddr, nil
}
// EthAddressFromPubKey returns the Ethereum address corresponding to an
// uncompressed secp256k1 public key.
func EthAddressFromPubKey(pubk []byte) ([]byte, error) {
// if we get an uncompressed public key (that's what we get from the library,
// but putting this check here for defensiveness), strip the prefix
if pubk[0] != 0x04 {
return nil, fmt.Errorf("expected first byte of secp256k1 to be 0x04 (uncompressed)")
}
pubk = pubk[1:]
// Calculate the Ethereum address based on the keccak hash of the pubkey.
hasher := sha3.NewLegacyKeccak256()
hasher.Write(pubk)
ethAddr := hasher.Sum(nil)[12:]
return ethAddr, nil
}

View File

@ -1,6 +1,7 @@
package key
import (
"github.com/filecoin-project/lotus/chain/types/ethtypes"
"golang.org/x/xerrors"
"github.com/filecoin-project/go-address"
@ -9,7 +10,6 @@ import (
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/lib/sigs"
"github.com/filecoin-project/lotus/lib/sigs/delegated"
)
func GenerateKey(typ types.KeyType) (*Key, error) {
@ -54,7 +54,7 @@ func NewKey(keyinfo types.KeyInfo) (*Key, error) {
}
case types.KTDelegated:
// Assume eth for now
ethAddr, err := delegated.EthAddressFromPubKey(k.PublicKey)
ethAddr, err := ethtypes.EthAddressFromPubKey(k.PublicKey)
if err != nil {
return nil, xerrors.Errorf("failed to calculate Eth address from public key: %w", err)
}

View File

@ -26,7 +26,6 @@ import (
"github.com/filecoin-project/lotus/chain/types/ethtypes"
"github.com/filecoin-project/lotus/chain/wallet/key"
"github.com/filecoin-project/lotus/lib/sigs"
"github.com/filecoin-project/lotus/lib/sigs/delegated"
)
// EVM groups EVM-related actions.
@ -134,7 +133,7 @@ func (e *EVM) NewAccount() (*key.Key, ethtypes.EthAddress, address.Address) {
key, err := key.GenerateKey(types.KTSecp256k1)
require.NoError(e.t, err)
ethAddr, err := delegated.EthAddressFromPubKey(key.PublicKey)
ethAddr, err := ethtypes.EthAddressFromPubKey(key.PublicKey)
require.NoError(e.t, err)
addr, err := address.NewDelegatedAddress(builtintypes.EthereumAddressManagerActorID, ethAddr)

View File

@ -1,26 +0,0 @@
package delegated
import (
"fmt"
"golang.org/x/crypto/sha3"
)
// EthAddressFromPubKey returns the Ethereum address corresponding to an
// uncompressed secp256k1 public key.
//
// TODO move somewhere else, this likely doesn't belong here.
func EthAddressFromPubKey(pubk []byte) ([]byte, error) {
// if we get an uncompressed public key (that's what we get from the library,
// but putting this check here for defensiveness), strip the prefix
if pubk[0] != 0x04 {
return nil, fmt.Errorf("expected first byte of secp256k1 to be 0x04 (uncompressed)")
}
pubk = pubk[1:]
// Calculate the Ethereum address based on the keccak hash of the pubkey.
hasher := sha3.NewLegacyKeccak256()
hasher.Write(pubk)
ethAddr := hasher.Sum(nil)[12:]
return ethAddr, nil
}