forked from zksync-sdk/zksync2-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEIP712.go
76 lines (66 loc) · 2.03 KB
/
EIP712.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
package zksync2
import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/signer/core/apitypes"
"math/big"
)
type EIP712TypedData interface {
GetEIP712Type() string
GetEIP712Types() []apitypes.Type
GetEIP712Message() apitypes.TypedDataMessage
}
type Eip712Meta struct {
ErgsPerPubdata *hexutil.Big `json:"ergsPerPubdata,omitempty"`
CustomSignature hexutil.Bytes `json:"customSignature"`
FactoryDeps []hexutil.Bytes `json:"factoryDeps"`
PaymasterParams *PaymasterParams `json:"paymasterParams"`
}
type PaymasterParams struct {
Paymaster common.Address `json:"paymaster"`
PaymasterInput hexutil.Bytes `json:"paymasterInput"`
}
type Eip712Domain struct {
Name string `json:"name"`
Version string `json:"version"`
ChainId *big.Int `json:"chainId"`
VerifyingContract *common.Address `json:"verifyingContract"`
}
func (d *Eip712Domain) GetEIP712Type() string {
return "EIP712Domain"
}
func (d *Eip712Domain) GetEIP712Types() []apitypes.Type {
types := []apitypes.Type{
{Name: "name", Type: "string"},
{Name: "version", Type: "string"},
{Name: "chainId", Type: "uint256"},
}
if d.VerifyingContract != nil {
types = append(types, apitypes.Type{Name: "verifyingContract", Type: "address"})
}
return types
}
func (d *Eip712Domain) GetEIP712Domain() apitypes.TypedDataDomain {
domain := apitypes.TypedDataDomain{
Name: d.Name,
Version: d.Version,
ChainId: math.NewHexOrDecimal256(d.ChainId.Int64()),
}
if d.VerifyingContract != nil {
domain.VerifyingContract = d.VerifyingContract.String()
}
return domain
}
const (
Eip712DomainDefaultName = `zkSync`
Eip712DomainDefaultVersion = `2`
)
func DefaultEip712Domain(chainId int64) *Eip712Domain {
return &Eip712Domain{
Name: Eip712DomainDefaultName,
Version: Eip712DomainDefaultVersion,
ChainId: big.NewInt(chainId),
VerifyingContract: nil,
}
}