Skip to content

Commit

Permalink
fix: mutex in account
Browse files Browse the repository at this point in the history
  • Loading branch information
glouvigny committed Mar 5, 2020
1 parent cd98fb8 commit 93d806f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 21 deletions.
14 changes: 14 additions & 0 deletions go/internal/account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/rand"
"encoding/hex"
"strings"
"sync"

"github.com/aead/ecdh"
"github.com/ipfs/go-ipfs/keystore"
Expand All @@ -17,6 +18,7 @@ import (

type Account struct {
ks keystore.Keystore
mu sync.Mutex
}

const (
Expand All @@ -30,16 +32,25 @@ const (

// AccountPrivKey returns the private key associated with the current account
func (a *Account) AccountPrivKey() (crypto.PrivKey, error) {
a.mu.Lock()
defer a.mu.Unlock()

return a.getOrGenerateNamedKey(keyAccount)
}

// AccountProofPrivKey returns the private key associated with the current account
func (a *Account) AccountProofPrivKey() (crypto.PrivKey, error) {
a.mu.Lock()
defer a.mu.Unlock()

return a.getOrGenerateNamedKey(keyAccountProof)
}

// DevicePrivKey returns the current device private key
func (a *Account) DevicePrivKey() (crypto.PrivKey, error) {
a.mu.Lock()
defer a.mu.Unlock()

return a.getOrGenerateNamedKey(keyDevice)
}

Expand Down Expand Up @@ -122,6 +133,9 @@ func (a *Account) getOrGenerateNamedKey(name string) (crypto.PrivKey, error) {
}

func (a *Account) getOrGenerateDeviceKeyForGroupDevice(pk crypto.PubKey) (crypto.PrivKey, error) {
a.mu.Lock()
defer a.mu.Unlock()

groupPKRaw, err := pk.Raw()
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion go/internal/ipfsutil/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func CreateRepo(dstore ipfs_datastore.Batching, opts *BuildOpts) (ipfs_repo.Repo
} else {
portOffsetBI, err := rand.Int(rand.Reader, big.NewInt(100))
if err != nil {
panic(err)
return nil, errcode.TODO.Wrap(err)
}

portOffset := portOffsetBI.Int64() % 100
Expand Down
23 changes: 3 additions & 20 deletions go/internal/ipfsutil/keystore_datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package ipfsutil

import (
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/query"
"github.com/ipfs/go-ipfs/keystore"
"github.com/libp2p/go-libp2p-core/crypto"

"berty.tech/berty/go/pkg/errcode"
)

type datastoreKeystore struct {
Expand Down Expand Up @@ -40,25 +41,7 @@ func (k *datastoreKeystore) Delete(name string) error {
}

func (k *datastoreKeystore) List() ([]string, error) {
// Not supported
res, err := k.ds.Query(query.Query{KeysOnly: true})
if err != nil {
return nil, err
}

result := []string(nil)

for {
val, next := res.NextSync()

result = append(result, val.Key)

if !next {
break
}
}

return result, nil
return nil, errcode.ErrNotImplemented
}

func NewDatastoreKeystore(ds datastore.Datastore) keystore.Keystore {
Expand Down

0 comments on commit 93d806f

Please sign in to comment.