Files
sonr/x/did/keeper/ipfs.go
T

118 lines
2.8 KiB
Go
Raw Normal View History

2024-09-05 18:37:38 -04:00
package keeper
import (
2024-09-07 18:12:58 -04:00
"context"
"fmt"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ipfs/boxo/files"
"github.com/ipfs/boxo/path"
2024-09-07 18:12:58 -04:00
"github.com/ipfs/kubo/client/rpc"
"github.com/ipfs/kubo/core/coreiface/options"
2024-09-07 18:12:58 -04:00
"github.com/onsonr/sonr/internal/vfs"
)
2024-09-07 18:12:58 -04:00
// assembleInitialVault assembles the initial vault
func (k Keeper) assembleInitialVault(ctx sdk.Context) (string, int64, error) {
2024-09-18 02:22:17 -04:00
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 {
2024-09-07 18:12:58 -04:00
return "", 0, err
}
2024-09-07 18:12:58 -04:00
return cid.String(), k.GetExpirationBlockHeight(ctx, time.Second*15), nil
}
2024-09-07 18:12:58 -04:00
// pinInitialVault pins the initial vault to the local IPFS node
2024-09-11 15:10:54 -04:00
func (k Keeper) pinInitialVault(_ sdk.Context, cid string, address string) (bool, error) {
2024-09-07 18:12:58 -04:00
// Resolve the path
path, err := path.NewPath(cid)
if err != nil {
2024-09-11 15:10:54 -04:00
return false, err
2024-09-07 18:12:58 -04:00
}
// 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 {
2024-09-11 15:10:54 -04:00
return false, err
2024-09-07 18:12:58 -04:00
}
// 4. Insert the accounts into x/auth
// 5. Insert the controller into state
2024-09-11 15:10:54 -04:00
return true, nil
2024-09-07 18:12:58 -04:00
}
// 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
}
2024-09-07 18:12:58 -04:00
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
}
2024-09-07 18:12:58 -04:00
// 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
}
2024-09-07 18:12:58 -04:00
// 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
}