lotus/tvx/chain/wallet/wallet.go
Anton Evangelatov 5353210814
generate test-vectors based on tests from chain-validation project (#181)
* Read a subset of filecoin state over the full node API

* wip

* import export wip

* extract actors from message

* generate car for any state

* library for providing a pruned statetree

* test vector schema draft + example 'message' class test vector.

* message => messages test vector class.

* fixup

* wip

* use lb.NewBlockstore with ID

* fixup lotus-soup, and generate

* fix deals

* magic params

* work on schema / export of test vector

* fixup

* wip deserialise state tree

* pass at building a test case from a message

* progress loading / serializing

* recreation of ipld nodes

* generation of vector creates json

* kick off tvx tool.

* wip

* wip

* retain init actor state.

* initial test with printed out state

* remove testing.T, but keep require and assert libraries

* wip refactor state tree plucking.

* simplified

* removed factories

* remove builder.Build ; remove interface - use concrete iface

* comment out validateState

* remove Validator

* remove TestDriverBuilder

* remove client

* remove box

* remove gen

* remove factories

* remove KeyManager interfafce

* moved stuff around

* remove ValidationConfig

* extract randomness

* extract config and key_manager

* extract statewrapper

* extract applier

* rename factories to various

* flatten chain-validation package

* initial marshal of test vector

* do not require schema package

* fixup

* run all messages tests

* better names

* run all messages tests

* remove Indent setting from JSON encoder for now

* refactor, and actually running successfully ;-)

* remove irrelevant files; rename extract-msg command.

* remove root CID from state_tree object in schema.

* add tvx/lotus package; adjust .gitignore.

* tidy up command flag management.

* add comment.

* remove validateState and trackState

* remove xerrors

* remove NewVM

* remove commented out RootCID sets

* enable more tests

* add all `message_application` tests

* delete all.json

* update Message struct

* fix message serialization

* support multiple messages

* gofmt

* remove custom Message and SignedMessage types

* update tests with gzip and adhere to new schema for compressed CAR

* improved iface for Marshal

* update Validation

* remove test-suites and utils

* better names for chain. methods

* go mod tidy

* remove top-level dummyT

Co-authored-by: Will Scott <will@cypherpunk.email>
Co-authored-by: Raúl Kripalani <raul@protocol.ai>
2020-08-05 19:40:09 +02:00

114 lines
2.6 KiB
Go

package wallet
import (
"errors"
"fmt"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/specs-actors/actors/crypto"
"github.com/ipsn/go-secp256k1"
blake2b "github.com/minio/blake2b-simd"
bls "github.com/filecoin-project/filecoin-ffi"
)
// KeyInfo is used for storing keys in KeyStore
type KeyInfo struct {
Type crypto.SigType
PrivateKey []byte
}
type Key struct {
KeyInfo
PublicKey []byte
Address address.Address
}
func NewKey(keyinfo KeyInfo) (*Key, error) {
k := &Key{
KeyInfo: keyinfo,
}
var err error
k.PublicKey, err = ToPublic(k.Type, k.PrivateKey)
if err != nil {
return nil, err
}
switch k.Type {
case crypto.SigTypeSecp256k1:
k.Address, err = address.NewSecp256k1Address(k.PublicKey)
if err != nil {
return nil, fmt.Errorf("converting Secp256k1 to address: %w", err)
}
case crypto.SigTypeBLS:
k.Address, err = address.NewBLSAddress(k.PublicKey)
if err != nil {
return nil, fmt.Errorf("converting BLS to address: %w", err)
}
default:
return nil, errors.New("unknown key type")
}
return k, nil
}
func Sign(data []byte, secretKey []byte, sigtype crypto.SigType) (crypto.Signature, error) {
var signature []byte
var err error
if sigtype == crypto.SigTypeSecp256k1 {
hash := blake2b.Sum256(data)
signature, err = SignSecp(secretKey, hash[:])
} else if sigtype == crypto.SigTypeBLS {
signature, err = SignBLS(secretKey, data)
} else {
err = fmt.Errorf("unknown signature type %d", sigtype)
}
return crypto.Signature{
Type: sigtype,
Data: signature,
}, err
}
func SignSecp(sk, msg []byte) ([]byte, error) {
return secp256k1.Sign(msg, sk)
}
// SignBLS signs the given message with BLS.
func SignBLS(sk, msg []byte) ([]byte, error) {
var privateKey bls.PrivateKey
copy(privateKey[:], sk)
sig := bls.PrivateKeySign(privateKey, msg)
return sig[:], nil
}
// ported from lotus
// SigShim is used for introducing signature functions
type SigShim interface {
GenPrivate() ([]byte, error)
ToPublic(pk []byte) ([]byte, error)
Sign(pk []byte, msg []byte) ([]byte, error)
Verify(sig []byte, a address.Address, msg []byte) error
}
var sigs map[crypto.SigType]SigShim
// RegisterSig should be only used during init
func RegisterSignature(typ crypto.SigType, vs SigShim) {
if sigs == nil {
sigs = make(map[crypto.SigType]SigShim)
}
sigs[typ] = vs
}
// ToPublic converts private key to public key
func ToPublic(sigType crypto.SigType, pk []byte) ([]byte, error) {
sv, ok := sigs[sigType]
if !ok {
return nil, fmt.Errorf("cannot generate public key of unsupported type: %v", sigType)
}
return sv.ToPublic(pk)
}