Skip to content

Commit

Permalink
refactor: Update vaultFS struct and methods structure
Browse files Browse the repository at this point in the history
Update the vaultFS struct fields and methods to use auth package.
Add necessary imports and update the GetInfoFile method.
  • Loading branch information
prnk28 committed Jun 4, 2024
1 parent bec9f45 commit 9b309c7
Show file tree
Hide file tree
Showing 13 changed files with 200 additions and 33 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ require (
github.com/oklog/run v1.1.0 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/orcaman/concurrent-map v1.0.0 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/petar/GoLLRB v0.0.0-20210522233825-ae3b015fd3e9 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2103,6 +2103,8 @@ github.com/ory/dockertest v3.3.5+incompatible/go.mod h1:1vX4m9wsvi00u5bseYwXaSnh
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pascaldekloe/goe v0.1.0 h1:cBOtyMzM9HTpWjXfbbunk26uA6nG3a8n06Wieeh0MwY=
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/paulbellamy/ratecounter v0.2.0/go.mod h1:Hfx1hDpSGoqxkVVpBi/IlYD7kChlfo5C6hzIHwPqfFE=
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 h1:onHthvaw9LFnH4t2DcNVpwGmV9E1BkGknEliJkfwQj0=
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58/go.mod h1:DXv8WO4yhMYhSNPKjeNKa5WY9YCIEBRbNzFFPJbWO6Y=
Expand Down
3 changes: 0 additions & 3 deletions internal/local/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/segmentio/ksuid"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
Expand Down Expand Up @@ -35,13 +34,11 @@ type SonrContext struct {
PeerID string
ChainID string
Token string
SDKContext sdk.Context
}

// UnwrapContext uses context.Context to retreive grpc.Metadata
func UnwrapContext(ctx context.Context) SonrContext {
sctx := SonrContext{
SDKContext: sdk.UnwrapSDKContext(ctx),
Context: ctx,
ValidatorAddress: valAddr,
ChainID: chainID,
Expand Down
41 changes: 41 additions & 0 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cache

import (
"time"

"github.com/patrickmn/go-cache"
)

type stringConstraint interface {
~string
}

type Cache[K stringConstraint, T any] struct {
cache *cache.Cache
}

func New[K stringConstraint, T any](defaultExpiration time.Duration, cleanupInterval time.Duration) *Cache[K, T] {
c := cache.New(defaultExpiration, cleanupInterval)

return &Cache[K, T]{
cache: c,
}
}

func (c *Cache[K, T]) Set(key K, val any) {
c.cache.Set(string(key), val, cache.DefaultExpiration)
}

func (c *Cache[K, T]) Get(key K) (*T, bool) {
v, ok := c.cache.Get(string(key))
if !ok {
return nil, false
}

vTyped, ok := v.(T)
if !ok {
return nil, false
}

return &vTyped, true
}
106 changes: 106 additions & 0 deletions pkg/cache/cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package cache_test

import (
"testing"
"time"

"github.com/di-dao/sonr/pkg/cache"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestCache_SetGet(t *testing.T) {
type MyString string
type Data struct {
Value int
}

cache := cache.New[MyString, Data](5*time.Minute, 10*time.Minute)

key := MyString("testKey")
expectedValue := Data{Value: 42}

cache.Set(key, expectedValue)
retrievedValue, ok := cache.Get(key)

require.True(t, ok, "Expected to retrieve a value from the cache")
require.NotNil(t, retrievedValue, "Expected a non-nil value")
assert.Equal(t, expectedValue, *retrievedValue, "Retrieved value does not match the expected value")
}

func TestCache_Get_NonExistentKey(t *testing.T) {
type MyString string
type Data struct {
Value int
}

cache := cache.New[MyString, Data](5*time.Minute, 10*time.Minute)

key := MyString("nonExistentKey")
retrievedValue, ok := cache.Get(key)

require.False(t, ok, "Expected not to retrieve a value from the cache for a non-existent key")
require.Nil(t, retrievedValue, "Expected a nil value for a non-existent key")
}

func TestCache_Set_Overwrite(t *testing.T) {
type MyString string
type Data struct {
Value int
}

cache := cache.New[MyString, Data](5*time.Minute, 10*time.Minute)

key := MyString("testKey")
initialValue := Data{Value: 42}
updatedValue := Data{Value: 100}

cache.Set(key, initialValue)
cache.Set(key, updatedValue)

retrievedValue, ok := cache.Get(key)

require.True(t, ok, "Expected to retrieve a value from the cache")
require.NotNil(t, retrievedValue, "Expected a non-nil value")
assert.Equal(t, updatedValue, *retrievedValue, "Retrieved value does not match the updated value")
}

func TestCache_Set_Get_OtherType(t *testing.T) {
type MyString string
type Data struct {
Value int
}

cache := cache.New[MyString, Data](5*time.Minute, 10*time.Minute)

key := MyString("testKey")
intVal := 42

cache.Set(key, intVal)

retrievedValue, ok := cache.Get(key)

require.False(t, ok, "Expected not to retrieve a value from the cache for a key with a wrong type")
require.Nil(t, retrievedValue, "Expected a nil value for a key with a wrong type")
}

func TestCache_Set_WithExpiration(t *testing.T) {
type MyString string
type Data struct {
Value int
}

expiration := 1 * time.Second
cache := cache.New[MyString, Data](expiration, 10*time.Minute)

key := MyString("testKey")
expectedValue := Data{Value: 42}

cache.Set(key, expectedValue)
time.Sleep(2 * time.Second)

retrievedValue, ok := cache.Get(key)

require.False(t, ok, "Expected not to retrieve a value from the cache after expiration")
require.Nil(t, retrievedValue, "Expected a nil value after expiration")
}
16 changes: 16 additions & 0 deletions pkg/vault/auth/data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package auth

import "encoding/json"

type InfoFile struct {
Creds Credentials `json:"credentials"`
Properties Properties `json:"properties"`
}

func (i *InfoFile) Marshal() ([]byte, error) {
return json.Marshal(i)
}

func (i *InfoFile) Unmarshal(data []byte) error {
return json.Unmarshal(data, i)
}
2 changes: 1 addition & 1 deletion pkg/vault/props/props.go → pkg/vault/auth/props.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package props
package auth

import (
"errors"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package props_test
package auth_test

import (
"testing"

"github.com/di-dao/sonr/crypto/mpc"
"github.com/di-dao/sonr/pkg/vault/props"
props "github.com/di-dao/sonr/pkg/vault/auth"
"github.com/stretchr/testify/require"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/vault/props/secret.go → pkg/vault/auth/secret.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package props
package auth

import (
"github.com/di-dao/sonr/crypto/accumulator"
Expand Down
21 changes: 21 additions & 0 deletions pkg/vault/cache.go
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
package vault

import (
"time"

"github.com/di-dao/sonr/pkg/cache"
)

var vaultCache *cache.Cache[contextKey, vaultFS]

type contextKey string

func (c contextKey) String() string {
return "vault context key " + string(c)
}

var clientCtxKey = contextKey("vault-client-id")

func init() {
// This is a placeholder
vaultCache = cache.New[contextKey, vaultFS](time.Minute*5, time.Minute)
}
4 changes: 1 addition & 3 deletions pkg/vault/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package vault

import (
"context"

"github.com/go-webauthn/webauthn/protocol"
)

type Client interface {
Assign(ctx context.Context, resp *protocol.CredentialCreationResponse) error
Assign(ctx context.Context, resp string) error
}
30 changes: 7 additions & 23 deletions pkg/vault/fs.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,21 @@
package vault

import (
"encoding/json"

"github.com/di-dao/sonr/crypto/kss"
"github.com/di-dao/sonr/pkg/ipfs"
"github.com/di-dao/sonr/pkg/vault/auth"
"github.com/di-dao/sonr/pkg/vault/props"
"github.com/di-dao/sonr/pkg/vault/wallet"
"github.com/ipfs/boxo/files"
)

type InfoFile struct {
Creds auth.Credentials `json:"credentials"`
Properties props.Properties `json:"properties"`
}

func (i *InfoFile) Marshal() ([]byte, error) {
return json.Marshal(i)
}

func (i *InfoFile) Unmarshal(data []byte) error {
return json.Unmarshal(data, i)
}

type vaultFS struct {
Wallet *wallet.Wallet
Creds auth.Credentials
Properties props.Properties
Wallet *wallet.Wallet `json:"wallet"`
Creds auth.Credentials `json:"credentials"`
Properties auth.Properties `json:"properties"`
}

func (v *vaultFS) GetInfoFile() *InfoFile {
return &InfoFile{
func (v *vaultFS) GetInfoFile() *auth.InfoFile {
return &auth.InfoFile{
Creds: v.Creds,
Properties: v.Properties,
}
Expand Down Expand Up @@ -67,7 +51,7 @@ func createVaultFS(set kss.Set) (*vaultFS, error) {
return &vaultFS{
Wallet: wallet,
Creds: auth.NewCredentials(),
Properties: props.NewProperties(),
Properties: auth.NewProperties(),
}, nil
}

Expand All @@ -83,7 +67,7 @@ func loadVaultFS(vfs ipfs.VFS) (*vaultFS, error) {
return nil, err
}

info := &InfoFile{}
info := &auth.InfoFile{}
infoBz, err := vfs.Get("info.json")
if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions pkg/vault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func Generate(ctx context.Context) (Vault, error) {
// Update the context with the wallet address
snrCtx.UserAddress = fs.Wallet.SonrAddress()
local.WrapContext(snrCtx)
vaultCache.Set(contextKey(snrCtx.SessionID), fs)

// Create a new vault
return &vault{
Expand Down

0 comments on commit 9b309c7

Please sign in to comment.