Skip to content

Commit

Permalink
Support creation and signing of Eth keys
Browse files Browse the repository at this point in the history
  • Loading branch information
arajasek committed Dec 15, 2022
1 parent 36b9fe6 commit 6a6a65a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
2 changes: 1 addition & 1 deletion chain/types/ethtypes/eth_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 22 additions & 1 deletion chain/wallet/key/key.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package key

import (
"golang.org/x/crypto/sha3"

"github.com/filecoin-project/go-state-types/builtin"

"golang.org/x/xerrors"

"github.com/filecoin-project/go-address"
Expand Down Expand Up @@ -45,11 +49,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 {
Expand All @@ -58,6 +78,7 @@ func NewKey(keyinfo types.KeyInfo) (*Key, error) {
default:
return nil, xerrors.Errorf("unsupported key type: %s", k.Type)
}

return k, nil

}
Expand Down
12 changes: 10 additions & 2 deletions lib/sigs/delegated/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 6a6a65a

Please # to comment.