Support creation and signing of Eth keys
This commit is contained in:
parent
36b9fe6386
commit
f8f07dbc70
@ -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
|
||||
|
@ -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
|
||||
|
||||
}
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user