Skip to content

Commit

Permalink
intent session signers
Browse files Browse the repository at this point in the history
  • Loading branch information
marino39 committed Feb 9, 2024
1 parent 3823437 commit 5c20a2a
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 89 deletions.
5 changes: 2 additions & 3 deletions intentv1/intent_typed.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ func NewIntentTypedFromIntent[T any](intent *Intent) (*IntentTyped[T], error) {
}

func (i *IntentTyped[T]) ToIntent() *Intent {
var intentCopy = i.Intent
intentCopy.Data = i.Data
return &intentCopy
i.Intent.Data = i.Data
return &i.Intent
}
40 changes: 31 additions & 9 deletions intentv1/intent_typed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,15 @@ func TestIntentNewIntentTypedFromIntent(t *testing.T) {
}

func TestIntentIsValid(t *testing.T) {
signer, err := ethwallet.NewWalletFromRandomEntropy()
require.NoError(t, err)

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

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

session := NewSession256K1(wallet)
err = session.Sign(intent.ToIntent())
require.NoError(t, err)

assert.NoError(t, intent.IsValid())
Expand All @@ -99,7 +101,11 @@ func TestIntentIsValid(t *testing.T) {
t.Run("valid_p256k1Signature", func(t *testing.T) {
intent := NewIntentTyped(IntentDataOpenSession{SessionId: "0x1234"})

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

session := NewSession256K1(wallet)
err = session.Sign(intent.ToIntent())
require.NoError(t, err)

assert.NoError(t, intent.IsValid())
Expand All @@ -111,7 +117,8 @@ func TestIntentIsValid(t *testing.T) {
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
require.NoError(t, err)

err = SignIntentWithP256R1(privateKey, intent)
session := NewSession256R1(privateKey)
err = session.Sign(intent.ToIntent())
require.NoError(t, err)

assert.NoError(t, intent.IsValid())
Expand All @@ -120,7 +127,10 @@ func TestIntentIsValid(t *testing.T) {
t.Run("valid_legacySignature", func(t *testing.T) {
intent := NewIntentTyped(IntentDataOpenSession{SessionId: "0x1234"})

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

err = SignIntentWithWalletLegacy(wallet, intent)
require.NoError(t, err)

assert.NoError(t, intent.IsValid())
Expand All @@ -130,7 +140,11 @@ func TestIntentIsValid(t *testing.T) {
intent := NewIntentTyped(IntentDataOpenSession{SessionId: "0x1234"})
intent.Expires = 0

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

session := NewSession256K1(wallet)
err = session.Sign(intent.ToIntent())
require.NoError(t, err)

assert.ErrorContains(t, intent.IsValid(), "expired")
Expand All @@ -140,7 +154,11 @@ func TestIntentIsValid(t *testing.T) {
intent := NewIntentTyped(IntentDataOpenSession{SessionId: "0x1234"})
intent.Issued = uint64(1 << 63)

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

session := NewSession256K1(wallet)
err = session.Sign(intent.ToIntent())
require.NoError(t, err)

assert.ErrorContains(t, intent.IsValid(), "issued in the future")
Expand All @@ -155,7 +173,11 @@ func TestIntentIsValid(t *testing.T) {
t.Run("invalidSignature", func(t *testing.T) {
intent := NewIntentTyped(IntentDataOpenSession{SessionId: "0x1234"})

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

session := NewSession256K1(wallet)
err = session.Sign(intent.ToIntent())
require.NoError(t, err)

intent.Signatures[0].Signature = "0x1234"
Expand Down
119 changes: 119 additions & 0 deletions intentv1/session.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package intents

import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"fmt"
"strings"

"github.com/0xsequence/ethkit/ethwallet"
"github.com/0xsequence/ethkit/go-ethereum/common"
)

type Session interface {
SessionId() string
Sign(intent *Intent) error
}

type session256K1 struct {
wallet *ethwallet.Wallet
}

func NewSession256K1(wallet *ethwallet.Wallet) Session {
return &session256K1{wallet: wallet}
}

func (s session256K1) SessionId() string {
return strings.ToLower(
fmt.Sprintf(
"0x%s",
common.Bytes2Hex(
append([]byte{byte(KeyTypeSECP256K1)}, s.wallet.Address().Bytes()...),
),
),
)
}

func (s session256K1) Sign(intent *Intent) error {
hash, err := intent.Hash()
if err != nil {
return err
}

signature, err := s.wallet.SignMessage(hash)
if err != nil {
return err
}

intent.Signatures = append(intent.Signatures, &Signature{
SessionId: s.SessionId(),
Signature: bytesToSignature(signature),
})
return nil
}

type session256R1 struct {
privateKey *ecdsa.PrivateKey
}

func NewSession256R1(privateKey *ecdsa.PrivateKey) Session {
return &session256R1{privateKey: privateKey}
}

func (s session256R1) SessionId() string {
pubKey := elliptic.Marshal(s.privateKey.Curve, s.privateKey.PublicKey.X, s.privateKey.PublicKey.Y)
return strings.ToLower(
fmt.Sprintf(
"0x%s",
common.Bytes2Hex(
append([]byte{byte(KeyTypeSECP256R1)}, pubKey...),
),
),
)
}

func (s session256R1) Sign(intent *Intent) error {
hash, err := intent.Hash()
if err != nil {
return err
}

sha256Hash := sha256.Sum256(hash)

sr, ss, err := ecdsa.Sign(rand.Reader, s.privateKey, sha256Hash[:])
if err != nil {
return err
}

signature := append(sr.Bytes(), ss.Bytes()...)

intent.Signatures = append(intent.Signatures, &Signature{
SessionId: s.SessionId(),
Signature: bytesToSignature(signature),
})
return nil
}

func SignIntentWithWalletLegacy[T any](wallet *ethwallet.Wallet, intent *IntentTyped[T]) error {
hash, err := intent.Hash()
if err != nil {
return err
}

signature, err := wallet.SignMessage(hash)
if err != nil {
return err
}

intent.Signatures = append(intent.Signatures, &Signature{
SessionId: strings.ToLower(wallet.Address().String()),
Signature: strings.ToLower(fmt.Sprintf("0x%s", common.Bytes2Hex(signature))),
})
return nil
}

func bytesToSignature(sig []byte) string {
return strings.ToLower(fmt.Sprintf("0x%s", common.Bytes2Hex(sig)))
}
77 changes: 0 additions & 77 deletions intentv1/sign.go

This file was deleted.

0 comments on commit 5c20a2a

Please sign in to comment.