Skip to content

Commit

Permalink
Merge pull request #1 from RTradeLtd/ds-pass
Browse files Browse the repository at this point in the history
Change Construction Of Krab [Breaking Change]
  • Loading branch information
postables authored Jun 18, 2019
2 parents 7bcdcdc + 4d89eae commit ebe8607
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 28 deletions.
32 changes: 7 additions & 25 deletions krab.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
"strings"

"github.com/RTradeLtd/crypto/v2"
datastore "github.com/ipfs/go-datastore"
ds "github.com/ipfs/go-datastore"
namespace "github.com/ipfs/go-datastore/namespace"
"github.com/ipfs/go-datastore/query"
badger "github.com/ipfs/go-ds-badger"
kstore "github.com/ipfs/go-ipfs-keystore"
ci "github.com/libp2p/go-libp2p-core/crypto"
)
Expand All @@ -19,29 +20,15 @@ var _ kstore.Keystore = (*Keystore)(nil)
// Keystore is used to manage an encrypted IPFS keystore
type Keystore struct {
em *crypto.EncryptManager
ds *badger.Datastore
}

// Opts is used to configure a Krab keystore
type Opts struct {
Passphrase string
DSPath string
ReadOnly bool
ds datastore.Batching
}

// NewKeystore is used to create a new krab ipfs keystore manager
func NewKeystore(opts Opts) (*Keystore, error) {
badgerOpts := &badger.DefaultOptions
if opts.ReadOnly {
badgerOpts.ReadOnly = opts.ReadOnly
}
ds, err := badger.NewDatastore(opts.DSPath, badgerOpts)
if err != nil {
return nil, err
}
func NewKeystore(ds datastore.Batching, passphrase string) (*Keystore, error) {
wrapDS := namespace.Wrap(ds, datastore.NewKey("/krabkeystore"))
return &Keystore{
em: crypto.NewEncryptManager(opts.Passphrase),
ds: ds,
em: crypto.NewEncryptManager(passphrase),
ds: wrapDS,
}, nil
}

Expand Down Expand Up @@ -127,8 +114,3 @@ func (km *Keystore) List() ([]string, error) {
}
return ids, nil
}

// Close is used to close our badger connection
func (km *Keystore) Close() error {
return km.ds.Close()
}
11 changes: 8 additions & 3 deletions krab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
ci "github.com/libp2p/go-libp2p-crypto"

"github.com/RTradeLtd/krab"
badger "github.com/ipfs/go-ds-badger"
)

const (
Expand All @@ -18,14 +19,18 @@ const (
func TestKrab(t *testing.T) {
defer func() {
if err := os.RemoveAll(dsPath); err != nil {
t.Fatal(err)
t.Error(err)
}
}()
km, err := krab.NewKeystore(krab.Opts{Passphrase: passphrase, DSPath: dsPath, ReadOnly: false})
ds, err := badger.NewDatastore(dsPath, &badger.DefaultOptions)
if err != nil {
t.Fatal(err)
}
defer ds.Close()
km, err := krab.NewKeystore(ds, passphrase)
if err != nil {
t.Fatal(err)
}
defer km.Close()
// this should fail
if has, err := km.Has(testKeyName); err == nil {
t.Fatal("key was found when it shouldn't have been")
Expand Down

0 comments on commit ebe8607

Please sign in to comment.