Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
ba3919ca
Unverified
Commit
ba3919ca
authored
Jul 22, 2022
by
Nikhil Suri
Committed by
GitHub
Jul 22, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
signer/core: add canonical TypedData hashing methods (#25283)
parent
1764f8f5
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
21 additions
and
8 deletions
+21
-8
types.go
signer/core/apitypes/types.go
+19
-0
signed_data.go
signer/core/signed_data.go
+2
-8
No files found.
signer/core/apitypes/types.go
View file @
ba3919ca
...
...
@@ -251,6 +251,25 @@ type TypedDataDomain struct {
Salt
string
`json:"salt"`
}
// TypedDataAndHash is a helper function that calculates a hash for typed data conforming to EIP-712.
// This hash can then be safely used to calculate a signature.
//
// See https://eips.ethereum.org/EIPS/eip-712 for the full specification.
//
// This gives context to the signed typed data and prevents signing of transactions.
func
TypedDataAndHash
(
typedData
TypedData
)
([]
byte
,
string
,
error
)
{
domainSeparator
,
err
:=
typedData
.
HashStruct
(
"EIP712Domain"
,
typedData
.
Domain
.
Map
())
if
err
!=
nil
{
return
nil
,
""
,
err
}
typedDataHash
,
err
:=
typedData
.
HashStruct
(
typedData
.
PrimaryType
,
typedData
.
Message
)
if
err
!=
nil
{
return
nil
,
""
,
err
}
rawData
:=
fmt
.
Sprintf
(
"
\x19\x01
%s%s"
,
string
(
domainSeparator
),
string
(
typedDataHash
))
return
crypto
.
Keccak256
([]
byte
(
rawData
)),
rawData
,
nil
}
// HashStruct generates a keccak256 hash of the encoding of the provided data
func
(
typedData
*
TypedData
)
HashStruct
(
primaryType
string
,
data
TypedDataMessage
)
(
hexutil
.
Bytes
,
error
)
{
encodedData
,
err
:=
typedData
.
EncodeData
(
primaryType
,
data
,
1
)
...
...
signer/core/signed_data.go
View file @
ba3919ca
...
...
@@ -233,23 +233,17 @@ func (api *SignerAPI) SignTypedData(ctx context.Context, addr common.MixedcaseAd
// - the signature preimage (hash)
func
(
api
*
SignerAPI
)
signTypedData
(
ctx
context
.
Context
,
addr
common
.
MixedcaseAddress
,
typedData
apitypes
.
TypedData
,
validationMessages
*
apitypes
.
ValidationMessages
)
(
hexutil
.
Bytes
,
hexutil
.
Bytes
,
error
)
{
domainSeparator
,
err
:=
typedData
.
HashStruct
(
"EIP712Domain"
,
typedData
.
Domain
.
Map
()
)
sighash
,
rawData
,
err
:=
apitypes
.
TypedDataAndHash
(
typedData
)
if
err
!=
nil
{
return
nil
,
nil
,
err
}
typedDataHash
,
err
:=
typedData
.
HashStruct
(
typedData
.
PrimaryType
,
typedData
.
Message
)
if
err
!=
nil
{
return
nil
,
nil
,
err
}
rawData
:=
[]
byte
(
fmt
.
Sprintf
(
"
\x19\x01
%s%s"
,
string
(
domainSeparator
),
string
(
typedDataHash
)))
sighash
:=
crypto
.
Keccak256
(
rawData
)
messages
,
err
:=
typedData
.
Format
()
if
err
!=
nil
{
return
nil
,
nil
,
err
}
req
:=
&
SignDataRequest
{
ContentType
:
apitypes
.
DataTyped
.
Mime
,
Rawdata
:
rawData
,
Rawdata
:
[]
byte
(
rawData
)
,
Messages
:
messages
,
Hash
:
sighash
,
Address
:
addr
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment