Store libp2p key in keystore
This commit is contained in:
@@ -14,7 +14,6 @@ import (
|
||||
badger "github.com/ipfs/go-ds-badger"
|
||||
fslock "github.com/ipfs/go-fs-lock"
|
||||
logging "github.com/ipfs/go-log"
|
||||
"github.com/libp2p/go-libp2p-core/crypto"
|
||||
"github.com/mitchellh/go-homedir"
|
||||
"github.com/multiformats/go-base32"
|
||||
"github.com/multiformats/go-multiaddr"
|
||||
@@ -224,58 +223,6 @@ func (fsr *fsLockedRepo) Config() (*config.Root, error) {
|
||||
return config.FromFile(fsr.join(fsConfig))
|
||||
}
|
||||
|
||||
func (fsr *fsLockedRepo) Libp2pIdentity() (crypto.PrivKey, error) {
|
||||
if err := fsr.stillValid(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
kpath := fsr.join(fsLibp2pKey)
|
||||
stat, err := os.Stat(kpath)
|
||||
|
||||
if os.IsNotExist(err) {
|
||||
pk, err := genLibp2pKey()
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("could not generate private key: %w", err)
|
||||
}
|
||||
pkb, err := pk.Bytes()
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("could not serialize private key: %w", err)
|
||||
}
|
||||
err = ioutil.WriteFile(kpath, pkb, 0600)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("could not write private key: %w", err)
|
||||
}
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
stat, err = os.Stat(kpath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if stat.Mode()&0066 != 0 {
|
||||
return nil, xerrors.New("libp2p identity has too wide access permissions, " +
|
||||
fsLibp2pKey + " should have permission 0600")
|
||||
}
|
||||
|
||||
f, err := os.Open(kpath)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("could not open private key file: %w", err)
|
||||
}
|
||||
defer f.Close() //nolint: errcheck // read-only op
|
||||
|
||||
pkbytes, err := ioutil.ReadAll(f)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("could not read private key file: %w", err)
|
||||
}
|
||||
|
||||
pk, err := crypto.UnmarshalPrivateKey(pkbytes)
|
||||
if err != nil {
|
||||
return nil, xerrors.Errorf("could not unmarshal private key: %w", err)
|
||||
}
|
||||
return pk, nil
|
||||
}
|
||||
|
||||
func (fsr *fsLockedRepo) SetAPIEndpoint(ma multiaddr.Multiaddr) error {
|
||||
if err := fsr.stillValid(); err != nil {
|
||||
return err
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"errors"
|
||||
|
||||
"github.com/ipfs/go-datastore"
|
||||
"github.com/libp2p/go-libp2p-core/crypto"
|
||||
"github.com/multiformats/go-multiaddr"
|
||||
|
||||
"github.com/filecoin-project/go-lotus/chain/types"
|
||||
@@ -42,9 +41,6 @@ type LockedRepo interface {
|
||||
// Returns config in this repo
|
||||
Config() (*config.Root, error)
|
||||
|
||||
// Libp2pIdentity returns private key for libp2p indentity
|
||||
Libp2pIdentity() (crypto.PrivKey, error)
|
||||
|
||||
// SetAPIEndpoint sets the endpoint of the current API
|
||||
// so it can be read by API clients
|
||||
SetAPIEndpoint(multiaddr.Multiaddr) error
|
||||
|
||||
+3
-30
@@ -1,13 +1,11 @@
|
||||
package repo
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"sync"
|
||||
|
||||
"github.com/ipfs/go-datastore"
|
||||
"github.com/ipfs/go-datastore/namespace"
|
||||
dssync "github.com/ipfs/go-datastore/sync"
|
||||
"github.com/libp2p/go-libp2p-core/crypto"
|
||||
"github.com/multiformats/go-multiaddr"
|
||||
"golang.org/x/xerrors"
|
||||
|
||||
@@ -27,7 +25,6 @@ type MemRepo struct {
|
||||
|
||||
datastore datastore.Datastore
|
||||
configF func() *config.Root
|
||||
libp2pKey crypto.PrivKey
|
||||
keystore map[string]types.KeyInfo
|
||||
}
|
||||
|
||||
@@ -46,18 +43,9 @@ var _ Repo = &MemRepo{}
|
||||
|
||||
// MemRepoOptions contains options for memory repo
|
||||
type MemRepoOptions struct {
|
||||
Ds datastore.Datastore
|
||||
ConfigF func() *config.Root
|
||||
Libp2pKey crypto.PrivKey
|
||||
KeyStore map[string]types.KeyInfo
|
||||
}
|
||||
|
||||
func genLibp2pKey() (crypto.PrivKey, error) {
|
||||
pk, _, err := crypto.GenerateEd25519Key(rand.Reader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pk, nil
|
||||
Ds datastore.Datastore
|
||||
ConfigF func() *config.Root
|
||||
KeyStore map[string]types.KeyInfo
|
||||
}
|
||||
|
||||
// NewMemory creates new memory based repo with provided options.
|
||||
@@ -73,13 +61,6 @@ func NewMemory(opts *MemRepoOptions) *MemRepo {
|
||||
if opts.Ds == nil {
|
||||
opts.Ds = dssync.MutexWrap(datastore.NewMapDatastore())
|
||||
}
|
||||
if opts.Libp2pKey == nil {
|
||||
pk, err := genLibp2pKey()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
opts.Libp2pKey = pk
|
||||
}
|
||||
if opts.KeyStore == nil {
|
||||
opts.KeyStore = make(map[string]types.KeyInfo)
|
||||
}
|
||||
@@ -89,7 +70,6 @@ func NewMemory(opts *MemRepoOptions) *MemRepo {
|
||||
|
||||
datastore: opts.Ds,
|
||||
configF: opts.ConfigF,
|
||||
libp2pKey: opts.Libp2pKey,
|
||||
keystore: opts.KeyStore,
|
||||
}
|
||||
}
|
||||
@@ -170,13 +150,6 @@ func (lmem *lockedMemRepo) Config() (*config.Root, error) {
|
||||
return lmem.mem.configF(), nil
|
||||
}
|
||||
|
||||
func (lmem *lockedMemRepo) Libp2pIdentity() (crypto.PrivKey, error) {
|
||||
if err := lmem.checkToken(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return lmem.mem.libp2pKey, nil
|
||||
}
|
||||
|
||||
func (lmem *lockedMemRepo) SetAPIEndpoint(ma multiaddr.Multiaddr) error {
|
||||
if err := lmem.checkToken(); err != nil {
|
||||
return err
|
||||
|
||||
@@ -47,10 +47,6 @@ func basicTest(t *testing.T, repo Repo) {
|
||||
assert.NoError(t, err, "setting multiaddr shouldn't error")
|
||||
assert.Equal(t, ma, apima, "returned API multiaddr should be the same")
|
||||
|
||||
iden, err := lrepo.Libp2pIdentity()
|
||||
assert.NotNil(t, iden, "identity is not nil")
|
||||
assert.NoError(t, err, "identity should not error")
|
||||
|
||||
cfg, err := lrepo.Config()
|
||||
assert.Equal(t, config.Default(), cfg, "there should be a default config")
|
||||
assert.NoError(t, err, "config should not error")
|
||||
|
||||
Reference in New Issue
Block a user