Skip to content

Commit

Permalink
refactor: signer instead of delegator
Browse files Browse the repository at this point in the history
  • Loading branch information
yonadaa committed Apr 3, 2024
1 parent 74ca5d4 commit 5b10bc5
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe("callWithSignature", async () => {
types: callWithSignatureTypes,
primaryType: "Call",
message: {
delegator: delegator.address,
signer: delegator.address,
systemId,
callData,
nonce,
Expand Down
4 changes: 2 additions & 2 deletions packages/world-modules/mud.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,8 @@ export default defineWorld({
*
************************************************************************/
CallWithSignatureNonces: {
schema: { delegator: "address", nonce: "uint256" },
key: ["delegator"],
schema: { signer: "address", nonce: "uint256" },
key: ["signer"],
codegen: {
outputDirectory: "modules/delegation/tables",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,29 @@ contract Unstable_CallWithSignatureSystem is System {

/**
* @notice Calls a system with a given system ID using the given signature.
* @param delegator The address on whose behalf the system is called.
* @param signer The address on whose behalf the system is called.
* @param systemId The ID of the system to be called.
* @param callData The ABI data for the system call.
* @param signature The EIP712 signature.
* @return Return data from the system call.
*/
function callWithSignature(
address delegator,
address signer,
ResourceId systemId,
bytes memory callData,
bytes memory signature
) external payable returns (bytes memory) {
uint256 nonce = CallWithSignatureNonces.get(delegator);
bytes32 hash = getSignedMessageHash(delegator, systemId, callData, nonce, _world());
uint256 nonce = CallWithSignatureNonces.get(signer);
bytes32 hash = getSignedMessageHash(signer, systemId, callData, nonce, _world());

// If the message was not signed by the delegator or is invalid, revert
address signer = ECDSA.recover(hash, signature);
if (signer != delegator) {
revert InvalidSignature(signer);
address recoveredSigner = ECDSA.recover(hash, signature);
if (recoveredSigner != signer) {
revert InvalidSignature(recoveredSigner);
}

CallWithSignatureNonces.set(delegator, nonce + 1);
CallWithSignatureNonces.set(signer, nonce + 1);

return SystemCall.callWithHooksOrRevert(delegator, systemId, callData, _msgValue());
return SystemCall.callWithHooksOrRevert(signer, systemId, callData, _msgValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ pragma solidity >=0.8.24;

import { ResourceId } from "@latticexyz/store/src/ResourceId.sol";

bytes32 constant CALL_TYPEHASH = keccak256("Call(address delegator,bytes32 systemId,bytes callData,uint256 nonce)");
bytes32 constant CALL_TYPEHASH = keccak256("Call(address signer,bytes32 systemId,bytes callData,uint256 nonce)");

/**
* @notice Generate the message hash for a given delegation signature.
* For EIP712 signatures https://eips.ethereum.org/EIPS/eip-712
* @dev We include the signer address to prevent generating a signature that recovers to a random address that didn't sign the message.
* @param delegator The address on whose behalf the system is called.
* @param signer The address on whose behalf the system is called.
* @param systemId The ID of the system to be called.
* @param callData The ABI data for the system call.
* @param nonce The nonce of the delegator
* @param nonce The nonce of the signer
* @param worldAddress The world address
* @return Return the message hash.
*/
function getSignedMessageHash(
address delegator,
address signer,
ResourceId systemId,
bytes memory callData,
uint256 nonce,
Expand All @@ -32,7 +32,7 @@ function getSignedMessageHash(
abi.encodePacked(
"\x19\x01",
domainSeperator,
keccak256(abi.encode(CALL_TYPEHASH, delegator, systemId, keccak256(callData), nonce))
keccak256(abi.encode(CALL_TYPEHASH, signer, systemId, keccak256(callData), nonce))
)
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ library CallWithSignatureNonces {
*/
function getKeyNames() internal pure returns (string[] memory keyNames) {
keyNames = new string[](1);
keyNames[0] = "delegator";
keyNames[0] = "signer";
}

/**
Expand Down Expand Up @@ -63,9 +63,9 @@ library CallWithSignatureNonces {
/**
* @notice Get nonce.
*/
function getNonce(address delegator) internal view returns (uint256 nonce) {
function getNonce(address signer) internal view returns (uint256 nonce) {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(uint160(delegator)));
_keyTuple[0] = bytes32(uint256(uint160(signer)));

bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout);
return (uint256(bytes32(_blob)));
Expand All @@ -74,9 +74,9 @@ library CallWithSignatureNonces {
/**
* @notice Get nonce.
*/
function _getNonce(address delegator) internal view returns (uint256 nonce) {
function _getNonce(address signer) internal view returns (uint256 nonce) {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(uint160(delegator)));
_keyTuple[0] = bytes32(uint256(uint160(signer)));

bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout);
return (uint256(bytes32(_blob)));
Expand All @@ -85,9 +85,9 @@ library CallWithSignatureNonces {
/**
* @notice Get nonce.
*/
function get(address delegator) internal view returns (uint256 nonce) {
function get(address signer) internal view returns (uint256 nonce) {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(uint160(delegator)));
_keyTuple[0] = bytes32(uint256(uint160(signer)));

bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout);
return (uint256(bytes32(_blob)));
Expand All @@ -96,9 +96,9 @@ library CallWithSignatureNonces {
/**
* @notice Get nonce.
*/
function _get(address delegator) internal view returns (uint256 nonce) {
function _get(address signer) internal view returns (uint256 nonce) {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(uint160(delegator)));
_keyTuple[0] = bytes32(uint256(uint160(signer)));

bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout);
return (uint256(bytes32(_blob)));
Expand All @@ -107,59 +107,59 @@ library CallWithSignatureNonces {
/**
* @notice Set nonce.
*/
function setNonce(address delegator, uint256 nonce) internal {
function setNonce(address signer, uint256 nonce) internal {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(uint160(delegator)));
_keyTuple[0] = bytes32(uint256(uint160(signer)));

StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((nonce)), _fieldLayout);
}

/**
* @notice Set nonce.
*/
function _setNonce(address delegator, uint256 nonce) internal {
function _setNonce(address signer, uint256 nonce) internal {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(uint160(delegator)));
_keyTuple[0] = bytes32(uint256(uint160(signer)));

StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((nonce)), _fieldLayout);
}

/**
* @notice Set nonce.
*/
function set(address delegator, uint256 nonce) internal {
function set(address signer, uint256 nonce) internal {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(uint160(delegator)));
_keyTuple[0] = bytes32(uint256(uint160(signer)));

StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((nonce)), _fieldLayout);
}

/**
* @notice Set nonce.
*/
function _set(address delegator, uint256 nonce) internal {
function _set(address signer, uint256 nonce) internal {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(uint160(delegator)));
_keyTuple[0] = bytes32(uint256(uint160(signer)));

StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((nonce)), _fieldLayout);
}

/**
* @notice Delete all data for given keys.
*/
function deleteRecord(address delegator) internal {
function deleteRecord(address signer) internal {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(uint160(delegator)));
_keyTuple[0] = bytes32(uint256(uint160(signer)));

StoreSwitch.deleteRecord(_tableId, _keyTuple);
}

/**
* @notice Delete all data for given keys.
*/
function _deleteRecord(address delegator) internal {
function _deleteRecord(address signer) internal {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(uint160(delegator)));
_keyTuple[0] = bytes32(uint256(uint160(signer)));

StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout);
}
Expand Down Expand Up @@ -190,9 +190,9 @@ library CallWithSignatureNonces {
/**
* @notice Encode keys as a bytes32 array using this table's field layout.
*/
function encodeKeyTuple(address delegator) internal pure returns (bytes32[] memory) {
function encodeKeyTuple(address signer) internal pure returns (bytes32[] memory) {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(uint160(delegator)));
_keyTuple[0] = bytes32(uint256(uint160(signer)));

return _keyTuple;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/world-modules/test/CallWithSignatureModule.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ contract Unstable_CallWithSignatureModuleTest is Test, GasReporter {
vm.expectRevert(
abi.encodeWithSelector(
Unstable_CallWithSignatureSystem.InvalidSignature.selector,
0xFEb25028eF691bc606f66760260f446b84c9f160
0x824E5E0aF3eA693b906527Dc41E4a29F037d515b
)
);
Unstable_CallWithSignatureSystem(address(world)).callWithSignature(
Expand Down
2 changes: 1 addition & 1 deletion packages/world/ts/callWithSignatureTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

export const callWithSignatureTypes = {
Call: [
{ name: "delegator", type: "address" },
{ name: "signer", type: "address" },
{ name: "systemId", type: "bytes32" },
{ name: "callData", type: "bytes" },
{ name: "nonce", type: "uint256" },
Expand Down

0 comments on commit 5b10bc5

Please sign in to comment.