sonr/x/did/keeper/ipfs.go
Prad Nukala 2c1cf56e3c
feature/driver indexed db (#14)
* fix: update commitizen version

* feat: add WASM build tags to db actions

* feat: Update all actions to follow `AddAsset` for error handling

* feat: remove database dependency in dwn and motr commands

* feat: add basic info form to registration view

* feat: implement basic browser navigation component

* refactor: move database related files to middleware

* fix: remove unused test command

* fix: update source directory for buf-publish workflow

* feat: embed dwn config data

* feat: add Sync RPC to query service

* refactor: rename  package to  for better organization

* feat: add new javascript exception handling for server requests

* refactor: move dwn.wasm to embed directory

* refactor: move server files to a new directory

* refactor: move session related code to client package

* refactor: Update dwn.wasm build path

* refactor: move dwn wasm build to vfs

* feat: introduce config loading middleware

* feat: introduce DWN config and address JSON

* refactor: move dwn wasm build output to embed directory

* feat: introduce  config and IndexedDB model

* refactor: move DWN config file generation to vfs

* refactor: move config package to

* feat: add Sonr.ID IPFS gateway proxy

* feat: add SWT data structure

* feat: update index.html to use Sonr styles and scripts

* feat(dwn): remove index.html server endpoint

* feat: add Navigator API for web credential management
2024-09-18 02:22:17 -04:00

118 lines
2.8 KiB
Go

package keeper
import (
"context"
"fmt"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ipfs/boxo/files"
"github.com/ipfs/boxo/path"
"github.com/ipfs/kubo/client/rpc"
"github.com/ipfs/kubo/core/coreiface/options"
"github.com/onsonr/sonr/internal/vfs"
)
// assembleInitialVault assembles the initial vault
func (k Keeper) assembleInitialVault(ctx sdk.Context) (string, int64, error) {
cnfg, err := vfs.NewDWNConfigFile("test", "test")
if err != nil {
return "", 0, err
}
fileMap := map[string]files.Node{
"config.pkl": cnfg,
"sw.js": vfs.SWJSFile(),
"app.wasm": vfs.DWNWasmFile(),
"index.html": vfs.IndexFile(),
}
cid, err := k.ipfsClient.Unixfs().Add(context.Background(), files.NewMapDirectory(fileMap))
if err != nil {
return "", 0, err
}
return cid.String(), k.GetExpirationBlockHeight(ctx, time.Second*15), nil
}
// pinInitialVault pins the initial vault to the local IPFS node
func (k Keeper) pinInitialVault(_ sdk.Context, cid string, address string) (bool, error) {
// Resolve the path
path, err := path.NewPath(cid)
if err != nil {
return false, err
}
// 1. Initialize vault.db sqlite database in local IPFS with Mount
// 2. Insert the InitialWalletAccounts
// 3. Publish the path to the IPNS
_, err = k.ipfsClient.Name().Publish(context.Background(), path, options.Name.Key(address))
if err != nil {
return false, err
}
// 4. Insert the accounts into x/auth
// 5. Insert the controller into state
return true, nil
}
// GetFromIPFS gets a file from the local IPFS node
func (k Keeper) GetFromIPFS(ctx sdk.Context, cid string) (files.Directory, error) {
path, err := path.NewPath(cid)
if err != nil {
return nil, err
}
node, err := k.ipfsClient.Unixfs().Get(ctx, path)
if err != nil {
return nil, err
}
dir, ok := node.(files.Directory)
if !ok {
return nil, fmt.Errorf("retrieved node is not a directory")
}
return dir, nil
}
// HasIPFSConnection returns true if the IPFS client is initialized
func (k *Keeper) HasIPFSConnection() bool {
if k.ipfsClient == nil {
ipfsClient, err := rpc.NewLocalApi()
if err != nil {
return false
}
k.ipfsClient = ipfsClient
}
return k.ipfsClient != nil
}
// HasPathInIPFS checks if a file is in the local IPFS node
func (k Keeper) HasPathInIPFS(ctx sdk.Context, cid string) (bool, error) {
path, err := path.NewPath(cid)
if err != nil {
return false, err
}
v, err := k.ipfsClient.Unixfs().Get(ctx, path)
if err != nil {
return false, err
}
if v == nil {
return false, nil
}
return true, nil
}
// PinToIPFS pins a file to the local IPFS node
func (k Keeper) PinToIPFS(ctx sdk.Context, cid string, name string) error {
path, err := path.NewPath(cid)
if err != nil {
return err
}
err = k.ipfsClient.Pin().Add(ctx, path, options.Pin.Name(name))
if err != nil {
return err
}
return nil
}