2019-10-17 04:53:25 +00:00
|
|
|
package validation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2020-02-27 22:17:08 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin"
|
2019-10-17 04:53:25 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
"github.com/ipfs/go-datastore"
|
|
|
|
blockstore "github.com/ipfs/go-ipfs-blockstore"
|
2020-02-04 22:19:05 +00:00
|
|
|
cbor "github.com/ipfs/go-ipld-cbor"
|
2019-12-06 00:27:32 +00:00
|
|
|
"golang.org/x/xerrors"
|
2019-11-26 21:58:43 +00:00
|
|
|
|
2020-02-27 22:17:08 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi/big"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/runtime"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/util/adt"
|
2019-10-17 04:53:25 +00:00
|
|
|
|
2020-02-27 22:17:08 +00:00
|
|
|
vstate "github.com/filecoin-project/chain-validation/state"
|
2020-02-11 20:48:03 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/chain/gen/genesis"
|
2019-10-17 04:53:25 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/state"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
)
|
|
|
|
|
2020-02-27 22:17:08 +00:00
|
|
|
var _ vstate.VMWrapper = &StateWrapper{}
|
|
|
|
|
2019-10-17 04:53:25 +00:00
|
|
|
type StateWrapper struct {
|
|
|
|
// The blockstore underlying the state tree and storage.
|
|
|
|
bs blockstore.Blockstore
|
2020-03-03 01:24:07 +00:00
|
|
|
|
|
|
|
ds datastore.Batching
|
2019-10-17 04:53:25 +00:00
|
|
|
// HAMT-CBOR store on top of the blockstore.
|
2020-02-04 22:19:05 +00:00
|
|
|
cst cbor.IpldStore
|
2019-10-17 04:53:25 +00:00
|
|
|
|
|
|
|
// CID of the root of the state tree.
|
|
|
|
stateRoot cid.Cid
|
|
|
|
}
|
|
|
|
|
2020-02-27 22:17:08 +00:00
|
|
|
func NewState() *StateWrapper {
|
|
|
|
bs := blockstore.NewBlockstore(datastore.NewMapDatastore())
|
|
|
|
cst := cbor.NewCborStore(bs)
|
|
|
|
// Put EmptyObjectCid value in the store. When an actor is initially created its Head is set to this value.
|
|
|
|
_, err := cst.Put(context.TODO(), map[string]string{})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
treeImpl, err := state.NewStateTree(cst)
|
|
|
|
if err != nil {
|
|
|
|
panic(err) // Never returns error, the error return should be removed.
|
|
|
|
}
|
|
|
|
root, err := treeImpl.Flush(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-03-03 01:24:07 +00:00
|
|
|
return &StateWrapper{
|
|
|
|
bs: bs,
|
|
|
|
ds: datastore.NewMapDatastore(),
|
|
|
|
cst: cst,
|
|
|
|
stateRoot: root,
|
|
|
|
}
|
2020-02-27 22:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StateWrapper) Root() cid.Cid {
|
|
|
|
return s.stateRoot
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StateWrapper) Store() adt.Store {
|
|
|
|
tree, err := state.LoadStateTree(s.cst, s.stateRoot)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return &contextStore{tree.Store, context.Background()}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StateWrapper) Actor(addr address.Address) (vstate.Actor, error) {
|
|
|
|
tree, err := state.LoadStateTree(s.cst, s.stateRoot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fcActor, err := tree.GetActor(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &actorWrapper{*fcActor}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StateWrapper) SetActorState(addr address.Address, balance abi.TokenAmount, actorState runtime.CBORMarshaler) (vstate.Actor, error) {
|
|
|
|
tree, err := state.LoadStateTree(s.cst, s.stateRoot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// actor should exist
|
|
|
|
act, err := tree.GetActor(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// add the state to the store and get a new head cid
|
|
|
|
actHead, err := tree.Store.Put(context.Background(), actorState)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// update the actor object with new head and balance parameter
|
|
|
|
actr := &actorWrapper{types.Actor{
|
|
|
|
Code: act.Code,
|
|
|
|
Nonce: act.Nonce,
|
|
|
|
// updates
|
|
|
|
Head: actHead,
|
|
|
|
Balance: balance,
|
|
|
|
}}
|
|
|
|
if err := tree.SetActor(addr, &actr.Actor); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return actr, s.flush(tree)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StateWrapper) CreateActor(code cid.Cid, addr address.Address, balance abi.TokenAmount, actorState runtime.CBORMarshaler) (vstate.Actor, address.Address, error) {
|
|
|
|
if addr == builtin.InitActorAddr || addr == builtin.StoragePowerActorAddr || addr == builtin.StorageMarketActorAddr {
|
|
|
|
act, err := s.SetupSingletonActor(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, address.Undef, err
|
|
|
|
}
|
|
|
|
return act, addr, nil
|
|
|
|
}
|
|
|
|
tree, err := state.LoadStateTree(s.cst, s.Root())
|
|
|
|
if err != nil {
|
|
|
|
return nil, address.Undef, err
|
|
|
|
}
|
|
|
|
actHead, err := tree.Store.Put(context.Background(), actorState)
|
|
|
|
if err != nil {
|
|
|
|
return nil, address.Undef, err
|
|
|
|
}
|
|
|
|
actr := &actorWrapper{types.Actor{
|
|
|
|
Code: code,
|
|
|
|
Head: actHead,
|
|
|
|
Balance: balance,
|
|
|
|
}}
|
2020-03-03 23:01:35 +00:00
|
|
|
|
2020-02-27 22:17:08 +00:00
|
|
|
idAddr, err := tree.RegisterNewAddress(addr, &actr.Actor)
|
|
|
|
if err != nil {
|
|
|
|
return nil, address.Undef, xerrors.Errorf("register new address for actor: %w", err)
|
|
|
|
}
|
2020-03-03 23:01:35 +00:00
|
|
|
|
|
|
|
if err := tree.SetActor(addr, &actr.Actor); err != nil {
|
|
|
|
return nil, address.Undef, xerrors.Errorf("setting new actor for actor: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-02-27 22:17:08 +00:00
|
|
|
return actr, idAddr, s.flush(tree)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flushes a state tree to storage and sets this state's root to that tree's root CID.
|
|
|
|
func (s *StateWrapper) flush(tree *state.StateTree) (err error) {
|
|
|
|
s.stateRoot, err = tree.Flush(context.TODO())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Actor Wrapper
|
|
|
|
//
|
|
|
|
|
|
|
|
type actorWrapper struct {
|
|
|
|
types.Actor
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *actorWrapper) Code() cid.Cid {
|
|
|
|
return a.Actor.Code
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *actorWrapper) Head() cid.Cid {
|
|
|
|
return a.Actor.Head
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *actorWrapper) CallSeqNum() int64 {
|
|
|
|
return int64(a.Actor.Nonce)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *actorWrapper) Balance() big.Int {
|
|
|
|
return a.Actor.Balance
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Storage
|
|
|
|
//
|
|
|
|
|
|
|
|
type contextStore struct {
|
|
|
|
cbor.IpldStore
|
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *contextStore) Context() context.Context {
|
|
|
|
return s.ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StateWrapper) SetupSingletonActor(addr address.Address) (vstate.Actor, error) {
|
|
|
|
tree, err := state.LoadStateTree(s.cst, s.stateRoot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
switch addr {
|
|
|
|
case builtin.InitActorAddr:
|
|
|
|
// FIXME this is going to be a problem if go-filecoin and lotus setup their init actors with different netnames
|
|
|
|
// ideally lotus should use the init actor constructor
|
|
|
|
initact, err := genesis.SetupInitActor(s.bs, "chain-validation", nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("setup init actor: %w", err)
|
|
|
|
}
|
|
|
|
if err := tree.SetActor(builtin.InitActorAddr, initact); err != nil {
|
|
|
|
return nil, xerrors.Errorf("set init actor: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &actorWrapper{*initact}, s.flush(tree)
|
|
|
|
case builtin.StorageMarketActorAddr:
|
|
|
|
smact, err := genesis.SetupStorageMarketActor(s.bs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("setup storage marker actor: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tree.SetActor(builtin.StorageMarketActorAddr, smact); err != nil {
|
|
|
|
return nil, xerrors.Errorf("set storage marker actor: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &actorWrapper{*smact}, s.flush(tree)
|
|
|
|
case builtin.StoragePowerActorAddr:
|
|
|
|
spact, err := genesis.SetupStoragePowerActor(s.bs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("setup storage power actor: %w", err)
|
|
|
|
}
|
|
|
|
if err := tree.SetActor(builtin.StoragePowerActorAddr, spact); err != nil {
|
|
|
|
return nil, xerrors.Errorf("set storage power actor: %w", err)
|
|
|
|
}
|
|
|
|
return &actorWrapper{*spact}, s.flush(tree)
|
|
|
|
default:
|
|
|
|
return nil, xerrors.Errorf("%v is not a singleton actor address", addr)
|
|
|
|
}
|
|
|
|
}
|
2019-10-17 04:53:25 +00:00
|
|
|
|
2020-02-27 22:17:08 +00:00
|
|
|
/*
|
2019-10-17 04:53:25 +00:00
|
|
|
func NewState() *StateWrapper {
|
|
|
|
bs := blockstore.NewBlockstore(datastore.NewMapDatastore())
|
2020-02-04 22:19:05 +00:00
|
|
|
cst := cbor.NewCborStore(bs)
|
2019-10-17 04:53:25 +00:00
|
|
|
// Put EmptyObjectCid value in the store. When an actor is initially created its Head is set to this value.
|
|
|
|
_, err := cst.Put(context.TODO(), map[string]string{})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
treeImpl, err := state.NewStateTree(cst)
|
|
|
|
if err != nil {
|
|
|
|
panic(err) // Never returns error, the error return should be removed.
|
|
|
|
}
|
2020-01-22 20:56:40 +00:00
|
|
|
root, err := treeImpl.Flush(context.TODO())
|
2019-10-17 04:53:25 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
storageImpl := &directStorage{cst}
|
|
|
|
return &StateWrapper{bs, cst, newKeyStore(), root, storageImpl}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StateWrapper) Cid() cid.Cid {
|
|
|
|
return s.stateRoot
|
|
|
|
}
|
|
|
|
|
2020-02-27 22:17:08 +00:00
|
|
|
func (s *StateWrapper) Actor(addr address.Address) (vstate.Actor, error) {
|
2019-11-26 21:58:43 +00:00
|
|
|
vaddr, err := address.NewFromBytes(addr.Bytes())
|
2019-10-17 04:53:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tree, err := state.LoadStateTree(s.cst, s.stateRoot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fcActor, err := tree.GetActor(vaddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &actorWrapper{*fcActor}, nil
|
|
|
|
}
|
|
|
|
|
2020-02-27 22:17:08 +00:00
|
|
|
func (s *StateWrapper) Storage(addr address.Address) (vstate.Storage, error) {
|
2019-10-17 04:53:25 +00:00
|
|
|
return s.storage, nil
|
|
|
|
}
|
|
|
|
|
2020-02-27 22:17:08 +00:00
|
|
|
func (s *StateWrapper) NewAccountAddress() (address.Address, error) {
|
2019-10-17 04:53:25 +00:00
|
|
|
return s.keys.NewAddress()
|
|
|
|
}
|
|
|
|
|
2020-02-27 22:17:08 +00:00
|
|
|
func (s *StateWrapper) SetActor(addr address.Address, code vactors.ActorCodeID, balance vtypes.BigInt) (vstate.Actor, vstate.Storage, error) {
|
2019-11-26 21:58:43 +00:00
|
|
|
addrInt, err := address.NewFromBytes(addr.Bytes())
|
2019-10-17 04:53:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
tree, err := state.LoadStateTree(s.cst, s.stateRoot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
actr := &actorWrapper{types.Actor{
|
|
|
|
Code: fromActorCode(code),
|
2019-11-26 21:58:43 +00:00
|
|
|
Balance: types.BigInt{balance.Int},
|
2019-10-17 04:53:25 +00:00
|
|
|
Head: vm.EmptyObjectCid,
|
|
|
|
}}
|
|
|
|
// The ID-based address is dropped here, but should be reported back to the caller.
|
|
|
|
_, err = tree.RegisterNewAddress(addrInt, &actr.Actor)
|
|
|
|
if err != nil {
|
2019-11-22 16:20:56 +00:00
|
|
|
return nil, nil, xerrors.Errorf("register new address for actor: %w", err)
|
2019-10-17 04:53:25 +00:00
|
|
|
}
|
|
|
|
return actr, s.storage, s.flush(tree)
|
|
|
|
}
|
|
|
|
|
2019-11-26 21:58:43 +00:00
|
|
|
func (s *StateWrapper) SetSingletonActor(addr vactors.SingletonActorID, balance vtypes.BigInt) (vstate.Actor, vstate.Storage, error) {
|
2019-10-17 04:53:25 +00:00
|
|
|
vaddr := fromSingletonAddress(addr)
|
2019-11-26 21:58:43 +00:00
|
|
|
|
2019-10-17 04:53:25 +00:00
|
|
|
tree, err := state.LoadStateTree(s.cst, s.stateRoot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2019-11-26 21:58:43 +00:00
|
|
|
|
|
|
|
lotusAddr, err := address.NewFromBytes(vaddr.Bytes())
|
2019-10-17 04:53:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2019-11-26 21:58:43 +00:00
|
|
|
|
2019-10-17 04:53:25 +00:00
|
|
|
switch lotusAddr {
|
|
|
|
case actors.InitAddress:
|
2020-02-23 07:49:15 +00:00
|
|
|
initact, err := genesis.SetupInitActor(s.bs, "testing", nil)
|
2019-10-17 04:53:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if err := tree.SetActor(actors.InitAddress, initact); err != nil {
|
2019-11-22 16:20:56 +00:00
|
|
|
return nil, nil, xerrors.Errorf("set init actor: %w", err)
|
2019-10-17 04:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &actorWrapper{*initact}, s.storage, s.flush(tree)
|
|
|
|
case actors.StorageMarketAddress:
|
2020-02-23 07:49:15 +00:00
|
|
|
nsroot, err := genesis.SetupStorageMarketActor(s.bs)
|
2019-10-17 04:53:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2019-11-29 03:38:18 +00:00
|
|
|
s.stateRoot = nsroot
|
|
|
|
|
|
|
|
tree, err = state.LoadStateTree(s.cst, s.stateRoot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
smact, err := tree.GetActor(actors.StorageMarketAddress)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2019-10-17 04:53:25 +00:00
|
|
|
}
|
|
|
|
return &actorWrapper{*smact}, s.storage, s.flush(tree)
|
|
|
|
case actors.StoragePowerAddress:
|
2020-02-11 20:48:03 +00:00
|
|
|
spact, err := genesis.SetupStoragePowerActor(s.bs)
|
2019-10-17 04:53:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if err := tree.SetActor(actors.StoragePowerAddress, spact); err != nil {
|
2019-11-22 16:20:56 +00:00
|
|
|
return nil, nil, xerrors.Errorf("set network storage market actor: %w", err)
|
2019-10-17 04:53:25 +00:00
|
|
|
}
|
|
|
|
return &actorWrapper{*spact}, s.storage, s.flush(tree)
|
|
|
|
case actors.NetworkAddress:
|
|
|
|
ntwkact := &types.Actor{
|
|
|
|
Code: actors.AccountCodeCid,
|
2019-11-26 21:58:43 +00:00
|
|
|
Balance: types.BigInt{balance.Int},
|
2019-10-17 04:53:25 +00:00
|
|
|
Head: vm.EmptyObjectCid,
|
|
|
|
}
|
|
|
|
if err := tree.SetActor(actors.NetworkAddress, ntwkact); err != nil {
|
2019-11-22 16:20:56 +00:00
|
|
|
return nil, nil, xerrors.Errorf("set network actor: %w", err)
|
2019-10-17 04:53:25 +00:00
|
|
|
}
|
|
|
|
return &actorWrapper{*ntwkact}, s.storage, s.flush(tree)
|
|
|
|
case actors.BurntFundsAddress:
|
|
|
|
ntwkact := &types.Actor{
|
|
|
|
Code: actors.AccountCodeCid,
|
2019-11-26 21:58:43 +00:00
|
|
|
Balance: types.BigInt{balance.Int},
|
2019-10-17 04:53:25 +00:00
|
|
|
Head: vm.EmptyObjectCid,
|
|
|
|
}
|
|
|
|
if err := tree.SetActor(actors.BurntFundsAddress, ntwkact); err != nil {
|
2019-11-22 16:20:56 +00:00
|
|
|
return nil, nil, xerrors.Errorf("set network actor: %w", err)
|
2019-10-17 04:53:25 +00:00
|
|
|
}
|
|
|
|
return &actorWrapper{*ntwkact}, s.storage, s.flush(tree)
|
|
|
|
default:
|
2019-11-22 18:31:15 +00:00
|
|
|
return nil, nil, xerrors.Errorf("%v is not a singleton actor address", addr)
|
2019-10-17 04:53:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-27 22:17:08 +00:00
|
|
|
func (s *StateWrapper) Sign(ctx context.Context, addr address.Address, data []byte) (*vtypes.Signature, error) {
|
2019-11-26 21:58:43 +00:00
|
|
|
sig, err := s.keys.Sign(ctx, addr, data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &vtypes.Signature{
|
|
|
|
Type: sig.Type,
|
|
|
|
Data: sig.Data,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-10-17 04:53:25 +00:00
|
|
|
func (s *StateWrapper) Signer() *keyStore {
|
|
|
|
return s.keys
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flushes a state tree to storage and sets this state's root to that tree's root CID.
|
|
|
|
func (s *StateWrapper) flush(tree *state.StateTree) (err error) {
|
2020-01-22 20:56:40 +00:00
|
|
|
s.stateRoot, err = tree.Flush(context.TODO())
|
2019-10-17 04:53:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Key store
|
|
|
|
//
|
|
|
|
type keyStore struct {
|
|
|
|
// Private keys by address
|
2020-02-27 22:17:08 +00:00
|
|
|
keys map[address.Address]vtypes.KeyInfo
|
2019-10-17 04:53:25 +00:00
|
|
|
// Seed for deterministic key generation.
|
|
|
|
seed int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func newKeyStore() *keyStore {
|
|
|
|
return &keyStore{
|
2020-02-27 22:17:08 +00:00
|
|
|
keys: make(map[address.Address]vtypes.KeyInfo),
|
2019-10-17 04:53:25 +00:00
|
|
|
seed: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-27 22:17:08 +00:00
|
|
|
func (s *keyStore) NewAddress() (address.Address, error) {
|
2019-10-17 04:53:25 +00:00
|
|
|
randSrc := rand.New(rand.NewSource(s.seed))
|
|
|
|
prv, err := crypto.GenerateKeyFromSeed(randSrc)
|
|
|
|
if err != nil {
|
2020-02-27 22:17:08 +00:00
|
|
|
return address.Undef, err
|
2019-10-17 04:53:25 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 21:58:43 +00:00
|
|
|
vki := vtypes.KeyInfo{
|
2019-10-17 04:53:25 +00:00
|
|
|
PrivateKey: prv,
|
|
|
|
Type: types.KTSecp256k1,
|
|
|
|
}
|
2019-11-26 21:58:43 +00:00
|
|
|
key, err := wallet.NewKey(types.KeyInfo{
|
|
|
|
Type: vki.Type,
|
|
|
|
PrivateKey: vki.PrivateKey,
|
|
|
|
})
|
2019-10-17 04:53:25 +00:00
|
|
|
if err != nil {
|
2020-02-27 22:17:08 +00:00
|
|
|
return address.Undef, err
|
2019-10-17 04:53:25 +00:00
|
|
|
}
|
2020-02-27 22:17:08 +00:00
|
|
|
vaddr, err := address.NewFromBytes(key.Address.Bytes())
|
2019-11-26 21:58:43 +00:00
|
|
|
if err != nil {
|
2020-02-27 22:17:08 +00:00
|
|
|
return address.Undef, err
|
2019-11-26 21:58:43 +00:00
|
|
|
}
|
|
|
|
s.keys[vaddr] = vki
|
2019-10-17 04:53:25 +00:00
|
|
|
s.seed++
|
2020-02-27 22:17:08 +00:00
|
|
|
return address.NewFromBytes(key.Address.Bytes())
|
2019-10-17 04:53:25 +00:00
|
|
|
}
|
|
|
|
|
2020-02-27 22:17:08 +00:00
|
|
|
func (s *keyStore) Sign(ctx context.Context, addr address.Address, data []byte) (*types.Signature, error) {
|
2019-10-17 04:53:25 +00:00
|
|
|
ki, ok := s.keys[addr]
|
|
|
|
if !ok {
|
|
|
|
return &types.Signature{}, fmt.Errorf("unknown address %v", addr)
|
|
|
|
}
|
2019-11-26 21:58:43 +00:00
|
|
|
b2sum := blake2b.Sum256(data)
|
|
|
|
digest, err := crypto.Sign(ki.PrivateKey, b2sum[:])
|
2019-10-17 04:53:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return &types.Signature{}, err
|
|
|
|
}
|
|
|
|
return &types.Signature{
|
|
|
|
Type: types.KTSecp256k1,
|
|
|
|
Data: digest,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Storage
|
|
|
|
//
|
|
|
|
|
|
|
|
type directStorage struct {
|
2020-02-04 22:19:05 +00:00
|
|
|
cst cbor.IpldStore
|
2019-10-17 04:53:25 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 21:58:43 +00:00
|
|
|
func (d *directStorage) Get(c cid.Cid, out interface{}) error {
|
|
|
|
if err := d.cst.Get(context.TODO(), c, out.(cbg.CBORUnmarshaler)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2019-10-17 04:53:25 +00:00
|
|
|
}
|
|
|
|
|
2020-02-27 22:17:08 +00:00
|
|
|
*/
|