mirror of
https://github.com/sonr-io/sonr.git
synced 2026-08-03 18:01:39 +00:00
feature/implement vault allocation (#11)
* feat: add authentication middleware * feat: add REST API endpoints for database interactions * refactor: move DiscoveryDocument Pkl schema to oidc module * fix: replace sonrd with test_node.sh * feat: use NFT keeper to mint DID namespace NFT * refactor: move NFT class configuration to types * feat: add GlobalIntegrity genesis state * fix: ensure GlobalIntegrity is initialized in genesis * refactor: update all references to transactions module * refactor: improve genesis state struct * chore(did): update discovery endpoint to reflect base url * feat: remove unused context cache and client code * refactor: remove middleware dependency from keeper * feat: Add new query handlers for DID module * feat: Implement unimplemented params queries * feat: add support for first-party caveats * refactor: move motr command to cmd directory * feat: add support for GitHub releases * fix(motr): build app.wasm for motr package * feat: add card component * feat: add IndexedDB support for persistent storage * feat: Add Row and Column components * feat: add and components * refactor: improve button component * refactor: remove unnecessary button parameter in renderButton * feat: add vault service endpoint * feat: add input component
This commit is contained in:
+249
-10
@@ -12,11 +12,11 @@ func createInitialTables(db *gorm.DB) (*DB, error) {
|
||||
err := db.AutoMigrate(
|
||||
&orm.Account{},
|
||||
&orm.Asset{},
|
||||
&orm.Keyshare{},
|
||||
&orm.Credential{},
|
||||
&orm.Keyshare{},
|
||||
&orm.Permission{},
|
||||
&orm.Profile{},
|
||||
&orm.Property{},
|
||||
&orm.Permission{},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create table: %w", err)
|
||||
@@ -35,12 +35,75 @@ func (db *DB) AddAccount(account *orm.Account) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddKeyshare adds a new keyshare to the database
|
||||
func (db *DB) AddKeyshare(keyshare *orm.Keyshare) error {
|
||||
tx := db.Create(keyshare)
|
||||
// GetAccount gets an account from the database
|
||||
func (db *DB) GetAccount(account *orm.Account) error {
|
||||
tx := db.First(account)
|
||||
if tx.Error != nil {
|
||||
return fmt.Errorf("failed to get account: %w", tx.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateAccount updates an existing account in the database
|
||||
func (db *DB) UpdateAccount(account *orm.Account) error {
|
||||
tx := db.Save(account)
|
||||
if tx.Error != nil {
|
||||
return fmt.Errorf("failed to update account: %w", tx.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteAccount deletes an existing account from the database
|
||||
func (db *DB) DeleteAccount(account *orm.Account) error {
|
||||
tx := db.Delete(account)
|
||||
if tx.Error != nil {
|
||||
return fmt.Errorf("failed to delete account: %w", tx.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddAsset adds a new asset to the database
|
||||
func (db *DB) AddAsset(asset *orm.Asset) error {
|
||||
tx := db.Create(asset)
|
||||
|
||||
if tx.Error != nil {
|
||||
return fmt.Errorf("failed to add keyshare: %w", tx.Error)
|
||||
return fmt.Errorf("failed to add asset: %w", tx.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetAsset gets an asset from the database
|
||||
func (db *DB) GetAsset(asset *orm.Asset) error {
|
||||
tx := db.First(asset)
|
||||
|
||||
if tx.Error != nil {
|
||||
return fmt.Errorf("failed to get asset: %w", tx.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateAsset updates an existing asset in the database
|
||||
func (db *DB) UpdateAsset(asset *orm.Asset) error {
|
||||
tx := db.Save(asset)
|
||||
|
||||
if tx.Error != nil {
|
||||
return fmt.Errorf("failed to update asset: %w", tx.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteAsset deletes an existing asset from the database
|
||||
func (db *DB) DeleteAsset(asset *orm.Asset) error {
|
||||
tx := db.Delete(asset)
|
||||
|
||||
if tx.Error != nil {
|
||||
return fmt.Errorf("failed to delete asset: %w", tx.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -57,6 +120,127 @@ func (db *DB) AddCredential(credential *orm.Credential) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetCredential gets an credential from the database
|
||||
func (db *DB) GetCredential(credential *orm.Credential) error {
|
||||
tx := db.First(credential)
|
||||
|
||||
if tx.Error != nil {
|
||||
return fmt.Errorf("failed to get credential: %w", tx.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateCredential updates an existing credential in the database
|
||||
func (db *DB) UpdateCredential(credential *orm.Credential) error {
|
||||
tx := db.Save(credential)
|
||||
|
||||
if tx.Error != nil {
|
||||
return fmt.Errorf("failed to update credential: %w", tx.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteCredential deletes an existing credential from the database
|
||||
func (db *DB) DeleteCredential(credential *orm.Credential) error {
|
||||
tx := db.Delete(credential)
|
||||
|
||||
if tx.Error != nil {
|
||||
return fmt.Errorf("failed to delete credential: %w", tx.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddKeyshare adds a new keyshare to the database
|
||||
func (db *DB) AddKeyshare(keyshare *orm.Keyshare) error {
|
||||
tx := db.Create(keyshare)
|
||||
|
||||
if tx.Error != nil {
|
||||
return fmt.Errorf("failed to add keyshare: %w", tx.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetKeyshare gets an keyshare from the database
|
||||
func (db *DB) GetKeyshare(keyshare *orm.Keyshare) error {
|
||||
tx := db.First(keyshare)
|
||||
|
||||
if tx.Error != nil {
|
||||
return fmt.Errorf("failed to get keyshare: %w", tx.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateKeyshare updates an existing keyshare in the database
|
||||
func (db *DB) UpdateKeyshare(keyshare *orm.Keyshare) error {
|
||||
tx := db.Save(keyshare)
|
||||
|
||||
if tx.Error != nil {
|
||||
return fmt.Errorf("failed to update keyshare: %w", tx.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteKeyshare deletes an existing keyshare from the database
|
||||
func (db *DB) DeleteKeyshare(keyshare *orm.Keyshare) error {
|
||||
tx := db.Delete(keyshare)
|
||||
|
||||
if tx.Error != nil {
|
||||
return fmt.Errorf("failed to delete keyshare: %w", tx.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddPermission adds a new permission to the database
|
||||
func (db *DB) AddPermission(permission *orm.Permission) error {
|
||||
tx := db.Create(permission)
|
||||
|
||||
if tx.Error != nil {
|
||||
return fmt.Errorf("failed to add permission: %w", tx.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetPermission gets an permission from the database
|
||||
func (db *DB) GetPermission(permission *orm.Permission) error {
|
||||
tx := db.First(permission)
|
||||
|
||||
if tx.Error != nil {
|
||||
return fmt.Errorf("failed to get permission: %w", tx.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdatePermission updates an existing permission in the database
|
||||
func (db *DB) UpdatePermission(permission *orm.Permission) error {
|
||||
tx := db.Save(permission)
|
||||
|
||||
if tx.Error != nil {
|
||||
return fmt.Errorf("failed to update permission: %w", tx.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeletePermission deletes an existing permission from the database
|
||||
func (db *DB) DeletePermission(permission *orm.Permission) error {
|
||||
tx := db.Delete(permission)
|
||||
|
||||
if tx.Error != nil {
|
||||
return fmt.Errorf("failed to delete permission: %w", tx.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddProfile adds a new profile to the database
|
||||
func (db *DB) AddProfile(profile *orm.Profile) error {
|
||||
tx := db.Create(profile)
|
||||
@@ -68,6 +252,39 @@ func (db *DB) AddProfile(profile *orm.Profile) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetProfile gets an profile from the database
|
||||
func (db *DB) GetProfile(profile *orm.Profile) error {
|
||||
tx := db.First(profile)
|
||||
|
||||
if tx.Error != nil {
|
||||
return fmt.Errorf("failed to get profile: %w", tx.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateProfile updates an existing profile in the database
|
||||
func (db *DB) UpdateProfile(profile *orm.Profile) error {
|
||||
tx := db.Save(profile)
|
||||
|
||||
if tx.Error != nil {
|
||||
return fmt.Errorf("failed to update profile: %w", tx.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteProfile deletes an existing profile from the database
|
||||
func (db *DB) DeleteProfile(profile *orm.Profile) error {
|
||||
tx := db.Delete(profile)
|
||||
|
||||
if tx.Error != nil {
|
||||
return fmt.Errorf("failed to delete profile: %w", tx.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddProperty adds a new property to the database
|
||||
func (db *DB) AddProperty(property *orm.Property) error {
|
||||
tx := db.Create(property)
|
||||
@@ -79,12 +296,34 @@ func (db *DB) AddProperty(property *orm.Property) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddPermission adds a new permission to the database
|
||||
func (db *DB) AddPermission(permission *orm.Permission) error {
|
||||
tx := db.Create(permission)
|
||||
// GetProperty gets an property from the database
|
||||
func (db *DB) GetProperty(property *orm.Property) error {
|
||||
tx := db.First(property)
|
||||
|
||||
if tx.Error != nil {
|
||||
return fmt.Errorf("failed to add permission: %w", tx.Error)
|
||||
return fmt.Errorf("failed to get property: %w", tx.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateProperty updates an existing property in the database
|
||||
func (db *DB) UpdateProperty(property *orm.Property) error {
|
||||
tx := db.Save(property)
|
||||
|
||||
if tx.Error != nil {
|
||||
return fmt.Errorf("failed to update property: %w", tx.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteProperty deletes an existing property from the database
|
||||
func (db *DB) DeleteProperty(property *orm.Property) error {
|
||||
tx := db.Delete(property)
|
||||
|
||||
if tx.Error != nil {
|
||||
return fmt.Errorf("failed to delete property: %w", tx.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
+40
-3
@@ -1,17 +1,54 @@
|
||||
package db
|
||||
|
||||
import "gorm.io/gorm"
|
||||
import (
|
||||
"crypto/rand"
|
||||
|
||||
"github.com/ncruces/go-sqlite3/gormlite"
|
||||
"golang.org/x/crypto/argon2"
|
||||
"gorm.io/gorm"
|
||||
"lukechampine.com/adiantum/hbsh"
|
||||
"lukechampine.com/adiantum/hpolyc"
|
||||
)
|
||||
|
||||
type DB struct {
|
||||
*gorm.DB
|
||||
}
|
||||
|
||||
func New(opts ...DBOption) *DBConfig {
|
||||
func New(opts ...DBOption) (*DB, error) {
|
||||
config := &DBConfig{
|
||||
fileName: "vault.db",
|
||||
}
|
||||
for _, opt := range opts {
|
||||
opt(config)
|
||||
}
|
||||
return config
|
||||
gormdb, err := gorm.Open(gormlite.Open(config.ConnectionString()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
db, err := createInitialTables(gormdb)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return db, nil
|
||||
}
|
||||
|
||||
// HBSH creates an HBSH cipher given a key.
|
||||
func (c *DB) HBSH(key []byte) *hbsh.HBSH {
|
||||
if len(key) != 32 {
|
||||
// Key is not appropriate, return nil.
|
||||
return nil
|
||||
}
|
||||
return hpolyc.New(key)
|
||||
}
|
||||
|
||||
// KDF gets a key from a secret.
|
||||
func (c *DB) KDF(secret string) []byte {
|
||||
if secret == "" {
|
||||
// No secret is given, generate a random key.
|
||||
key := make([]byte, 32)
|
||||
n, _ := rand.Read(key)
|
||||
return key[:n]
|
||||
}
|
||||
// Hash the secret with a KDF.
|
||||
return argon2.IDKey([]byte(secret), []byte("hpolyc"), 3, 64*1024, 4, 32)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,195 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/onsonr/sonr/internal/db/orm"
|
||||
)
|
||||
|
||||
func (db *DB) ServeEcho(e *echo.Group) {
|
||||
e.GET("/accounts", db.HandleAccount)
|
||||
e.GET("/assets", db.HandleAsset)
|
||||
e.GET("/credentials", db.HandleCredential)
|
||||
e.GET("/keyshares", db.HandleKeyshare)
|
||||
e.GET("/permissions", db.HandlePermission)
|
||||
e.GET("/profiles", db.HandleProfile)
|
||||
e.GET("/properties", db.HandleProperty)
|
||||
}
|
||||
|
||||
func (db *DB) HandleAccount(c echo.Context) error {
|
||||
data := new(orm.Account)
|
||||
if err := c.Bind(data); err != nil {
|
||||
return err
|
||||
}
|
||||
// Check the method for GET, POST, PUT, DELETE
|
||||
switch c.Request().Method {
|
||||
case echo.POST:
|
||||
|
||||
if err := db.AddAccount(data); err != nil {
|
||||
return err
|
||||
}
|
||||
return c.JSON(200, "OK")
|
||||
case echo.PUT:
|
||||
|
||||
if err := db.UpdateAccount(data); err != nil {
|
||||
return err
|
||||
}
|
||||
return c.JSON(200, "OK")
|
||||
case echo.DELETE:
|
||||
if err := db.DeleteAccount(data); err != nil {
|
||||
return err
|
||||
}
|
||||
return c.JSON(200, nil)
|
||||
}
|
||||
return c.JSON(200, data)
|
||||
}
|
||||
|
||||
func (db *DB) HandleAsset(c echo.Context) error {
|
||||
data := new(orm.Asset)
|
||||
if err := c.Bind(data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch c.Request().Method {
|
||||
case echo.POST:
|
||||
if err := db.AddAsset(data); err != nil {
|
||||
return err
|
||||
}
|
||||
return c.JSON(200, "OK")
|
||||
case echo.PUT:
|
||||
if err := db.UpdateAsset(data); err != nil {
|
||||
return err
|
||||
}
|
||||
return c.JSON(200, "OK")
|
||||
case echo.DELETE:
|
||||
if err := db.DeleteAsset(data); err != nil {
|
||||
return err
|
||||
}
|
||||
return c.JSON(200, "OK")
|
||||
}
|
||||
return c.JSON(200, data)
|
||||
}
|
||||
|
||||
func (db *DB) HandleCredential(c echo.Context) error {
|
||||
data := new(orm.Credential)
|
||||
if err := c.Bind(data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch c.Request().Method {
|
||||
case echo.POST:
|
||||
if err := db.AddCredential(data); err != nil {
|
||||
return err
|
||||
}
|
||||
return c.JSON(200, "OK")
|
||||
case echo.PUT:
|
||||
if err := db.UpdateCredential(data); err != nil {
|
||||
return err
|
||||
}
|
||||
return c.JSON(200, "OK")
|
||||
case echo.DELETE:
|
||||
if err := db.DeleteCredential(data); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return c.JSON(200, data)
|
||||
}
|
||||
|
||||
func (db *DB) HandleKeyshare(c echo.Context) error {
|
||||
data := new(orm.Keyshare)
|
||||
if err := c.Bind(data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch c.Request().Method {
|
||||
case echo.POST:
|
||||
if err := db.AddKeyshare(data); err != nil {
|
||||
return err
|
||||
}
|
||||
return c.JSON(200, "OK")
|
||||
case echo.PUT:
|
||||
if err := db.UpdateKeyshare(data); err != nil {
|
||||
return err
|
||||
}
|
||||
return c.JSON(200, "OK")
|
||||
case echo.DELETE:
|
||||
if err := db.DeleteKeyshare(data); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return c.JSON(200, data)
|
||||
}
|
||||
|
||||
func (db *DB) HandlePermission(c echo.Context) error {
|
||||
data := new(orm.Permission)
|
||||
if err := c.Bind(data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch c.Request().Method {
|
||||
case echo.POST:
|
||||
if err := db.AddPermission(data); err != nil {
|
||||
return err
|
||||
}
|
||||
return c.JSON(200, "OK")
|
||||
case echo.PUT:
|
||||
if err := db.UpdatePermission(data); err != nil {
|
||||
return err
|
||||
}
|
||||
return c.JSON(200, "OK")
|
||||
case echo.DELETE:
|
||||
if err := db.DeletePermission(data); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return c.JSON(200, data)
|
||||
}
|
||||
|
||||
func (db *DB) HandleProfile(c echo.Context) error {
|
||||
data := new(orm.Profile)
|
||||
if err := c.Bind(data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch c.Request().Method {
|
||||
case echo.POST:
|
||||
if err := db.AddProfile(data); err != nil {
|
||||
return err
|
||||
}
|
||||
return c.JSON(200, "OK")
|
||||
case echo.PUT:
|
||||
if err := db.UpdateProfile(data); err != nil {
|
||||
return err
|
||||
}
|
||||
return c.JSON(200, "OK")
|
||||
case echo.DELETE:
|
||||
if err := db.DeleteProfile(data); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return c.JSON(200, data)
|
||||
}
|
||||
|
||||
func (db *DB) HandleProperty(c echo.Context) error {
|
||||
data := new(orm.Property)
|
||||
if err := c.Bind(data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch c.Request().Method {
|
||||
case echo.POST:
|
||||
if err := db.AddProperty(data); err != nil {
|
||||
return err
|
||||
}
|
||||
return c.JSON(200, "OK")
|
||||
case echo.PUT:
|
||||
if err := db.UpdateProperty(data); err != nil {
|
||||
return err
|
||||
}
|
||||
return c.JSON(200, "OK")
|
||||
case echo.DELETE:
|
||||
if err := db.DeleteProperty(data); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return c.JSON(200, data)
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
//go:build js && wasm
|
||||
// +build js,wasm
|
||||
|
||||
package idb
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"syscall/js"
|
||||
|
||||
"github.com/hack-pad/go-indexeddb/idb"
|
||||
)
|
||||
|
||||
// Model is an interface that must be implemented by types used with Table
|
||||
type Model interface {
|
||||
Table() string
|
||||
}
|
||||
|
||||
// Table is a generic wrapper around IDB for easier database operations on a specific table
|
||||
type Table[T Model] struct {
|
||||
db *idb.Database
|
||||
dbName string
|
||||
keyPath string
|
||||
}
|
||||
|
||||
// NewTable creates a new Table instance
|
||||
func NewTable[T Model](dbName string, version uint, keyPath string) (*Table[T], error) {
|
||||
ctx := context.Background()
|
||||
factory := idb.Global()
|
||||
|
||||
var model T
|
||||
tableName := model.Table()
|
||||
|
||||
openRequest, err := factory.Open(ctx, dbName, version, func(db *idb.Database, oldVersion, newVersion uint) error {
|
||||
_, err := db.CreateObjectStore(tableName, idb.ObjectStoreOptions{
|
||||
KeyPath: js.ValueOf(keyPath),
|
||||
})
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
db, err := openRequest.Await(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Table[T]{
|
||||
db: db,
|
||||
dbName: dbName,
|
||||
keyPath: keyPath,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Insert adds a new record to the table
|
||||
func (t *Table[T]) Insert(data T) error {
|
||||
tx, err := t.db.Transaction(idb.TransactionReadWrite, data.Table())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Commit()
|
||||
|
||||
objectStore, err := tx.ObjectStore(data.Table())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = objectStore.Add(js.ValueOf(string(jsonData)))
|
||||
return err
|
||||
}
|
||||
|
||||
// Query retrieves a record from the table based on a key
|
||||
func (t *Table[T]) Query(key interface{}) (T, error) {
|
||||
var result T
|
||||
|
||||
tx, err := t.db.Transaction(idb.TransactionReadOnly, result.Table())
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
defer tx.Commit()
|
||||
|
||||
objectStore, err := tx.ObjectStore(result.Table())
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
request, err := objectStore.Get(js.ValueOf(key))
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
value, err := request.Await(context.Background())
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
if value.IsUndefined() || value.IsNull() {
|
||||
return result, errors.New("record not found")
|
||||
}
|
||||
|
||||
err = json.Unmarshal([]byte(value.String()), &result)
|
||||
return result, err
|
||||
}
|
||||
|
||||
// Delete removes a record from the table based on a key
|
||||
func (t *Table[T]) Delete(key interface{}) error {
|
||||
var model T
|
||||
tx, err := t.db.Transaction(idb.TransactionReadWrite, model.Table())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Commit()
|
||||
|
||||
objectStore, err := tx.ObjectStore(model.Table())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = objectStore.Delete(js.ValueOf(key))
|
||||
return err
|
||||
}
|
||||
|
||||
// Close closes the database connection
|
||||
func (t *Table[T]) Close() error {
|
||||
return t.db.Close()
|
||||
}
|
||||
+11
-41
@@ -1,20 +1,13 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
|
||||
"github.com/ncruces/go-sqlite3/gormlite"
|
||||
"golang.org/x/crypto/argon2"
|
||||
"gorm.io/gorm"
|
||||
"lukechampine.com/adiantum/hbsh"
|
||||
"lukechampine.com/adiantum/hpolyc"
|
||||
|
||||
_ "github.com/ncruces/go-sqlite3/embed"
|
||||
"github.com/onsonr/sonr/internal/db/orm"
|
||||
)
|
||||
|
||||
type DBOption func(config *DBConfig)
|
||||
|
||||
func WithDir(dir string) DBOption {
|
||||
func WitDir(dir string) DBOption {
|
||||
return func(config *DBConfig) {
|
||||
config.Dir = dir
|
||||
}
|
||||
@@ -30,41 +23,18 @@ type DBConfig struct {
|
||||
Dir string
|
||||
SecretKey string
|
||||
|
||||
fileName string
|
||||
fileName string
|
||||
initialAccounts []*orm.Account
|
||||
initialAssets []*orm.Asset
|
||||
initialCredentials []*orm.Credential
|
||||
initialKeyshares []*orm.Keyshare
|
||||
initialPermissions []*orm.Permission
|
||||
initialProfiles []*orm.Profile
|
||||
initialProperties []*orm.Property
|
||||
}
|
||||
|
||||
func (config *DBConfig) ConnectionString() string {
|
||||
connStr := "file:"
|
||||
connStr += config.Dir + "/" + config.fileName
|
||||
connStr += config.fileName
|
||||
return connStr
|
||||
}
|
||||
|
||||
// GormDialector creates a gorm dialector for the database.
|
||||
func (config *DBConfig) Open() (*DB, error) {
|
||||
db, err := gorm.Open(gormlite.Open(config.ConnectionString()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return createInitialTables(db)
|
||||
}
|
||||
|
||||
// HBSH creates an HBSH cipher given a key.
|
||||
func (c *DBConfig) HBSH(key []byte) *hbsh.HBSH {
|
||||
if len(key) != 32 {
|
||||
// Key is not appropriate, return nil.
|
||||
return nil
|
||||
}
|
||||
return hpolyc.New(key)
|
||||
}
|
||||
|
||||
// KDF gets a key from a secret.
|
||||
func (c *DBConfig) KDF(secret string) []byte {
|
||||
if secret == "" {
|
||||
// No secret is given, generate a random key.
|
||||
key := make([]byte, 32)
|
||||
n, _ := rand.Read(key)
|
||||
return key[:n]
|
||||
}
|
||||
// Hash the secret with a KDF.
|
||||
return argon2.IDKey([]byte(secret), []byte("hpolyc"), 3, 64*1024, 4, 32)
|
||||
}
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
// Code generated from Pkl module `orm`. DO NOT EDIT.
|
||||
package orm
|
||||
|
||||
type DiscoveryDocument struct {
|
||||
Issuer string `pkl:"issuer" json:"issuer,omitempty" param:"issuer"`
|
||||
|
||||
AuthorizationEndpoint string `pkl:"authorization_endpoint" json:"authorization_endpoint,omitempty" param:"authorization_endpoint"`
|
||||
|
||||
TokenEndpoint string `pkl:"token_endpoint" json:"token_endpoint,omitempty" param:"token_endpoint"`
|
||||
|
||||
UserinfoEndpoint string `pkl:"userinfo_endpoint" json:"userinfo_endpoint,omitempty" param:"userinfo_endpoint"`
|
||||
|
||||
JwksUri string `pkl:"jwks_uri" json:"jwks_uri,omitempty" param:"jwks_uri"`
|
||||
|
||||
RegistrationEndpoint string `pkl:"registration_endpoint" json:"registration_endpoint,omitempty" param:"registration_endpoint"`
|
||||
|
||||
ScopesSupported []string `pkl:"scopes_supported" json:"scopes_supported,omitempty" param:"scopes_supported"`
|
||||
|
||||
ResponseTypesSupported []string `pkl:"response_types_supported" json:"response_types_supported,omitempty" param:"response_types_supported"`
|
||||
|
||||
ResponseModesSupported []string `pkl:"response_modes_supported" json:"response_modes_supported,omitempty" param:"response_modes_supported"`
|
||||
|
||||
SubjectTypesSupported []string `pkl:"subject_types_supported" json:"subject_types_supported,omitempty" param:"subject_types_supported"`
|
||||
|
||||
IdTokenSigningAlgValuesSupported []string `pkl:"id_token_signing_alg_values_supported" json:"id_token_signing_alg_values_supported,omitempty" param:"id_token_signing_alg_values_supported"`
|
||||
|
||||
ClaimsSupported []string `pkl:"claims_supported" json:"claims_supported,omitempty" param:"claims_supported"`
|
||||
|
||||
GrantTypesSupported []string `pkl:"grant_types_supported" json:"grant_types_supported,omitempty" param:"grant_types_supported"`
|
||||
|
||||
AcrValuesSupported []string `pkl:"acr_values_supported" json:"acr_values_supported,omitempty" param:"acr_values_supported"`
|
||||
|
||||
TokenEndpointAuthMethodsSupported []string `pkl:"token_endpoint_auth_methods_supported" json:"token_endpoint_auth_methods_supported,omitempty" param:"token_endpoint_auth_methods_supported"`
|
||||
}
|
||||
@@ -4,17 +4,9 @@ package orm
|
||||
type Keyshare struct {
|
||||
Id uint `pkl:"id" gorm:"primaryKey,autoIncrement" json:"id,omitempty" query:"id"`
|
||||
|
||||
Metadata string `pkl:"metadata" json:"metadata,omitempty" param:"metadata"`
|
||||
|
||||
Payloads string `pkl:"payloads" json:"payloads,omitempty" param:"payloads"`
|
||||
|
||||
Protocol string `pkl:"protocol" json:"protocol,omitempty" param:"protocol"`
|
||||
|
||||
PublicKey string `pkl:"publicKey" json:"publicKey,omitempty" param:"publicKey"`
|
||||
Data string `pkl:"data" json:"data,omitempty" param:"data"`
|
||||
|
||||
Role int `pkl:"role" json:"role,omitempty" param:"role"`
|
||||
|
||||
Version int `pkl:"version" json:"version,omitempty" param:"version"`
|
||||
|
||||
CreatedAt *string `pkl:"createdAt" json:"createdAt,omitempty" param:"createdAt"`
|
||||
}
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
// Code generated from Pkl module `orm`. DO NOT EDIT.
|
||||
package orm
|
||||
|
||||
type PublicKey struct {
|
||||
Id uint `pkl:"id" gorm:"primaryKey,autoIncrement" json:"id,omitempty" query:"id"`
|
||||
|
||||
Role int `pkl:"role" json:"role,omitempty" param:"role"`
|
||||
|
||||
Algorithm int `pkl:"algorithm" json:"algorithm,omitempty" param:"algorithm"`
|
||||
|
||||
Encoding int `pkl:"encoding" json:"encoding,omitempty" param:"encoding"`
|
||||
|
||||
Jwk string `pkl:"jwk" json:"jwk,omitempty" param:"jwk"`
|
||||
|
||||
CreatedAt *string `pkl:"createdAt" json:"createdAt,omitempty" param:"createdAt"`
|
||||
}
|
||||
@@ -12,7 +12,5 @@ func init() {
|
||||
pkl.RegisterMapping("orm#Profile", Profile{})
|
||||
pkl.RegisterMapping("orm#Property", Property{})
|
||||
pkl.RegisterMapping("orm#Keyshare", Keyshare{})
|
||||
pkl.RegisterMapping("orm#PublicKey", PublicKey{})
|
||||
pkl.RegisterMapping("orm#Permission", Permission{})
|
||||
pkl.RegisterMapping("orm#DiscoveryDocument", DiscoveryDocument{})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package orm
|
||||
|
||||
func (a *Account) Table() string {
|
||||
return "accounts"
|
||||
}
|
||||
|
||||
func (a *Asset) Table() string {
|
||||
return "assets"
|
||||
}
|
||||
|
||||
func (a *Credential) Table() string {
|
||||
return "credentials"
|
||||
}
|
||||
|
||||
func (a *Keyshare) Table() string {
|
||||
return "keyshares"
|
||||
}
|
||||
|
||||
func (a *Permission) Table() string {
|
||||
return "permissions"
|
||||
}
|
||||
|
||||
func (a *Profile) Table() string {
|
||||
return "profiles"
|
||||
}
|
||||
|
||||
func (a *Property) Table() string {
|
||||
return "properties"
|
||||
}
|
||||
Reference in New Issue
Block a user