-
Notifications
You must be signed in to change notification settings - Fork 8
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 (#180)
- Loading branch information
Showing
9 changed files
with
244 additions
and
30 deletions.
There are no files selected for viewing
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
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
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,81 @@ | ||
package intents | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"math/big" | ||
|
||
"github.com/0xsequence/ethkit/go-ethereum/common" | ||
"github.com/0xsequence/go-sequence" | ||
) | ||
|
||
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 := p.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)) | ||
} | ||
|
||
func (p *IntentDataSignTypedData) IsValidInterpretation(subdigest common.Hash) bool { | ||
selfSubDigest, err := p.subdigest() | ||
if err != nil { | ||
return false | ||
} | ||
|
||
return bytes.Equal(selfSubDigest, subdigest[:]) | ||
} | ||
|
||
// 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 | ||
// } |
Oops, something went wrong.