cdf3812e40
Co-authored-by: Steven Allen <steven@stebalien.com> Co-authored-by: Raul Kripalani <raulk@users.noreply.github.com> Co-authored-by: Kevin Li <ychiaoli18@users.noreply.github.com> Co-authored-by: vyzo <vyzo@hackzen.org> Co-authored-by: Ian Davis <nospam@iandavis.com> Co-authored-by: Aayush Rajasekaran <arajasek94@gmail.com> Co-authored-by: Jiaying Wang <42981373+jennijuju@users.noreply.github.com> Co-authored-by: Jennifer Wang <jiayingw703@gmail.com> Co-authored-by: Geoff Stuart <geoff.vball@gmail.com> Co-authored-by: Shrenuj Bansal <shrenuj.bansal@protocol.ai> Co-authored-by: Shrenuj Bansal <108157875+shrenujbansal@users.noreply.github.com> Co-authored-by: Geoff Stuart <geoffrey.stuart@protocol.ai> Co-authored-by: Aayush Rajasekaran <aayushrajasekaran@Aayushs-MacBook-Pro.local> Co-authored-by: ZenGround0 <5515260+ZenGround0@users.noreply.github.com> Co-authored-by: zenground0 <ZenGround0@users.noreply.github.com>
95 lines
2.2 KiB
Go
95 lines
2.2 KiB
Go
package key
|
|
|
|
import (
|
|
"golang.org/x/xerrors"
|
|
|
|
"github.com/filecoin-project/go-address"
|
|
"github.com/filecoin-project/go-state-types/crypto"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
"github.com/filecoin-project/lotus/chain/types/ethtypes"
|
|
"github.com/filecoin-project/lotus/lib/sigs"
|
|
)
|
|
|
|
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:
|
|
// Transitory Delegated signature verification as per FIP-0055
|
|
ethAddr, err := ethtypes.EthAddressFromPubKey(k.PublicKey)
|
|
if err != nil {
|
|
return nil, xerrors.Errorf("failed to calculate Eth address from public key: %w", err)
|
|
}
|
|
|
|
ea, err := ethtypes.CastEthAddress(ethAddr)
|
|
if err != nil {
|
|
return nil, xerrors.Errorf("failed to create ethereum address from bytes: %w", err)
|
|
}
|
|
|
|
k.Address, err = ea.ToFilecoinAddress()
|
|
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
|
|
}
|
|
}
|