Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

intents: SessionAuthProof #127

Merged
merged 10 commits into from
Mar 21, 2024
71 changes: 64 additions & 7 deletions intents/intent.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 17 additions & 3 deletions intents/intent.gen.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable */
// sequence-waas-intents v0.1.0 2c4dea0eacfd30c622628970fba756e9f4a21e61
// sequence-waas-intents v0.1.0 4e0514d799b9cb7630838b4ff70717bbfb3ceebb
// --
// Code generated by [email protected].0-dev with typescript generator. DO NOT EDIT.
// Code generated by [email protected].5 with typescript generator. DO NOT EDIT.
//
// webrpc-gen -schema=intent.ridl -target=typescript -client -out=./intent.gen.ts

Expand All @@ -12,7 +12,7 @@ export const WebRPCVersion = "v1"
export const WebRPCSchemaVersion = "v0.1.0"

// Schema hash generated from your RIDL schema
export const WebRPCSchemaHash = "2c4dea0eacfd30c622628970fba756e9f4a21e61"
export const WebRPCSchemaHash = "4e0514d799b9cb7630838b4ff70717bbfb3ceebb"

//
// Types
Expand Down Expand Up @@ -71,6 +71,12 @@ export interface IntentDataGetSession {
wallet: string
}

export interface IntentDataSessionAuthProof {
network: string
wallet: string
nonce?: string
}

export interface IntentDataSignMessage {
network: string
wallet: string
Expand Down Expand Up @@ -179,6 +185,14 @@ export interface IntentResponseGetSession {
validated: boolean
}

export interface IntentResponseSessionAuthProof {
sessionId: string
network: string
wallet: string
message: string
signature: string
}

export interface IntentResponseSignedMessage {
signature: string
message: string
Expand Down
16 changes: 16 additions & 0 deletions intents/intent.ridl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ struct Signature
# - finishValidateSession
# - listSessions
# - getSession
# - sessionAuthProof
# - signMessage
# - sendTransaction
# - getTransactionReceipt
Expand Down Expand Up @@ -62,6 +63,11 @@ struct IntentDataGetSession
+ go.field.name = SessionID
- wallet: string

struct IntentDataSessionAuthProof
- network: string
- wallet: string
- nonce?: string

struct IntentDataSignMessage
- network: string
- wallet: string
Expand Down Expand Up @@ -145,6 +151,7 @@ struct IntentResponse
# - validationRequired
# - validationStarted
# - validationFinished
# - sessionAuthProof
# - signedMessage
# - transactionReceipt
# - transactionFailed
Expand Down Expand Up @@ -178,6 +185,15 @@ struct IntentResponseGetSession
- wallet: string
- validated: bool

struct IntentResponseSessionAuthProof
- sessionId: string
+ go.field.name = SessionID
- network: string
- wallet: string
# The message contents: “SessionAuthProof <sessionId> <wallet> <nonce?>” hex encoded
- message: string
- signature: string

struct IntentResponseSignedMessage
- signature: string
- message: string
Expand Down
16 changes: 15 additions & 1 deletion intents/intent_data_ext.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package intents

import "fmt"
import (
"fmt"

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

func (id *IntentDataOpenSession) IsValid() error {
if id.SessionID == "" {
Expand All @@ -12,3 +16,13 @@ func (id *IntentDataOpenSession) IsValid() error {
}
return nil
}

func (id *IntentDataSessionAuthProof) IsValidInterpretation(sessionID string, message string) error {
message2 := "0x" + common.Bytes2Hex(
[]byte(SessionAuthProofMessage(sessionID, id.Wallet, id.Nonce)),
)
if message != message2 {
return fmt.Errorf("proof message does not match: '%s' != '%s'", message, message2)
}
return nil
}
4 changes: 4 additions & 0 deletions intents/intent_ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
IntentNameFinishValidateSession = "finishValidateSession"
IntentNameListSessions = "listSessions"
IntentNameGetSession = "getSession"
IntentNameSessionAuthProof = "sessionAuthProof"
IntentNameSignMessage = "signMessage"
IntentNameFeeOptions = "feeOptions"
IntentNameSendTransaction = "sendTransaction"
Expand All @@ -29,6 +30,9 @@ const (
IntentResponseCodeValidationRequired = "validationRequired"
IntentResponseCodeValidationStarted = "validationStarted"
IntentResponseCodeValidationFinished = "validationFinished"
IntentResponseCodeSessionClosed = "sessionClosed"
IntentResponseCodeSessionsListed = "sessionsListed"
IntentResponseCodeSessionAuthProof = "sessionAuthProof"
IntentResponseCodeSignedMessage = "signedMessage"
IntentResponseCodeFeeOptions = "feeOptions"
IntentResponseCodeTransactionReceipt = "transactionReceipt"
Expand Down
84 changes: 84 additions & 0 deletions intents/intent_response_typed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package intents

import (
"encoding/json"
"fmt"
)

func IntentResponseTypeToCode[T any](t *T) string {
var data any = t
switch data.(type) {
case *IntentResponseSessionOpened:
return IntentResponseCodeSessionOpened
case *IntentResponseValidationStarted:
return IntentResponseCodeValidationStarted
case *IntentResponseValidationFinished:
return IntentResponseCodeValidationFinished
case *IntentResponseSessionAuthProof:
return IntentResponseCodeSessionAuthProof
case *IntentResponseSignedMessage:
return IntentResponseCodeSignedMessage
case *IntentResponseFeeOptions:
return IntentResponseCodeFeeOptions
case *IntentResponseTransactionReceipt:
return IntentResponseCodeTransactionReceipt
case *IntentResponseTransactionFailed:
return IntentResponseCodeTransactionFailed
case *IntentResponseGetSession:
return IntentResponseCodeGetSessionResponse
default:
return ""
}
}

type IntentResponseTyped[T any] struct {
IntentResponse
Data T `json:"data"`
}

func NewIntentResponseTyped[T any](data T) *IntentResponseTyped[T] {
return &IntentResponseTyped[T]{
IntentResponse: IntentResponse{
Code: IntentResponseTypeToCode(&data),
Data: data,
},
Data: data,
}
}

func NewIntentResponseTypedFromIntentResponse[T any](res *IntentResponse) (*IntentResponseTyped[T], error) {
switch res.Data.(type) {
case T:
return &IntentResponseTyped[T]{
IntentResponse: *res,
Data: res.Data.(T),
}, nil
case map[string]any:
data := res.Data.(map[string]any)

// convert to json
dataJSON, err := json.Marshal(data)
if err != nil {
return nil, err
}

// convert to typed data
var typedData T
err = json.Unmarshal(dataJSON, &typedData)
if err != nil {
return nil, err
}

// check if intent response code and data type match
if IntentResponseTypeToCode(&typedData) != res.Code {
return nil, fmt.Errorf("intent response code and data type mismatch")
}

return &IntentResponseTyped[T]{
IntentResponse: *res,
Data: typedData,
}, nil
default:
return nil, fmt.Errorf("invalid intent data type")
}
}
2 changes: 2 additions & 0 deletions intents/intent_typed.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ func IntentDataTypeToName[T any](t *T) string {
return IntentNameListSessions
case *IntentDataGetSession:
return IntentNameGetSession
case *IntentDataSessionAuthProof:
return IntentNameSessionAuthProof
case *IntentDataSignMessage:
return IntentNameSignMessage
case *IntentDataFeeOptions:
Expand Down
7 changes: 7 additions & 0 deletions intents/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,10 @@ func KeyTypeFromSessionId(sessionId string) KeyType {
func bytesToSignature(sig []byte) string {
return strings.ToLower(fmt.Sprintf("0x%s", common.Bytes2Hex(sig)))
}

func SessionAuthProofMessage(sessionId string, wallet string, nonce *string) string {
if nonce == nil {
return fmt.Sprintf("SessionAuthProof %s %s", sessionId, wallet)
}
return fmt.Sprintf("SessionAuthProof %s %s %s", sessionId, wallet, *nonce)
}
16 changes: 16 additions & 0 deletions intents/session_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package intents

import (
"testing"

"github.com/0xsequence/ethkit"
"github.com/stretchr/testify/assert"
)

func TestSessionAuthProofMessage(t *testing.T) {
// no nonce provided
assert.Equal(t, "SessionAuthProof r1:0x01 0x02", SessionAuthProofMessage("r1:0x01", "0x02", nil))

// nonce provided
assert.Equal(t, "SessionAuthProof r1:0x01 0x02 0x03", SessionAuthProofMessage("r1:0x01", "0x02", ethkit.ToPtr("0x03")))
}
Loading