lotus/chain/wallet/key/key.go
raulk e7aa7cb04f
add a basic FEVM integration test. (#9922)
* add a basic FEVM integration test.

Exercises f4 addresses, placeholder transitions, Ethereum Account.

* remove unused parameter from newEthTxFromFilecoinMessageLookup.

* break when found in newEthTxFromFilecoinMessageLookup.

* fixup test.

* lint and gen.

* move test to itests root package.

Co-authored-by: Shrenuj Bansal <shrenuj.bansal@protocol.ai>
2022-12-21 15:12:07 -05:00

91 lines
2.1 KiB
Go

package key
import (
"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"
"github.com/filecoin-project/lotus/lib/sigs"
"github.com/filecoin-project/lotus/lib/sigs/delegated"
)
func GenerateKey(typ types.KeyType) (*Key, error) {
ctyp := ActSigType(typ)
if ctyp == crypto.SigTypeUnknown {
return nil, xerrors.Errorf("unknown sig type: %s", typ)
}
pk, err := sigs.Generate(ctyp)
if err != nil {
return nil, err
}
ki := types.KeyInfo{
Type: typ,
PrivateKey: pk,
}
return NewKey(ki)
}
type Key struct {
types.KeyInfo
PublicKey []byte
Address address.Address
}
func NewKey(keyinfo types.KeyInfo) (*Key, error) {
k := &Key{
KeyInfo: keyinfo,
}
var err error
k.PublicKey, err = sigs.ToPublic(ActSigType(k.Type), k.PrivateKey)
if err != nil {
return nil, err
}
switch k.Type {
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
ethAddr, err := delegated.EthAddressFromPubKey(k.PublicKey)
if err != nil {
return nil, xerrors.Errorf("failed to calculate Eth address from public key: %w", err)
}
k.Address, err = address.NewDelegatedAddress(builtin.EthereumAddressManagerActorID, ethAddr)
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 {
return nil, xerrors.Errorf("converting BLS to address: %w", err)
}
default:
return nil, xerrors.Errorf("unsupported key type: %s", k.Type)
}
return k, nil
}
func ActSigType(typ types.KeyType) crypto.SigType {
switch typ {
case types.KTBLS:
return crypto.SigTypeBLS
case types.KTSecp256k1:
return crypto.SigTypeSecp256k1
case types.KTDelegated:
return crypto.SigTypeDelegated
default:
return crypto.SigTypeUnknown
}
}