Skip to content

Commit 4dc88f0

Browse files
Merge pull request #14 from zksync-sdk/develop
Add toggle 2factor authentication request
2 parents 037abea + dc9031a commit 4dc88f0

17 files changed

Lines changed: 236 additions & 0 deletions
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.zksync.domain.auth;
2+
3+
import io.zksync.signer.EthSignature;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Data;
6+
7+
@Data
8+
@AllArgsConstructor
9+
public class Toggle2FA {
10+
11+
private boolean enable;
12+
13+
private Integer accountId;
14+
15+
private Long timestamp;
16+
17+
private EthSignature signature;
18+
}

src/main/java/io/zksync/provider/AsyncProvider.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.apache.commons.lang3.tuple.Pair;
99

1010
import io.zksync.domain.ChainId;
11+
import io.zksync.domain.auth.Toggle2FA;
1112
import io.zksync.domain.contract.ContractAddress;
1213
import io.zksync.domain.fee.TransactionFeeBatchRequest;
1314
import io.zksync.domain.fee.TransactionFeeDetails;
@@ -144,6 +145,14 @@ public interface AsyncProvider {
144145
*/
145146
CompletableFuture<String> getEthTransactionForWithdrawal(String zkSyncWithdrawalHash);
146147

148+
/**
149+
* Send signed toggle 2FA request to ZkSync network.
150+
*
151+
* @param toggle2FA - Request object
152+
* @return - true if successful, false otherwise
153+
*/
154+
CompletableFuture<Boolean> toggle2FA(Toggle2FA toggle2FA);
155+
147156
/**
148157
* Fetch and update local cache of token list
149158
*

src/main/java/io/zksync/provider/DefaultAsyncProvider.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.apache.commons.lang3.tuple.Pair;
44

5+
import io.zksync.domain.auth.Toggle2FA;
56
import io.zksync.domain.contract.ContractAddress;
67
import io.zksync.domain.fee.TransactionFeeBatchRequest;
78
import io.zksync.domain.fee.TransactionFeeDetails;
@@ -13,13 +14,15 @@
1314
import io.zksync.domain.transaction.TransactionDetails;
1415
import io.zksync.domain.transaction.ZkSyncTransaction;
1516
import io.zksync.signer.EthSignature;
17+
import io.zksync.transport.ZkSyncSuccess;
1618
import io.zksync.transport.ZkSyncTransport;
1719
import io.zksync.transport.response.ZksAccountState;
1820
import io.zksync.transport.response.ZksContractAddress;
1921
import io.zksync.transport.response.ZksEthOpInfo;
2022
import io.zksync.transport.response.ZksGetConfirmationsForEthOpAmount;
2123
import io.zksync.transport.response.ZksSentTransaction;
2224
import io.zksync.transport.response.ZksSentTransactionBatch;
25+
import io.zksync.transport.response.ZksToggle2FA;
2326
import io.zksync.transport.response.ZksTokenPrice;
2427
import io.zksync.transport.response.ZksTokens;
2528
import io.zksync.transport.response.ZksTransactionDetails;
@@ -163,6 +166,13 @@ public CompletableFuture<String> getEthTransactionForWithdrawal(String zkSyncWit
163166
return response;
164167
}
165168

169+
@Override
170+
public CompletableFuture<Boolean> toggle2FA(Toggle2FA toggle2fa) {
171+
final CompletableFuture<ZkSyncSuccess> result = transport.sendAsync("toggle_2fa", Collections.singletonList(toggle2fa), ZksToggle2FA.class);
172+
173+
return result.thenApply(ZkSyncSuccess::getSuccess);
174+
}
175+
166176
@Override
167177
public CompletableFuture<Tokens> updateTokenSet() {
168178
final CompletableFuture<Tokens> response = transport.sendAsync("tokens", Collections.emptyList(), ZksTokens.class);

src/main/java/io/zksync/provider/DefaultProvider.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.apache.commons.lang3.tuple.Pair;
44

5+
import io.zksync.domain.auth.Toggle2FA;
56
import io.zksync.domain.contract.ContractAddress;
67
import io.zksync.domain.fee.TransactionFeeBatchRequest;
78
import io.zksync.domain.fee.TransactionFeeDetails;
@@ -13,13 +14,15 @@
1314
import io.zksync.domain.transaction.TransactionDetails;
1415
import io.zksync.domain.transaction.ZkSyncTransaction;
1516
import io.zksync.signer.EthSignature;
17+
import io.zksync.transport.ZkSyncSuccess;
1618
import io.zksync.transport.ZkSyncTransport;
1719
import io.zksync.transport.response.ZksAccountState;
1820
import io.zksync.transport.response.ZksContractAddress;
1921
import io.zksync.transport.response.ZksEthOpInfo;
2022
import io.zksync.transport.response.ZksGetConfirmationsForEthOpAmount;
2123
import io.zksync.transport.response.ZksSentTransaction;
2224
import io.zksync.transport.response.ZksSentTransactionBatch;
25+
import io.zksync.transport.response.ZksToggle2FA;
2326
import io.zksync.transport.response.ZksTokenPrice;
2427
import io.zksync.transport.response.ZksTokens;
2528
import io.zksync.transport.response.ZksTransactionDetails;
@@ -163,6 +166,13 @@ public String getEthTransactionForWithdrawal(String zkSyncWithdrawalHash) {
163166
return response;
164167
}
165168

169+
@Override
170+
public boolean toggle2FA(Toggle2FA toggle2fa) {
171+
final ZkSyncSuccess result = transport.send("toggle_2fa", Collections.singletonList(toggle2fa), ZksToggle2FA.class);
172+
173+
return result.getSuccess();
174+
}
175+
166176
public void updateTokenSet() {
167177
final Tokens response = transport.send("tokens", Collections.emptyList(), ZksTokens.class);
168178
this.tokens = response;

src/main/java/io/zksync/provider/Provider.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.apache.commons.lang3.tuple.Pair;
88

99
import io.zksync.domain.ChainId;
10+
import io.zksync.domain.auth.Toggle2FA;
1011
import io.zksync.domain.contract.ContractAddress;
1112
import io.zksync.domain.fee.TransactionFeeBatchRequest;
1213
import io.zksync.domain.fee.TransactionFeeDetails;
@@ -144,6 +145,14 @@ public interface Provider {
144145
*/
145146
String getEthTransactionForWithdrawal(String zkSyncWithdrawalHash);
146147

148+
/**
149+
* Send signed toggle 2FA request to ZkSync network.
150+
*
151+
* @param toggle2FA - Request object
152+
* @return - true if successful, false otherwise
153+
*/
154+
boolean toggle2FA(Toggle2FA toggle2FA);
155+
147156
/**
148157
* Fetch and update local cache of token list
149158
*

src/main/java/io/zksync/signer/Create2EthSigner.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ public CompletableFuture<ChangePubKey<ChangePubKeyCREATE2>> signAuth(
7777
return CompletableFuture.completedFuture(changePubKey);
7878
}
7979

80+
@Override
81+
public CompletableFuture<EthSignature> signToggle(boolean enable, Long timestamp) {
82+
return CompletableFuture.completedFuture(null);
83+
}
84+
8085
@Override
8186
public <T extends ZkSyncTransaction> CompletableFuture<EthSignature> signTransaction(T transaction, Integer nonce,
8287
Token token, BigInteger fee) {

src/main/java/io/zksync/signer/DefaultEthSigner.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ public CompletableFuture<ChangePubKey<ChangePubKeyECDSA>> signAuth(ChangePubKey<
114114
});
115115
}
116116

117+
@Override
118+
public CompletableFuture<EthSignature> signToggle(boolean enable, Long timestamp) {
119+
final String message = SigningUtils.getToggle2FAMessage(enable, timestamp);
120+
121+
return signMessage(message.getBytes(), true);
122+
}
123+
117124
public <T extends ZkSyncTransaction> CompletableFuture<EthSignature> signTransaction(T tx, Integer nonce, Token token, BigInteger fee) {
118125
switch (tx.getType()) {
119126
case "ChangePubKey":

src/main/java/io/zksync/signer/EthSigner.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public interface EthSigner<A extends ChangePubKeyVariant> {
3737

3838
CompletableFuture<ChangePubKey<A>> signAuth(ChangePubKey<A> changePubKey);
3939

40+
CompletableFuture<EthSignature> signToggle(boolean enable, Long timestamp);
41+
4042
/**
4143
* Sign `ZkSync` type operation message
4244
*

src/main/java/io/zksync/signer/SigningUtils.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,28 @@ public static String getSwapMessagePart(Token token, BigInteger fee) {
118118
return result;
119119
}
120120

121+
public static String getToggle2FAMessage(boolean require2FA, Long timestamp) {
122+
final String message;
123+
if (require2FA) {
124+
message = String.format(
125+
"By signing this message, you are opting into Two-factor Authentication protection by the zkSync Server.\n" +
126+
"Transactions now require signatures by both your L1 and L2 private key.\n" +
127+
"Timestamp: %d",
128+
timestamp
129+
);
130+
} else {
131+
message = String.format(
132+
"You are opting out of Two-factor Authentication protection by the zkSync Server.\n" +
133+
"Transactions now only require signatures by your L2 private key.\n" +
134+
"BY SIGNING THIS MESSAGE, YOU ARE TRUSTING YOUR WALLET CLIENT TO KEEP YOUR L2 PRIVATE KEY SAFE!\n" +
135+
"Timestamp: %d",
136+
timestamp
137+
);
138+
}
139+
140+
return message;
141+
}
142+
121143
public static String getNonceMessagePart(Integer nonce) {
122144
return String.format("Nonce: %s", nonce);
123145
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.zksync.transport;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class ZkSyncSuccess {
7+
8+
private Boolean success;
9+
10+
}

0 commit comments

Comments
 (0)