Skip to content

Commit

Permalink
intent data validators support
Browse files Browse the repository at this point in the history
  • Loading branch information
marino39 committed Feb 9, 2024
1 parent 93873c2 commit 4a4e14e
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
14 changes: 14 additions & 0 deletions intentv1/intent_data_ext.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package intents

import "fmt"

func (id *IntentDataOpenSession) IsValid() error {
if id.SessionId == "" {
return fmt.Errorf("session id is empty")
}

if id.IdToken == nil && id.Email == nil {
return fmt.Errorf("idToken and email are both nil")
}
return nil
}
4 changes: 4 additions & 0 deletions intentv1/intent_ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ const (
KeyTypeUnknown
)

type IntentDataValidator interface {
IsValid() error
}

func (intent *Intent) Hash() ([]byte, error) {
// copy intent and remove signatures
var intentCopy = *intent
Expand Down
17 changes: 17 additions & 0 deletions intentv1/intent_typed.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,23 @@ func NewIntentTypedFromIntent[T any](intent *Intent) (*IntentTyped[T], error) {
}
}

func (i *IntentTyped[T]) IsValid() error {
// check if the intent is valid
if err := i.Intent.IsValid(); err != nil {
return err
}

// check if the intent data is valid
var data any = &i.Data
if validator, ok := data.(IntentDataValidator); ok {
if err := validator.IsValid(); err != nil {
return fmt.Errorf("invalid intent data: %w", err)
}
}
// the intent is valid
return nil
}

func (i *IntentTyped[T]) AsIntent() *Intent {
i.Intent.Data = i.Data
return &i.Intent
Expand Down
29 changes: 29 additions & 0 deletions intentv1/intent_typed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/rand"
"testing"

"github.com/0xsequence/ethkit"
"github.com/0xsequence/ethkit/ethwallet"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -185,3 +186,31 @@ func TestIntentIsValid(t *testing.T) {
assert.ErrorContains(t, intent.IsValid(), "invalid signature")
})
}

func TestIntentDataValidator(t *testing.T) {
t.Run("valid", func(t *testing.T) {
intent := NewIntentTyped(IntentDataOpenSession{SessionId: "0x1234", Email: ethkit.ToPtr("[email protected]")})

wallet, err := ethwallet.NewWalletFromRandomEntropy()
require.NoError(t, err)

session := NewSessionP256K1(wallet)
err = session.Sign(intent.AsIntent())
require.NoError(t, err)

assert.NoError(t, intent.IsValid())
})

t.Run("invalid", func(t *testing.T) {
intent := NewIntentTyped(IntentDataOpenSession{SessionId: "0x1234"})

wallet, err := ethwallet.NewWalletFromRandomEntropy()
require.NoError(t, err)

session := NewSessionP256K1(wallet)
err = session.Sign(intent.AsIntent())
require.NoError(t, err)

assert.ErrorContains(t, intent.IsValid(), "invalid intent data")
})
}

0 comments on commit 4a4e14e

Please sign in to comment.