forked from matter-labs/zksync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signer.rs
398 lines (349 loc) · 11.9 KB
/
signer.rs
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// Built-in imports
use std::fmt;
// External uses
use num::BigUint;
// Workspace uses
use zksync_crypto::PrivateKey;
use zksync_eth_signer::{error::SignerError, EthereumSigner};
use zksync_types::{
tx::{
eip712_signature::Eip712Domain, ChangePubKey, ChangePubKeyECDSAData,
ChangePubKeyEIP712Data, ChangePubKeyEthAuthData, PackedEthSignature, TimeRange,
TxEthSignature,
},
AccountId, Address, ChainId, ForcedExit, MintNFT, Nonce, PubKeyHash, Token, TokenId, Transfer,
Withdraw, WithdrawNFT, H256,
};
// Local imports
use crate::WalletCredentials;
fn signing_failed_error(err: impl ToString) -> SignerError {
SignerError::SigningFailed(err.to_string())
}
pub struct Signer<S: EthereumSigner> {
pub pubkey_hash: PubKeyHash,
pub address: Address,
pub(crate) private_key: PrivateKey,
pub(crate) eth_signer: Option<S>,
pub(crate) account_id: Option<AccountId>,
}
impl<S: EthereumSigner> fmt::Debug for Signer<S> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut pk_contents = Vec::new();
self.private_key
.write(&mut pk_contents)
.expect("Failed writing the private key contents");
f.debug_struct("Signer")
.field("pubkey_hash", &self.pubkey_hash)
.field("address", &self.address)
.finish()
}
}
impl<S: EthereumSigner> Signer<S> {
pub fn new(private_key: PrivateKey, address: Address, eth_signer: Option<S>) -> Self {
let pubkey_hash = PubKeyHash::from_privkey(&private_key);
Self {
private_key,
pubkey_hash,
address,
eth_signer,
account_id: None,
}
}
/// Construct a `Signer` with the given credentials
pub fn with_credentials(credentials: WalletCredentials<S>) -> Self {
Self::new(
credentials.zksync_private_key,
credentials.eth_address,
credentials.eth_signer,
)
}
pub fn pubkey_hash(&self) -> &PubKeyHash {
&self.pubkey_hash
}
pub fn set_account_id(&mut self, account_id: Option<AccountId>) {
self.account_id = account_id;
}
pub fn get_account_id(&self) -> Option<AccountId> {
self.account_id
}
#[deprecated]
#[doc(hidden)]
/// This method required only for backward compatibility with tests.
/// You should use `sign_change_pubkey_tx` method with EIP712 ChangePubKey for offchain signature
pub async fn __old_sign_change_pubkey_tx_ecdsa(
&self,
nonce: Nonce,
fee_token: Token,
fee: BigUint,
time_range: TimeRange,
) -> Result<ChangePubKey, SignerError> {
let account_id = self.account_id.ok_or(SignerError::NoSigningKey)?;
let mut change_pubkey = ChangePubKey::new_signed(
account_id,
self.address,
self.pubkey_hash,
fee_token.id,
fee,
nonce,
time_range,
None,
&self.private_key,
None,
)
.map_err(signing_failed_error)?;
let eth_signer = self
.eth_signer
.as_ref()
.ok_or(SignerError::MissingEthSigner)?;
let sign_bytes = change_pubkey
.get_eth_signed_data()
.map_err(signing_failed_error)?;
let eth_signature = eth_signer
.sign_message(&sign_bytes)
.await
.map_err(signing_failed_error)?;
let eth_signature = match eth_signature {
TxEthSignature::EthereumSignature(packed_signature) => Ok(packed_signature),
TxEthSignature::EIP1271Signature(..) => Err(SignerError::CustomError(
"Can't sign ChangePubKey message with EIP1271 signer".to_string(),
)),
}?;
change_pubkey.eth_auth_data = Some(ChangePubKeyEthAuthData::ECDSA(ChangePubKeyECDSAData {
eth_signature,
batch_hash: H256::zero(),
}));
assert!(
change_pubkey.is_eth_auth_data_valid(),
"eth auth data is incorrect"
);
Ok(change_pubkey)
}
pub async fn sign_change_pubkey_tx(
&self,
nonce: Nonce,
auth_onchain: bool,
fee_token: Token,
fee: BigUint,
time_range: TimeRange,
chain_id: Option<ChainId>,
) -> Result<ChangePubKey, SignerError> {
let account_id = self.account_id.ok_or(SignerError::NoSigningKey)?;
let mut change_pubkey = ChangePubKey::new_signed(
account_id,
self.address,
self.pubkey_hash,
fee_token.id,
fee,
nonce,
time_range,
None,
&self.private_key,
chain_id,
)
.map_err(signing_failed_error)?;
let eth_auth_data = if auth_onchain {
ChangePubKeyEthAuthData::Onchain
} else {
let eth_signer = self
.eth_signer
.as_ref()
.ok_or(SignerError::MissingEthSigner)?;
let chain_id = chain_id.ok_or_else(|| {
SignerError::CustomError("Can't sign eip712 without chain id".to_string())
})?;
let domain = Eip712Domain::new(chain_id);
let eth_signature = eth_signer
.sign_typed_data(&domain, &change_pubkey)
.await
.map_err(|err| SignerError::SigningFailed(err.to_string()))?;
ChangePubKeyEthAuthData::EIP712(ChangePubKeyEIP712Data {
eth_signature,
batch_hash: Default::default(),
})
};
change_pubkey.eth_auth_data = Some(eth_auth_data);
assert!(
change_pubkey.is_eth_auth_data_valid(),
"eth auth data is incorrect"
);
Ok(change_pubkey)
}
#[allow(clippy::too_many_arguments)]
pub async fn sign_transfer(
&self,
token: Token,
amount: BigUint,
fee: BigUint,
to: Address,
nonce: Nonce,
time_range: TimeRange,
) -> Result<(Transfer, Option<PackedEthSignature>), SignerError> {
let account_id = self.account_id.ok_or(SignerError::NoSigningKey)?;
let transfer = Transfer::new_signed(
account_id,
self.address,
to,
token.id,
amount,
fee,
nonce,
time_range,
&self.private_key,
)
.map_err(signing_failed_error)?;
let eth_signature = match &self.eth_signer {
Some(signer) => {
let message = transfer.get_ethereum_sign_message(&token.symbol, token.decimals);
let signature = signer.sign_message(message.as_bytes()).await?;
if let TxEthSignature::EthereumSignature(packed_signature) = signature {
Some(packed_signature)
} else {
return Err(SignerError::MissingEthSigner);
}
}
_ => None,
};
Ok((transfer, eth_signature))
}
pub async fn sign_withdraw(
&self,
token: Token,
amount: BigUint,
fee: BigUint,
eth_address: Address,
nonce: Nonce,
time_range: TimeRange,
) -> Result<(Withdraw, Option<PackedEthSignature>), SignerError> {
let account_id = self.account_id.ok_or(SignerError::NoSigningKey)?;
let withdraw = Withdraw::new_signed(
account_id,
self.address,
eth_address,
token.id,
amount,
fee,
nonce,
time_range,
&self.private_key,
)
.map_err(signing_failed_error)?;
let eth_signature = match &self.eth_signer {
Some(signer) => {
let message = withdraw.get_ethereum_sign_message(&token.symbol, token.decimals);
let signature = signer.sign_message(message.as_bytes()).await?;
if let TxEthSignature::EthereumSignature(packed_signature) = signature {
Some(packed_signature)
} else {
return Err(SignerError::MissingEthSigner);
}
}
_ => None,
};
Ok((withdraw, eth_signature))
}
pub async fn sign_forced_exit(
&self,
target: Address,
token: Token,
fee: BigUint,
nonce: Nonce,
time_range: TimeRange,
) -> Result<(ForcedExit, Option<PackedEthSignature>), SignerError> {
let account_id = self.account_id.ok_or(SignerError::NoSigningKey)?;
let forced_exit = ForcedExit::new_signed(
account_id,
target,
token.id,
fee,
nonce,
time_range,
&self.private_key,
)
.map_err(signing_failed_error)?;
let eth_signature = match &self.eth_signer {
Some(signer) => {
let message = forced_exit.get_ethereum_sign_message(&token.symbol, token.decimals);
let signature = signer.sign_message(message.as_bytes()).await?;
if let TxEthSignature::EthereumSignature(packed_signature) = signature {
Some(packed_signature)
} else {
return Err(SignerError::MissingEthSigner);
}
}
_ => None,
};
Ok((forced_exit, eth_signature))
}
pub async fn sign_mint_nft(
&self,
recipient: Address,
content_hash: H256,
fee_token: Token,
fee: BigUint,
nonce: Nonce,
) -> Result<(MintNFT, Option<PackedEthSignature>), SignerError> {
let account_id = self.account_id.ok_or(SignerError::NoSigningKey)?;
let mint_nft = MintNFT::new_signed(
account_id,
self.address,
content_hash,
recipient,
fee,
fee_token.id,
nonce,
&self.private_key,
)
.map_err(signing_failed_error)?;
let eth_signature = match &self.eth_signer {
Some(signer) => {
let message =
mint_nft.get_ethereum_sign_message(&fee_token.symbol, fee_token.decimals);
let signature = signer.sign_message(message.as_bytes()).await?;
if let TxEthSignature::EthereumSignature(packed_signature) = signature {
Some(packed_signature)
} else {
return Err(SignerError::MissingEthSigner);
}
}
_ => None,
};
Ok((mint_nft, eth_signature))
}
pub async fn sign_withdraw_nft(
&self,
to: Address,
token: TokenId,
fee_token: Token,
fee: BigUint,
nonce: Nonce,
time_range: TimeRange,
) -> Result<(WithdrawNFT, Option<PackedEthSignature>), SignerError> {
let account_id = self.account_id.ok_or(SignerError::NoSigningKey)?;
let withdraw_nft = WithdrawNFT::new_signed(
account_id,
self.address,
to,
token,
fee_token.id,
fee,
nonce,
time_range,
&self.private_key,
)
.map_err(signing_failed_error)?;
let eth_signature = match &self.eth_signer {
Some(signer) => {
let message =
withdraw_nft.get_ethereum_sign_message(&fee_token.symbol, fee_token.decimals);
let signature = signer.sign_message(message.as_bytes()).await?;
if let TxEthSignature::EthereumSignature(packed_signature) = signature {
Some(packed_signature)
} else {
return Err(SignerError::MissingEthSigner);
}
}
_ => None,
};
Ok((withdraw_nft, eth_signature))
}
}