forked from zksync-sdk/zksync2-go
/
contract.go
247 lines (227 loc) · 9.44 KB
/
contract.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package utils
import (
"crypto/sha256"
"errors"
"fmt"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/zksync-sdk/zksync2-go/contracts/contractdeployer"
"github.com/zksync-sdk/zksync2-go/types"
"math/big"
"strings"
)
var (
EthAddress = common.HexToAddress("0x0000000000000000000000000000000000000000")
BootloaderFormalAddress = common.HexToAddress("0x0000000000000000000000000000000000008001")
ContractDeployerAddress = common.HexToAddress("0x0000000000000000000000000000000000008006")
L1MessengerAddress = common.HexToAddress("0x0000000000000000000000000000000000008008")
L2EthTokenAddress = common.HexToAddress("0x000000000000000000000000000000000000800a")
// L1ToL2AliasOffset Used for applying and undoing aliases on contract addresses during bridging from L1 to L2.
L1ToL2AliasOffset = common.HexToAddress("0x1111000000000000000000000000000000001111")
AddressModulo = new(big.Int).Exp(big.NewInt(2), big.NewInt(160), nil)
)
var contractDeployerABI *abi.ABI
// ApplyL1ToL2Alias converts the address of smart contract that submitted a transaction to the inbox on L1 to the
// `msg.sender` viewed on L2.
func ApplyL1ToL2Alias(address common.Address) common.Address {
result := new(big.Int).Add(new(big.Int).SetBytes(address.Bytes()), new(big.Int).SetBytes(L1ToL2AliasOffset.Bytes()))
result.Mod(result, AddressModulo)
return common.BytesToAddress(result.Bytes())
}
// UndoL1ToL2Alias converts the address of smart contract that submitted a transaction to the inbox on L2 to the
// `msg.sender` viewed on L1.
func UndoL1ToL2Alias(address common.Address) common.Address {
result := new(big.Int).Sub(new(big.Int).SetBytes(address.Bytes()), new(big.Int).SetBytes(L1ToL2AliasOffset.Bytes()))
if result.Sign() < 0 {
result = result.Add(result, AddressModulo)
}
return common.BytesToAddress(result.Bytes())
}
func getContractDeployerABI() (*abi.ABI, error) {
if contractDeployerABI == nil {
cda, err := abi.JSON(strings.NewReader(contractdeployer.ContractDeployerMetaData.ABI))
if err != nil {
return nil, fmt.Errorf("failed to load Deployer ABI: %w", err)
}
contractDeployerABI = &cda
}
return contractDeployerABI, nil
}
// Create2Address generates a future-proof contract address using salt plus bytecode which allows determination
// of an address before deployment.
func Create2Address(sender common.Address, bytecode, constructor, salt []byte) (common.Address, error) {
if len(salt) == 0 {
salt = make([]byte, 32)
} else if len(salt) != 32 {
return common.Address{}, errors.New("salt must be 32 bytes")
}
if constructor == nil {
constructor = []byte{}
}
bytecodeHash, err := HashBytecode(bytecode)
if err != nil {
return common.Address{}, fmt.Errorf("failed to get hash of bytecode: %w", err)
}
bytes := make([]byte, 5*32) // concatenate five 32-bytes slices
copy(bytes[0:32], crypto.Keccak256([]byte("zksyncCreate2"))) // 1 - prefix
copy(bytes[44:64], sender[:]) // 2 - sender (20 bytes right padded to 32)
copy(bytes[64:96], salt) // 3 - salt
copy(bytes[96:128], bytecodeHash) // 4 - bytecode hash
copy(bytes[128:160], crypto.Keccak256(constructor)) // 5 - constructor hash
result := crypto.Keccak256(bytes)
return common.BytesToAddress(result[12:]), nil
}
// CreateAddress generates a contract address from deployer's account and nonce.
func CreateAddress(sender common.Address, nonce *big.Int) (common.Address, error) {
nonceBytes := nonce.Bytes()
bytes := make([]byte, 3*32) // concatenate three 32-bytes slices
copy(bytes[0:32], crypto.Keccak256([]byte("zksyncCreate"))) // 1 - prefix
copy(bytes[44:64], sender[:]) // 2 - sender (20 bytes right padded to 32)
copy(bytes[64+(32-len(nonceBytes)):96], nonceBytes) // 3 - nonce (some bytes right padded to 32)
result := crypto.Keccak256(bytes)
return common.BytesToAddress(result[12:]), nil
}
// EncodeCreate2 returns the encoded constructor data for CREATE2 method used for smart contract deployment.
func EncodeCreate2(bytecode, calldata, salt []byte) ([]byte, error) {
cdABI, err := getContractDeployerABI()
if err != nil {
return nil, err
}
// prepare
if len(salt) == 0 {
salt = make([]byte, 32)
} else if len(salt) != 32 {
return nil, errors.New("salt must be 32 bytes")
}
hash, err := HashBytecode(bytecode)
if err != nil {
return nil, fmt.Errorf("failed to get hash of bytecode: %w", err)
}
salt32 := [32]byte{}
copy(salt32[:], salt)
hash32 := [32]byte{}
copy(hash32[:], hash)
res, err := cdABI.Pack("create2", salt32, hash32, calldata)
if err != nil {
return nil, fmt.Errorf("failed to pack create2 function: %w", err)
}
return res, nil
}
// EncodeCreate encodes the constructor data for CREATE method used for smart contract deployment.
func EncodeCreate(bytecode, calldata []byte) ([]byte, error) {
cdABI, err := getContractDeployerABI()
if err != nil {
return nil, err
}
hash, err := HashBytecode(bytecode)
if err != nil {
return nil, fmt.Errorf("failed to get hash of bytecode: %w", err)
}
salt32 := [32]byte{} // will be empty
hash32 := [32]byte{}
copy(hash32[:], hash)
res, err := cdABI.Pack("create", salt32, hash32, calldata)
if err != nil {
return nil, fmt.Errorf("failed to pack create function: %w", err)
}
return res, nil
}
// EncodeCreate2Account encodes the constructor data for CREATE2 method used for smart account deployment.
func EncodeCreate2Account(bytecode, calldata, salt []byte, version types.AccountAbstractionVersion) ([]byte, error) {
cdABI, err := getContractDeployerABI()
if err != nil {
return nil, err
}
// prepare
if len(salt) == 0 {
salt = make([]byte, 32)
} else if len(salt) != 32 {
return nil, errors.New("salt must be 32 bytes")
}
hash, err := HashBytecode(bytecode)
if err != nil {
return nil, fmt.Errorf("failed to get hash of bytecode: %w", err)
}
salt32 := [32]byte{}
copy(salt32[:], salt)
hash32 := [32]byte{}
copy(hash32[:], hash)
res, err := cdABI.Pack("create2Account", salt32, hash32, calldata, version)
if err != nil {
return nil, fmt.Errorf("failed to pack create2Account function: %w", err)
}
return res, nil
}
// EncodeCreateAccount encodes the constructor data for CREATE method used for smart account deployment.
func EncodeCreateAccount(bytecode, calldata []byte, version types.AccountAbstractionVersion) ([]byte, error) {
cdABI, err := getContractDeployerABI()
if err != nil {
return nil, err
}
hash, err := HashBytecode(bytecode)
if err != nil {
return nil, fmt.Errorf("failed to get hash of bytecode: %w", err)
}
salt32 := [32]byte{} // will be empty
hash32 := [32]byte{}
copy(hash32[:], hash)
res, err := cdABI.Pack("createAccount", salt32, hash32, calldata, version)
if err != nil {
return nil, fmt.Errorf("failed to pack createAccount function: %w", err)
}
return res, nil
}
// HashBytecode returns the hash of given bytecode.
func HashBytecode(bytecode []byte) ([]byte, error) {
if len(bytecode)%32 != 0 {
return nil, errors.New("bytecode length in bytes must be divisible by 32")
}
bytecodeHash := sha256.Sum256(bytecode)
// get real length of bytecode, which is presented as 32-byte words
length := big.NewInt(int64(len(bytecode) / 32))
if length.BitLen() > 16 {
return nil, errors.New("bytecode length must be less than 2^16 bytes")
}
// replace first 2 bytes of hash with version
version := []byte{1, 0}
copy(bytecodeHash[0:2], version)
// replace second 2 bytes of hash with bytecode length
length2b := make([]byte, 2)
length2b = length.FillBytes(length2b) // 0-padded in 2 bytes
copy(bytecodeHash[2:4], length2b)
return bytecodeHash[:], nil
}
// Deprecated: Will be removed in the future releases.
func ComputeL2Create2Address(sender common.Address, bytecode, constructor, salt []byte) (common.Address, error) {
if len(salt) == 0 {
salt = make([]byte, 32)
} else if len(salt) != 32 {
return common.Address{}, errors.New("salt must be 32 bytes")
}
if constructor == nil {
constructor = []byte{}
}
bytecodeHash, err := HashBytecode(bytecode)
if err != nil {
return common.Address{}, fmt.Errorf("failed to get hash of bytecode: %w", err)
}
bytes := make([]byte, 5*32) // concatenate five 32-bytes slices
copy(bytes[0:32], crypto.Keccak256([]byte("zksyncCreate2"))) // 1 - prefix
copy(bytes[44:64], sender[:]) // 2 - sender (20 bytes right padded to 32)
copy(bytes[64:96], salt) // 3 - salt
copy(bytes[96:128], bytecodeHash) // 4 - bytecode hash
copy(bytes[128:160], crypto.Keccak256(constructor)) // 5 - constructor hash
result := crypto.Keccak256(bytes)
return common.BytesToAddress(result[12:]), nil
}
// Deprecated: Will be removed in the future releases.
func ComputeL2CreateAddress(sender common.Address, nonce *big.Int) (common.Address, error) {
nonceBytes := nonce.Bytes()
bytes := make([]byte, 3*32) // concatenate three 32-bytes slices
copy(bytes[0:32], crypto.Keccak256([]byte("zksyncCreate"))) // 1 - prefix
copy(bytes[44:64], sender[:]) // 2 - sender (20 bytes right padded to 32)
copy(bytes[64+(32-len(nonceBytes)):96], nonceBytes) // 3 - nonce (some bytes right padded to 32)
result := crypto.Keccak256(bytes)
return common.BytesToAddress(result[12:]), nil
}