Files
sonr/pkg/gateway/middleware/vaults.go
T

58 lines
1.4 KiB
Go
Raw Normal View History

2024-12-16 15:29:54 -05:00
package middleware
import (
"fmt"
"github.com/labstack/echo/v4"
"github.com/onsonr/sonr/crypto/mpc"
"github.com/onsonr/sonr/internal/context"
"github.com/onsonr/sonr/pkg/gateway/types"
"lukechampine.com/blake3"
)
func Spawn(c echo.Context) (types.CreatePasskeyParams, error) {
cc := c.(*GatewayContext)
block := fmt.Sprintf("%d", CurrentBlock(c))
handle := GetHandle(c)
origin := GetOrigin(c)
challenge := GetSessionChallenge(c)
sid := GetSessionID(c)
nonce, err := calcNonce(sid)
if err != nil {
return types.DefaultCreatePasskeyParams(), err
}
encl, err := mpc.GenEnclave(nonce)
if err != nil {
return types.DefaultCreatePasskeyParams(), err
}
cc.stagedEnclaves[sid] = encl
context.WriteCookie(c, context.SonrAddress, encl.Address())
return types.CreatePasskeyParams{
Address: encl.Address(),
Handle: handle,
Name: origin,
Challenge: challenge,
CreationBlock: block,
}, nil
}
func Claim() (types.CreatePasskeyParams, error) {
return types.CreatePasskeyParams{}, 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
}