Skip to content

Commit

Permalink
suggestion (#73)
Browse files Browse the repository at this point in the history
Co-authored-by: Liubomur <[email protected]>
  • Loading branch information
Morzaka and Liubomur authored Sep 23, 2022
1 parent e68199e commit 42a608b
Show file tree
Hide file tree
Showing 18 changed files with 185 additions and 185 deletions.
7 changes: 3 additions & 4 deletions beast/beast.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@ import (
"context"

filePL "github.com/functionland/go-fula/protocols/file"
logging "github.com/ipfs/go-log"
"github.com/ipfs/kubo/core"
"github.com/ipfs/kubo/core/coreapi"
"github.com/ipfs/kubo/plugin"
logging "github.com/ipfs/go-log"
"github.com/libp2p/go-libp2p-core/network"
)

var log = logging.Logger("plugin/beast")

func check(err error) {

}
// TODO implement check func or remove it
func check(err error) {}

type BeastPlugin struct {
plugin.PluginDaemonInternal
Expand Down
33 changes: 18 additions & 15 deletions crypto/cipher.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (

var log = logging.Logger("fula:crypto")

const randomKeyGenError = "something goes wording with random generator: %s"

type IChipher interface {
Encrypt()
Decrypt()
Expand All @@ -21,22 +23,25 @@ type Cipher struct {
SymKey []byte
}

func NewEnCipher() (Cipher, error) {
func NewEnCipher() *Cipher {
symKey, err := RandomKey(32)
if err != nil {
log.Error("somthing goes worng with random generator")
return Cipher{}, err
log.Errorf(randomKeyGenError, err.Error())
return &Cipher{}
}
iv, err := RandomKey(16)
if err != nil {
log.Error("somthing goes worng with random generator")
return Cipher{}, err
log.Errorf(randomKeyGenError, err.Error())
return &Cipher{}
}
return Cipher{Iv: iv, SymKey: symKey}, nil
return &Cipher{Iv: iv, SymKey: symKey}
}

func NewDeCipher(iv []byte, symKey []byte) (Cipher, error) {
return Cipher{Iv: iv, SymKey: symKey}, nil
func NewDeCipher(iv []byte, symKey []byte) *Cipher {
return &Cipher{
Iv: iv,
SymKey: symKey,
}
}

// RandomKey generate array of size n with random data.
Expand All @@ -52,11 +57,10 @@ func RandomKey(n int) ([]byte, error) {
}

// Encrypt encrypts plain text string into cipher text string
func (c *Cipher) Encrypt(unencrypted []byte, n int) ([]byte, error) {
func (c *Cipher) Encrypt(unencrypted []byte) ([]byte, error) {

if len(unencrypted)%aes.BlockSize != 0 {
err := fmt.Errorf(`plainText: "%s" has the wrong block size`, unencrypted)
return nil, err
return nil, fmt.Errorf(`plainText: "%s" has the wrong block size`, unencrypted)
}

block, err := aes.NewCipher(c.SymKey)
Expand All @@ -68,7 +72,7 @@ func (c *Cipher) Encrypt(unencrypted []byte, n int) ([]byte, error) {

mode := cipher.NewCBCEncrypter(block, c.Iv)
mode.CryptBlocks(encData, unencrypted)
log.Debug("encypted buff size:", len(encData))
log.Debug("encrypted buff size:", len(encData))
return encData, nil
}

Expand All @@ -79,12 +83,11 @@ func (c *Cipher) Decrypt(encrypted []byte) ([]byte, error) {
return nil, err
}
decData := make([]byte, len(encrypted))

log.Debug("size of encrypted input", len(encrypted))
mode := cipher.NewCBCDecrypter(block, c.Iv)
mode.CryptBlocks(decData, encrypted)
log.Debug("size of dencrypted input", len(decData))
if err != nil {
return nil, err
}

return decData, nil
}
33 changes: 17 additions & 16 deletions crypto/pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,24 @@ import (
"sync"

"github.com/mergermarket/go-pkcs7"
// logging "github.com/ipfs/go-log"
)

// var log = logging.Logger("fula:crypto")

const CHUNK_SIZE = 1024 * aes.BlockSize
const ChunkSize = 1024 * aes.BlockSize

type encoder struct {
reader io.Reader
EnCipher Cipher
}

func NewEncoder(reader io.Reader) *encoder {
encipher, _ := NewEnCipher()
return &encoder{reader: reader, EnCipher: encipher}
encipher := NewEnCipher()
return &encoder{reader: reader, EnCipher: *encipher}
}

func (c *encoder) EncryptOnFly(fileCh chan<- []byte, wg *sync.WaitGroup) error {
defer close(fileCh)
log.Debug("start EncryptOnFly")
fileBuf := make([]byte, CHUNK_SIZE)
fileBuf := make([]byte, ChunkSize)
for {
n, err := c.reader.Read(fileBuf)
if err == io.EOF {
Expand All @@ -45,7 +42,7 @@ func (c *encoder) EncryptOnFly(fileCh chan<- []byte, wg *sync.WaitGroup) error {
return err
}
}
encBuf, err := c.EnCipher.Encrypt(fileBuf, n)
encBuf, err := c.EnCipher.Encrypt(fileBuf)
if err != nil {
return err
}
Expand All @@ -67,21 +64,25 @@ type decoder struct {
}

func NewDecoder(reader io.Reader, iv []byte, symKey []byte) *decoder {
decipher, _ := NewDeCipher(iv, symKey)
return &decoder{reader: reader, DeCipher: decipher}
decipher := NewDeCipher(iv, symKey)
return &decoder{reader: reader, DeCipher: *decipher}
}

func (c *decoder) DycryptOnFly(filePath string) error {
size:=16
func (c *decoder) DecryptOnFly(filePath string) error {
const size = 16
buffer := make([]byte, size)
file, err := os.Create(filePath)
if err != nil {
return err
}
defer file.Close()
var cache []byte = nil
var cacheDec []byte = nil
chunkIndex := 0

var (
cache []byte = nil
cacheDec []byte = nil
chunkIndex = 0
)

for {
n, err := c.reader.Read(buffer)
if err == io.EOF {
Expand All @@ -104,7 +105,7 @@ func (c *decoder) DycryptOnFly(filePath string) error {
if n > 0 {
cache = append(cache, buffer...)
chunkIndex += 1 * size
if chunkIndex >= CHUNK_SIZE {
if chunkIndex >= ChunkSize {
dec, err := c.DeCipher.Decrypt(cache)
if err != nil {
return err
Expand Down
16 changes: 8 additions & 8 deletions drive/drive-space.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type ListEntry struct {
}

// DriveSpace holds information about a space inside a user's drive
// A drive space can be either Private or Public, SpactType indicates this
// A drive space can be either Private or Public, SpaceType indicates this
type DriveSpace struct {
Ctx context.Context
Api fxiface.CoreAPI
Expand All @@ -49,12 +49,12 @@ type DriveSpace struct {
Drive *UserDrive
}

// Public space inside a Drive
// DrivePublicSpace implement public space inside a Drive
type DrivePublicSpace struct {
DriveSpace
}

// Private space inside a Drive
// DrivePrivateSpace implement private space inside a Drive
type DrivePrivateSpace struct {
DriveSpace
}
Expand All @@ -63,7 +63,7 @@ func makePath(dirs ...string) path.Path {
return path.Join(path.New("/ipfs"), dirs...)
}

// Sets the space's root cid in it's parent drive
// Save sets the space's root cid in it's parent drive
// This should be called before UserDrive.Publish()
func (ds *DriveSpace) Save() {
switch ds.SpaceType {
Expand Down Expand Up @@ -98,7 +98,7 @@ func (ds *DriveSpace) MkDir(p string, options MkDirOpts) (string, error) {
return ds.RootCid, nil
}

// Writes a file into drive at a given location (in public space).
// WriteFile writes a file into drive at a given location (in public space).
func (ds *DrivePublicSpace) WriteFile(p string, file files.File, options WriteFileOpts) (string, error) {
// @TODO handle options.parents = true (create the directories in the path)

Expand All @@ -122,7 +122,7 @@ func (ds *DrivePublicSpace) WriteFile(p string, file files.File, options WriteFi
return ds.RootCid, nil
}

// Writes a file into private space in a drive at a given location, it takes a byte array called JWE in addition to DrivePublicSpace.WriteFile
// WriteFile writes a file into private space in a drive at a given location, it takes a byte array called JWE in addition to DrivePublicSpace.WriteFile
func (ds *DrivePrivateSpace) WriteFile(p string, file files.File, jwe []byte, options WriteFileOpts) (string, error) {
// @TODO handle options.parents = true (create the not-existing directories in the path)

Expand All @@ -148,7 +148,7 @@ func (ds *DrivePrivateSpace) WriteFile(p string, file files.File, jwe []byte, op
return ds.RootCid, nil
}

// Reads a file from the drive at a given location
// ReadFile reads a file from the drive at a given location
func (ds *DrivePublicSpace) ReadFile(p string, options ReadFileOpts) (files.File, error) {
file, err := ds.Api.PublicFS().Get(ds.Ctx, makePath(ds.RootCid, p))
if err != nil {
Expand Down Expand Up @@ -176,7 +176,7 @@ func (ds *DrivePrivateSpace) ReadFile(p string, options ReadFileOpts) (pfs.Encod
return file.(pfs.EncodedFile), nil
}

// Deletes a file at a given location on the drive
// DeleteFile deletes a file at a given location on the drive
func (ds *DriveSpace) DeleteFile(p string, options DeleteFileOpts) (string, error) {
newRoot, err := deletefileDAG(ds.RootDir, p)
if err != nil {
Expand Down
19 changes: 8 additions & 11 deletions drive/drive.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package drive
import (
"context"

logging "github.com/ipfs/go-log"

fxiface "github.com/functionland/go-fula/fxfs/core/iface"
"github.com/functionland/go-fula/fxfs/core/pfs"
files "github.com/ipfs/go-ipfs-files"
logging "github.com/ipfs/go-log"
)

var log = logging.Logger("fula-drive")
Expand All @@ -22,22 +21,20 @@ type UserDrive struct {
ds DriveStore
}

// Create a new null Drive, the only field set is the UserDID, other fields are initialized by first call to Publish
// NewDrive create a new null Drive, the only field set is the UserDID, other fields are initialized by first call to Publish
func NewDrive(userDID string, ds DriveStore) UserDrive {
return UserDrive{UserDID: userDID,
PrivateSpaceCid: "",
PublicSpaceCid: "",
Dirs: nil,
ds: ds,
return UserDrive{
UserDID: userDID,
ds: ds,
}
}

// Check if a drive is null
// IsNull check if a drive is null
func (ud *UserDrive) IsNull() bool {
return ud.PrivateSpaceCid == "" && ud.PublicSpaceCid == "" && ud.Dirs == nil
}

// Create a DrivePublicSpace struct. PublicSpace creates a Directory instance by getting the root cid of the drive using FS API
// PublicSpace create a DrivePublicSpace struct. PublicSpace creates a Directory instance by getting the root cid of the drive using FS API
func (ud *UserDrive) PublicSpace(ctx context.Context, api fxiface.CoreAPI) (*DrivePublicSpace, error) {
rpath := makePath(ud.PublicSpaceCid)
rootDir, err := api.PublicFS().Get(ctx, rpath)
Expand All @@ -55,7 +52,7 @@ func (ud *UserDrive) PublicSpace(ctx context.Context, api fxiface.CoreAPI) (*Dri
Drive: ud}}, err
}

// Create a DrivePrivateSpace struct. PrivateSpace creates a Directory instance by getting the root cid of the drive using FS API
// PrivateSpace create a DrivePrivateSpace struct. PrivateSpace creates a Directory instance by getting the root cid of the drive using FS API
func (ud *UserDrive) PrivateSpace(ctx context.Context, api fxiface.CoreAPI) (*DrivePrivateSpace, error) {
rpath := makePath(ud.PrivateSpaceCid)
rootDir, err := api.PrivateFS().Get(ctx, rpath)
Expand Down
8 changes: 4 additions & 4 deletions drive/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import (
"errors"
)

// An in-memory list of drives which acts as a testing infra for DriveStore
// DriveIndex an in-memory list of drives which acts as a testing infra for DriveStore
type DriveIndex struct {
drives map[string]UserDrive
}

// Interface for a Drive Store. DriveStore handles logic for discovering, updating and persisting a drive
// DriveStore Interface for a Drive Store. DriveStore handles logic for discovering, updating and persisting a drive
type DriveStore interface {
Resolve(string) (UserDrive, error)
ResolveCreate(string) (UserDrive, error)
Put(UserDrive) error
Update(UserDrive) error
}

// Create a DriveIndex with an empty list of drives
// NewDriveStore create a DriveIndex with an empty list of drives
func NewDriveStore() *DriveIndex {
return &DriveIndex{drives: make(map[string]UserDrive)}
}
Expand All @@ -31,7 +31,7 @@ func (di *DriveIndex) Resolve(userDID string) (UserDrive, error) {
return UserDrive{}, errors.New("Drive not found")
}

// Resolve a Drive based on UserDID, if the drive does not exist creates one, puts it in the store and returns it
// ResolveCreate resolve a Drive based on UserDID, if the drive does not exist creates one, puts it in the store and returns it
func (di *DriveIndex) ResolveCreate(userDID string) (UserDrive, error) {
d, err := di.Resolve(userDID)
if err == nil {
Expand Down
3 changes: 2 additions & 1 deletion drive/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package drive

import "strings"

// Convert a path into a slice containing segments of the path
// PathSlice convert a path into a slice containing segments of the path
// TODO: make the unit test for PathSlice
func PathSlice(path string) []string {
sp := strings.Split(path, "/")

Expand Down
4 changes: 2 additions & 2 deletions event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package event

import (
"github.com/ipld/go-ipld-prime"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p/core"
)

type Event struct {
Version string
Previous ipld.Link
Peer peer.ID
Peer core.PeerID
Signature []byte
}
1 change: 1 addition & 0 deletions event/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var (

func init() {
typeSystem, err := ipld.LoadSchemaBytes(schemaBytes)
//TODO: add retry logic instead of panic
if err != nil {
panic(fmt.Errorf("cannot load schema: %w", err))
}
Expand Down
6 changes: 3 additions & 3 deletions fxfs/core/api/coreapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,17 @@ func NewCoreAPI(n *core.IpfsNode, c ipfsiface.CoreAPI, opts ...options.ApiOption
return (&CoreAPI{nd: n, parentOpts: *parentOpts, nc: c}).WithIPFSOptions(opts...)
}

// Unixfs returns the UnixfsAPI interface implementation backed by the go-ipfs node
// PrivateFS unixfs returns the UnixfsAPI interface implementation backed by the go-ipfs node
func (api *CoreAPI) PrivateFS() fxfsiface.PrivateFS {
return (*PrivateAPI)(api)
}

// Block returns the BlockAPI interface implementation backed by the go-ipfs node
// PublicFS block returns the BlockAPI interface implementation backed by the go-ipfs node
func (api *CoreAPI) PublicFS() fxfsiface.PublicFS {
return (*PublicAPI)(api)
}

// WithOptions returns fxCoreAPI api with global options applied
// WithIPFSOptions withOptions returns fxCoreAPI api with global options applied
func (api *CoreAPI) WithIPFSOptions(opts ...options.ApiOption) (fxfsiface.CoreAPI, error) {
settings := api.parentOpts // make sure to copy
_, err := options.ApiOptionsTo(&settings, opts...)
Expand Down
Loading

0 comments on commit 42a608b

Please sign in to comment.