Skip to content

Commit

Permalink
feat: Add InfoFile struct and methods for marshaling and unmarshaling
Browse files Browse the repository at this point in the history
feat: Implement GetInfoFile and ToFileMap methods in vaultFS

feat: Add AddFileMap method to virtual file system

fix: Update vault struct initialization in Generate function

feat: Add NewFSWithKss method to create VFS with kss keys

feat: Update VFS interface to include AddFileMap method
  • Loading branch information
prnk28 committed Jun 3, 2024
1 parent 965f7e8 commit b31c735
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
23 changes: 22 additions & 1 deletion pkg/ipfs/vfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"fmt"
"io"

"github.com/di-dao/sonr/crypto/kss"
"github.com/ipfs/boxo/files"
"github.com/ipfs/boxo/path"
)

// VFS is an interface for interacting with a virtual file system.
type VFS interface {
Add(path string, data []byte) error
AddFileMap(files map[string]files.File) error
Get(path string) ([]byte, error)
Rm(path string) error
Ls(path string) ([]string, error)
Expand All @@ -30,13 +32,24 @@ type vfs struct {
}

// NewVFS creates a new virtual file system.
func NewFileSystem(name string) VFS {
func NewFS(name string) VFS {
return &vfs{
files: make(map[string]files.File, 0),
name: name,
}
}

// NewFSWithKss creates a new virtual file system from a kss setPath
func NewFSWithKss(kss kss.Set, name string) VFS {
fs := &vfs{
files: make(map[string]files.File, 0),
name: name,
}
fs.Add("user.ks", kss.BytesUsr())
fs.Add("val.ks", kss.BytesVal())
return fs
}

// Load creates a new virtual file system from a given files.Node.
func Load(name string, node files.Node) (VFS, error) {
entry := files.FileEntry(name, node)
Expand Down Expand Up @@ -91,6 +104,14 @@ func (v *vfs) Add(path string, data []byte) error {
return nil
}

// AddFileMap adds a file map to the virtual file system
func (v *vfs) AddFileMap(files map[string]files.File) error {
for k, f := range files {
v.files[k] = f
}
return nil
}

// Get retrieves a file from the virtual file system.
func (v *vfs) Get(path string) ([]byte, error) {
if file, ok := v.files[path]; ok {
Expand Down
44 changes: 44 additions & 0 deletions pkg/vault/fs.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,62 @@
package vault

import (
"encoding/json"

"github.com/di-dao/sonr/crypto/kss"
"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
}

func (v *vaultFS) GetInfoFile() *InfoFile {
return &InfoFile{
Creds: v.Creds,
Properties: v.Properties,
}
}

func (v *vaultFS) ToFileMap() (map[string]files.File, error) {
flMap := make(map[string]files.File)

wallBz, err := v.Wallet.Marshal()
if err != nil {
return nil, err
}
walletFile := files.NewBytesFile(wallBz)
flMap["wallet.json"] = walletFile

info := v.GetInfoFile()
infoBz, err := info.Marshal()
if err != nil {
return nil, err
}
infoFile := files.NewBytesFile(infoBz)
flMap["info.json"] = infoFile

return flMap, nil
}

func createVaultFS(set kss.Set) (*vaultFS, error) {
wallet, err := wallet.New(set)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/vault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func Generate(ctx context.Context) (Vault, error) {

// Create a new vault
return &vault{
vfs: ipfs.NewFileSystem(fs.Wallet.SonrAddress()),
vltFS: fs,
vfs: ipfs.NewFSWithKss(keyshares, fs.Wallet.SonrAddress()),
}, nil
}

0 comments on commit b31c735

Please sign in to comment.