Support creation and signing of Eth keys

This commit is contained in:
Aayush 2022-12-15 16:39:12 -05:00
parent 36b9fe6386
commit f8f07dbc70
3 changed files with 31 additions and 4 deletions

View File

@ -304,7 +304,7 @@ func EthAddressFromHex(s string) (EthAddress, error) {
func EthAddressFromBytes(b []byte) (EthAddress, error) { func EthAddressFromBytes(b []byte) (EthAddress, error) {
var a EthAddress var a EthAddress
if len(b) != EthAddressLength { 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[:]) copy(a[:], b[:])
return a, nil return a, nil

View File

@ -1,9 +1,11 @@
package key package key
import ( import (
"golang.org/x/crypto/sha3"
"golang.org/x/xerrors" "golang.org/x/xerrors"
"github.com/filecoin-project/go-address" "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/go-state-types/crypto"
"github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/chain/types"
@ -45,11 +47,27 @@ func NewKey(keyinfo types.KeyInfo) (*Key, error) {
} }
switch k.Type { switch k.Type {
case types.KTSecp256k1, types.KTDelegated: case types.KTSecp256k1:
k.Address, err = address.NewSecp256k1Address(k.PublicKey) k.Address, err = address.NewSecp256k1Address(k.PublicKey)
if err != nil { if err != nil {
return nil, xerrors.Errorf("converting Secp256k1 to address: %w", err) 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: case types.KTBLS:
k.Address, err = address.NewBLSAddress(k.PublicKey) k.Address, err = address.NewBLSAddress(k.PublicKey)
if err != nil { if err != nil {
@ -58,6 +76,7 @@ func NewKey(keyinfo types.KeyInfo) (*Key, error) {
default: default:
return nil, xerrors.Errorf("unsupported key type: %s", k.Type) return nil, xerrors.Errorf("unsupported key type: %s", k.Type)
} }
return k, nil return k, nil
} }

View File

@ -27,8 +27,16 @@ func (delegatedSigner) ToPublic(pk []byte) ([]byte, error) {
return gocrypto.PublicKey(pk), nil return gocrypto.PublicKey(pk), nil
} }
func (delegatedSigner) Sign(pk []byte, msg []byte) ([]byte, error) { func (s delegatedSigner) Sign(pk []byte, msg []byte) ([]byte, error) {
return nil, fmt.Errorf("not implemented") 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 { func (delegatedSigner) Verify(sig []byte, a address.Address, msg []byte) error {