Skip to content

Commit 471e4de

Browse files
committed
refactor
1 parent fa8b58b commit 471e4de

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

contracts/utils/cryptography/ZKEmailUtils.sol

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {AbstractSigner} from "./AbstractSigner.sol";
2626
* mechanism to ensure the email was actually sent and received without revealing its contents. Defined by an `IVerifier` interface.
2727
*/
2828
library ZKEmailUtils {
29+
using CommandUtils for bytes[];
2930
using Strings for string;
3031

3132
/// @dev Enumeration of possible email proof validation errors.
@@ -93,34 +94,25 @@ library ZKEmailUtils {
9394
return EmailProofError.MaskedCommandLength;
9495
} else if (emailAuthMsg.skippedCommandPrefix >= verifier.commandBytes()) {
9596
return EmailProofError.SkippedCommandPrefixSize;
96-
} else if (
97-
stringCase == Case.ANY
98-
? _matchAnyCase(emailAuthMsg, template)
99-
: _matchCase(emailAuthMsg, template, stringCase)
100-
) {
101-
return verifier.verifyEmailProof(emailAuthMsg.proof) ? EmailProofError.NoError : EmailProofError.EmailProof;
102-
} else {
97+
} else if (!_commandMatch(emailAuthMsg, template, stringCase)) {
10398
return EmailProofError.MismatchedCommand;
99+
} else {
100+
return verifier.verifyEmailProof(emailAuthMsg.proof) ? EmailProofError.NoError : EmailProofError.EmailProof;
104101
}
105102
}
106103

107-
/// @dev Checks if the command matches the expected command for any string case.
108-
function _matchAnyCase(EmailAuthMsg memory emailAuthMsg, string[] memory template) private pure returns (bool) {
109-
return
110-
_matchCase(emailAuthMsg, template, Case.LOWERCASE) ||
111-
_matchCase(emailAuthMsg, template, Case.UPPERCASE) ||
112-
_matchCase(emailAuthMsg, template, Case.CHECKSUM);
113-
}
114-
115-
/// @dev MUST NOT be called with `Case.ANY` to avoid unexpected behavior.
116-
function _matchCase(
104+
function _commandMatch(
117105
EmailAuthMsg memory emailAuthMsg,
118106
string[] memory template,
119107
Case stringCase
120108
) private pure returns (bool) {
109+
bytes[] memory commandParams = emailAuthMsg.commandParams; // Not a memory copy
110+
string memory maskedCommand = emailAuthMsg.proof.maskedCommand; // Not a memory copy
121111
return
122-
CommandUtils.computeExpectedCommand(emailAuthMsg.commandParams, template, uint8(stringCase)).equal(
123-
emailAuthMsg.proof.maskedCommand
124-
);
112+
stringCase == Case.ANY
113+
? (commandParams.computeExpectedCommand(template, uint8(Case.LOWERCASE)).equal(maskedCommand) ||
114+
commandParams.computeExpectedCommand(template, uint8(Case.UPPERCASE)).equal(maskedCommand) ||
115+
commandParams.computeExpectedCommand(template, uint8(Case.CHECKSUM)).equal(maskedCommand))
116+
: commandParams.computeExpectedCommand(template, uint8(stringCase)).equal(maskedCommand);
125117
}
126118
}

0 commit comments

Comments
 (0)