Skip to content

Commit 8f32c71

Browse files
committed
Remove code duplication and deprecate *SignBytes() methods
1 parent 81da602 commit 8f32c71

File tree

3 files changed

+79
-130
lines changed

3 files changed

+79
-130
lines changed

changelog/js-sdk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ All notable changes to `zksync.js` will be documented in this file.
1010

1111
### Deprecated
1212

13+
- `Signer.transferSignBytes` method
14+
- `Signer.withdrawSignBytes` method
15+
- `Signer.forcedExitSignBytes` method
16+
- `Signer.changePubKeySignBytes` method
17+
1318
### Fixed
1419

1520
## Version 0.9.0 (15.02.2021)

sdk/zksync.js/src/signer.ts

Lines changed: 70 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,6 @@
11
import { privateKeyFromSeed, signTransactionBytes, privateKeyToPubKeyHash } from './crypto';
22
import { BigNumber, BigNumberish, ethers } from 'ethers';
3-
import {
4-
getEthSignatureType,
5-
signMessagePersonalAPI,
6-
getSignedBytesFromMessage,
7-
serializeAccountId,
8-
serializeAddress,
9-
serializeTokenId,
10-
serializeAmountPacked,
11-
serializeFeePacked,
12-
serializeNonce,
13-
serializeAmountFull,
14-
getCREATE2AddressAndSalt,
15-
serializeTimestamp
16-
} from './utils';
3+
import * as utils from './utils';
174
import {
185
Address,
196
EthSignerType,
@@ -39,6 +26,9 @@ export class Signer {
3926
return await privateKeyToPubKeyHash(this.#privateKey);
4027
}
4128

29+
/**
30+
* @deprecated `Signer.*SignBytes` methods will be removed in future. Use `utils.serializeTx` instead.
31+
*/
4232
transferSignBytes(transfer: {
4333
accountId: number;
4434
from: Address;
@@ -50,17 +40,11 @@ export class Signer {
5040
validFrom: number;
5141
validUntil: number;
5242
}): Uint8Array {
53-
const type = new Uint8Array([5]); // tx type
54-
const accountId = serializeAccountId(transfer.accountId);
55-
const from = serializeAddress(transfer.from);
56-
const to = serializeAddress(transfer.to);
57-
const token = serializeTokenId(transfer.tokenId);
58-
const amount = serializeAmountPacked(transfer.amount);
59-
const fee = serializeFeePacked(transfer.fee);
60-
const nonce = serializeNonce(transfer.nonce);
61-
const validFrom = serializeTimestamp(transfer.validFrom);
62-
const validUntil = serializeTimestamp(transfer.validUntil);
63-
return ethers.utils.concat([type, accountId, from, to, token, amount, fee, nonce, validFrom, validUntil]);
43+
return utils.serializeTransfer({
44+
...transfer,
45+
type: 'Transfer',
46+
token: transfer.tokenId
47+
});
6448
}
6549

6650
async signSyncTransfer(transfer: {
@@ -74,24 +58,25 @@ export class Signer {
7458
validFrom: number;
7559
validUntil: number;
7660
}): Promise<Transfer> {
77-
const msgBytes = this.transferSignBytes(transfer);
61+
const tx: Transfer = {
62+
...transfer,
63+
type: 'Transfer',
64+
token: transfer.tokenId
65+
};
66+
const msgBytes = utils.serializeTransfer(tx);
7867
const signature = await signTransactionBytes(this.#privateKey, msgBytes);
7968

8069
return {
81-
type: 'Transfer',
82-
accountId: transfer.accountId,
83-
from: transfer.from,
84-
to: transfer.to,
85-
token: transfer.tokenId,
70+
...tx,
8671
amount: BigNumber.from(transfer.amount).toString(),
8772
fee: BigNumber.from(transfer.fee).toString(),
88-
nonce: transfer.nonce,
89-
validFrom: transfer.validFrom,
90-
validUntil: transfer.validUntil,
9173
signature
9274
};
9375
}
9476

77+
/**
78+
* @deprecated `Signer.*SignBytes` methods will be removed in future. Use `utils.serializeTx` instead.
79+
*/
9580
withdrawSignBytes(withdraw: {
9681
accountId: number;
9782
from: Address;
@@ -103,28 +88,12 @@ export class Signer {
10388
validFrom: number;
10489
validUntil: number;
10590
}): Uint8Array {
106-
const typeBytes = new Uint8Array([3]);
107-
const accountId = serializeAccountId(withdraw.accountId);
108-
const accountBytes = serializeAddress(withdraw.from);
109-
const ethAddressBytes = serializeAddress(withdraw.ethAddress);
110-
const tokenIdBytes = serializeTokenId(withdraw.tokenId);
111-
const amountBytes = serializeAmountFull(withdraw.amount);
112-
const feeBytes = serializeFeePacked(withdraw.fee);
113-
const nonceBytes = serializeNonce(withdraw.nonce);
114-
const validFrom = serializeTimestamp(withdraw.validFrom);
115-
const validUntil = serializeTimestamp(withdraw.validUntil);
116-
return ethers.utils.concat([
117-
typeBytes,
118-
accountId,
119-
accountBytes,
120-
ethAddressBytes,
121-
tokenIdBytes,
122-
amountBytes,
123-
feeBytes,
124-
nonceBytes,
125-
validFrom,
126-
validUntil
127-
]);
91+
return utils.serializeWithdraw({
92+
...withdraw,
93+
type: 'Withdraw',
94+
to: withdraw.ethAddress,
95+
token: withdraw.tokenId
96+
});
12897
}
12998

13099
async signSyncWithdraw(withdraw: {
@@ -138,24 +107,26 @@ export class Signer {
138107
validFrom: number;
139108
validUntil: number;
140109
}): Promise<Withdraw> {
141-
const msgBytes = this.withdrawSignBytes(withdraw);
110+
const tx: Withdraw = {
111+
...withdraw,
112+
type: 'Withdraw',
113+
to: withdraw.ethAddress,
114+
token: withdraw.tokenId
115+
};
116+
const msgBytes = utils.serializeWithdraw(tx);
142117
const signature = await signTransactionBytes(this.#privateKey, msgBytes);
143118

144119
return {
145-
type: 'Withdraw',
146-
accountId: withdraw.accountId,
147-
from: withdraw.from,
148-
to: withdraw.ethAddress,
149-
token: withdraw.tokenId,
120+
...tx,
150121
amount: BigNumber.from(withdraw.amount).toString(),
151122
fee: BigNumber.from(withdraw.fee).toString(),
152-
nonce: withdraw.nonce,
153-
validFrom: withdraw.validFrom,
154-
validUntil: withdraw.validUntil,
155123
signature
156124
};
157125
}
158126

127+
/**
128+
* @deprecated `Signer.*SignBytes` methods will be removed in future. Use `utils.serializeTx` instead.
129+
*/
159130
forcedExitSignBytes(forcedExit: {
160131
initiatorAccountId: number;
161132
target: Address;
@@ -165,24 +136,11 @@ export class Signer {
165136
validFrom: number;
166137
validUntil: number;
167138
}): Uint8Array {
168-
const typeBytes = new Uint8Array([8]);
169-
const initiatorAccountIdBytes = serializeAccountId(forcedExit.initiatorAccountId);
170-
const targetBytes = serializeAddress(forcedExit.target);
171-
const tokenIdBytes = serializeTokenId(forcedExit.tokenId);
172-
const feeBytes = serializeFeePacked(forcedExit.fee);
173-
const nonceBytes = serializeNonce(forcedExit.nonce);
174-
const validFrom = serializeTimestamp(forcedExit.validFrom);
175-
const validUntil = serializeTimestamp(forcedExit.validUntil);
176-
return ethers.utils.concat([
177-
typeBytes,
178-
initiatorAccountIdBytes,
179-
targetBytes,
180-
tokenIdBytes,
181-
feeBytes,
182-
nonceBytes,
183-
validFrom,
184-
validUntil
185-
]);
139+
return utils.serializeForcedExit({
140+
...forcedExit,
141+
type: 'ForcedExit',
142+
token: forcedExit.tokenId
143+
});
186144
}
187145

188146
async signSyncForcedExit(forcedExit: {
@@ -194,21 +152,23 @@ export class Signer {
194152
validFrom: number;
195153
validUntil: number;
196154
}): Promise<ForcedExit> {
197-
const msgBytes = this.forcedExitSignBytes(forcedExit);
155+
const tx: ForcedExit = {
156+
...forcedExit,
157+
type: 'ForcedExit',
158+
token: forcedExit.tokenId
159+
};
160+
const msgBytes = utils.serializeForcedExit(tx);
198161
const signature = await signTransactionBytes(this.#privateKey, msgBytes);
199162
return {
200-
type: 'ForcedExit',
201-
initiatorAccountId: forcedExit.initiatorAccountId,
202-
target: forcedExit.target,
203-
token: forcedExit.tokenId,
163+
...tx,
204164
fee: BigNumber.from(forcedExit.fee).toString(),
205-
nonce: forcedExit.nonce,
206-
validFrom: forcedExit.validFrom,
207-
validUntil: forcedExit.validUntil,
208165
signature
209166
};
210167
}
211168

169+
/**
170+
* @deprecated `Signer.*SignBytes` methods will be removed in future. Use `utils.serializeTx` instead.
171+
*/
212172
changePubKeySignBytes(changePubKey: {
213173
accountId: number;
214174
account: Address;
@@ -219,26 +179,13 @@ export class Signer {
219179
validFrom: number;
220180
validUntil: number;
221181
}): Uint8Array {
222-
const typeBytes = new Uint8Array([7]); // Tx type (1 byte)
223-
const accountIdBytes = serializeAccountId(changePubKey.accountId);
224-
const accountBytes = serializeAddress(changePubKey.account);
225-
const pubKeyHashBytes = serializeAddress(changePubKey.newPkHash);
226-
const tokenIdBytes = serializeTokenId(changePubKey.feeTokenId);
227-
const feeBytes = serializeFeePacked(changePubKey.fee);
228-
const nonceBytes = serializeNonce(changePubKey.nonce);
229-
const validFrom = serializeTimestamp(changePubKey.validFrom);
230-
const validUntil = serializeTimestamp(changePubKey.validUntil);
231-
return ethers.utils.concat([
232-
typeBytes,
233-
accountIdBytes,
234-
accountBytes,
235-
pubKeyHashBytes,
236-
tokenIdBytes,
237-
feeBytes,
238-
nonceBytes,
239-
validFrom,
240-
validUntil
241-
]);
182+
return utils.serializeChangePubKey({
183+
...changePubKey,
184+
type: 'ChangePubKey',
185+
feeToken: changePubKey.feeTokenId,
186+
// this is not important for serialization
187+
ethAuthData: { type: 'Onchain' }
188+
});
242189
}
243190

244191
async signSyncChangePubKey(changePubKey: {
@@ -252,20 +199,17 @@ export class Signer {
252199
validFrom: number;
253200
validUntil: number;
254201
}): Promise<ChangePubKey> {
255-
const msgBytes = this.changePubKeySignBytes(changePubKey);
202+
const tx: ChangePubKey = {
203+
...changePubKey,
204+
type: 'ChangePubKey',
205+
feeToken: changePubKey.feeTokenId
206+
};
207+
const msgBytes = utils.serializeChangePubKey(tx);
256208
const signature = await signTransactionBytes(this.#privateKey, msgBytes);
257209
return {
258-
type: 'ChangePubKey',
259-
accountId: changePubKey.accountId,
260-
account: changePubKey.account,
261-
newPkHash: changePubKey.newPkHash,
262-
feeToken: changePubKey.feeTokenId,
210+
...tx,
263211
fee: BigNumber.from(changePubKey.fee).toString(),
264-
nonce: changePubKey.nonce,
265-
signature,
266-
ethAuthData: changePubKey.ethAuthData,
267-
validFrom: changePubKey.validFrom,
268-
validUntil: changePubKey.validUntil
212+
signature
269213
};
270214
}
271215

@@ -292,10 +236,10 @@ export class Signer {
292236
if (chainID !== 1) {
293237
message += `\nChain ID: ${chainID}.`;
294238
}
295-
const signedBytes = getSignedBytesFromMessage(message, false);
296-
const signature = await signMessagePersonalAPI(ethSigner, signedBytes);
239+
const signedBytes = utils.getSignedBytesFromMessage(message, false);
240+
const signature = await utils.signMessagePersonalAPI(ethSigner, signedBytes);
297241
const address = await ethSigner.getAddress();
298-
const ethSignatureType = await getEthSignatureType(ethSigner.provider, message, signature, address);
242+
const ethSignatureType = await utils.getEthSignatureType(ethSigner.provider, message, signature, address);
299243
const seed = ethers.utils.arrayify(signature);
300244
const signer = await Signer.fromSeed(seed);
301245
return { signer, ethSignatureType };
@@ -317,7 +261,7 @@ export class Create2WalletSigner extends ethers.Signer {
317261
value: provider,
318262
writable: false
319263
});
320-
const create2Info = getCREATE2AddressAndSalt(zkSyncPubkeyHash, create2WalletData);
264+
const create2Info = utils.getCREATE2AddressAndSalt(zkSyncPubkeyHash, create2WalletData);
321265
this.address = create2Info.address;
322266
this.salt = create2Info.salt;
323267
}

sdk/zksync.js/src/types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export interface Transfer {
8181
amount: BigNumberish;
8282
fee: BigNumberish;
8383
nonce: number;
84-
signature: Signature;
84+
signature?: Signature;
8585
validFrom: number;
8686
validUntil: number;
8787
}
@@ -95,7 +95,7 @@ export interface Withdraw {
9595
amount: BigNumberish;
9696
fee: BigNumberish;
9797
nonce: number;
98-
signature: Signature;
98+
signature?: Signature;
9999
validFrom: number;
100100
validUntil: number;
101101
}
@@ -107,7 +107,7 @@ export interface ForcedExit {
107107
token: number;
108108
fee: BigNumberish;
109109
nonce: number;
110-
signature: Signature;
110+
signature?: Signature;
111111
validFrom: number;
112112
validUntil: number;
113113
}
@@ -139,7 +139,7 @@ export interface ChangePubKey {
139139
feeToken: number;
140140
fee: BigNumberish;
141141
nonce: number;
142-
signature: Signature;
142+
signature?: Signature;
143143
ethAuthData: ChangePubKeyOnchain | ChangePubKeyECDSA | ChangePubKeyCREATE2;
144144
validFrom: number;
145145
validUntil: number;

0 commit comments

Comments
 (0)