Skip to content

Commit

Permalink
sign message ext
Browse files Browse the repository at this point in the history
  • Loading branch information
marino39 committed Feb 12, 2024
1 parent 4e71f0d commit 5acdaf7
Show file tree
Hide file tree
Showing 11 changed files with 427 additions and 11 deletions.
6 changes: 3 additions & 3 deletions intentv1/intent.gen.go

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

6 changes: 3 additions & 3 deletions intentv1/intent.gen.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable */
// sequence-waas-intents v0.1.0 b4f2c5263bf9e85831646dacc7ac913b72430819
// sequence-waas-intents v0.1.0 aa1ecacec8e54e06aeffbf733fbbad763d533fcb
// --
// Code generated by [email protected] with typescript generator. DO NOT EDIT.
//
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 = "b4f2c5263bf9e85831646dacc7ac913b72430819"
export const WebRPCSchemaHash = "aa1ecacec8e54e06aeffbf733fbbad763d533fcb"

//
// Types
Expand Down Expand Up @@ -64,7 +64,7 @@ export interface IntentDataGetSession {
wallet: string
}

export interface IntentDataSign {
export interface IntentDataSignMessage {
network: string
wallet: string
message: string
Expand Down
4 changes: 2 additions & 2 deletions intentv1/intent.ridl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct Signature
# - finishValidateSession
# - listSessions
# - getSession
# - sign
# - signMessage
# - sendTransaction

struct IntentDataOpenSession
Expand Down Expand Up @@ -51,7 +51,7 @@ struct IntentDataGetSession
- sessionId: string
- wallet: string

struct IntentDataSign
struct IntentDataSignMessage
- network: string
- wallet: string
- message: string
Expand Down
48 changes: 48 additions & 0 deletions intentv1/intent_data_sign_message_ext.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package intents

import (
"bytes"
"fmt"
"math/big"

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

func (p *IntentDataSignMessage) chainID() (*big.Int, error) {
n, ok := sequence.ParseHexOrDec(p.Network)
if !ok {
return nil, fmt.Errorf("invalid network id '%s'", p.Network)
}

return n, nil
}

func (p *IntentDataSignMessage) message() []byte {
return common.FromHex(p.Message)
}

func (p *IntentDataSignMessage) wallet() common.Address {
return common.HexToAddress(p.Wallet)
}

func (p *IntentDataSignMessage) subdigest() ([]byte, error) {
chainID, err := p.chainID()
if err != nil {
return nil, err
}

return sequence.SubDigest(chainID, p.wallet(), sequence.MessageDigest(p.message()))
}

// A SignMessagePacket (intent) *MUST* be mapped to a regular "SignMessage" Sequence action, this means that
// it must adhere to the following rules:
// - the subdigest must match `SubDigest(chainID, Wallet, Digest(Message))`
func (p *IntentDataSignMessage) IsValidInterpretation(subdigest common.Hash) bool {
selfSubDigest, err := p.subdigest()
if err != nil {
return false
}

return bytes.Equal(selfSubDigest, subdigest[:])
}
67 changes: 67 additions & 0 deletions intentv1/intent_data_sign_message_ext_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package intents

import (
"encoding/json"
"math/big"
"testing"
"time"

"github.com/0xsequence/ethkit/ethwallet"
"github.com/0xsequence/ethkit/go-ethereum/common"
"github.com/0xsequence/go-sequence"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestRecoverMessageIntent(t *testing.T) {
data := `{
"version": "1",
"name": "signMessage",
"issued": 0,
"expires": 0,
"data": {
"wallet": "0xD67FC48b298B09Ed3D03403d930769C527186c4e",
"network": "1",
"message": "0xdeadbeef"
},
"signatures": []
}`

intent := &Intent{}
err := json.Unmarshal([]byte(data), intent)
assert.Nil(t, err)

assert.Equal(t, "1", intent.Version)
assert.Equal(t, "signMessage", intent.Name)

hash, err := intent.Hash()
assert.Nil(t, err)
assert.NotNil(t, common.Bytes2Hex(hash))

intent.IssuedAt = uint64(time.Now().Unix())
intent.ExpiresAt = uint64(time.Now().Unix()) + 60

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

session := NewSessionP256K1(wallet)

err = session.Sign(intent)
require.Nil(t, err)

intentTyped, err := NewIntentTypedFromIntent[IntentDataSignMessage](intent)
require.NoError(t, err)

signers := intent.Signers()
assert.Equal(t, 1, len(signers))
assert.Equal(t, "0x"+common.Bytes2Hex(append([]byte{0x00}, wallet.Address().Bytes()...)), signers[0])

subdigest, err := sequence.SubDigest(
big.NewInt(1),
common.HexToAddress("0xD67FC48b298B09Ed3D03403d930769C527186c4e"),
sequence.MessageDigest(common.Hex2Bytes("deadbeef")),
)

assert.Nil(t, err)
assert.True(t, intentTyped.Data.IsValidInterpretation(common.BytesToHash(subdigest)))
}
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 5acdaf7

Please sign in to comment.