-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathZkSigner.go
370 lines (351 loc) · 10.7 KB
/
ZkSigner.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package zksync
import (
"bytes"
"fmt"
"github.com/pkg/errors"
"github.com/zksync-sdk/zksync-sdk-go"
"math/big"
)
const (
Message = "Access zkSync account.\n\nOnly sign this message for a trusted client!"
TransactionVersion byte = 0x01
)
func NewZkSignerFromSeed(seed []byte) (*ZkSigner, error) {
privateKey, err := zkscrypto.NewPrivateKey(seed)
if err != nil {
return nil, errors.Wrap(err, "failed to create private key")
}
return newZkSignerFromPrivateKey(privateKey)
}
func NewZkSignerFromRawPrivateKey(rawPk []byte) (*ZkSigner, error) {
privateKey, err := zkscrypto.NewPrivateKeyRaw(rawPk)
if err != nil {
return nil, errors.Wrap(err, "failed to create private key from raw bytes")
}
return newZkSignerFromPrivateKey(privateKey)
}
func NewZkSignerFromEthSigner(es EthSigner, cid ChainId) (*ZkSigner, error) {
signMsg := Message
if cid != ChainIdMainnet {
signMsg = fmt.Sprintf("%s\nChain ID: %d.", Message, cid)
}
sig, err := es.SignMessage([]byte(signMsg))
if err != nil {
return nil, errors.Wrap(err, "failed to sign special message")
}
return NewZkSignerFromSeed(sig)
}
func newZkSignerFromPrivateKey(privateKey *zkscrypto.PrivateKey) (*ZkSigner, error) {
publicKey, err := privateKey.PublicKey()
if err != nil {
return nil, errors.Wrap(err, "failed to create public key")
}
publicKeyHash, err := publicKey.Hash()
if err != nil {
return nil, errors.Wrap(err, "failed to get public key hash")
}
return &ZkSigner{
privateKey: privateKey,
publicKey: publicKey,
publicKeyHash: publicKeyHash,
}, nil
}
type ZkSigner struct {
privateKey *zkscrypto.PrivateKey
publicKey *zkscrypto.PublicKey
publicKeyHash *zkscrypto.PublicKeyHash
}
func (s *ZkSigner) Sign(message []byte) (*zkscrypto.Signature, error) {
signature, err := s.privateKey.Sign(message)
if err != nil {
return nil, errors.Wrap(err, "failed to sign message")
}
return signature, nil
}
func (s *ZkSigner) SignChangePubKey(txData *ChangePubKey) (*Signature, error) {
buf := bytes.Buffer{}
buf.WriteByte(0xff - 0x07)
buf.WriteByte(TransactionVersion)
buf.Write(Uint32ToBytes(txData.AccountId))
buf.Write(txData.Account[:])
pkhBytes, err := pkhToBytes(txData.NewPkHash)
if err != nil {
return nil, errors.Wrap(err, "failed to get pkh bytes")
}
buf.Write(pkhBytes)
buf.Write(Uint32ToBytes(txData.FeeToken))
fee, ok := big.NewInt(0).SetString(txData.Fee, 10)
if !ok {
return nil, errors.New("failed to convert string fee to big.Int")
}
packedFee, err := packFee(fee)
if err != nil {
return nil, errors.Wrap(err, "failed to pack fee")
}
buf.Write(packedFee)
buf.Write(Uint32ToBytes(txData.Nonce))
buf.Write(Uint64ToBytes(txData.TimeRange.ValidFrom))
buf.Write(Uint64ToBytes(txData.TimeRange.ValidUntil))
sig, err := s.Sign(buf.Bytes())
if err != nil {
return nil, errors.Wrap(err, "failed to sign ChangePubKey tx data")
}
res := &Signature{
PubKey: s.GetPublicKey(),
Signature: sig.HexString(),
}
return res, nil
}
func (s *ZkSigner) SignTransfer(txData *Transfer) (*Signature, error) {
buf := bytes.Buffer{}
buf.WriteByte(0xff - 0x05)
buf.WriteByte(TransactionVersion)
buf.Write(Uint32ToBytes(txData.AccountId))
buf.Write(txData.From[:])
buf.Write(txData.To[:])
buf.Write(Uint32ToBytes(txData.Token.Id))
packedAmount, err := packAmount(txData.Amount)
if err != nil {
return nil, errors.Wrap(err, "failed to pack amount")
}
buf.Write(packedAmount)
fee, ok := big.NewInt(0).SetString(txData.Fee, 10)
if !ok {
return nil, errors.New("failed to convert string fee to big.Int")
}
packedFee, err := packFee(fee)
if err != nil {
return nil, errors.Wrap(err, "failed to pack fee")
}
buf.Write(packedFee)
buf.Write(Uint32ToBytes(txData.Nonce))
buf.Write(Uint64ToBytes(txData.TimeRange.ValidFrom))
buf.Write(Uint64ToBytes(txData.TimeRange.ValidUntil))
sig, err := s.Sign(buf.Bytes())
if err != nil {
return nil, errors.Wrap(err, "failed to sign Transfer tx data")
}
res := &Signature{
PubKey: s.GetPublicKey(),
Signature: sig.HexString(),
}
return res, nil
}
func (s *ZkSigner) SignWithdraw(txData *Withdraw) (*Signature, error) {
buf := bytes.Buffer{}
buf.WriteByte(0xff - 0x03)
buf.WriteByte(TransactionVersion)
buf.Write(Uint32ToBytes(txData.AccountId))
buf.Write(txData.From[:])
buf.Write(txData.To[:])
buf.Write(Uint32ToBytes(txData.TokenId))
amountBytes := txData.Amount.Bytes()
buf.Write(make([]byte, 16-len(amountBytes))) // total amount slot is 16 bytes BE
buf.Write(amountBytes)
fee, ok := big.NewInt(0).SetString(txData.Fee, 10)
if !ok {
return nil, errors.New("failed to convert string fee to big.Int")
}
packedFee, err := packFee(fee)
if err != nil {
return nil, errors.Wrap(err, "failed to pack fee")
}
buf.Write(packedFee)
buf.Write(Uint32ToBytes(txData.Nonce))
buf.Write(Uint64ToBytes(txData.TimeRange.ValidFrom))
buf.Write(Uint64ToBytes(txData.TimeRange.ValidUntil))
sig, err := s.Sign(buf.Bytes())
if err != nil {
return nil, errors.Wrap(err, "failed to sign Withdraw tx data")
}
res := &Signature{
PubKey: s.GetPublicKey(),
Signature: sig.HexString(),
}
return res, nil
}
func (s *ZkSigner) SignForcedExit(txData *ForcedExit) (*Signature, error) {
buf := bytes.Buffer{}
buf.WriteByte(0xff - 0x08)
buf.WriteByte(TransactionVersion)
buf.Write(Uint32ToBytes(txData.AccountId))
buf.Write(txData.Target[:])
buf.Write(Uint32ToBytes(txData.TokenId))
fee, ok := big.NewInt(0).SetString(txData.Fee, 10)
if !ok {
return nil, errors.New("failed to convert string fee to big.Int")
}
packedFee, err := packFee(fee)
if err != nil {
return nil, errors.Wrap(err, "failed to pack fee")
}
buf.Write(packedFee)
buf.Write(Uint32ToBytes(txData.Nonce))
buf.Write(Uint64ToBytes(txData.TimeRange.ValidFrom))
buf.Write(Uint64ToBytes(txData.TimeRange.ValidUntil))
sig, err := s.Sign(buf.Bytes())
if err != nil {
return nil, errors.Wrap(err, "failed to sign ForcedExit tx data")
}
res := &Signature{
PubKey: s.GetPublicKey(),
Signature: sig.HexString(),
}
return res, nil
}
func (s *ZkSigner) SignMintNFT(txData *MintNFT) (*Signature, error) {
buf := bytes.Buffer{}
buf.WriteByte(0xff - 0x09)
buf.WriteByte(TransactionVersion)
buf.Write(Uint32ToBytes(txData.CreatorId))
buf.Write(txData.CreatorAddress[:])
buf.Write(txData.ContentHash.Bytes())
buf.Write(txData.Recipient[:])
buf.Write(Uint32ToBytes(txData.FeeToken))
fee, ok := big.NewInt(0).SetString(txData.Fee, 10)
if !ok {
return nil, errors.New("failed to convert string fee to big.Int")
}
packedFee, err := packFee(fee)
if err != nil {
return nil, errors.Wrap(err, "failed to pack fee")
}
buf.Write(packedFee)
buf.Write(Uint32ToBytes(txData.Nonce))
sig, err := s.Sign(buf.Bytes())
if err != nil {
return nil, errors.Wrap(err, "failed to sign MintNFT tx data")
}
res := &Signature{
PubKey: s.GetPublicKey(),
Signature: sig.HexString(),
}
return res, nil
}
func (s *ZkSigner) SignWithdrawNFT(txData *WithdrawNFT) (*Signature, error) {
buf := bytes.Buffer{}
buf.WriteByte(0xff - 0x0a)
buf.WriteByte(TransactionVersion)
buf.Write(Uint32ToBytes(txData.AccountId))
buf.Write(txData.From[:])
buf.Write(txData.To[:])
buf.Write(Uint32ToBytes(txData.Token))
buf.Write(Uint32ToBytes(txData.FeeToken))
fee, ok := big.NewInt(0).SetString(txData.Fee, 10)
if !ok {
return nil, errors.New("failed to convert string fee to big.Int")
}
packedFee, err := packFee(fee)
if err != nil {
return nil, errors.Wrap(err, "failed to pack fee")
}
buf.Write(packedFee)
buf.Write(Uint32ToBytes(txData.Nonce))
buf.Write(Uint64ToBytes(txData.TimeRange.ValidFrom))
buf.Write(Uint64ToBytes(txData.TimeRange.ValidUntil))
sig, err := s.Sign(buf.Bytes())
if err != nil {
return nil, errors.Wrap(err, "failed to sign WithdrawNFT tx data")
}
res := &Signature{
PubKey: s.GetPublicKey(),
Signature: sig.HexString(),
}
return res, nil
}
func (s *ZkSigner) SignOrder(order *Order) (*Signature, error) {
message, err := s.getOrderBytes(order)
if err != nil {
return nil, errors.Wrap(err, "failed to get order bytes")
}
sig, err := s.Sign(message)
if err != nil {
return nil, errors.Wrap(err, "failed to sign Order data")
}
res := &Signature{
PubKey: s.GetPublicKey(),
Signature: sig.HexString(),
}
return res, nil
}
func (s *ZkSigner) getOrderBytes(order *Order) ([]byte, error) {
buf := bytes.Buffer{}
buf.WriteByte(0x6f) // ASCII 'o' in hex for (o)rder
buf.WriteByte(TransactionVersion)
buf.Write(Uint32ToBytes(order.AccountId))
buf.Write(order.RecipientAddress[:])
buf.Write(Uint32ToBytes(order.Nonce))
buf.Write(Uint32ToBytes(order.TokenSell))
buf.Write(Uint32ToBytes(order.TokenBuy))
if len(order.Ratio) != 2 {
return nil, errors.New("invalid ratio")
}
buf.Write(BigIntToBytesBE(order.Ratio[0], 15))
buf.Write(BigIntToBytesBE(order.Ratio[1], 15))
packedAmount, err := packAmount(order.Amount)
if err != nil {
return nil, errors.Wrap(err, "failed to pack amount")
}
buf.Write(packedAmount)
buf.Write(Uint64ToBytes(order.TimeRange.ValidFrom))
buf.Write(Uint64ToBytes(order.TimeRange.ValidUntil))
return buf.Bytes(), nil
}
func (s *ZkSigner) SignSwap(txData *Swap) (*Signature, error) {
buf := bytes.Buffer{}
buf.WriteByte(0xff - 0x0b)
buf.WriteByte(TransactionVersion)
buf.Write(Uint32ToBytes(txData.SubmitterId))
buf.Write(txData.SubmitterAddress[:])
buf.Write(Uint32ToBytes(txData.Nonce))
if len(txData.Orders) != 2 {
return nil, errors.New("invalid orders in Swap tx")
}
order1, err := s.getOrderBytes(txData.Orders[0])
if err != nil {
return nil, errors.Wrap(err, "failed to get order1 bytes")
}
order2, err := s.getOrderBytes(txData.Orders[1])
if err != nil {
return nil, errors.Wrap(err, "failed to get order2 bytes")
}
buf.Write(zkscrypto.ResqueHashOrders(append(order1, order2...)).GetBytes())
buf.Write(Uint32ToBytes(txData.FeeToken))
fee, ok := big.NewInt(0).SetString(txData.Fee, 10)
if !ok {
return nil, errors.New("failed to convert string fee to big.Int")
}
packedFee, err := packFee(fee)
if err != nil {
return nil, errors.Wrap(err, "failed to pack fee")
}
buf.Write(packedFee)
if len(txData.Amounts) != 2 {
return nil, errors.New("invalid amounts in Swap tx")
}
packedAmount, err := packAmount(txData.Amounts[0])
if err != nil {
return nil, errors.Wrap(err, "failed to pack amount1")
}
buf.Write(packedAmount)
packedAmount, err = packAmount(txData.Amounts[1])
if err != nil {
return nil, errors.Wrap(err, "failed to pack amount2")
}
buf.Write(packedAmount)
sig, err := s.Sign(buf.Bytes())
if err != nil {
return nil, errors.Wrap(err, "failed to sign Swap tx data")
}
res := &Signature{
PubKey: s.GetPublicKey(),
Signature: sig.HexString(),
}
return res, nil
}
func (s *ZkSigner) GetPublicKeyHash() string {
return "sync:" + s.publicKeyHash.HexString()
}
func (s *ZkSigner) GetPublicKey() string {
return s.publicKey.HexString()
}