From f8f07dbc70640b572dc7ffba13a9bed2c887c390 Mon Sep 17 00:00:00 2001 From: Aayush Date: Thu, 15 Dec 2022 16:39:12 -0500 Subject: [PATCH] Support creation and signing of Eth keys --- chain/types/ethtypes/eth_types.go | 2 +- chain/wallet/key/key.go | 21 ++++++++++++++++++++- lib/sigs/delegated/init.go | 12 ++++++++++-- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/chain/types/ethtypes/eth_types.go b/chain/types/ethtypes/eth_types.go index 6eefb5bf0..75846e10a 100644 --- a/chain/types/ethtypes/eth_types.go +++ b/chain/types/ethtypes/eth_types.go @@ -304,7 +304,7 @@ func EthAddressFromHex(s string) (EthAddress, error) { func EthAddressFromBytes(b []byte) (EthAddress, error) { var a EthAddress if len(b) != EthAddressLength { - return EthAddress{}, xerrors.Errorf("cannot parse bytes into anœ EthAddress: incorrect input length") + return EthAddress{}, xerrors.Errorf("cannot parse bytes into an EthAddress: incorrect input length") } copy(a[:], b[:]) return a, nil diff --git a/chain/wallet/key/key.go b/chain/wallet/key/key.go index cb769ef09..a52a431c6 100644 --- a/chain/wallet/key/key.go +++ b/chain/wallet/key/key.go @@ -1,9 +1,11 @@ package key import ( + "golang.org/x/crypto/sha3" "golang.org/x/xerrors" "github.com/filecoin-project/go-address" + "github.com/filecoin-project/go-state-types/builtin" "github.com/filecoin-project/go-state-types/crypto" "github.com/filecoin-project/lotus/chain/types" @@ -45,11 +47,27 @@ func NewKey(keyinfo types.KeyInfo) (*Key, error) { } switch k.Type { - case types.KTSecp256k1, types.KTDelegated: + case types.KTSecp256k1: k.Address, err = address.NewSecp256k1Address(k.PublicKey) if err != nil { return nil, xerrors.Errorf("converting Secp256k1 to address: %w", err) } + case types.KTDelegated: + // Assume eth for now + hasher := sha3.NewLegacyKeccak256() + pubk := k.PublicKey + // 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 { + pubk = pubk[1:] + } + + hasher.Write(pubk) + + k.Address, err = address.NewDelegatedAddress(builtin.EthereumAddressManagerActorID, hasher.Sum(nil)[12:]) + if err != nil { + return nil, xerrors.Errorf("converting Delegated to address: %w", err) + } case types.KTBLS: k.Address, err = address.NewBLSAddress(k.PublicKey) if err != nil { @@ -58,6 +76,7 @@ func NewKey(keyinfo types.KeyInfo) (*Key, error) { default: return nil, xerrors.Errorf("unsupported key type: %s", k.Type) } + return k, nil } diff --git a/lib/sigs/delegated/init.go b/lib/sigs/delegated/init.go index e092bc576..e25e8e9cb 100644 --- a/lib/sigs/delegated/init.go +++ b/lib/sigs/delegated/init.go @@ -27,8 +27,16 @@ func (delegatedSigner) ToPublic(pk []byte) ([]byte, error) { return gocrypto.PublicKey(pk), nil } -func (delegatedSigner) Sign(pk []byte, msg []byte) ([]byte, error) { - return nil, fmt.Errorf("not implemented") +func (s delegatedSigner) Sign(pk []byte, msg []byte) ([]byte, error) { + hasher := sha3.NewLegacyKeccak256() + hasher.Write(msg) + hashSum := hasher.Sum(nil) + sig, err := gocrypto.Sign(pk, hashSum) + if err != nil { + return nil, err + } + + return sig, nil } func (delegatedSigner) Verify(sig []byte, a address.Address, msg []byte) error {