-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
intents: add eip712 support via new IntentDataSignTypedData
- Loading branch information
Showing
6 changed files
with
223 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package intents | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"math/big" | ||
|
||
"github.com/0xsequence/ethkit/ethcoder" | ||
"github.com/0xsequence/ethkit/go-ethereum/common" | ||
"github.com/0xsequence/go-sequence" | ||
) | ||
|
||
func (p *IntentDataSignTypedData) UnmarshalJSON(data []byte) error { | ||
type Raw struct { | ||
Network string `json:"network"` | ||
Wallet string `json:"wallet"` | ||
TypedData json.RawMessage `json:"typedData"` | ||
} | ||
|
||
dec := json.NewDecoder(bytes.NewReader(data)) | ||
var raw Raw | ||
if err := dec.Decode(&raw); err != nil { | ||
return err | ||
} | ||
|
||
typedData, err := ethcoder.TypedDataFromJSON(string(raw.TypedData)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
p.Network = raw.Network | ||
p.Wallet = raw.Wallet | ||
p.TypedData = typedData | ||
return nil | ||
} | ||
|
||
func (p *IntentDataSignTypedData) 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 *IntentDataSignTypedData) message() ([]byte, error) { | ||
typedData, ok := p.TypedData.(*ethcoder.TypedData) | ||
if !ok { | ||
return nil, fmt.Errorf("typedData field is not a valid typed data object") | ||
} | ||
|
||
_, encodedMessage, err := typedData.Encode() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return encodedMessage, nil | ||
} | ||
|
||
func (p *IntentDataSignTypedData) wallet() common.Address { | ||
return common.HexToAddress(p.Wallet) | ||
} | ||
|
||
func (p *IntentDataSignTypedData) subdigest() ([]byte, error) { | ||
chainID, err := p.chainID() | ||
if err != nil { | ||
return nil, err | ||
} | ||
msgData, err := p.message() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return sequence.SubDigest(chainID, p.wallet(), sequence.MessageDigest(msgData)) | ||
} | ||
|
||
// 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 *IntentDataSignTypedData) IsValidInterpretation(subdigest common.Hash) bool { | ||
selfSubDigest, err := p.subdigest() | ||
if err != nil { | ||
return false | ||
} | ||
|
||
return bytes.Equal(selfSubDigest, subdigest[:]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package intents | ||
|
||
import ( | ||
"encoding/json" | ||
"math/big" | ||
"testing" | ||
"time" | ||
|
||
"github.com/0xsequence/ethkit/ethcoder" | ||
"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 TestRecoverTypedDataIntent(t *testing.T) { | ||
data := `{ | ||
"version": "1", | ||
"name": "signTypedData", | ||
"issued": 0, | ||
"expires": 0, | ||
"data": { | ||
"wallet": "0xD67FC48b298B09Ed3D03403d930769C527186c4e", | ||
"network": "1", | ||
"typedData": { | ||
"types": { | ||
"EIP712Domain": [ | ||
{"name": "name", "type": "string"}, | ||
{"name": "version", "type": "string"}, | ||
{"name": "chainId", "type": "uint256"}, | ||
{"name": "verifyingContract", "type": "address"} | ||
], | ||
"Person": [ | ||
{"name": "name", "type": "string"}, | ||
{"name": "wallet", "type": "address"}, | ||
{"name": "count", "type": "uint8"} | ||
] | ||
}, | ||
"primaryType": "Person", | ||
"domain": { | ||
"name": "Ether Mail", | ||
"version": "1", | ||
"chainId": 1, | ||
"verifyingContract": "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC" | ||
}, | ||
"message": { | ||
"name": "Bob", | ||
"wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB", | ||
"count": 4 | ||
} | ||
} | ||
}, | ||
"signatures": [] | ||
}` | ||
|
||
intent := &Intent{} | ||
err := json.Unmarshal([]byte(data), intent) | ||
require.Nil(t, err) | ||
|
||
require.Equal(t, "1", intent.Version) | ||
require.Equal(t, IntentName_signTypedData, intent.Name) | ||
|
||
hash, err := intent.Hash() | ||
require.Nil(t, err) | ||
require.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[IntentDataSignTypedData](intent) | ||
require.NoError(t, err) | ||
|
||
typedData, ok := intentTyped.Data.TypedData.(*ethcoder.TypedData) | ||
require.True(t, ok) | ||
require.NotNil(t, typedData) | ||
|
||
signers := intent.Signers() | ||
require.Equal(t, 1, len(signers)) | ||
require.Equal(t, "0x"+common.Bytes2Hex(append([]byte{0x00}, wallet.Address().Bytes()...)), signers[0]) | ||
|
||
messageDigest, err := typedData.EncodeDigest() | ||
require.NoError(t, err) | ||
require.NotNil(t, messageDigest) | ||
|
||
subdigest, err := sequence.SubDigest( | ||
big.NewInt(1), | ||
common.HexToAddress("0xD67FC48b298B09Ed3D03403d930769C527186c4e"), | ||
common.BytesToHash(messageDigest), | ||
) | ||
|
||
assert.Nil(t, err) | ||
assert.True(t, intentTyped.Data.IsValidInterpretation(common.BytesToHash(subdigest))) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters