Skip to content

Commit 4d99c7b

Browse files
committed
smoke test passing
1 parent 6d571b2 commit 4d99c7b

File tree

3 files changed

+93
-297
lines changed

3 files changed

+93
-297
lines changed

packages/ethereum-contracts/contracts/interfaces/utils/IUserDefinedMacro.sol

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,11 @@ interface IUserDefinedMacro {
4444
* You can consult the related test code in `MacroForwarderTest.t.sol` for examples.
4545
*/
4646
}
47+
48+
interface IUserDefined712Macro is IUserDefinedMacro {
49+
// TODO: this probably needs to be a function of the message, for the dispatching pattern
50+
// the metaphor being: a macro is like an api, an action is like an endpoint (defining the set of arguments)
51+
function getMessageTypeHash() external view returns (bytes32);
52+
// TODO: should it take the known and required fields already decoded instead?
53+
function getMessageStructHash(bytes memory message) external view returns (bytes32);
54+
}

packages/ethereum-contracts/contracts/utils/Only712MacroForwarder.sol

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ contract Only712MacroForwarder is ForwarderBase, EIP712 {
2626
//string language;
2727
//string disclaimer;
2828
}
29-
bytes32 constant TYPEHASH_META = keccak256("Meta(string domain,string version)");
29+
bytes32 internal constant _TYPEHASH_META = keccak256("Meta(string domain,string version)");
3030
struct PayloadMessage {
3131
string title;
3232
//string description;
@@ -39,7 +39,7 @@ contract Only712MacroForwarder is ForwarderBase, EIP712 {
3939
//uint256 validBefore;
4040
uint256 nonce;
4141
}
42-
bytes32 constant TYPEHASH_SECURITY = keccak256("Security(string provider,uint256 nonce)");
42+
bytes32 internal constant _TYPEHASH_SECURITY = keccak256("Security(string provider,uint256 nonce)");
4343

4444
error InvalidPayload(string message);
4545
error InvalidProvider(string provider);
@@ -53,9 +53,10 @@ contract Only712MacroForwarder is ForwarderBase, EIP712 {
5353
* @param m Target macro.
5454
* @param params Encoded payload
5555
*/
56-
function runMacro(IUserDefined712Macro m, bytes calldata params, address signer, bytes calldata signature) external payable returns (bool) {
57-
//bytes memory appParams = abi.decode(params, (bytes));
58-
56+
function runMacro(IUserDefined712Macro m, bytes calldata params, address signer, bytes calldata signature)
57+
external payable
58+
returns (bool)
59+
{
5960
// decode the payload
6061
Payload memory payload = abi.decode(params, (Payload));
6162
require(
@@ -64,14 +65,37 @@ contract Only712MacroForwarder is ForwarderBase, EIP712 {
6465
);
6566
// TODO: verify nonce (replay protection)
6667

67-
bytes32 metaStructHash = getMetaStructHash(payload.meta);
68+
bytes32 digest = _getDigest(m, payload);
69+
70+
// verify the signature - this also works for ERC1271 (contract signatures)
71+
if (!SignatureChecker.isValidSignatureNow(signer, digest, signature)) {
72+
revert InvalidSignature();
73+
}
74+
75+
// get the operations array from the user macro based on the payload message
76+
ISuperfluid.Operation[] memory operations =
77+
m.buildBatchOperations(_host, payload.message.customPayload, signer);
78+
79+
// forward the operations
80+
bool retVal = _forwardBatchCallWithValue(operations, msg.value);
81+
// TODO: is customPayload the correct argument here?
82+
m.postCheck(_host, payload.message.customPayload, signer);
83+
return retVal;
84+
}
85+
86+
function getDigest(IUserDefined712Macro m, bytes calldata params) external view returns (bytes32) {
87+
return _getDigest(m, abi.decode(params, (Payload)));
88+
}
89+
90+
function _getDigest(IUserDefined712Macro m, Payload memory payload) internal view returns (bytes32) {
91+
bytes32 metaStructHash = _getMetaStructHash(payload.meta);
6892

6993
// the message fragment is handled by the user macro.
7094
bytes32 messageStructHash = m.getMessageStructHash(
7195
abi.encode(payload.message.title, payload.message.customPayload)
7296
);
7397

74-
bytes32 securityStructHash = getSecurityStructHash(payload.security);
98+
bytes32 securityStructHash = _getSecurityStructHash(payload.security);
7599

76100
// get the typehash
77101
bytes32 primaryTypeHash = keccak256(
@@ -80,8 +104,8 @@ contract Only712MacroForwarder is ForwarderBase, EIP712 {
80104
"Payload(Meta meta,Message message,Security security)",
81105
// nested components need to be in alphabetical order
82106
m.getMessageTypeHash(),
83-
TYPEHASH_META,
84-
TYPEHASH_SECURITY
107+
_TYPEHASH_META,
108+
_TYPEHASH_SECURITY
85109
)
86110
);
87111

@@ -96,32 +120,20 @@ contract Only712MacroForwarder is ForwarderBase, EIP712 {
96120
)
97121
)
98122
);
99-
100-
// verify the signature - this also works for ERC1271 (contract signatures)
101-
if (!SignatureChecker.isValidSignatureNow(signer, digest, signature)) {
102-
revert InvalidSignature(); // or custom error
103-
}
104-
105-
// get the operations array from the user macro based on the payload message
106-
ISuperfluid.Operation[] memory operations = m.buildBatchOperations(_host, payload.message.customPayload, msg.sender);
107-
108-
// forward the operations
109-
bool retVal = _forwardBatchCallWithValue(operations, msg.value);
110-
m.postCheck(_host, payload.message.customPayload, msg.sender);
111-
return retVal;
123+
return digest;
112124
}
113125

114-
function getMetaStructHash(PayloadMeta memory meta) internal pure returns (bytes32) {
126+
function _getMetaStructHash(PayloadMeta memory meta) internal pure returns (bytes32) {
115127
return keccak256(abi.encode(
116-
TYPEHASH_META,
128+
_TYPEHASH_META,
117129
keccak256(bytes(meta.domain)),
118130
keccak256(bytes(meta.version))
119131
));
120132
}
121133

122-
function getSecurityStructHash(PayloadSecurity memory security) internal pure returns (bytes32) {
134+
function _getSecurityStructHash(PayloadSecurity memory security) internal pure returns (bytes32) {
123135
return keccak256(abi.encode(
124-
TYPEHASH_SECURITY,
136+
_TYPEHASH_SECURITY,
125137
keccak256(bytes(security.provider)),
126138
security.nonce
127139
));

0 commit comments

Comments
 (0)