Skip to content

Commit

Permalink
feat: Add Connect function to existing vault preparation
Browse files Browse the repository at this point in the history
feat: Add Register Options for WebAuthn authentication

feat: Add EncryptedSet interface for keyshare set
  • Loading branch information
prnk28 committed Jun 3, 2024
1 parent 50b0c6b commit 30b3728
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 0 deletions.
9 changes: 9 additions & 0 deletions crypto/kss/set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package kss

import "github.com/di-dao/sonr/crypto"

// KssI is the interface for the keyshare set
type EncryptedSet interface {
Decrypt(key []byte) (Set, error)
PublicKey() crypto.PublicKey
}
91 changes: 91 additions & 0 deletions pkg/vault/auth/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package auth

import (
"context"

"github.com/di-dao/sonr/internal/local"
"github.com/go-webauthn/webauthn/protocol"
"github.com/go-webauthn/webauthn/protocol/webauthncose"
)

func GetRegisterOptions(ctx context.Context, challenge protocol.URLEncodedBase64) (protocol.PublicKeyCredentialCreationOptions, error) {
return protocol.PublicKeyCredentialCreationOptions{
Challenge: challenge,
AuthenticatorSelection: defaultAuthenticationSelection(),
RelyingParty: GetRelayingPartyEntity(ctx),
User: GetUserEntity(ctx),
Parameters: defaultRegistrationCredentialParameters(),
}, nil
}

func GetUserEntity(ctx context.Context) protocol.UserEntity {
snrctx := local.UnwrapContext(ctx)
return protocol.UserEntity{
ID: snrctx.UserAddress,
DisplayName: snrctx.UserAddress,
CredentialEntity: protocol.CredentialEntity{
Name: snrctx.UserAddress,
},
}
}

func GetRelayingPartyEntity(ctx context.Context) protocol.RelyingPartyEntity {
snrctx := local.UnwrapContext(ctx)
return protocol.RelyingPartyEntity{
ID: snrctx.ServiceOrigin,
CredentialEntity: protocol.CredentialEntity{
Name: snrctx.ServiceOrigin,
},
}
}

func defaultAuthenticationSelection() protocol.AuthenticatorSelection {
return protocol.AuthenticatorSelection{
AuthenticatorAttachment: protocol.CrossPlatform,
}
}

func defaultRegistrationCredentialParameters() []protocol.CredentialParameter {
return []protocol.CredentialParameter{
{
Type: protocol.PublicKeyCredentialType,
Algorithm: webauthncose.AlgES256,
},
{
Type: protocol.PublicKeyCredentialType,
Algorithm: webauthncose.AlgES384,
},
{
Type: protocol.PublicKeyCredentialType,
Algorithm: webauthncose.AlgES512,
},
{
Type: protocol.PublicKeyCredentialType,
Algorithm: webauthncose.AlgRS256,
},
{
Type: protocol.PublicKeyCredentialType,
Algorithm: webauthncose.AlgRS384,
},
{
Type: protocol.PublicKeyCredentialType,
Algorithm: webauthncose.AlgRS512,
},
{
Type: protocol.PublicKeyCredentialType,
Algorithm: webauthncose.AlgPS256,
},
{
Type: protocol.PublicKeyCredentialType,
Algorithm: webauthncose.AlgPS384,
},
{
Type: protocol.PublicKeyCredentialType,
Algorithm: webauthncose.AlgPS512,
},
{
Type: protocol.PublicKeyCredentialType,
Algorithm: webauthncose.AlgEdDSA,
},
}
}
30 changes: 30 additions & 0 deletions pkg/vault/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"

"github.com/di-dao/sonr/crypto/kss"
"github.com/di-dao/sonr/pkg/ipfs"
"github.com/di-dao/sonr/pkg/vault/auth"
"github.com/di-dao/sonr/pkg/vault/props"
"github.com/di-dao/sonr/pkg/vault/wallet"
Expand Down Expand Up @@ -69,3 +70,32 @@ func createVaultFS(set kss.Set) (*vaultFS, error) {
Properties: props.NewProperties(),
}, nil
}

func loadVaultFS(vfs ipfs.VFS) (*vaultFS, error) {
wallet := &wallet.Wallet{}
walletBz, err := vfs.Get("wallet.json")
if err != nil {
return nil, err
}

err = wallet.Unmarshal(walletBz)
if err != nil {
return nil, err
}

info := &InfoFile{}
infoBz, err := vfs.Get("info.json")
if err != nil {
return nil, err
}
err = info.Unmarshal(infoBz)
if err != nil {
return nil, err
}

return &vaultFS{
Wallet: wallet,
Creds: info.Creds,
Properties: info.Properties,
}, nil
}
23 changes: 23 additions & 0 deletions pkg/vault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,26 @@ func Generate(ctx context.Context) (Vault, error) {
vfs: ipfs.NewFSWithKss(keyshares, fs.Wallet.SonrAddress()),
}, nil
}

// Connect connects to an existing vault.
func Connect(ctx context.Context, address string) (Vault, error) {
snrCtx := local.UnwrapContext(ctx)
vfs, err := ipfs.GetFileSystem(ctx, address)
if err != nil {
return nil, err
}
fs, err := loadVaultFS(vfs)
if err != nil {
return nil, err
}

// Update the context with the wallet address
snrCtx.UserAddress = fs.Wallet.SonrAddress()
local.WrapContext(snrCtx)

// Create a new vault
return &vault{
vfs: vfs,
vltFS: fs,
}, nil
}

0 comments on commit 30b3728

Please sign in to comment.