mirror of
https://github.com/cf-sonr/hway.git
synced 2026-01-12 03:09:14 +00:00
refactor: move common package to pkg directory
This commit is contained in:
80
pkg/common/chain_query.go
Normal file
80
pkg/common/chain_query.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
bankv1beta1 "cosmossdk.io/api/cosmos/bank/v1beta1"
|
||||
nodev1beta1 "github.com/cosmos/cosmos-sdk/client/grpc/node"
|
||||
didv1 "github.com/onsonr/sonr/api/did/v1"
|
||||
dwnv1 "github.com/onsonr/sonr/api/dwn/v1"
|
||||
svcv1 "github.com/onsonr/sonr/api/svc/v1"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type (
|
||||
StatusResponse = nodev1beta1.StatusResponse // StatusResponse is the response type for the Service.Status RPC method.
|
||||
StatusRequest = nodev1beta1.StatusRequest // StatusRequest is the request type for the Service.Status RPC method.
|
||||
BalanceRequest = bankv1beta1.QueryBalanceRequest // BalanceRequest is the request type for the Bank.Balance RPC method.
|
||||
BalanceResponse = bankv1beta1.QueryBalanceResponse // BalanceResponse is the response type for the Bank.Balance RPC method.
|
||||
AllBalancesRequest = bankv1beta1.QueryAllBalancesRequest // AllBalancesRequest is the request type for the Bank.AllBalances RPC method.
|
||||
AllBalancesResponse = bankv1beta1.QueryAllBalancesResponse // AllBalancesResponse is the response type for the Bank.AllBalances RPC method.
|
||||
TotalSupplyRequest = bankv1beta1.QueryTotalSupplyRequest // TotalSupplyRequest is the request type for the Bank.TotalSupply RPC method.
|
||||
TotalSupplyResponse = bankv1beta1.QueryTotalSupplyResponse // TotalSupplyResponse is the response type for the Bank.TotalSupply RPC method.
|
||||
DenomMetadataRequest = bankv1beta1.QueryDenomMetadataRequest // DenomMetadataRequest is the request type for the Bank.DenomMetadata RPC method.
|
||||
DenomMetadataResponse = bankv1beta1.QueryDenomMetadataResponse // DenomMetadataResponse is the response type for the Bank.DenomMetadata RPC method.
|
||||
BankParamsRequest = bankv1beta1.QueryParamsRequest // BankParamsRequest is the request type for the Bank.Params RPC method.
|
||||
BankParamsResponse = bankv1beta1.QueryParamsResponse // BankParamsResponse is the response type for the Bank.Params RPC method.
|
||||
DIDParamsRequest = didv1.QueryRequest // DIDParamsRequest is the request type for the DID.Params RPC method.
|
||||
DIDParamsResponse = didv1.QueryParamsResponse // DIDParamsResponse is the response type for the DID.Params RPC method.
|
||||
DIDResolveResponse = didv1.QueryResolveResponse // DIDResolveResponse is the response type for the DID.Resolve RPC method.
|
||||
DWNParamsRequest = dwnv1.QueryParamsRequest // DWNParamsRequest is the request type for the DWN.Params RPC method.
|
||||
DWNParamsResponse = dwnv1.QueryParamsResponse // DWNParamsResponse is the response type for the DWN.Params RPC method.
|
||||
SVCParamsRequest = svcv1.QueryParamsRequest // SVCParamsRequest is the request type for the SVC.Params RPC method.
|
||||
SVCParamsResponse = svcv1.QueryParamsResponse // SVCParamsResponse is the response type for the SVC.Params RPC method.
|
||||
)
|
||||
|
||||
func conn(addr string) (*grpc.ClientConn, error) {
|
||||
grpcConn, err := grpc.NewClient(addr, grpc.WithInsecure())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return grpcConn, nil
|
||||
}
|
||||
|
||||
func NewBankClient(addr string) (bankv1beta1.QueryClient, error) {
|
||||
conn, err := conn(addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return bankv1beta1.NewQueryClient(conn), nil
|
||||
}
|
||||
|
||||
func NewDIDClient(addr string) (didv1.QueryClient, error) {
|
||||
conn, err := conn(addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return didv1.NewQueryClient(conn), nil
|
||||
}
|
||||
|
||||
func NewDWNClient(addr string) (dwnv1.QueryClient, error) {
|
||||
conn, err := conn(addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return dwnv1.NewQueryClient(conn), nil
|
||||
}
|
||||
|
||||
func NewNodeClient(addr string) (nodev1beta1.ServiceClient, error) {
|
||||
conn, err := conn(addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nodev1beta1.NewServiceClient(conn), nil
|
||||
}
|
||||
|
||||
func NewSVCClient(addr string) (svcv1.QueryClient, error) {
|
||||
conn, err := conn(addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return svcv1.NewQueryClient(conn), nil
|
||||
}
|
||||
124
pkg/common/http_cookies.go
Normal file
124
pkg/common/http_cookies.go
Normal file
@@ -0,0 +1,124 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
// CookieKey is a type alias for string.
|
||||
type CookieKey string
|
||||
|
||||
const (
|
||||
// SessionID is the key for the session ID cookie.
|
||||
SessionID CookieKey = "session.id"
|
||||
|
||||
// SessionChallenge is the key for the session challenge cookie.
|
||||
SessionChallenge CookieKey = "session.challenge"
|
||||
|
||||
// SessionRole is the key for the session role cookie.
|
||||
SessionRole CookieKey = "session.role"
|
||||
|
||||
// SonrAddress is the key for the Sonr address cookie.
|
||||
SonrAddress CookieKey = "sonr.address"
|
||||
|
||||
// SonrDID is the key for the Sonr DID cookie.
|
||||
SonrDID CookieKey = "sonr.did"
|
||||
|
||||
// UserAvatar is the key for the User Avatar cookie.
|
||||
UserAvatar CookieKey = "user.avatar"
|
||||
|
||||
// UserHandle is the key for the User Handle cookie.
|
||||
UserHandle CookieKey = "user.handle"
|
||||
|
||||
// UserName is the key for the User Name cookie.
|
||||
UserName CookieKey = "user.full_name"
|
||||
|
||||
// VaultAddress is the key for the Vault address cookie.
|
||||
VaultAddress CookieKey = "vault.address"
|
||||
|
||||
// VaultCID is the key for the Vault CID cookie.
|
||||
VaultCID CookieKey = "vault.cid"
|
||||
|
||||
// VaultSchema is the key for the Vault schema cookie.
|
||||
VaultSchema CookieKey = "vault.schema"
|
||||
)
|
||||
|
||||
// String returns the string representation of the CookieKey.
|
||||
func (c CookieKey) String() string {
|
||||
return string(c)
|
||||
}
|
||||
|
||||
// ╭───────────────────────────────────────────────────────────╮
|
||||
// │ Utility Methods │
|
||||
// ╰───────────────────────────────────────────────────────────╯
|
||||
|
||||
func CookieExists(c echo.Context, key CookieKey) bool {
|
||||
ck, err := c.Cookie(key.String())
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return ck != nil
|
||||
}
|
||||
|
||||
func ReadCookie(c echo.Context, key CookieKey) (string, error) {
|
||||
cookie, err := c.Cookie(key.String())
|
||||
if err != nil {
|
||||
// Cookie not found or other error
|
||||
return "", err
|
||||
}
|
||||
if cookie == nil || cookie.Value == "" {
|
||||
// Cookie is empty
|
||||
return "", http.ErrNoCookie
|
||||
}
|
||||
return cookie.Value, nil
|
||||
}
|
||||
|
||||
func ReadCookieBytes(c echo.Context, key CookieKey) ([]byte, error) {
|
||||
cookie, err := c.Cookie(key.String())
|
||||
if err != nil {
|
||||
// Cookie not found or other error
|
||||
return nil, err
|
||||
}
|
||||
if cookie == nil || cookie.Value == "" {
|
||||
// Cookie is empty
|
||||
return nil, http.ErrNoCookie
|
||||
}
|
||||
return base64.RawURLEncoding.DecodeString(cookie.Value)
|
||||
}
|
||||
|
||||
func ReadCookieUnsafe(c echo.Context, key CookieKey) string {
|
||||
ck, err := c.Cookie(key.String())
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return ck.Value
|
||||
}
|
||||
|
||||
func WriteCookie(c echo.Context, key CookieKey, value string) error {
|
||||
cookie := &http.Cookie{
|
||||
Name: key.String(),
|
||||
Value: value,
|
||||
Expires: time.Now().Add(24 * time.Hour),
|
||||
HttpOnly: true,
|
||||
Path: "/",
|
||||
// Add Secure and SameSite attributes as needed
|
||||
}
|
||||
c.SetCookie(cookie)
|
||||
return nil
|
||||
}
|
||||
|
||||
func WriteCookieBytes(c echo.Context, key CookieKey, value []byte) error {
|
||||
cookie := &http.Cookie{
|
||||
Name: key.String(),
|
||||
Value: base64.RawURLEncoding.EncodeToString(value),
|
||||
Expires: time.Now().Add(24 * time.Hour),
|
||||
HttpOnly: true,
|
||||
Path: "/",
|
||||
// Add Secure and SameSite attributes as needed
|
||||
}
|
||||
c.SetCookie(cookie)
|
||||
return nil
|
||||
}
|
||||
52
pkg/common/http_headers.go
Normal file
52
pkg/common/http_headers.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package common
|
||||
|
||||
import "github.com/labstack/echo/v4"
|
||||
|
||||
type HeaderKey string
|
||||
|
||||
const (
|
||||
Authorization HeaderKey = "Authorization"
|
||||
|
||||
// User Agent
|
||||
Architecture HeaderKey = "Sec-CH-UA-Arch"
|
||||
Bitness HeaderKey = "Sec-CH-UA-Bitness"
|
||||
FullVersionList HeaderKey = "Sec-CH-UA-Full-Version-List"
|
||||
Mobile HeaderKey = "Sec-CH-UA-Mobile"
|
||||
Model HeaderKey = "Sec-CH-UA-Model"
|
||||
Platform HeaderKey = "Sec-CH-UA-Platform"
|
||||
PlatformVersion HeaderKey = "Sec-CH-UA-Platform-Version"
|
||||
UserAgent HeaderKey = "Sec-CH-UA"
|
||||
|
||||
// Sonr Injected
|
||||
SonrAPIURL HeaderKey = "X-Sonr-API"
|
||||
SonrgRPCURL HeaderKey = "X-Sonr-GRPC"
|
||||
SonrRPCURL HeaderKey = "X-Sonr-RPC"
|
||||
SonrWSURL HeaderKey = "X-Sonr-WS"
|
||||
)
|
||||
|
||||
func (h HeaderKey) String() string {
|
||||
return string(h)
|
||||
}
|
||||
|
||||
// ╭───────────────────────────────────────────────────────────╮
|
||||
// │ Utility Methods │
|
||||
// ╰───────────────────────────────────────────────────────────╯
|
||||
|
||||
func HeaderEquals(c echo.Context, key HeaderKey, value string) bool {
|
||||
return c.Response().Header().Get(key.String()) == value
|
||||
}
|
||||
|
||||
// HeaderExists returns true if the request has the header Key.
|
||||
func HeaderExists(c echo.Context, key HeaderKey) bool {
|
||||
return c.Response().Header().Get(key.String()) != ""
|
||||
}
|
||||
|
||||
// HeaderRead returns the header value for the Key.
|
||||
func HeaderRead(c echo.Context, key HeaderKey) string {
|
||||
return c.Response().Header().Get(key.String())
|
||||
}
|
||||
|
||||
// HeaderWrite sets the header value for the Key.
|
||||
func HeaderWrite(c echo.Context, key HeaderKey, value string) {
|
||||
c.Response().Header().Set(key.String(), value)
|
||||
}
|
||||
136
pkg/common/ipfs_api.go
Normal file
136
pkg/common/ipfs_api.go
Normal file
@@ -0,0 +1,136 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/ipfs/boxo/files"
|
||||
"github.com/ipfs/boxo/path"
|
||||
"github.com/ipfs/kubo/client/rpc"
|
||||
"github.com/ipfs/kubo/core/coreiface/options"
|
||||
)
|
||||
|
||||
// IPFS represents a wrapper interface abstracting the localhost api
|
||||
type IPFS interface {
|
||||
Add(data []byte) (string, error)
|
||||
AddFile(file File) (string, error)
|
||||
AddFolder(folder Folder) (string, error)
|
||||
Exists(cid string) (bool, error)
|
||||
Get(cid string) ([]byte, error)
|
||||
IsPinned(ipns string) (bool, error)
|
||||
Ls(cid string) ([]string, error)
|
||||
Pin(cid string, name string) error
|
||||
Unpin(cid string) error
|
||||
}
|
||||
|
||||
type File interface {
|
||||
files.File
|
||||
Name() string
|
||||
}
|
||||
|
||||
func NewFileMap(vs []File) map[string]files.Node {
|
||||
m := make(map[string]files.Node)
|
||||
for _, f := range vs {
|
||||
m[f.Name()] = f
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
type client struct {
|
||||
api *rpc.HttpApi
|
||||
}
|
||||
|
||||
func NewIPFS() (IPFS, error) {
|
||||
api, err := rpc.NewLocalApi()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &client{api: api}, nil
|
||||
}
|
||||
|
||||
func (c *client) Add(data []byte) (string, error) {
|
||||
file := files.NewBytesFile(data)
|
||||
cidFile, err := c.api.Unixfs().Add(context.Background(), file)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return cidFile.String(), nil
|
||||
}
|
||||
|
||||
func (c *client) Get(cid string) ([]byte, error) {
|
||||
p, err := path.NewPath(cid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
node, err := c.api.Unixfs().Get(context.Background(), p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
file, ok := node.(files.File)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected node type: %T", node)
|
||||
}
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
if _, err := io.Copy(buf, file); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
func (c *client) IsPinned(ipns string) (bool, error) {
|
||||
_, err := c.api.Name().Resolve(context.Background(), ipns)
|
||||
if err != nil {
|
||||
return false, nil
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (c *client) Exists(cid string) (bool, error) {
|
||||
p, err := path.NewPath(cid)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
_, err = c.api.Block().Stat(context.Background(), p)
|
||||
if err != nil {
|
||||
return false, nil
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (c *client) Pin(cid string, name string) error {
|
||||
p, err := path.NewPath(cid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.api.Pin().Add(context.Background(), p, options.Pin.Name(name))
|
||||
}
|
||||
|
||||
func (c *client) Unpin(cid string) error {
|
||||
p, err := path.NewPath(cid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.api.Pin().Rm(context.Background(), p)
|
||||
}
|
||||
|
||||
func (c *client) Ls(cid string) ([]string, error) {
|
||||
p, err := path.NewPath(cid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
node, err := c.api.Unixfs().Ls(context.Background(), p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var files []string
|
||||
for entry := range node {
|
||||
files = append(files, entry.Name)
|
||||
}
|
||||
return files, nil
|
||||
}
|
||||
28
pkg/common/ipfs_file.go
Normal file
28
pkg/common/ipfs_file.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/ipfs/boxo/files"
|
||||
)
|
||||
|
||||
type file struct {
|
||||
files.File
|
||||
name string
|
||||
}
|
||||
|
||||
func (f *file) Name() string {
|
||||
return f.name
|
||||
}
|
||||
|
||||
func NewFile(name string, data []byte) File {
|
||||
return &file{File: files.NewBytesFile(data), name: name}
|
||||
}
|
||||
|
||||
func (c *client) AddFile(file File) (string, error) {
|
||||
cidFile, err := c.api.Unixfs().Add(context.Background(), file)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return cidFile.String(), nil
|
||||
}
|
||||
21
pkg/common/ipfs_folder.go
Normal file
21
pkg/common/ipfs_folder.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/ipfs/boxo/files"
|
||||
)
|
||||
|
||||
type Folder = files.Directory
|
||||
|
||||
func NewFolder(fs ...File) Folder {
|
||||
return files.NewMapDirectory(NewFileMap(fs))
|
||||
}
|
||||
|
||||
func (c *client) AddFolder(folder Folder) (string, error) {
|
||||
cidFile, err := c.api.Unixfs().Add(context.Background(), folder)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return cidFile.String(), nil
|
||||
}
|
||||
142
pkg/common/ucan_store.go
Normal file
142
pkg/common/ucan_store.go
Normal file
@@ -0,0 +1,142 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
"sync"
|
||||
|
||||
"github.com/golang-jwt/jwt"
|
||||
"github.com/ipfs/go-cid"
|
||||
"github.com/onsonr/crypto/keys"
|
||||
"github.com/onsonr/crypto/ucan"
|
||||
)
|
||||
|
||||
type IPFSTokenStore interface {
|
||||
ucan.TokenStore
|
||||
ResolveCIDBytes(ctx context.Context, id cid.Cid) ([]byte, error)
|
||||
ResolveDIDKey(ctx context.Context, did string) (keys.DID, error)
|
||||
}
|
||||
|
||||
// ipfsUCANStore is a token store that uses IPFS to store tokens. It uses the memory store as a cache
|
||||
// for CID strings to be used as keys for retrieving tokens.
|
||||
type ipfsUCANStore struct {
|
||||
sync.Mutex
|
||||
ipfs IPFS
|
||||
cache map[string]string
|
||||
}
|
||||
|
||||
// NewUCANStore creates a new IPFS-backed token store
|
||||
func NewUCANStore(ipfsClient IPFS) IPFSTokenStore {
|
||||
return &ipfsUCANStore{
|
||||
ipfs: ipfsClient,
|
||||
cache: make(map[string]string),
|
||||
}
|
||||
}
|
||||
|
||||
func (st *ipfsUCANStore) PutToken(ctx context.Context, key string, raw string) error {
|
||||
// Validate token format
|
||||
p := &jwt.Parser{
|
||||
UseJSONNumber: true,
|
||||
SkipClaimsValidation: false,
|
||||
}
|
||||
if _, _, err := p.ParseUnverified(raw, jwt.MapClaims{}); err != nil {
|
||||
return fmt.Errorf("%w: %s", ucan.ErrInvalidToken, err)
|
||||
}
|
||||
|
||||
// Store token in IPFS
|
||||
cid, err := st.ipfs.Add([]byte(raw))
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to store token in IPFS: %w", err)
|
||||
}
|
||||
|
||||
// Update cache
|
||||
st.Lock()
|
||||
defer st.Unlock()
|
||||
st.cache[key] = cid
|
||||
return nil
|
||||
}
|
||||
|
||||
func (st *ipfsUCANStore) RawToken(ctx context.Context, key string) (string, error) {
|
||||
st.Lock()
|
||||
cid, exists := st.cache[key]
|
||||
st.Unlock()
|
||||
|
||||
if !exists {
|
||||
return "", ucan.ErrTokenNotFound
|
||||
}
|
||||
|
||||
// Retrieve token from IPFS
|
||||
data, err := st.ipfs.Get(cid)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to retrieve token from IPFS: %w", err)
|
||||
}
|
||||
|
||||
return string(data), nil
|
||||
}
|
||||
|
||||
func (st *ipfsUCANStore) DeleteToken(ctx context.Context, key string) error {
|
||||
st.Lock()
|
||||
defer st.Unlock()
|
||||
|
||||
cid, exists := st.cache[key]
|
||||
if !exists {
|
||||
return ucan.ErrTokenNotFound
|
||||
}
|
||||
|
||||
// Unpin from IPFS
|
||||
if err := st.ipfs.Unpin(cid); err != nil {
|
||||
return fmt.Errorf("failed to unpin token from IPFS: %w", err)
|
||||
}
|
||||
|
||||
delete(st.cache, key)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (st *ipfsUCANStore) ListTokens(ctx context.Context, offset, limit int) ([]ucan.RawToken, error) {
|
||||
st.Lock()
|
||||
defer st.Unlock()
|
||||
|
||||
tokens := make(ucan.RawTokens, 0, len(st.cache))
|
||||
for key, cid := range st.cache {
|
||||
data, err := st.ipfs.Get(cid)
|
||||
if err != nil {
|
||||
continue // Skip invalid tokens
|
||||
}
|
||||
tokens = append(tokens, ucan.RawToken{
|
||||
Key: key,
|
||||
Raw: string(data),
|
||||
})
|
||||
}
|
||||
|
||||
// Sort tokens
|
||||
sort.Sort(tokens)
|
||||
|
||||
// Apply pagination
|
||||
if offset >= len(tokens) {
|
||||
return []ucan.RawToken{}, nil
|
||||
}
|
||||
|
||||
end := offset + limit
|
||||
if end > len(tokens) || limit <= 0 {
|
||||
end = len(tokens)
|
||||
}
|
||||
|
||||
return tokens[offset:end], nil
|
||||
}
|
||||
|
||||
func (st *ipfsUCANStore) ResolveCIDBytes(ctx context.Context, id cid.Cid) ([]byte, error) {
|
||||
data, err := st.ipfs.Get(id.String())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to resolve CID bytes: %w", err)
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (st *ipfsUCANStore) ResolveDIDKey(ctx context.Context, did string) (keys.DID, error) {
|
||||
id, err := keys.Parse(did)
|
||||
if err != nil {
|
||||
return keys.DID{}, fmt.Errorf("failed to parse DID: %w", err)
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
103
pkg/config/Hway.pkl.go
Normal file
103
pkg/config/Hway.pkl.go
Normal file
@@ -0,0 +1,103 @@
|
||||
// Code generated from Pkl module `sonr.net.Hway`. DO NOT EDIT.
|
||||
package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/apple/pkl-go/pkl"
|
||||
)
|
||||
|
||||
type Hway interface {
|
||||
GetServePort() int
|
||||
|
||||
GetSqliteFile() string
|
||||
|
||||
GetChainId() string
|
||||
|
||||
GetIpfsGatewayUrl() string
|
||||
|
||||
GetSonrApiUrl() string
|
||||
|
||||
GetSonrGrpcUrl() string
|
||||
|
||||
GetSonrRpcUrl() string
|
||||
|
||||
GetPsqlDSN() string
|
||||
}
|
||||
|
||||
var _ Hway = (*HwayImpl)(nil)
|
||||
|
||||
type HwayImpl struct {
|
||||
ServePort int `pkl:"servePort"`
|
||||
|
||||
SqliteFile string `pkl:"sqliteFile"`
|
||||
|
||||
ChainId string `pkl:"chainId"`
|
||||
|
||||
IpfsGatewayUrl string `pkl:"ipfsGatewayUrl"`
|
||||
|
||||
SonrApiUrl string `pkl:"sonrApiUrl"`
|
||||
|
||||
SonrGrpcUrl string `pkl:"sonrGrpcUrl"`
|
||||
|
||||
SonrRpcUrl string `pkl:"sonrRpcUrl"`
|
||||
|
||||
PsqlDSN string `pkl:"psqlDSN"`
|
||||
}
|
||||
|
||||
func (rcv *HwayImpl) GetServePort() int {
|
||||
return rcv.ServePort
|
||||
}
|
||||
|
||||
func (rcv *HwayImpl) GetSqliteFile() string {
|
||||
return rcv.SqliteFile
|
||||
}
|
||||
|
||||
func (rcv *HwayImpl) GetChainId() string {
|
||||
return rcv.ChainId
|
||||
}
|
||||
|
||||
func (rcv *HwayImpl) GetIpfsGatewayUrl() string {
|
||||
return rcv.IpfsGatewayUrl
|
||||
}
|
||||
|
||||
func (rcv *HwayImpl) GetSonrApiUrl() string {
|
||||
return rcv.SonrApiUrl
|
||||
}
|
||||
|
||||
func (rcv *HwayImpl) GetSonrGrpcUrl() string {
|
||||
return rcv.SonrGrpcUrl
|
||||
}
|
||||
|
||||
func (rcv *HwayImpl) GetSonrRpcUrl() string {
|
||||
return rcv.SonrRpcUrl
|
||||
}
|
||||
|
||||
func (rcv *HwayImpl) GetPsqlDSN() string {
|
||||
return rcv.PsqlDSN
|
||||
}
|
||||
|
||||
// LoadFromPath loads the pkl module at the given path and evaluates it into a Hway
|
||||
func LoadFromPath(ctx context.Context, path string) (ret Hway, err error) {
|
||||
evaluator, err := pkl.NewEvaluator(ctx, pkl.PreconfiguredOptions)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() {
|
||||
cerr := evaluator.Close()
|
||||
if err == nil {
|
||||
err = cerr
|
||||
}
|
||||
}()
|
||||
ret, err = Load(ctx, evaluator, pkl.FileSource(path))
|
||||
return ret, err
|
||||
}
|
||||
|
||||
// Load loads the pkl module at the given source and evaluates it with the given evaluator into a Hway
|
||||
func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (Hway, error) {
|
||||
var ret HwayImpl
|
||||
if err := evaluator.EvaluateModule(ctx, source, &ret); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &ret, nil
|
||||
}
|
||||
8
pkg/config/init.pkl.go
Normal file
8
pkg/config/init.pkl.go
Normal file
@@ -0,0 +1,8 @@
|
||||
// Code generated from Pkl module `sonr.net.Hway`. DO NOT EDIT.
|
||||
package config
|
||||
|
||||
import "github.com/apple/pkl-go/pkl"
|
||||
|
||||
func init() {
|
||||
pkl.RegisterMapping("sonr.net.Hway", HwayImpl{})
|
||||
}
|
||||
19
pkg/context/credentials.go
Normal file
19
pkg/context/credentials.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package context
|
||||
|
||||
import (
|
||||
"github.com/go-webauthn/webauthn/protocol"
|
||||
)
|
||||
|
||||
func (c *GatewayContext) NewChallenge() string {
|
||||
chal, _ := protocol.CreateChallenge()
|
||||
chalStr := chal.String()
|
||||
return chalStr
|
||||
}
|
||||
|
||||
func (cc *GatewayContext) ListCredentials(handle string) ([]*CredentialDescriptor, error) {
|
||||
creds, err := cc.GetCredentialsByHandle(bgCtx(), handle)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return CredentialArrayToDescriptors(creds), nil
|
||||
}
|
||||
64
pkg/context/middleware.go
Normal file
64
pkg/context/middleware.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package context
|
||||
|
||||
import (
|
||||
gocontext "context"
|
||||
"net/http"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/medama-io/go-useragent"
|
||||
"github.com/onsonr/crypto/mpc"
|
||||
"github.com/onsonr/hway/pkg/common"
|
||||
hway "github.com/onsonr/hway/pkg/config"
|
||||
hwayorm "github.com/onsonr/hway/pkg/models"
|
||||
)
|
||||
|
||||
type GatewayContext struct {
|
||||
echo.Context
|
||||
hwayorm.Querier
|
||||
id string
|
||||
ipfsClient common.IPFS
|
||||
agent useragent.UserAgent
|
||||
tokenStore common.IPFSTokenStore
|
||||
stagedEnclaves map[string]mpc.Enclave
|
||||
grpcAddr string
|
||||
turnstileSiteKey string
|
||||
}
|
||||
|
||||
func GetGateway(c echo.Context) (*GatewayContext, error) {
|
||||
cc, ok := c.(*GatewayContext)
|
||||
if !ok {
|
||||
return nil, echo.NewHTTPError(http.StatusInternalServerError, "Gateway Context not found")
|
||||
}
|
||||
return cc, nil
|
||||
}
|
||||
|
||||
func UseGateway(env hway.Hway, ipc common.IPFS, db *hwayorm.Queries) echo.MiddlewareFunc {
|
||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
ua := useragent.NewParser()
|
||||
ctx := &GatewayContext{
|
||||
Context: c,
|
||||
Querier: db,
|
||||
ipfsClient: ipc,
|
||||
agent: ua.Parse(c.Request().UserAgent()),
|
||||
grpcAddr: env.GetSonrGrpcUrl(),
|
||||
tokenStore: common.NewUCANStore(ipc),
|
||||
// turnstileSiteKey: env.GetTurnstileSiteKey(),
|
||||
}
|
||||
return next(ctx)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BG() gocontext.Context {
|
||||
ctx := gocontext.Background()
|
||||
return ctx
|
||||
}
|
||||
|
||||
func (cc *GatewayContext) ReadCookie(k common.CookieKey) string {
|
||||
return common.ReadCookieUnsafe(cc.Context, k)
|
||||
}
|
||||
|
||||
func (cc *GatewayContext) WriteCookie(k common.CookieKey, v string) {
|
||||
common.WriteCookie(cc.Context, k, v)
|
||||
}
|
||||
53
pkg/context/profiles.go
Normal file
53
pkg/context/profiles.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package context
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
hwayorm "github.com/onsonr/hway/pkg/models"
|
||||
)
|
||||
|
||||
func UpdateProfile(c echo.Context) (*hwayorm.Profile, error) {
|
||||
ctx, ok := c.(*GatewayContext)
|
||||
if !ok {
|
||||
return nil, echo.NewHTTPError(http.StatusInternalServerError, "Profile Context not found")
|
||||
}
|
||||
address := c.FormValue("address")
|
||||
handle := c.FormValue("handle")
|
||||
name := c.FormValue("name")
|
||||
profile, err := ctx.UpdateProfile(bgCtx(), hwayorm.UpdateProfileParams{
|
||||
Address: address,
|
||||
Handle: handle,
|
||||
Name: name,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &profile, nil
|
||||
}
|
||||
|
||||
func ReadProfile(c echo.Context) (*hwayorm.Profile, error) {
|
||||
ctx, ok := c.(*GatewayContext)
|
||||
if !ok {
|
||||
return nil, echo.NewHTTPError(http.StatusInternalServerError, "Profile Context not found")
|
||||
}
|
||||
handle := c.Param("handle")
|
||||
profile, err := ctx.GetProfileByHandle(bgCtx(), handle)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &profile, nil
|
||||
}
|
||||
|
||||
func DeleteProfile(c echo.Context) error {
|
||||
ctx, ok := c.(*GatewayContext)
|
||||
if !ok {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Profile Context not found")
|
||||
}
|
||||
address := c.Param("address")
|
||||
err := ctx.SoftDeleteProfile(bgCtx(), address)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
42
pkg/context/renderer.go
Normal file
42
pkg/context/renderer.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package context
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"github.com/a-h/templ"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/onsonr/hway/app/views"
|
||||
)
|
||||
|
||||
func Render(c echo.Context, cmp templ.Component) error {
|
||||
// Create a buffer to store the rendered HTML
|
||||
buf := &bytes.Buffer{}
|
||||
// Render the component to the buffer
|
||||
err := cmp.Render(c.Request().Context(), buf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Set the content type
|
||||
c.Response().Header().Set(echo.HeaderContentType, echo.MIMETextHTML)
|
||||
|
||||
// Write the buffered content to the response
|
||||
_, err = c.Response().Write(buf.Bytes())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.Response().WriteHeader(200)
|
||||
return nil
|
||||
}
|
||||
|
||||
func RenderError(c echo.Context, err error) error {
|
||||
return Render(c, views.ErrorView(err.Error()))
|
||||
}
|
||||
|
||||
func RenderInitial(c echo.Context) error {
|
||||
return Render(c, views.InitialView())
|
||||
}
|
||||
|
||||
func RenderLoading(c echo.Context) error {
|
||||
return Render(c, views.LoadingView())
|
||||
}
|
||||
105
pkg/context/resolver.go
Normal file
105
pkg/context/resolver.go
Normal file
@@ -0,0 +1,105 @@
|
||||
package context
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/onsonr/hway/pkg/common"
|
||||
)
|
||||
|
||||
// ParamsBank returns the bank params
|
||||
func (cc *GatewayContext) ParamsBank() (*common.BankParamsResponse, error) {
|
||||
cl, err := common.NewBankClient(cc.grpcAddr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := cl.Params(bgCtx(), &common.BankParamsRequest{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// ParamsDID returns the DID params
|
||||
func (cc *GatewayContext) ParamsDID() (*common.DIDParamsResponse, error) {
|
||||
cl, err := common.NewDIDClient(cc.grpcAddr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := cl.Params(bgCtx(), &common.DIDParamsRequest{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// ParamsDWN returns the DWN params
|
||||
func (cc *GatewayContext) ParamsDWN() (*common.DWNParamsResponse, error) {
|
||||
cl, err := common.NewDWNClient(cc.grpcAddr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := cl.Params(bgCtx(), &common.DWNParamsRequest{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// ParamsSVC returns the SVC params
|
||||
func (cc *GatewayContext) ParamsSVC() (*common.SVCParamsResponse, error) {
|
||||
cl, err := common.NewSVCClient(cc.grpcAddr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := cl.Params(bgCtx(), &common.SVCParamsRequest{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// StatusBlock returns the current block
|
||||
func (cc *GatewayContext) StatusBlock() string {
|
||||
qc, err := common.NewNodeClient(cc.grpcAddr)
|
||||
if err != nil {
|
||||
return "-1"
|
||||
}
|
||||
resp, err := qc.Status(bgCtx(), &common.StatusRequest{})
|
||||
if err != nil {
|
||||
return "-1"
|
||||
}
|
||||
return fmt.Sprintf("%d", resp.GetHeight())
|
||||
}
|
||||
|
||||
// StatusNode returns the node status
|
||||
func (cc *GatewayContext) StatusNode() (*common.StatusResponse, error) {
|
||||
cl, err := common.NewNodeClient(cc.grpcAddr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := cl.Status(bgCtx(), &common.StatusRequest{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// TxBroadcast broadcasts a transaction to the network
|
||||
func (cc *GatewayContext) TxBroadcast() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// TxEncode encodes a transaction
|
||||
func (cc *GatewayContext) TxEncode() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// TxDecode decodes a transaction
|
||||
func (cc *GatewayContext) TxDecode() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// TxSimulate simulates a transaction on the network
|
||||
func (cc *GatewayContext) TxSimulate() error {
|
||||
return nil
|
||||
}
|
||||
148
pkg/context/sessions.go
Normal file
148
pkg/context/sessions.go
Normal file
@@ -0,0 +1,148 @@
|
||||
package context
|
||||
|
||||
import (
|
||||
gocontext "context"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/onsonr/hway/pkg/common"
|
||||
"github.com/segmentio/ksuid"
|
||||
"lukechampine.com/blake3"
|
||||
)
|
||||
|
||||
func NewSession(c echo.Context) error {
|
||||
cc, ok := c.(*GatewayContext)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
baseSessionCreateParams := BaseSessionCreateParams(cc)
|
||||
cc.id = baseSessionCreateParams.ID
|
||||
if _, err := cc.CreateSession(bgCtx(), baseSessionCreateParams); err != nil {
|
||||
return err
|
||||
}
|
||||
// Set Cookie
|
||||
if err := common.WriteCookie(c, common.SessionID, cc.id); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Uses blake3 to hash the sessionID to generate a nonce of length 12 bytes
|
||||
func GetNonce(sessionID string) ([]byte, error) {
|
||||
hash := blake3.New(32, nil)
|
||||
_, err := hash.Write([]byte(sessionID))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Read the hash into a byte slice
|
||||
nonce := make([]byte, 12)
|
||||
_, err = hash.Write(nonce)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nonce, nil
|
||||
}
|
||||
|
||||
// ForbiddenDevice returns true if the device is unavailable
|
||||
func ForbiddenDevice(c echo.Context) bool {
|
||||
cc, ok := c.(*GatewayContext)
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
return cc.agent.IsBot() || cc.agent.IsTV()
|
||||
}
|
||||
|
||||
func GetOrigin(c echo.Context) string {
|
||||
return c.Request().Host
|
||||
}
|
||||
|
||||
func GetSessionID(c echo.Context) string {
|
||||
// Check from context
|
||||
cc, ok := c.(*GatewayContext)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
// check from cookie
|
||||
if cc.id == "" {
|
||||
if ok := common.CookieExists(c, common.SessionID); !ok {
|
||||
return ""
|
||||
}
|
||||
cc.id = common.ReadCookieUnsafe(c, common.SessionID)
|
||||
}
|
||||
return cc.id
|
||||
}
|
||||
|
||||
func GetAuthChallenge(c echo.Context) string {
|
||||
cc, ok := c.(*GatewayContext)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
s, err := cc.GetChallengeBySessionID(bgCtx(), cc.id)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func GetProfileHandle(c echo.Context) string {
|
||||
// First check for the cookie
|
||||
handle := common.ReadCookieUnsafe(c, common.UserHandle)
|
||||
if handle != "" {
|
||||
return handle
|
||||
}
|
||||
|
||||
// Then check the session
|
||||
cc, ok := c.(*GatewayContext)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
s, err := cc.GetSessionByID(bgCtx(), cc.id)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
profile, err := cc.GetProfileByID(bgCtx(), s.ProfileID)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return profile.Handle
|
||||
}
|
||||
|
||||
//
|
||||
// func GetHumanVerificationNumbers(c echo.Context) (int64, int64) {
|
||||
// cc, ok := c.(*GatewayContext)
|
||||
// if !ok {
|
||||
// return 0, 0
|
||||
// }
|
||||
// s, err := cc.dbq.GetHumanVerificationNumbers(bgCtx(), cc.id)
|
||||
// if err != nil {
|
||||
// return 0, 0
|
||||
// }
|
||||
// return s.IsHumanFirst, s.IsHumanLast
|
||||
// }
|
||||
|
||||
// utility function to get a context
|
||||
func bgCtx() gocontext.Context {
|
||||
ctx := gocontext.Background()
|
||||
return ctx
|
||||
}
|
||||
|
||||
func getOrCreateSessionID(c echo.Context) string {
|
||||
if ok := common.CookieExists(c, common.SessionID); !ok {
|
||||
sessionID := ksuid.New().String()
|
||||
common.WriteCookie(c, common.SessionID, sessionID)
|
||||
return sessionID
|
||||
}
|
||||
|
||||
sessionID, err := common.ReadCookie(c, common.SessionID)
|
||||
if err != nil {
|
||||
sessionID = ksuid.New().String()
|
||||
common.WriteCookie(c, common.SessionID, sessionID)
|
||||
}
|
||||
return sessionID
|
||||
}
|
||||
|
||||
func boolToInt64(b bool) int64 {
|
||||
if b {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
102
pkg/context/types.go
Normal file
102
pkg/context/types.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package context
|
||||
|
||||
import (
|
||||
"github.com/go-webauthn/webauthn/protocol"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/medama-io/go-useragent"
|
||||
hwayorm "github.com/onsonr/hway/pkg/models"
|
||||
)
|
||||
|
||||
// Define the credential structure matching our frontend data
|
||||
type CredentialDescriptor struct {
|
||||
ID string `json:"id"`
|
||||
RawID string `json:"rawId"`
|
||||
Type string `json:"type"`
|
||||
AuthenticatorAttachment string `json:"authenticatorAttachment"`
|
||||
Transports string `json:"transports"`
|
||||
ClientExtensionResults map[string]string `json:"clientExtensionResults"`
|
||||
Response struct {
|
||||
AttestationObject string `json:"attestationObject"`
|
||||
ClientDataJSON string `json:"clientDataJSON"`
|
||||
} `json:"response"`
|
||||
}
|
||||
|
||||
func (c *CredentialDescriptor) ToModel(handle, origin string) *hwayorm.Credential {
|
||||
return &hwayorm.Credential{
|
||||
Handle: handle,
|
||||
Origin: origin,
|
||||
CredentialID: c.ID,
|
||||
Type: c.Type,
|
||||
Transports: c.Transports,
|
||||
AuthenticatorAttachment: c.AuthenticatorAttachment,
|
||||
}
|
||||
}
|
||||
|
||||
func CredentialArrayToDescriptors(credentials []hwayorm.Credential) []*CredentialDescriptor {
|
||||
var descriptors []*CredentialDescriptor
|
||||
for _, cred := range credentials {
|
||||
cd := &CredentialDescriptor{
|
||||
ID: cred.CredentialID,
|
||||
RawID: cred.CredentialID,
|
||||
Type: cred.Type,
|
||||
AuthenticatorAttachment: cred.AuthenticatorAttachment,
|
||||
Transports: cred.Transports,
|
||||
}
|
||||
descriptors = append(descriptors, cd)
|
||||
}
|
||||
return descriptors
|
||||
}
|
||||
|
||||
func BaseSessionCreateParams(e echo.Context) hwayorm.CreateSessionParams {
|
||||
// f := rand.Intn(5) + 1
|
||||
// l := rand.Intn(4) + 1
|
||||
challenge, _ := protocol.CreateChallenge()
|
||||
id := getOrCreateSessionID(e)
|
||||
ua := useragent.NewParser()
|
||||
s := ua.Parse(e.Request().UserAgent())
|
||||
|
||||
return hwayorm.CreateSessionParams{
|
||||
ID: id,
|
||||
BrowserName: s.GetBrowser(),
|
||||
BrowserVersion: s.GetMajorVersion(),
|
||||
ClientIpaddr: e.RealIP(),
|
||||
Platform: s.GetOS(),
|
||||
IsMobile: s.IsMobile(),
|
||||
IsTablet: s.IsTablet(),
|
||||
IsDesktop: s.IsDesktop(),
|
||||
IsBot: s.IsBot(),
|
||||
IsTv: s.IsTV(),
|
||||
// IsHumanFirst: int64(f),
|
||||
// IsHumanLast: int64(l),
|
||||
Challenge: challenge.String(),
|
||||
}
|
||||
}
|
||||
|
||||
// ╭───────────────────────────────────────────────────────────╮
|
||||
// │ Create Passkey (/register/passkey) │
|
||||
// ╰───────────────────────────────────────────────────────────╯
|
||||
|
||||
// CreatePasskeyParams represents the parameters for creating a passkey
|
||||
type CreatePasskeyParams struct {
|
||||
Address string
|
||||
Handle string
|
||||
Name string
|
||||
Challenge string
|
||||
CreationBlock string
|
||||
}
|
||||
|
||||
// ╭───────────────────────────────────────────────────────────╮
|
||||
// │ Create Profile (/register/profile) │
|
||||
// ╰───────────────────────────────────────────────────────────╯
|
||||
|
||||
// CreateProfileParams represents the parameters for creating a profile
|
||||
type CreateProfileParams struct {
|
||||
TurnstileSiteKey string
|
||||
FirstNumber int
|
||||
LastNumber int
|
||||
}
|
||||
|
||||
// Sum returns the sum of the first and last number
|
||||
func (d CreateProfileParams) Sum() int {
|
||||
return d.FirstNumber + d.LastNumber
|
||||
}
|
||||
45
pkg/context/vaults.go
Normal file
45
pkg/context/vaults.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package context
|
||||
|
||||
import (
|
||||
"github.com/onsonr/crypto/mpc"
|
||||
"github.com/onsonr/hway/pkg/common"
|
||||
"lukechampine.com/blake3"
|
||||
)
|
||||
|
||||
func (cc *GatewayContext) Spawn(handle, origin string) (*CreatePasskeyParams, error) {
|
||||
challenge := GetAuthChallenge(cc)
|
||||
sid := GetSessionID(cc)
|
||||
nonce, err := calcNonce(sid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
encl, err := mpc.GenEnclave(nonce)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cc.stagedEnclaves[sid] = encl
|
||||
common.WriteCookie(cc, common.SonrAddress, encl.Address())
|
||||
return &CreatePasskeyParams{
|
||||
Address: encl.Address(),
|
||||
Handle: handle,
|
||||
Name: origin,
|
||||
Challenge: challenge,
|
||||
CreationBlock: cc.StatusBlock(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Uses blake3 to hash the sessionID to generate a nonce of length 12 bytes
|
||||
func calcNonce(sessionID string) ([]byte, error) {
|
||||
hash := blake3.New(32, nil)
|
||||
_, err := hash.Write([]byte(sessionID))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Read the hash into a byte slice
|
||||
nonce := make([]byte, 12)
|
||||
_, err = hash.Write(nonce)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nonce, nil
|
||||
}
|
||||
31
pkg/models/db.go
Normal file
31
pkg/models/db.go
Normal file
@@ -0,0 +1,31 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.27.0
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
type DBTX interface {
|
||||
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
|
||||
PrepareContext(context.Context, string) (*sql.Stmt, error)
|
||||
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
|
||||
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
|
||||
}
|
||||
|
||||
func New(db DBTX) *Queries {
|
||||
return &Queries{db: db}
|
||||
}
|
||||
|
||||
type Queries struct {
|
||||
db DBTX
|
||||
}
|
||||
|
||||
func (q *Queries) WithTx(tx *sql.Tx) *Queries {
|
||||
return &Queries{
|
||||
db: tx,
|
||||
}
|
||||
}
|
||||
99
pkg/models/models.go
Normal file
99
pkg/models/models.go
Normal file
@@ -0,0 +1,99 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.27.0
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Account struct {
|
||||
ID string `json:"id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt sql.NullTime `json:"deleted_at"`
|
||||
Number int64 `json:"number"`
|
||||
Sequence int64 `json:"sequence"`
|
||||
Address string `json:"address"`
|
||||
PublicKey string `json:"public_key"`
|
||||
ChainID string `json:"chain_id"`
|
||||
Controller string `json:"controller"`
|
||||
IsSubsidiary bool `json:"is_subsidiary"`
|
||||
IsValidator bool `json:"is_validator"`
|
||||
IsDelegator bool `json:"is_delegator"`
|
||||
IsAccountable bool `json:"is_accountable"`
|
||||
}
|
||||
|
||||
type Asset struct {
|
||||
ID string `json:"id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt sql.NullTime `json:"deleted_at"`
|
||||
Name string `json:"name"`
|
||||
Symbol string `json:"symbol"`
|
||||
Decimals int64 `json:"decimals"`
|
||||
ChainID string `json:"chain_id"`
|
||||
Channel string `json:"channel"`
|
||||
AssetType string `json:"asset_type"`
|
||||
CoingeckoID sql.NullString `json:"coingecko_id"`
|
||||
}
|
||||
|
||||
type Credential struct {
|
||||
ID string `json:"id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt sql.NullTime `json:"deleted_at"`
|
||||
Handle string `json:"handle"`
|
||||
CredentialID string `json:"credential_id"`
|
||||
AuthenticatorAttachment string `json:"authenticator_attachment"`
|
||||
Origin string `json:"origin"`
|
||||
Type string `json:"type"`
|
||||
Transports string `json:"transports"`
|
||||
}
|
||||
|
||||
type Profile struct {
|
||||
ID string `json:"id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt sql.NullTime `json:"deleted_at"`
|
||||
Address string `json:"address"`
|
||||
Handle string `json:"handle"`
|
||||
Origin string `json:"origin"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type Session struct {
|
||||
ID string `json:"id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt sql.NullTime `json:"deleted_at"`
|
||||
BrowserName string `json:"browser_name"`
|
||||
BrowserVersion string `json:"browser_version"`
|
||||
ClientIpaddr string `json:"client_ipaddr"`
|
||||
Platform string `json:"platform"`
|
||||
IsDesktop bool `json:"is_desktop"`
|
||||
IsMobile bool `json:"is_mobile"`
|
||||
IsTablet bool `json:"is_tablet"`
|
||||
IsTv bool `json:"is_tv"`
|
||||
IsBot bool `json:"is_bot"`
|
||||
Challenge string `json:"challenge"`
|
||||
IsHumanFirst bool `json:"is_human_first"`
|
||||
IsHumanLast bool `json:"is_human_last"`
|
||||
ProfileID string `json:"profile_id"`
|
||||
}
|
||||
|
||||
type Vault struct {
|
||||
ID string `json:"id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt sql.NullTime `json:"deleted_at"`
|
||||
Handle string `json:"handle"`
|
||||
Origin string `json:"origin"`
|
||||
Address string `json:"address"`
|
||||
Cid string `json:"cid"`
|
||||
Config string `json:"config"`
|
||||
SessionID string `json:"session_id"`
|
||||
RedirectUri string `json:"redirect_uri"`
|
||||
}
|
||||
34
pkg/models/querier.go
Normal file
34
pkg/models/querier.go
Normal file
@@ -0,0 +1,34 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.27.0
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type Querier interface {
|
||||
CheckHandleExists(ctx context.Context, handle string) (bool, error)
|
||||
CreateSession(ctx context.Context, arg CreateSessionParams) (Session, error)
|
||||
GetChallengeBySessionID(ctx context.Context, id string) (string, error)
|
||||
GetCredentialByID(ctx context.Context, credentialID string) (Credential, error)
|
||||
GetCredentialsByHandle(ctx context.Context, handle string) ([]Credential, error)
|
||||
GetHumanVerificationNumbers(ctx context.Context, id string) (GetHumanVerificationNumbersRow, error)
|
||||
GetProfileByAddress(ctx context.Context, address string) (Profile, error)
|
||||
GetProfileByHandle(ctx context.Context, handle string) (Profile, error)
|
||||
GetProfileByID(ctx context.Context, id string) (Profile, error)
|
||||
GetSessionByClientIP(ctx context.Context, clientIpaddr string) (Session, error)
|
||||
GetSessionByID(ctx context.Context, id string) (Session, error)
|
||||
GetVaultConfigByCID(ctx context.Context, cid string) (Vault, error)
|
||||
GetVaultRedirectURIBySessionID(ctx context.Context, sessionID string) (string, error)
|
||||
InsertCredential(ctx context.Context, arg InsertCredentialParams) (Credential, error)
|
||||
InsertProfile(ctx context.Context, arg InsertProfileParams) (Profile, error)
|
||||
SoftDeleteCredential(ctx context.Context, credentialID string) error
|
||||
SoftDeleteProfile(ctx context.Context, address string) error
|
||||
UpdateProfile(ctx context.Context, arg UpdateProfileParams) (Profile, error)
|
||||
UpdateSessionHumanVerification(ctx context.Context, arg UpdateSessionHumanVerificationParams) (Session, error)
|
||||
UpdateSessionWithProfileID(ctx context.Context, arg UpdateSessionWithProfileIDParams) (Session, error)
|
||||
}
|
||||
|
||||
var _ Querier = (*Queries)(nil)
|
||||
581
pkg/models/query.sql.go
Normal file
581
pkg/models/query.sql.go
Normal file
@@ -0,0 +1,581 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.27.0
|
||||
// source: query.sql
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
const checkHandleExists = `-- name: CheckHandleExists :one
|
||||
SELECT COUNT(*) > 0 as handle_exists FROM profiles
|
||||
WHERE handle = ?
|
||||
AND deleted_at IS NULL
|
||||
`
|
||||
|
||||
func (q *Queries) CheckHandleExists(ctx context.Context, handle string) (bool, error) {
|
||||
row := q.db.QueryRowContext(ctx, checkHandleExists, handle)
|
||||
var handle_exists bool
|
||||
err := row.Scan(&handle_exists)
|
||||
return handle_exists, err
|
||||
}
|
||||
|
||||
const createSession = `-- name: CreateSession :one
|
||||
INSERT INTO sessions (
|
||||
id,
|
||||
browser_name,
|
||||
browser_version,
|
||||
client_ipaddr,
|
||||
platform,
|
||||
is_desktop,
|
||||
is_mobile,
|
||||
is_tablet,
|
||||
is_tv,
|
||||
is_bot,
|
||||
challenge,
|
||||
is_human_first,
|
||||
is_human_last,
|
||||
profile_id
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
|
||||
RETURNING id, created_at, updated_at, deleted_at, browser_name, browser_version, client_ipaddr, platform, is_desktop, is_mobile, is_tablet, is_tv, is_bot, challenge, is_human_first, is_human_last, profile_id
|
||||
`
|
||||
|
||||
type CreateSessionParams struct {
|
||||
ID string `json:"id"`
|
||||
BrowserName string `json:"browser_name"`
|
||||
BrowserVersion string `json:"browser_version"`
|
||||
ClientIpaddr string `json:"client_ipaddr"`
|
||||
Platform string `json:"platform"`
|
||||
IsDesktop bool `json:"is_desktop"`
|
||||
IsMobile bool `json:"is_mobile"`
|
||||
IsTablet bool `json:"is_tablet"`
|
||||
IsTv bool `json:"is_tv"`
|
||||
IsBot bool `json:"is_bot"`
|
||||
Challenge string `json:"challenge"`
|
||||
IsHumanFirst bool `json:"is_human_first"`
|
||||
IsHumanLast bool `json:"is_human_last"`
|
||||
ProfileID string `json:"profile_id"`
|
||||
}
|
||||
|
||||
func (q *Queries) CreateSession(ctx context.Context, arg CreateSessionParams) (Session, error) {
|
||||
row := q.db.QueryRowContext(ctx, createSession,
|
||||
arg.ID,
|
||||
arg.BrowserName,
|
||||
arg.BrowserVersion,
|
||||
arg.ClientIpaddr,
|
||||
arg.Platform,
|
||||
arg.IsDesktop,
|
||||
arg.IsMobile,
|
||||
arg.IsTablet,
|
||||
arg.IsTv,
|
||||
arg.IsBot,
|
||||
arg.Challenge,
|
||||
arg.IsHumanFirst,
|
||||
arg.IsHumanLast,
|
||||
arg.ProfileID,
|
||||
)
|
||||
var i Session
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.DeletedAt,
|
||||
&i.BrowserName,
|
||||
&i.BrowserVersion,
|
||||
&i.ClientIpaddr,
|
||||
&i.Platform,
|
||||
&i.IsDesktop,
|
||||
&i.IsMobile,
|
||||
&i.IsTablet,
|
||||
&i.IsTv,
|
||||
&i.IsBot,
|
||||
&i.Challenge,
|
||||
&i.IsHumanFirst,
|
||||
&i.IsHumanLast,
|
||||
&i.ProfileID,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getChallengeBySessionID = `-- name: GetChallengeBySessionID :one
|
||||
SELECT challenge FROM sessions
|
||||
WHERE id = ? AND deleted_at IS NULL
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
func (q *Queries) GetChallengeBySessionID(ctx context.Context, id string) (string, error) {
|
||||
row := q.db.QueryRowContext(ctx, getChallengeBySessionID, id)
|
||||
var challenge string
|
||||
err := row.Scan(&challenge)
|
||||
return challenge, err
|
||||
}
|
||||
|
||||
const getCredentialByID = `-- name: GetCredentialByID :one
|
||||
SELECT id, created_at, updated_at, deleted_at, handle, credential_id, authenticator_attachment, origin, type, transports FROM credentials
|
||||
WHERE credential_id = ?
|
||||
AND deleted_at IS NULL
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
func (q *Queries) GetCredentialByID(ctx context.Context, credentialID string) (Credential, error) {
|
||||
row := q.db.QueryRowContext(ctx, getCredentialByID, credentialID)
|
||||
var i Credential
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.DeletedAt,
|
||||
&i.Handle,
|
||||
&i.CredentialID,
|
||||
&i.AuthenticatorAttachment,
|
||||
&i.Origin,
|
||||
&i.Type,
|
||||
&i.Transports,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getCredentialsByHandle = `-- name: GetCredentialsByHandle :many
|
||||
SELECT id, created_at, updated_at, deleted_at, handle, credential_id, authenticator_attachment, origin, type, transports FROM credentials
|
||||
WHERE handle = ?
|
||||
AND deleted_at IS NULL
|
||||
`
|
||||
|
||||
func (q *Queries) GetCredentialsByHandle(ctx context.Context, handle string) ([]Credential, error) {
|
||||
rows, err := q.db.QueryContext(ctx, getCredentialsByHandle, handle)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Credential
|
||||
for rows.Next() {
|
||||
var i Credential
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.DeletedAt,
|
||||
&i.Handle,
|
||||
&i.CredentialID,
|
||||
&i.AuthenticatorAttachment,
|
||||
&i.Origin,
|
||||
&i.Type,
|
||||
&i.Transports,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const getHumanVerificationNumbers = `-- name: GetHumanVerificationNumbers :one
|
||||
SELECT is_human_first, is_human_last FROM sessions
|
||||
WHERE id = ? AND deleted_at IS NULL
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
type GetHumanVerificationNumbersRow struct {
|
||||
IsHumanFirst bool `json:"is_human_first"`
|
||||
IsHumanLast bool `json:"is_human_last"`
|
||||
}
|
||||
|
||||
func (q *Queries) GetHumanVerificationNumbers(ctx context.Context, id string) (GetHumanVerificationNumbersRow, error) {
|
||||
row := q.db.QueryRowContext(ctx, getHumanVerificationNumbers, id)
|
||||
var i GetHumanVerificationNumbersRow
|
||||
err := row.Scan(&i.IsHumanFirst, &i.IsHumanLast)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getProfileByAddress = `-- name: GetProfileByAddress :one
|
||||
SELECT id, created_at, updated_at, deleted_at, address, handle, origin, name FROM profiles
|
||||
WHERE address = ? AND deleted_at IS NULL
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
func (q *Queries) GetProfileByAddress(ctx context.Context, address string) (Profile, error) {
|
||||
row := q.db.QueryRowContext(ctx, getProfileByAddress, address)
|
||||
var i Profile
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.DeletedAt,
|
||||
&i.Address,
|
||||
&i.Handle,
|
||||
&i.Origin,
|
||||
&i.Name,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getProfileByHandle = `-- name: GetProfileByHandle :one
|
||||
SELECT id, created_at, updated_at, deleted_at, address, handle, origin, name FROM profiles
|
||||
WHERE handle = ?
|
||||
AND deleted_at IS NULL
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
func (q *Queries) GetProfileByHandle(ctx context.Context, handle string) (Profile, error) {
|
||||
row := q.db.QueryRowContext(ctx, getProfileByHandle, handle)
|
||||
var i Profile
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.DeletedAt,
|
||||
&i.Address,
|
||||
&i.Handle,
|
||||
&i.Origin,
|
||||
&i.Name,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getProfileByID = `-- name: GetProfileByID :one
|
||||
SELECT id, created_at, updated_at, deleted_at, address, handle, origin, name FROM profiles
|
||||
WHERE id = ? AND deleted_at IS NULL
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
func (q *Queries) GetProfileByID(ctx context.Context, id string) (Profile, error) {
|
||||
row := q.db.QueryRowContext(ctx, getProfileByID, id)
|
||||
var i Profile
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.DeletedAt,
|
||||
&i.Address,
|
||||
&i.Handle,
|
||||
&i.Origin,
|
||||
&i.Name,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getSessionByClientIP = `-- name: GetSessionByClientIP :one
|
||||
SELECT id, created_at, updated_at, deleted_at, browser_name, browser_version, client_ipaddr, platform, is_desktop, is_mobile, is_tablet, is_tv, is_bot, challenge, is_human_first, is_human_last, profile_id FROM sessions
|
||||
WHERE client_ipaddr = ? AND deleted_at IS NULL
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
func (q *Queries) GetSessionByClientIP(ctx context.Context, clientIpaddr string) (Session, error) {
|
||||
row := q.db.QueryRowContext(ctx, getSessionByClientIP, clientIpaddr)
|
||||
var i Session
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.DeletedAt,
|
||||
&i.BrowserName,
|
||||
&i.BrowserVersion,
|
||||
&i.ClientIpaddr,
|
||||
&i.Platform,
|
||||
&i.IsDesktop,
|
||||
&i.IsMobile,
|
||||
&i.IsTablet,
|
||||
&i.IsTv,
|
||||
&i.IsBot,
|
||||
&i.Challenge,
|
||||
&i.IsHumanFirst,
|
||||
&i.IsHumanLast,
|
||||
&i.ProfileID,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getSessionByID = `-- name: GetSessionByID :one
|
||||
SELECT id, created_at, updated_at, deleted_at, browser_name, browser_version, client_ipaddr, platform, is_desktop, is_mobile, is_tablet, is_tv, is_bot, challenge, is_human_first, is_human_last, profile_id FROM sessions
|
||||
WHERE id = ? AND deleted_at IS NULL
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
func (q *Queries) GetSessionByID(ctx context.Context, id string) (Session, error) {
|
||||
row := q.db.QueryRowContext(ctx, getSessionByID, id)
|
||||
var i Session
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.DeletedAt,
|
||||
&i.BrowserName,
|
||||
&i.BrowserVersion,
|
||||
&i.ClientIpaddr,
|
||||
&i.Platform,
|
||||
&i.IsDesktop,
|
||||
&i.IsMobile,
|
||||
&i.IsTablet,
|
||||
&i.IsTv,
|
||||
&i.IsBot,
|
||||
&i.Challenge,
|
||||
&i.IsHumanFirst,
|
||||
&i.IsHumanLast,
|
||||
&i.ProfileID,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getVaultConfigByCID = `-- name: GetVaultConfigByCID :one
|
||||
SELECT id, created_at, updated_at, deleted_at, handle, origin, address, cid, config, session_id, redirect_uri FROM vaults
|
||||
WHERE cid = ?
|
||||
AND deleted_at IS NULL
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
func (q *Queries) GetVaultConfigByCID(ctx context.Context, cid string) (Vault, error) {
|
||||
row := q.db.QueryRowContext(ctx, getVaultConfigByCID, cid)
|
||||
var i Vault
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.DeletedAt,
|
||||
&i.Handle,
|
||||
&i.Origin,
|
||||
&i.Address,
|
||||
&i.Cid,
|
||||
&i.Config,
|
||||
&i.SessionID,
|
||||
&i.RedirectUri,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getVaultRedirectURIBySessionID = `-- name: GetVaultRedirectURIBySessionID :one
|
||||
SELECT redirect_uri FROM vaults
|
||||
WHERE session_id = ?
|
||||
AND deleted_at IS NULL
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
func (q *Queries) GetVaultRedirectURIBySessionID(ctx context.Context, sessionID string) (string, error) {
|
||||
row := q.db.QueryRowContext(ctx, getVaultRedirectURIBySessionID, sessionID)
|
||||
var redirect_uri string
|
||||
err := row.Scan(&redirect_uri)
|
||||
return redirect_uri, err
|
||||
}
|
||||
|
||||
const insertCredential = `-- name: InsertCredential :one
|
||||
INSERT INTO credentials (
|
||||
handle,
|
||||
credential_id,
|
||||
origin,
|
||||
type,
|
||||
transports
|
||||
) VALUES (?, ?, ?, ?, ?)
|
||||
RETURNING id, created_at, updated_at, deleted_at, handle, credential_id, authenticator_attachment, origin, type, transports
|
||||
`
|
||||
|
||||
type InsertCredentialParams struct {
|
||||
Handle string `json:"handle"`
|
||||
CredentialID string `json:"credential_id"`
|
||||
Origin string `json:"origin"`
|
||||
Type string `json:"type"`
|
||||
Transports string `json:"transports"`
|
||||
}
|
||||
|
||||
func (q *Queries) InsertCredential(ctx context.Context, arg InsertCredentialParams) (Credential, error) {
|
||||
row := q.db.QueryRowContext(ctx, insertCredential,
|
||||
arg.Handle,
|
||||
arg.CredentialID,
|
||||
arg.Origin,
|
||||
arg.Type,
|
||||
arg.Transports,
|
||||
)
|
||||
var i Credential
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.DeletedAt,
|
||||
&i.Handle,
|
||||
&i.CredentialID,
|
||||
&i.AuthenticatorAttachment,
|
||||
&i.Origin,
|
||||
&i.Type,
|
||||
&i.Transports,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const insertProfile = `-- name: InsertProfile :one
|
||||
INSERT INTO profiles (
|
||||
address,
|
||||
handle,
|
||||
origin,
|
||||
name
|
||||
) VALUES (?, ?, ?, ?)
|
||||
RETURNING id, created_at, updated_at, deleted_at, address, handle, origin, name
|
||||
`
|
||||
|
||||
type InsertProfileParams struct {
|
||||
Address string `json:"address"`
|
||||
Handle string `json:"handle"`
|
||||
Origin string `json:"origin"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
func (q *Queries) InsertProfile(ctx context.Context, arg InsertProfileParams) (Profile, error) {
|
||||
row := q.db.QueryRowContext(ctx, insertProfile,
|
||||
arg.Address,
|
||||
arg.Handle,
|
||||
arg.Origin,
|
||||
arg.Name,
|
||||
)
|
||||
var i Profile
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.DeletedAt,
|
||||
&i.Address,
|
||||
&i.Handle,
|
||||
&i.Origin,
|
||||
&i.Name,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const softDeleteCredential = `-- name: SoftDeleteCredential :exec
|
||||
UPDATE credentials
|
||||
SET deleted_at = CURRENT_TIMESTAMP
|
||||
WHERE credential_id = ?
|
||||
`
|
||||
|
||||
func (q *Queries) SoftDeleteCredential(ctx context.Context, credentialID string) error {
|
||||
_, err := q.db.ExecContext(ctx, softDeleteCredential, credentialID)
|
||||
return err
|
||||
}
|
||||
|
||||
const softDeleteProfile = `-- name: SoftDeleteProfile :exec
|
||||
UPDATE profiles
|
||||
SET deleted_at = CURRENT_TIMESTAMP
|
||||
WHERE address = ?
|
||||
`
|
||||
|
||||
func (q *Queries) SoftDeleteProfile(ctx context.Context, address string) error {
|
||||
_, err := q.db.ExecContext(ctx, softDeleteProfile, address)
|
||||
return err
|
||||
}
|
||||
|
||||
const updateProfile = `-- name: UpdateProfile :one
|
||||
UPDATE profiles
|
||||
SET
|
||||
name = ?,
|
||||
handle = ?,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE address = ?
|
||||
AND deleted_at IS NULL
|
||||
RETURNING id, created_at, updated_at, deleted_at, address, handle, origin, name
|
||||
`
|
||||
|
||||
type UpdateProfileParams struct {
|
||||
Name string `json:"name"`
|
||||
Handle string `json:"handle"`
|
||||
Address string `json:"address"`
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateProfile(ctx context.Context, arg UpdateProfileParams) (Profile, error) {
|
||||
row := q.db.QueryRowContext(ctx, updateProfile, arg.Name, arg.Handle, arg.Address)
|
||||
var i Profile
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.DeletedAt,
|
||||
&i.Address,
|
||||
&i.Handle,
|
||||
&i.Origin,
|
||||
&i.Name,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const updateSessionHumanVerification = `-- name: UpdateSessionHumanVerification :one
|
||||
UPDATE sessions
|
||||
SET
|
||||
is_human_first = ?,
|
||||
is_human_last = ?,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = ?
|
||||
RETURNING id, created_at, updated_at, deleted_at, browser_name, browser_version, client_ipaddr, platform, is_desktop, is_mobile, is_tablet, is_tv, is_bot, challenge, is_human_first, is_human_last, profile_id
|
||||
`
|
||||
|
||||
type UpdateSessionHumanVerificationParams struct {
|
||||
IsHumanFirst bool `json:"is_human_first"`
|
||||
IsHumanLast bool `json:"is_human_last"`
|
||||
ID string `json:"id"`
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateSessionHumanVerification(ctx context.Context, arg UpdateSessionHumanVerificationParams) (Session, error) {
|
||||
row := q.db.QueryRowContext(ctx, updateSessionHumanVerification, arg.IsHumanFirst, arg.IsHumanLast, arg.ID)
|
||||
var i Session
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.DeletedAt,
|
||||
&i.BrowserName,
|
||||
&i.BrowserVersion,
|
||||
&i.ClientIpaddr,
|
||||
&i.Platform,
|
||||
&i.IsDesktop,
|
||||
&i.IsMobile,
|
||||
&i.IsTablet,
|
||||
&i.IsTv,
|
||||
&i.IsBot,
|
||||
&i.Challenge,
|
||||
&i.IsHumanFirst,
|
||||
&i.IsHumanLast,
|
||||
&i.ProfileID,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const updateSessionWithProfileID = `-- name: UpdateSessionWithProfileID :one
|
||||
UPDATE sessions
|
||||
SET
|
||||
profile_id = ?,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = ?
|
||||
RETURNING id, created_at, updated_at, deleted_at, browser_name, browser_version, client_ipaddr, platform, is_desktop, is_mobile, is_tablet, is_tv, is_bot, challenge, is_human_first, is_human_last, profile_id
|
||||
`
|
||||
|
||||
type UpdateSessionWithProfileIDParams struct {
|
||||
ProfileID string `json:"profile_id"`
|
||||
ID string `json:"id"`
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateSessionWithProfileID(ctx context.Context, arg UpdateSessionWithProfileIDParams) (Session, error) {
|
||||
row := q.db.QueryRowContext(ctx, updateSessionWithProfileID, arg.ProfileID, arg.ID)
|
||||
var i Session
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.DeletedAt,
|
||||
&i.BrowserName,
|
||||
&i.BrowserVersion,
|
||||
&i.ClientIpaddr,
|
||||
&i.Platform,
|
||||
&i.IsDesktop,
|
||||
&i.IsMobile,
|
||||
&i.IsTablet,
|
||||
&i.IsTv,
|
||||
&i.IsBot,
|
||||
&i.Challenge,
|
||||
&i.IsHumanFirst,
|
||||
&i.IsHumanLast,
|
||||
&i.ProfileID,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
Reference in New Issue
Block a user