Skip to content

Commit

Permalink
Update encode approach
Browse files Browse the repository at this point in the history
  • Loading branch information
Phanco committed Dec 7, 2023
1 parent 26b54b4 commit 5584268
Show file tree
Hide file tree
Showing 4 changed files with 337 additions and 1,241 deletions.
37 changes: 16 additions & 21 deletions src/L2/L2Claim.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity 0.8.21;
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { MerkleProof } from "@openzeppelin/contracts/utils/cryptography/MerkleProof.sol";
import { Ed25519 } from "../utils/Ed25519.sol";
import "forge-std/console.sol";

struct MultisigKeys {
bytes32[] mandatoryKeys;
Expand All @@ -16,7 +17,7 @@ struct ED25519Signature {
}

contract L2Claim {
event LSKClaimed(bytes20 lskAddress, uint256 amount);
event LSKClaimed(bytes20 lskAddress, address recipient, uint256 amount);

uint256 public constant LSK_MULTIPLIER = 10 ** 10;

Expand All @@ -35,13 +36,8 @@ contract L2Claim {
require(Ed25519.check(_pubKey, _r, _s, _message, bytes9(0)), "Invalid Signature");
}

function encodeBytes32Array(bytes32[] calldata _input) internal pure returns (bytes memory data) {
for (uint256 i = 0; i < _input.length;) {
data = abi.encodePacked(data, _input[i]);
unchecked {
i++;
}
}
function doubleKeccak256(bytes memory _message) internal pure returns (bytes32) {
return keccak256(bytes.concat(keccak256(_message)));
}

function claim(
Expand All @@ -59,7 +55,7 @@ contract L2Claim {
l2LiskToken.transfer(_recipient, _amount * LSK_MULTIPLIER);

claimed[_lskAddress] = true;
emit LSKClaimed(_lskAddress, _amount);
emit LSKClaimed(_lskAddress, _recipient, _amount);
}

function claimRegularAccount(
Expand All @@ -72,9 +68,10 @@ contract L2Claim {
external
{
bytes20 lskAddress = bytes20(sha256(abi.encode(_pubKey)));
bytes32 node = keccak256(abi.encodePacked(lskAddress, _amount, uint256(0)));
bytes32 node = doubleKeccak256(abi.encode(lskAddress, _amount, uint32(0), new bytes32[](0), new bytes32[](0)));

verifySignature(_pubKey, _sig.r, _sig.s, keccak256(abi.encodePacked(node, _recipient)));
console.logBytes32(node);
verifySignature(_pubKey, _sig.r, _sig.s, keccak256(abi.encode(node, _recipient)));

claim(lskAddress, _amount, _proof, node, _recipient);
}
Expand All @@ -89,8 +86,10 @@ contract L2Claim {
)
external
{
require(_sigs.length == _keys.optionalKeys.length + _keys.mandatoryKeys.length, "Invalid Signature Length");

// If numberOfSignatures passes MerkleProof in later stage, that means this value is correct.
uint256 numberOfSignatures = _keys.mandatoryKeys.length;
uint32 numberOfSignatures = uint32(_keys.mandatoryKeys.length);

for (uint256 i = 0; i < _keys.optionalKeys.length; i++) {
if (_sigs[i + _keys.mandatoryKeys.length].r == bytes32(0)) {
Expand All @@ -99,17 +98,13 @@ contract L2Claim {
numberOfSignatures++;
}

bytes32 node = keccak256(
abi.encodePacked(
_lskAddress,
_amount,
numberOfSignatures,
encodeBytes32Array(_keys.mandatoryKeys),
encodeBytes32Array(_keys.optionalKeys)
)
bytes32 node = doubleKeccak256(
abi.encode(_lskAddress, _amount, numberOfSignatures, _keys.mandatoryKeys, _keys.optionalKeys)
);

bytes32 message = keccak256(abi.encodePacked(node, _recipient));
console.logBytes(abi.encode(node, _recipient));
console.logBytes32(keccak256(abi.encode(node, _recipient)));
bytes32 message = keccak256(abi.encode(node, _recipient));

for (uint256 i = 0; i < _keys.mandatoryKeys.length; i++) {
verifySignature(_keys.mandatoryKeys[i], _sigs[i].r, _sigs[i].s, message);
Expand Down
2 changes: 2 additions & 0 deletions test/L2/L2Claim.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Test, console, stdJson } from "forge-std/Test.sol";
import { L2Claim, ED25519Signature, MultisigKeys } from "src/L2/L2Claim.sol";
import { L2LiskToken } from "src/L2/L2LiskToken.sol";
import "../mock/MockERC20.sol";
import "forge-std/console.sol";

struct SigPair {
bytes32 pubKey;
Expand Down Expand Up @@ -224,6 +225,7 @@ contract L2ClaimTest is Test {
ED25519Signature[] memory ed25519Signatures = new ED25519Signature[](node.numberOfSignatures);

for (uint256 i; i < node.numberOfSignatures; i++) {
console.logBytes32(signature.sigs[i].r);
ed25519Signatures[i] = ED25519Signature(signature.sigs[i].r, signature.sigs[i].s);
}

Expand Down
Loading

0 comments on commit 5584268

Please sign in to comment.