Skip to content

Commit

Permalink
wallet: Move key.go into separate pkg to avoid import loops
Browse files Browse the repository at this point in the history
  • Loading branch information
magik6k committed Jun 14, 2022
1 parent d48b629 commit 6afb43a
Show file tree
Hide file tree
Showing 19 changed files with 61 additions and 59 deletions.
4 changes: 2 additions & 2 deletions chain/gen/genesis/miners.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import (
"github.com/filecoin-project/lotus/chain/store"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/vm"
"github.com/filecoin-project/lotus/chain/wallet"
"github.com/filecoin-project/lotus/chain/wallet/key"
"github.com/filecoin-project/lotus/genesis"
"github.com/filecoin-project/lotus/lib/sigs"
)
Expand Down Expand Up @@ -240,7 +240,7 @@ func SetupStorageMiners(ctx context.Context, cs *store.ChainStore, sys vm.Syscal
return cid.Undef, fmt.Errorf("failed to marshal proposal: %w", err)
}

sig, err := sigs.Sign(wallet.ActSigType(preseal.DealClientKey.Type), preseal.DealClientKey.PrivateKey, buf)
sig, err := sigs.Sign(key.ActSigType(preseal.DealClientKey.Type), preseal.DealClientKey.PrivateKey, buf)
if err != nil {
return cid.Undef, fmt.Errorf("failed to sign proposal: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion chain/wallet/key.go → chain/wallet/key/key.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package wallet
package key

import (
"golang.org/x/xerrors"
Expand Down
21 changes: 11 additions & 10 deletions chain/wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/wallet/key"
"github.com/filecoin-project/lotus/lib/sigs"
_ "github.com/filecoin-project/lotus/lib/sigs/bls" // enable bls signatures
_ "github.com/filecoin-project/lotus/lib/sigs/secp" // enable secp signatures
Expand All @@ -28,7 +29,7 @@ const (
)

type LocalWallet struct {
keys map[address.Address]*Key
keys map[address.Address]*key.Key
keystore types.KeyStore

lk sync.Mutex
Expand All @@ -41,15 +42,15 @@ type Default interface {

func NewWallet(keystore types.KeyStore) (*LocalWallet, error) {
w := &LocalWallet{
keys: make(map[address.Address]*Key),
keys: make(map[address.Address]*key.Key),
keystore: keystore,
}

return w, nil
}

func KeyWallet(keys ...*Key) *LocalWallet {
m := make(map[address.Address]*Key)
func KeyWallet(keys ...*key.Key) *LocalWallet {
m := make(map[address.Address]*key.Key)
for _, key := range keys {
m[key.Address] = key
}
Expand All @@ -68,10 +69,10 @@ func (w *LocalWallet) WalletSign(ctx context.Context, addr address.Address, msg
return nil, xerrors.Errorf("signing using key '%s': %w", addr.String(), types.ErrKeyInfoNotFound)
}

return sigs.Sign(ActSigType(ki.Type), ki.PrivateKey, msg)
return sigs.Sign(key.ActSigType(ki.Type), ki.PrivateKey, msg)
}

func (w *LocalWallet) findKey(addr address.Address) (*Key, error) {
func (w *LocalWallet) findKey(addr address.Address) (*key.Key, error) {
w.lk.Lock()
defer w.lk.Unlock()

Expand All @@ -91,7 +92,7 @@ func (w *LocalWallet) findKey(addr address.Address) (*Key, error) {
}
return nil, xerrors.Errorf("getting from keystore: %w", err)
}
k, err = NewKey(ki)
k, err = key.NewKey(ki)
if err != nil {
return nil, xerrors.Errorf("decoding from keystore: %w", err)
}
Expand Down Expand Up @@ -149,7 +150,7 @@ func (w *LocalWallet) WalletImport(ctx context.Context, ki *types.KeyInfo) (addr
w.lk.Lock()
defer w.lk.Unlock()

k, err := NewKey(*ki)
k, err := key.NewKey(*ki)
if err != nil {
return address.Undef, xerrors.Errorf("failed to make key: %w", err)
}
Expand Down Expand Up @@ -203,7 +204,7 @@ func (w *LocalWallet) GetDefault() (address.Address, error) {
return address.Undef, xerrors.Errorf("failed to get default key: %w", err)
}

k, err := NewKey(ki)
k, err := key.NewKey(ki)
if err != nil {
return address.Undef, xerrors.Errorf("failed to read default key from keystore: %w", err)
}
Expand Down Expand Up @@ -237,7 +238,7 @@ func (w *LocalWallet) WalletNew(ctx context.Context, typ types.KeyType) (address
w.lk.Lock()
defer w.lk.Unlock()

k, err := GenerateKey(typ)
k, err := key.GenerateKey(typ)
if err != nil {
return address.Undef, err
}
Expand Down
14 changes: 7 additions & 7 deletions cmd/lotus-seed/seed/seed.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/filecoin-project/specs-storage/storage"

"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/wallet"
"github.com/filecoin-project/lotus/chain/wallet/key"
"github.com/filecoin-project/lotus/extern/sector-storage/ffiwrapper"
"github.com/filecoin-project/lotus/extern/sector-storage/ffiwrapper/basicfs"
"github.com/filecoin-project/lotus/extern/sector-storage/stores"
Expand All @@ -36,7 +36,7 @@ import (

var log = logging.Logger("preseal")

func PreSeal(maddr address.Address, spt abi.RegisteredSealProof, offset abi.SectorNumber, sectors int, sbroot string, preimage []byte, key *types.KeyInfo, fakeSectors bool) (*genesis.Miner, *types.KeyInfo, error) {
func PreSeal(maddr address.Address, spt abi.RegisteredSealProof, offset abi.SectorNumber, sectors int, sbroot string, preimage []byte, ki *types.KeyInfo, fakeSectors bool) (*genesis.Miner, *types.KeyInfo, error) {
mid, err := address.IDFromAddress(maddr)
if err != nil {
return nil, nil, err
Expand Down Expand Up @@ -84,14 +84,14 @@ func PreSeal(maddr address.Address, spt abi.RegisteredSealProof, offset abi.Sect
sealedSectors = append(sealedSectors, preseal)
}

var minerAddr *wallet.Key
if key != nil {
minerAddr, err = wallet.NewKey(*key)
var minerAddr *key.Key
if ki != nil {
minerAddr, err = key.NewKey(*ki)
if err != nil {
return nil, nil, err
}
} else {
minerAddr, err = wallet.GenerateKey(types.KTBLS)
minerAddr, err = key.GenerateKey(types.KTBLS)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -249,7 +249,7 @@ func WriteGenesisMiner(maddr address.Address, sbroot string, gm *genesis.Miner,
return nil
}

func createDeals(m *genesis.Miner, k *wallet.Key, maddr address.Address, ssize abi.SectorSize) error {
func createDeals(m *genesis.Miner, k *key.Key, maddr address.Address, ssize abi.SectorSize) error {
for i, sector := range m.Sectors {
label, err := market8.NewLabelFromString(fmt.Sprintf("%d", i))
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions cmd/lotus-shed/keyinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/wallet"
"github.com/filecoin-project/lotus/chain/wallet/key"
_ "github.com/filecoin-project/lotus/lib/sigs/bls"
_ "github.com/filecoin-project/lotus/lib/sigs/secp"
"github.com/filecoin-project/lotus/node/modules"
Expand Down Expand Up @@ -317,7 +318,7 @@ var keyinfoInfoCmd = &cli.Command{
case types.KTSecp256k1, types.KTBLS:
kio.Type = keyInfo.Type

key, err := wallet.NewKey(keyInfo)
key, err := key.NewKey(keyInfo)
if err != nil {
return err
}
Expand Down Expand Up @@ -402,7 +403,7 @@ var keyinfoNewCmd = &cli.Command{

break
case types.KTSecp256k1, types.KTBLS:
key, err := wallet.GenerateKey(keyType)
key, err := key.GenerateKey(keyType)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions genesis/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
market8 "github.com/filecoin-project/go-state-types/builtin/v8/market"
"github.com/filecoin-project/go-state-types/network"

"github.com/filecoin-project/lotus/chain/wallet"
"github.com/filecoin-project/lotus/chain/wallet/key"
)

type ActorType string
Expand All @@ -26,7 +26,7 @@ type PreSeal struct {
CommD cid.Cid
SectorID abi.SectorNumber
Deal market8.DealProposal
DealClientKey *wallet.Key
DealClientKey *key.Key
ProofType abi.RegisteredSealProof
}

Expand Down
4 changes: 2 additions & 2 deletions itests/deals_publish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain/actors/builtin/market"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/wallet"
"github.com/filecoin-project/lotus/chain/wallet/key"
"github.com/filecoin-project/lotus/itests/kit"
"github.com/filecoin-project/lotus/markets/storageadapter"
"github.com/filecoin-project/lotus/node"
Expand All @@ -41,7 +41,7 @@ func TestPublishDealsBatching(t *testing.T) {

kit.QuietMiningLogs()

publisherKey, err := wallet.GenerateKey(types.KTSecp256k1)
publisherKey, err := key.GenerateKey(types.KTSecp256k1)
require.NoError(t, err)

opts := node.Options(
Expand Down
8 changes: 4 additions & 4 deletions itests/deals_retry_deal_no_funds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/filecoin-project/go-state-types/abi"

"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/wallet"
"github.com/filecoin-project/lotus/chain/wallet/key"
"github.com/filecoin-project/lotus/itests/kit"
"github.com/filecoin-project/lotus/markets/storageadapter"
"github.com/filecoin-project/lotus/node"
Expand Down Expand Up @@ -42,7 +42,7 @@ func TestDealsRetryLackOfFunds(t *testing.T) {
// Allow 8MB sectors
eightMBSectorsOpt := kit.SectorSize(8 << 20)

publishStorageDealKey, err := wallet.GenerateKey(types.KTSecp256k1)
publishStorageDealKey, err := key.GenerateKey(types.KTSecp256k1)
require.NoError(t, err)

opts := node.Options(
Expand Down Expand Up @@ -118,7 +118,7 @@ func TestDealsRetryLackOfFunds_blockInPublishDeal(t *testing.T) {
// Allow 8MB sectors
eightMBSectorsOpt := kit.SectorSize(8 << 20)

publishStorageDealKey, err := wallet.GenerateKey(types.KTSecp256k1)
publishStorageDealKey, err := key.GenerateKey(types.KTSecp256k1)
require.NoError(t, err)

opts := node.Options(
Expand Down Expand Up @@ -191,7 +191,7 @@ func TestDealsRetryLackOfFunds_belowLimit(t *testing.T) {
// Allow 8MB sectors
eightMBSectorsOpt := kit.SectorSize(8 << 20)

publishStorageDealKey, err := wallet.GenerateKey(types.KTSecp256k1)
publishStorageDealKey, err := key.GenerateKey(types.KTSecp256k1)
require.NoError(t, err)

opts := node.Options(
Expand Down
6 changes: 3 additions & 3 deletions itests/kit/ensemble.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import (
"github.com/filecoin-project/lotus/chain/messagepool"
"github.com/filecoin-project/lotus/chain/stmgr"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/wallet"
"github.com/filecoin-project/lotus/chain/wallet/key"
"github.com/filecoin-project/lotus/cmd/lotus-seed/seed"
"github.com/filecoin-project/lotus/cmd/lotus-worker/sealworker"
sectorstorage "github.com/filecoin-project/lotus/extern/sector-storage"
Expand Down Expand Up @@ -183,7 +183,7 @@ func (n *Ensemble) FullNode(full *TestFullNode, opts ...NodeOpt) *Ensemble {
require.NoError(n.t, err)
}

key, err := wallet.GenerateKey(types.KTBLS)
key, err := key.GenerateKey(types.KTBLS)
require.NoError(n.t, err)

if !n.bootstrapped && !options.balance.IsZero() {
Expand Down Expand Up @@ -256,7 +256,7 @@ func (n *Ensemble) Miner(minerNode *TestMiner, full *TestFullNode, opts ...NodeO
genm.PeerId = peerId

// create an owner key, and assign it some FIL.
ownerKey, err = wallet.NewKey(*k)
ownerKey, err = key.NewKey(*k)
require.NoError(n.t, err)

genacc := genesis.Actor{
Expand Down
8 changes: 4 additions & 4 deletions itests/kit/ensemble_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (

"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/stmgr"
"github.com/filecoin-project/lotus/chain/wallet"
"github.com/filecoin-project/lotus/chain/wallet/key"
)

type EnsembleOpt func(opts *ensembleOpts) error

type genesisAccount struct {
key *wallet.Key
key *key.Key
initialBalance abi.TokenAmount
}

Expand Down Expand Up @@ -47,7 +47,7 @@ func MockProofs() EnsembleOpt {

// RootVerifier specifies the key to be enlisted as the verified registry root,
// as well as the initial balance to be attributed during genesis.
func RootVerifier(key *wallet.Key, balance abi.TokenAmount) EnsembleOpt {
func RootVerifier(key *key.Key, balance abi.TokenAmount) EnsembleOpt {
return func(opts *ensembleOpts) error {
opts.verifiedRoot.key = key
opts.verifiedRoot.initialBalance = balance
Expand All @@ -56,7 +56,7 @@ func RootVerifier(key *wallet.Key, balance abi.TokenAmount) EnsembleOpt {
}

// Account sets up an account at genesis with the specified key and balance.
func Account(key *wallet.Key, balance abi.TokenAmount) EnsembleOpt {
func Account(key *key.Key, balance abi.TokenAmount) EnsembleOpt {
return func(opts *ensembleOpts) error {
opts.accounts = append(opts.accounts, genesisAccount{
key: key,
Expand Down
4 changes: 2 additions & 2 deletions itests/kit/node_full.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/api/v1api"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/wallet"
"github.com/filecoin-project/lotus/chain/wallet/key"
)

// TestFullNode represents a full node enrolled in an Ensemble.
Expand All @@ -27,7 +27,7 @@ type TestFullNode struct {
// ListenAddr is the address on which an API server is listening, if an
// API server is created for this Node.
ListenAddr multiaddr.Multiaddr
DefaultKey *wallet.Key
DefaultKey *key.Key

options nodeOpts
}
Expand Down
4 changes: 2 additions & 2 deletions itests/kit/node_miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/wallet"
"github.com/filecoin-project/lotus/chain/wallet/key"
"github.com/filecoin-project/lotus/extern/sector-storage/stores"
"github.com/filecoin-project/lotus/extern/sector-storage/storiface"
sealing "github.com/filecoin-project/lotus/extern/storage-sealing"
Expand Down Expand Up @@ -72,7 +72,7 @@ type TestMiner struct {
ListenAddr multiaddr.Multiaddr

ActorAddr address.Address
OwnerKey *wallet.Key
OwnerKey *key.Key
MineOne func(context.Context, miner.MineReq) error
Stop func(context.Context) error

Expand Down
6 changes: 3 additions & 3 deletions itests/kit/node_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/wallet"
"github.com/filecoin-project/lotus/chain/wallet/key"
"github.com/filecoin-project/lotus/extern/sector-storage/sealtasks"
"github.com/filecoin-project/lotus/extern/sector-storage/stores"
"github.com/filecoin-project/lotus/extern/storage-sealing/sealiface"
Expand All @@ -31,7 +31,7 @@ type nodeOpts struct {
lite bool
sectors int
rpc bool
ownerKey *wallet.Key
ownerKey *key.Key
extraNodeOpts []node.Option

subsystems MinerSubsystem
Expand Down Expand Up @@ -165,7 +165,7 @@ func ThroughRPC() NodeOpt {

// OwnerAddr sets the owner address of a miner. Only relevant when creating
// a miner.
func OwnerAddr(wk *wallet.Key) NodeOpt {
func OwnerAddr(wk *key.Key) NodeOpt {
return func(opts *nodeOpts) error {
opts.ownerKey = wk
return nil
Expand Down
Loading

0 comments on commit 6afb43a

Please sign in to comment.