2019-06-25 11:42:17 +00:00
|
|
|
package modules
|
2019-07-01 10:18:00 +00:00
|
|
|
|
|
|
|
import (
|
2019-07-08 13:36:43 +00:00
|
|
|
"context"
|
2019-07-23 20:37:06 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2019-07-12 09:59:18 +00:00
|
|
|
"path/filepath"
|
2019-07-08 15:14:36 +00:00
|
|
|
|
2019-07-24 01:16:17 +00:00
|
|
|
"github.com/gbrlsnchs/jwt/v3"
|
2019-07-24 21:10:27 +00:00
|
|
|
|
2019-07-08 13:36:43 +00:00
|
|
|
"github.com/ipfs/go-bitswap"
|
|
|
|
"github.com/ipfs/go-bitswap/network"
|
2019-07-16 16:02:51 +00:00
|
|
|
"github.com/ipfs/go-blockservice"
|
2019-07-10 15:38:35 +00:00
|
|
|
"github.com/ipfs/go-datastore"
|
2019-07-16 16:02:51 +00:00
|
|
|
"github.com/ipfs/go-datastore/namespace"
|
|
|
|
"github.com/ipfs/go-filestore"
|
2019-07-08 13:36:43 +00:00
|
|
|
blockstore "github.com/ipfs/go-ipfs-blockstore"
|
|
|
|
exchange "github.com/ipfs/go-ipfs-exchange-interface"
|
2019-07-12 09:59:18 +00:00
|
|
|
offline "github.com/ipfs/go-ipfs-exchange-offline"
|
|
|
|
ipld "github.com/ipfs/go-ipld-format"
|
2019-07-03 17:39:07 +00:00
|
|
|
logging "github.com/ipfs/go-log"
|
2019-07-16 16:02:51 +00:00
|
|
|
"github.com/ipfs/go-merkledag"
|
2019-07-08 13:36:43 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/host"
|
2019-07-01 10:18:00 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peerstore"
|
2019-07-08 13:36:43 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/routing"
|
2019-07-01 10:18:00 +00:00
|
|
|
record "github.com/libp2p/go-libp2p-record"
|
2019-07-08 13:36:43 +00:00
|
|
|
"go.uber.org/fx"
|
2019-07-24 01:16:17 +00:00
|
|
|
"golang.org/x/xerrors"
|
2019-07-08 15:14:36 +00:00
|
|
|
|
2019-07-24 01:16:17 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/api"
|
2019-07-08 15:14:36 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain"
|
2019-07-18 16:10:53 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/types"
|
2019-07-08 15:14:36 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/node/modules/helpers"
|
2019-07-10 15:38:35 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/node/repo"
|
2019-07-01 10:18:00 +00:00
|
|
|
)
|
|
|
|
|
2019-07-03 17:39:07 +00:00
|
|
|
var log = logging.Logger("modules")
|
|
|
|
|
2019-07-08 13:36:43 +00:00
|
|
|
type Genesis *chain.BlockHeader
|
|
|
|
|
2019-07-01 10:18:00 +00:00
|
|
|
// RecordValidator provides namesys compatible routing record validator
|
|
|
|
func RecordValidator(ps peerstore.Peerstore) record.Validator {
|
|
|
|
return record.NamespacedValidator{
|
2019-07-01 20:00:22 +00:00
|
|
|
"pk": record.PublicKeyValidator{},
|
2019-07-01 10:18:00 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-08 13:36:43 +00:00
|
|
|
|
|
|
|
func Bitswap(mctx helpers.MetricsCtx, lc fx.Lifecycle, host host.Host, rt routing.Routing, bs blockstore.GCBlockstore) exchange.Interface {
|
|
|
|
bitswapNetwork := network.NewFromIpfsHost(host, rt)
|
|
|
|
exch := bitswap.New(helpers.LifecycleCtx(mctx, lc), bitswapNetwork, bs)
|
|
|
|
lc.Append(fx.Hook{
|
|
|
|
OnStop: func(ctx context.Context) error {
|
|
|
|
return exch.Close()
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return exch
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetGenesis(cs *chain.ChainStore, g Genesis) error {
|
|
|
|
return cs.SetGenesis(g)
|
|
|
|
}
|
2019-07-10 15:38:35 +00:00
|
|
|
|
|
|
|
func LockedRepo(lr repo.LockedRepo) func(lc fx.Lifecycle) repo.LockedRepo {
|
|
|
|
return func(lc fx.Lifecycle) repo.LockedRepo {
|
|
|
|
lc.Append(fx.Hook{
|
|
|
|
OnStop: func(_ context.Context) error {
|
|
|
|
return lr.Close()
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return lr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-18 16:10:53 +00:00
|
|
|
func KeyStore(lr repo.LockedRepo) (types.KeyStore, error) {
|
|
|
|
return lr.KeyStore()
|
|
|
|
}
|
|
|
|
|
2019-07-23 20:23:44 +00:00
|
|
|
const JWTSecretName = "auth-jwt-private"
|
|
|
|
|
|
|
|
type APIAlg jwt.HMACSHA
|
|
|
|
|
2019-07-23 20:37:06 +00:00
|
|
|
type jwtPayload struct {
|
|
|
|
Allow []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func APISecret(keystore types.KeyStore, lr repo.LockedRepo) (*APIAlg, error) {
|
2019-07-23 20:23:44 +00:00
|
|
|
key, err := keystore.Get(JWTSecretName)
|
|
|
|
if err != nil {
|
2019-07-23 20:37:06 +00:00
|
|
|
log.Warn("Generating new API secret")
|
|
|
|
|
|
|
|
sk, err := ioutil.ReadAll(io.LimitReader(rand.Reader, 32))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
key = types.KeyInfo{
|
|
|
|
Type: "jwt-hmac-secret",
|
|
|
|
PrivateKey: sk,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := keystore.Put(JWTSecretName, key); err != nil {
|
|
|
|
return nil, xerrors.Errorf("writing API secret: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: make this configurable
|
|
|
|
p := jwtPayload{
|
|
|
|
Allow: api.AllPermissions,
|
|
|
|
}
|
|
|
|
|
|
|
|
cliToken, err := jwt.Sign(&p, jwt.NewHS256(key.PrivateKey))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := lr.SetAPIToken(cliToken); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-23 20:23:44 +00:00
|
|
|
}
|
2019-07-23 20:37:06 +00:00
|
|
|
|
2019-07-23 20:23:44 +00:00
|
|
|
return (*APIAlg)(jwt.NewHS256(key.PrivateKey)), nil
|
|
|
|
}
|
|
|
|
|
2019-07-10 15:38:35 +00:00
|
|
|
func Datastore(r repo.LockedRepo) (datastore.Batching, error) {
|
|
|
|
return r.Datastore("/metadata")
|
|
|
|
}
|
|
|
|
|
|
|
|
func Blockstore(r repo.LockedRepo) (blockstore.Blockstore, error) {
|
|
|
|
blocks, err := r.Datastore("/blocks")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
bs := blockstore.NewBlockstore(blocks)
|
|
|
|
return blockstore.NewIdStore(bs), nil
|
|
|
|
}
|
2019-07-12 09:59:18 +00:00
|
|
|
|
2019-07-12 10:44:01 +00:00
|
|
|
func ClientFstore(r repo.LockedRepo) (*filestore.Filestore, error) {
|
2019-07-12 09:59:18 +00:00
|
|
|
clientds, err := r.Datastore("/client")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
blocks := namespace.Wrap(clientds, datastore.NewKey("blocks"))
|
|
|
|
|
|
|
|
fm := filestore.NewFileManager(clientds, filepath.Dir(r.Path()))
|
2019-07-12 10:17:44 +00:00
|
|
|
fm.AllowFiles = true
|
|
|
|
// TODO: fm.AllowUrls (needs more code in client import)
|
2019-07-12 09:59:18 +00:00
|
|
|
|
|
|
|
bs := blockstore.NewBlockstore(blocks)
|
2019-07-12 10:44:01 +00:00
|
|
|
return filestore.NewFilestore(bs, fm), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ClientDAG(lc fx.Lifecycle, fstore *filestore.Filestore) ipld.DAGService {
|
2019-07-12 09:59:18 +00:00
|
|
|
ibs := blockstore.NewIdStore(fstore)
|
|
|
|
bsvc := blockservice.New(ibs, offline.Exchange(ibs))
|
|
|
|
dag := merkledag.NewDAGService(bsvc)
|
|
|
|
|
|
|
|
lc.Append(fx.Hook{
|
|
|
|
OnStop: func(_ context.Context) error {
|
|
|
|
return bsvc.Close()
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2019-07-12 10:44:01 +00:00
|
|
|
return dag
|
2019-07-12 09:59:18 +00:00
|
|
|
}
|
2019-07-24 21:10:27 +00:00
|
|
|
|
|
|
|
func ChainStore(lc fx.Lifecycle, bs blockstore.Blockstore, ds datastore.Batching) *chain.ChainStore {
|
|
|
|
chain := chain.NewChainStore(bs, ds)
|
|
|
|
|
|
|
|
lc.Append(fx.Hook{
|
|
|
|
OnStart: func(ctx context.Context) error {
|
|
|
|
return chain.Load()
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return chain
|
|
|
|
}
|