82 lines
1.5 KiB
Go
82 lines
1.5 KiB
Go
package wallet
|
|
|
|
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/lib/sigs"
|
|
)
|
|
|
|
func GenerateKey(typ crypto.SigType) (*Key, error) {
|
|
pk, err := sigs.Generate(typ)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ki := types.KeyInfo{
|
|
Type: kstoreSigType(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 KTSecp256k1:
|
|
k.Address, err = address.NewSecp256k1Address(k.PublicKey)
|
|
if err != nil {
|
|
return nil, xerrors.Errorf("converting Secp256k1 to address: %w", err)
|
|
}
|
|
case 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("unknown key type")
|
|
}
|
|
return k, nil
|
|
|
|
}
|
|
|
|
func kstoreSigType(typ crypto.SigType) string {
|
|
switch typ {
|
|
case crypto.SigTypeBLS:
|
|
return KTBLS
|
|
case crypto.SigTypeSecp256k1:
|
|
return KTSecp256k1
|
|
default:
|
|
return ""
|
|
}
|
|
}
|
|
|
|
func ActSigType(typ string) crypto.SigType {
|
|
switch typ {
|
|
case KTBLS:
|
|
return crypto.SigTypeBLS
|
|
case KTSecp256k1:
|
|
return crypto.SigTypeSecp256k1
|
|
default:
|
|
return 0
|
|
}
|
|
}
|