2024-10-07 10:24:30 -04:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2024-10-10 13:44:17 -04:00
|
|
|
dwngen "github.com/onsonr/sonr/internal/dwn/gen"
|
2024-10-07 10:24:30 -04:00
|
|
|
"github.com/onsonr/sonr/x/vault/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// assembleVault assembles the initial vault
|
2024-10-18 13:07:52 -04:00
|
|
|
func (k Keeper) assembleVault(cotx sdk.Context) (string, int64, error) {
|
2024-10-15 14:31:19 -04:00
|
|
|
_, con, err := k.DIDKeeper.NewController(cotx)
|
2024-10-07 10:24:30 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", 0, err
|
|
|
|
}
|
|
|
|
usrKs, err := con.ExportUserKs()
|
|
|
|
if err != nil {
|
|
|
|
return "", 0, err
|
|
|
|
}
|
2024-10-18 13:07:52 -04:00
|
|
|
sch, err := k.currentSchema(cotx)
|
2024-10-07 10:24:30 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", 0, err
|
|
|
|
}
|
|
|
|
v, err := types.NewVault(usrKs, con.SonrAddress(), con.ChainID(), sch)
|
|
|
|
if err != nil {
|
|
|
|
return "", 0, err
|
|
|
|
}
|
|
|
|
cid, err := k.ipfsClient.Unixfs().Add(context.Background(), v.FS)
|
|
|
|
if err != nil {
|
|
|
|
return "", 0, err
|
|
|
|
}
|
2024-10-18 13:07:52 -04:00
|
|
|
return cid.String(), calculateBlockExpiry(cotx, time.Second*30), nil
|
2024-10-07 10:24:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// currentSchema returns the current schema
|
2024-10-18 13:07:52 -04:00
|
|
|
func (k Keeper) currentSchema(ctx sdk.Context) (*dwngen.Schema, error) {
|
2024-10-07 10:24:30 -04:00
|
|
|
p, err := k.Params.Get(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
schema := p.Schema
|
2024-10-07 20:35:40 -04:00
|
|
|
return &dwngen.Schema{
|
2024-10-07 10:24:30 -04:00
|
|
|
Version: int(schema.Version),
|
|
|
|
Account: schema.Account,
|
|
|
|
Asset: schema.Asset,
|
|
|
|
Chain: schema.Chain,
|
|
|
|
Credential: schema.Credential,
|
|
|
|
Jwk: schema.Jwk,
|
|
|
|
Grant: schema.Grant,
|
|
|
|
Keyshare: schema.Keyshare,
|
|
|
|
Profile: schema.Profile,
|
|
|
|
}, nil
|
|
|
|
}
|
2024-10-18 13:07:52 -04:00
|
|
|
|
|
|
|
func calculateBlockExpiry(sdkctx sdk.Context, duration time.Duration) int64 {
|
|
|
|
blockTime := sdkctx.BlockTime()
|
|
|
|
avgBlockTime := float64(blockTime.Sub(blockTime).Seconds())
|
|
|
|
return int64(duration.Seconds() / avgBlockTime)
|
|
|
|
}
|