-
Notifications
You must be signed in to change notification settings - Fork 241
/
sign.go
79 lines (70 loc) · 2.15 KB
/
sign.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package typeddata
import (
"crypto/ecdsa"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
signercore "github.com/ethereum/go-ethereum/signer/core"
)
var (
// x19 to avoid collision with rlp encode. x01 version byte defined in EIP-191
messagePadding = []byte{0x19, 0x01}
)
func encodeData(typed TypedData) (rst common.Hash, err error) {
domainSeparator, err := hashStruct(eip712Domain, typed.Domain, typed.Types)
if err != nil {
return rst, err
}
primary, err := hashStruct(typed.PrimaryType, typed.Message, typed.Types)
if err != nil {
return rst, err
}
return crypto.Keccak256Hash(messagePadding, domainSeparator[:], primary[:]), nil
}
func encodeDataV4(typedData signercore.TypedData, chain *big.Int) ([]byte, 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 := []byte(fmt.Sprintf("\x19\x01%s%s", string(domainSeparator), string(typedDataHash)))
sighash := crypto.Keccak256(rawData)
return sighash, nil
}
func HashTypedDataV4(typedData signercore.TypedData, chain *big.Int) (common.Hash, error) {
hashBytes, err := encodeDataV4(typedData, chain)
if err != nil {
return common.Hash{}, err
}
return common.BytesToHash(hashBytes), nil
}
func SignTypedDataV4(typedData signercore.TypedData, prv *ecdsa.PrivateKey, chain *big.Int) (hexutil.Bytes, error) {
sighash, err := HashTypedDataV4(typedData, chain)
if err != nil {
return nil, err
}
sig, err := crypto.Sign(sighash[:], prv)
if err != nil {
return nil, err
}
sig[64] += 27
return sig, nil
}
// Sign TypedData with a given private key. Verify that chainId in the typed data matches currently selected chain.
func Sign(typed TypedData, prv *ecdsa.PrivateKey, chain *big.Int) ([]byte, error) {
hash, err := ValidateAndHash(typed, chain)
if err != nil {
return nil, err
}
sig, err := crypto.Sign(hash[:], prv)
if err != nil {
return nil, err
}
sig[64] += 27
return sig, nil
}