Skip to content

Commit

Permalink
feat: remove context Background
Browse files Browse the repository at this point in the history
Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com>
  • Loading branch information
gfanton committed Jan 13, 2022
1 parent 7153a23 commit 019860e
Show file tree
Hide file tree
Showing 20 changed files with 62 additions and 53 deletions.
2 changes: 1 addition & 1 deletion entry/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func CreateEntryWithIO(ctx context.Context, ipfsInstance core_iface.CoreAPI, ide
return nil, errmsg.ErrEntryNotHashable.Wrap(err)
}

signature, err := identity.Provider.Sign(identity, jsonBytes)
signature, err := identity.Provider.Sign(ctx, identity, jsonBytes)

if err != nil {
return nil, errmsg.ErrSigSign.Wrap(err)
Expand Down
23 changes: 12 additions & 11 deletions identityprovider/identities.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package identityprovider // import "berty.tech/go-ipfs-log/identityprovider"

import (
"context"
"encoding/hex"

"github.com/btcsuite/btcd/btcec"
Expand Down Expand Up @@ -32,8 +33,8 @@ func newIdentities(keyStore keystore.Interface) *Identities {
}
}

func (i *Identities) Sign(identity *Identity, data []byte) ([]byte, error) {
privKey, err := i.keyStore.GetKey(identity.ID)
func (i *Identities) Sign(ctx context.Context, identity *Identity, data []byte) ([]byte, error) {
privKey, err := i.keyStore.GetKey(ctx, identity.ID)
if err != nil {
return nil, errmsg.ErrKeyNotInKeystore.Wrap(err)
}
Expand Down Expand Up @@ -71,14 +72,14 @@ func compressedToUncompressedS256Key(pubKeyBytes []byte) ([]byte, error) {
}

// CreateIdentity creates a new Identity.
func (i *Identities) CreateIdentity(options *CreateIdentityOptions) (*Identity, error) {
func (i *Identities) CreateIdentity(ctx context.Context, options *CreateIdentityOptions) (*Identity, error) {
NewIdentityProvider, err := getHandlerFor(options.Type)
if err != nil {
return nil, errmsg.ErrIdentityProviderNotSupported.Wrap(err)
}

identityProvider := NewIdentityProvider(options)
id, err := identityProvider.GetID(options)
id, err := identityProvider.GetID(ctx, options)
if err != nil {
return nil, errmsg.ErrIdentityUnknown.Wrap(err)
}
Expand All @@ -90,7 +91,7 @@ func (i *Identities) CreateIdentity(options *CreateIdentityOptions) (*Identity,
// }
//}

publicKey, idSignature, err := i.signID(id)
publicKey, idSignature, err := i.signID(ctx, id)
if err != nil {
return nil, errmsg.ErrSigSign.Wrap(err)
}
Expand All @@ -108,7 +109,7 @@ func (i *Identities) CreateIdentity(options *CreateIdentityOptions) (*Identity,
}
}

pubKeyIDSignature, err := identityProvider.SignIdentity(append(publicKeyBytes, idSignature...), options.ID)
pubKeyIDSignature, err := identityProvider.SignIdentity(ctx, append(publicKeyBytes, idSignature...), options.ID)
if err != nil {
return nil, errmsg.ErrIdentityCreationFailed.Wrap(err)
}
Expand All @@ -125,10 +126,10 @@ func (i *Identities) CreateIdentity(options *CreateIdentityOptions) (*Identity,
}, nil
}

func (i *Identities) signID(id string) (crypto.PubKey, []byte, error) {
privKey, err := i.keyStore.GetKey(id)
func (i *Identities) signID(ctx context.Context, id string) (crypto.PubKey, []byte, error) {
privKey, err := i.keyStore.GetKey(ctx, id)
if err != nil {
privKey, err = i.keyStore.CreateKey(id)
privKey, err = i.keyStore.CreateKey(ctx, id)

if err != nil {
return nil, nil, errmsg.ErrSigSign.Wrap(err)
Expand Down Expand Up @@ -173,15 +174,15 @@ func (i *Identities) VerifyIdentity(identity *Identity) error {
}

// CreateIdentity creates a new identity.
func CreateIdentity(options *CreateIdentityOptions) (*Identity, error) {
func CreateIdentity(ctx context.Context, options *CreateIdentityOptions) (*Identity, error) {
ks := options.Keystore
if ks == nil {
return nil, errmsg.ErrKeystoreNotDefined
}

identities := newIdentities(ks)

return identities.CreateIdentity(options)
return identities.CreateIdentity(ctx, options)
}

// IsSupported checks if an identity type is supported.
Expand Down
8 changes: 5 additions & 3 deletions identityprovider/interface.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package identityprovider // import "berty.tech/go-ipfs-log/identityprovider"

import (
"context"

"berty.tech/go-ipfs-log/keystore"
crypto "github.com/libp2p/go-libp2p-core/crypto"
)
Expand All @@ -15,10 +17,10 @@ type CreateIdentityOptions struct {

type Interface interface {
// GetID returns id of identity (to be signed by orbit-db public key).
GetID(*CreateIdentityOptions) (string, error)
GetID(context.Context, *CreateIdentityOptions) (string, error)

// SignIdentity returns signature of OrbitDB public key signature.
SignIdentity(data []byte, id string) ([]byte, error)
SignIdentity(ctx context.Context, data []byte, id string) ([]byte, error)

// GetType returns the type for this identity provider.
GetType() string
Expand All @@ -27,7 +29,7 @@ type Interface interface {
VerifyIdentity(identity *Identity) error

// Sign will sign a value.
Sign(identity *Identity, bytes []byte) ([]byte, error)
Sign(ctx context.Context, identity *Identity, bytes []byte) ([]byte, error)

// UnmarshalPublicKey will provide a crypto.PubKey from a key bytes.
UnmarshalPublicKey(data []byte) (crypto.PubKey, error)
Expand Down
15 changes: 8 additions & 7 deletions identityprovider/orbitdb.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package identityprovider // import "berty.tech/go-ipfs-log/identityprovider"

import (
"context"
"encoding/hex"

"github.com/libp2p/go-libp2p-core/crypto"
Expand All @@ -26,10 +27,10 @@ func NewOrbitDBIdentityProvider(options *CreateIdentityOptions) Interface {
}

// GetID returns the identity's ID.
func (p *OrbitDBIdentityProvider) GetID(options *CreateIdentityOptions) (string, error) {
private, err := p.keystore.GetKey(options.ID)
func (p *OrbitDBIdentityProvider) GetID(ctx context.Context, options *CreateIdentityOptions) (string, error) {
private, err := p.keystore.GetKey(ctx, options.ID)
if err != nil || private == nil {
private, err = p.keystore.CreateKey(options.ID)
private, err = p.keystore.CreateKey(ctx, options.ID)
if err != nil {
return "", errmsg.ErrKeyStoreCreateEntry.Wrap(err)
}
Expand All @@ -44,8 +45,8 @@ func (p *OrbitDBIdentityProvider) GetID(options *CreateIdentityOptions) (string,
}

// SignIdentity signs an OrbitDB identity.
func (p *OrbitDBIdentityProvider) SignIdentity(data []byte, id string) ([]byte, error) {
key, err := p.keystore.GetKey(id)
func (p *OrbitDBIdentityProvider) SignIdentity(ctx context.Context, data []byte, id string) ([]byte, error) {
key, err := p.keystore.GetKey(ctx, id)
if err != nil {
return nil, errmsg.ErrKeyNotInKeystore
}
Expand All @@ -64,8 +65,8 @@ func (p *OrbitDBIdentityProvider) SignIdentity(data []byte, id string) ([]byte,
}

// Sign signs a value using the current.
func (p *OrbitDBIdentityProvider) Sign(identity *Identity, data []byte) ([]byte, error) {
key, err := p.keystore.GetKey(identity.ID)
func (p *OrbitDBIdentityProvider) Sign(ctx context.Context, identity *Identity, data []byte) ([]byte, error) {
key, err := p.keystore.GetKey(ctx, identity.ID)
if err != nil {
return nil, errmsg.ErrKeyNotInKeystore.Wrap(err)
}
Expand Down
12 changes: 8 additions & 4 deletions keystore/interface.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package keystore // import "berty.tech/go-ipfs-log/keystore"

import crypto "github.com/libp2p/go-libp2p-core/crypto"
import (
"context"

crypto "github.com/libp2p/go-libp2p-core/crypto"
)

type Interface interface {
HasKey(id string) (bool, error)
HasKey(ctx context.Context, id string) (bool, error)

CreateKey(id string) (crypto.PrivKey, error)
CreateKey(ctx context.Context, id string) (crypto.PrivKey, error)

GetKey(id string) (crypto.PrivKey, error)
GetKey(ctx context.Context, id string) (crypto.PrivKey, error)

Sign(pubKey crypto.PrivKey, bytes []byte) ([]byte, error)

Expand Down
12 changes: 6 additions & 6 deletions keystore/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ func NewKeystore(store datastore.Datastore) (*Keystore, error) {
}

// HasKey checks whether a given key ID exist in the keystore.
func (k *Keystore) HasKey(id string) (bool, error) {
func (k *Keystore) HasKey(ctx context.Context, id string) (bool, error) {
storedKey, ok := k.cache.Peek(id)

if ok == false {
value, err := k.store.Get(context.Background(), datastore.NewKey(id))
value, err := k.store.Get(ctx, datastore.NewKey(id))
if err != nil {
return false, errmsg.ErrKeyNotInKeystore.Wrap(err)
}
Expand All @@ -69,7 +69,7 @@ func (k *Keystore) HasKey(id string) (bool, error) {
}

// CreateKey creates a new key in the key store.
func (k *Keystore) CreateKey(id string) (crypto.PrivKey, error) {
func (k *Keystore) CreateKey(ctx context.Context, id string) (crypto.PrivKey, error) {
// FIXME: I kept Secp256k1 for compatibility with OrbitDB, should we change this?
priv, _, err := crypto.GenerateSecp256k1Key(rand.Reader)
if err != nil {
Expand All @@ -81,7 +81,7 @@ func (k *Keystore) CreateKey(id string) (crypto.PrivKey, error) {
return nil, errmsg.ErrInvalidPrivKeyFormat.Wrap(err)
}

if err := k.store.Put(context.Background(), datastore.NewKey(id), keyBytes); err != nil {
if err := k.store.Put(ctx, datastore.NewKey(id), keyBytes); err != nil {
return nil, errmsg.ErrKeyStorePutFailed.Wrap(err)
}

Expand All @@ -91,13 +91,13 @@ func (k *Keystore) CreateKey(id string) (crypto.PrivKey, error) {
}

// GetKey retrieves a key from the keystore.
func (k *Keystore) GetKey(id string) (crypto.PrivKey, error) {
func (k *Keystore) GetKey(ctx context.Context, id string) (crypto.PrivKey, error) {
var err error
var keyBytes []byte

cachedKey, ok := k.cache.Get(id)
if !ok || cachedKey == nil {
keyBytes, err = k.store.Get(context.Background(), datastore.NewKey(id))
keyBytes, err = k.store.Get(ctx, datastore.NewKey(id))

if err != nil {
return nil, errmsg.ErrKeyNotInKeystore.Wrap(err)
Expand Down
2 changes: 1 addition & 1 deletion test/bench_add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func BenchmarkAdd(b *testing.B) {
ks, err := keystore.NewKeystore(datastore)
require.NoError(b, err)

identity, err := idp.CreateIdentity(&idp.CreateIdentityOptions{
identity, err := idp.CreateIdentity(ctx, &idp.CreateIdentityOptions{
Keystore: ks,
ID: "userA",
Type: "orbitdb",
Expand Down
4 changes: 2 additions & 2 deletions test/bench_join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ func BenchmarkJoin(b *testing.B) {
ks, err := keystore.NewKeystore(datastore)
require.NoError(b, err)

identityA, err := idp.CreateIdentity(&idp.CreateIdentityOptions{
identityA, err := idp.CreateIdentity(ctx, &idp.CreateIdentityOptions{
Keystore: ks,
ID: "userA",
Type: "orbitdb",
})
require.NoError(b, err)

identityB, err := idp.CreateIdentity(&idp.CreateIdentityOptions{
identityB, err := idp.CreateIdentity(ctx, &idp.CreateIdentityOptions{
Keystore: ks,
ID: "userB",
Type: "orbitdb",
Expand Down
2 changes: 1 addition & 1 deletion test/entry_io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestEntryPersistence(t *testing.T) {
for i := 0; i < 4; i++ {
char := 'A' + i

identity, err := idp.CreateIdentity(&idp.CreateIdentityOptions{
identity, err := idp.CreateIdentity(ctx, &idp.CreateIdentityOptions{
Keystore: keystore,
ID: fmt.Sprintf("user%c", char),
Type: "orbitdb",
Expand Down
2 changes: 1 addition & 1 deletion test/entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestEntry(t *testing.T) {
keystore, err := ks.NewKeystore(datastore)
require.NoError(t, err)

identity, err := idp.CreateIdentity(&idp.CreateIdentityOptions{
identity, err := idp.CreateIdentity(ctx, &idp.CreateIdentityOptions{
Keystore: keystore,
ID: fmt.Sprintf("userA"),
Type: "orbitdb",
Expand Down
4 changes: 2 additions & 2 deletions test/log_append_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestLogAppend(t *testing.T) {
keystore, err := keystore.NewKeystore(datastore)
require.NoError(t, err)

identity, err := idp.CreateIdentity(&idp.CreateIdentityOptions{
identity, err := idp.CreateIdentity(ctx, &idp.CreateIdentityOptions{
Keystore: keystore,
ID: fmt.Sprintf("userA"),
Type: "orbitdb",
Expand Down Expand Up @@ -113,7 +113,7 @@ func TestLogAppendEncrypted(t *testing.T) {
keystore, err := keystore.NewKeystore(datastore)
require.NoError(t, err)

identity, err := idp.CreateIdentity(&idp.CreateIdentityOptions{
identity, err := idp.CreateIdentity(ctx, &idp.CreateIdentityOptions{
Keystore: keystore,
ID: fmt.Sprintf("userA"),
Type: "orbitdb",
Expand Down
2 changes: 1 addition & 1 deletion test/log_crdt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestLogCRDT(t *testing.T) {
var identities [3]*idp.Identity

for i, char := range []rune{'A', 'B', 'C'} {
identity, err := idp.CreateIdentity(&idp.CreateIdentityOptions{
identity, err := idp.CreateIdentity(ctx, &idp.CreateIdentityOptions{
Keystore: keystore,
ID: fmt.Sprintf("user%c", char),
Type: "orbitdb",
Expand Down
2 changes: 1 addition & 1 deletion test/log_heads_tails_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestLogHeadsTails(t *testing.T) {
for i := 0; i < 4; i++ {
char := 'A' + i

identity, err := idp.CreateIdentity(&idp.CreateIdentityOptions{
identity, err := idp.CreateIdentity(ctx, &idp.CreateIdentityOptions{
Keystore: keystore,
ID: fmt.Sprintf("user%c", char),
Type: "orbitdb",
Expand Down
2 changes: 1 addition & 1 deletion test/log_iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestLogIterator(t *testing.T) {
identities := make([]*idp.Identity, 4)

for i, char := range []rune{'A', 'B', 'C'} {
identity, err := idp.CreateIdentity(&idp.CreateIdentityOptions{
identity, err := idp.CreateIdentity(ctx, &idp.CreateIdentityOptions{
Keystore: keystore,
ID: fmt.Sprintf("user%c", char),
Type: "orbitdb",
Expand Down
2 changes: 1 addition & 1 deletion test/log_join_concurrent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestLogJoinConcurrent(t *testing.T) {
require.NoError(t, err)

t.Run("join", func(t *testing.T) {
identity, err := idp.CreateIdentity(&idp.CreateIdentityOptions{
identity, err := idp.CreateIdentity(ctx, &idp.CreateIdentityOptions{
Keystore: keystore,
ID: "userA",
Type: "orbitdb",
Expand Down
2 changes: 1 addition & 1 deletion test/log_join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestLogJoin(t *testing.T) {

for i, char := range []rune{'C', 'B', 'D', 'A'} {

identity, err := idp.CreateIdentity(&idp.CreateIdentityOptions{
identity, err := idp.CreateIdentity(ctx, &idp.CreateIdentityOptions{
Keystore: keystore,
ID: fmt.Sprintf("user%c", char),
Type: "orbitdb",
Expand Down
5 changes: 3 additions & 2 deletions test/log_load_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package test

import (
"berty.tech/go-ipfs-log/io/pb"
"bytes"
"context"
"fmt"
Expand All @@ -11,6 +10,8 @@ import (
"testing"
"time"

"berty.tech/go-ipfs-log/io/pb"

ipfslog "berty.tech/go-ipfs-log"
"berty.tech/go-ipfs-log/entry"
"berty.tech/go-ipfs-log/entry/sorting"
Expand Down Expand Up @@ -43,7 +44,7 @@ func TestLogLoad(t *testing.T) {
identities := make([]*idp.Identity, 4)

for i, char := range []rune{'C', 'B', 'D', 'A'} {
identity, err := idp.CreateIdentity(&idp.CreateIdentityOptions{
identity, err := idp.CreateIdentity(ctx, &idp.CreateIdentityOptions{
Keystore: keystore,
ID: fmt.Sprintf("user%c", char),
Type: "orbitdb",
Expand Down
4 changes: 2 additions & 2 deletions test/log_references_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestLogReferences(t *testing.T) {
keystore, err := ks.NewKeystore(datastore)
require.NoError(t, err)

identity, err := idp.CreateIdentity(&idp.CreateIdentityOptions{
identity, err := idp.CreateIdentity(ctx, &idp.CreateIdentityOptions{
Keystore: keystore,
ID: "userA",
Type: "orbitdb",
Expand Down Expand Up @@ -113,7 +113,7 @@ func TestLogReferences2(t *testing.T) {
keystore, err := ks.NewKeystore(datastore)
require.NoError(t, err)

identity, err := idp.CreateIdentity(&idp.CreateIdentityOptions{
identity, err := idp.CreateIdentity(ctx, &idp.CreateIdentityOptions{
Keystore: keystore,
ID: "userA",
Type: "orbitdb",
Expand Down

0 comments on commit 019860e

Please sign in to comment.