Skip to content

Commit d2ff27f

Browse files
committed
updated to revised simplified schema
1 parent 4359446 commit d2ff27f

File tree

3 files changed

+91
-113
lines changed

3 files changed

+91
-113
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,16 @@ interface IUserDefinedMacro {
4848
// Interface for a macro used with the Only712MacroForwarder.
4949
// Metaphor: a macro is like an api, an action is like an endpoint.
5050
// Each action can have its own type definition (list of arguments).
51-
// TODO: for multi-action macros, the getters probably all need to get the encoded message as an argument
5251
interface IUserDefined712Macro is IUserDefinedMacro {
5352
// Primary type name (required by the EIP712 type definition), usually rendered prominently by wallets.
5453
// From a users perspective, it should concisely name the action/intent to be signed.
55-
function getPrimaryTypeName() external view returns (string memory);
54+
function getPrimaryTypeName(bytes memory params) external view returns (string memory);
5655

5756
// The EIP-712 type definition of the action, required by Only712MacroForwarder.
58-
function getMessageTypeDefinition() external view returns (string memory);
57+
// Note that the name of this type must be "Action", only its content is free to choose.
58+
function getActionTypeDefinition(bytes memory params) external view returns (string memory);
5959

6060
// The struct hash of the action, required by Only712MacroForwarder.
6161
// This hash must be constructed based on the type definition and the data, according to the EIP-712 standard.
62-
function getMessageStructHash(bytes memory message) external view returns (bytes32);
62+
function getActionStructHash(bytes memory params) external view returns (bytes32);
6363
}

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

Lines changed: 35 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -56,34 +56,23 @@ contract Only712MacroForwarder is ForwarderBase, EIP712, NonceManager {
5656
// STRUCTS, CONSTANTS, IMMUTABLES
5757

5858
// top-level data structure
59-
// TODO: is "payload" a good name? Does EIP-712 give a good hint for naming this? Something "primary"?
60-
struct Payload {
61-
PayloadMeta meta;
62-
PayloadMessage message;
63-
PayloadSecurity security;
59+
struct PrimaryType {
60+
ActionType action;
61+
SecurityType security;
6462
}
65-
struct PayloadMeta {
66-
string domain;
67-
string version;
68-
//string language;
69-
//string disclaimer;
70-
}
71-
bytes internal constant _TYPEDEF_META = "Meta(string domain,string version)";
72-
bytes32 internal constant _TYPEHASH_META = keccak256(_TYPEDEF_META);
73-
struct PayloadMessage {
74-
string title;
75-
//string description;
76-
bytes customPayload;
63+
struct ActionType {
64+
bytes actionParams;
7765
}
78-
// the message typehash is user macro specific
79-
struct PayloadSecurity {
66+
// the action typehash is macro specific
67+
struct SecurityType {
68+
string domain;
8069
string provider;
8170
uint256 validAfter;
8271
uint256 validBefore;
8372
uint256 nonce;
8473
}
85-
bytes internal constant _TYPEDEF_SECURITY =
86-
"Security(string provider,uint256 validAfter,uint256 validBefore,uint256 nonce)";
74+
bytes internal constant _TYPEDEF_SECURITY =
75+
"Security(string domain,string provider,uint256 validAfter,uint256 validBefore,uint256 nonce)";
8776

8877
bytes32 internal constant _TYPEHASH_SECURITY = keccak256(_TYPEDEF_SECURITY);
8978

@@ -119,7 +108,7 @@ contract Only712MacroForwarder is ForwarderBase, EIP712, NonceManager {
119108
returns (bool)
120109
{
121110
// decode the payload
122-
Payload memory payload = abi.decode(params, (Payload));
111+
PrimaryType memory payload = abi.decode(params, (PrimaryType));
123112
bytes32 providerRole = keccak256(bytes(payload.security.provider));
124113
if (!_providerACL.hasRole(providerRole, msg.sender)) {
125114
revert ProviderNotAuthorized(payload.security.provider, msg.sender);
@@ -134,97 +123,84 @@ contract Only712MacroForwarder is ForwarderBase, EIP712, NonceManager {
134123
revert OutsideValidityWindow(block.timestamp, payload.security.validBefore, payload.security.validAfter);
135124
}
136125

137-
bytes32 digest = _getDigest(m, payload);
126+
bytes32 digest = _getDigest(m, params);
138127

139128
// verify the signature - this also works for ERC1271 (contract signatures)
140129
if (!SignatureChecker.isValidSignatureNow(signer, digest, signature)) {
141130
revert InvalidSignature();
142131
}
143132

144-
// get the operations array from the user macro based on the payload message
133+
// get the operations array from the user macro based on the action params
145134
ISuperfluid.Operation[] memory operations =
146-
m.buildBatchOperations(_host, payload.message.customPayload, signer);
135+
m.buildBatchOperations(_host, payload.action.actionParams, signer);
147136

148137
// forward the operations
149138
bool retVal = _forwardBatchCallWithValue(operations, msg.value);
150-
// TODO: is customPayload the correct argument here?
151-
m.postCheck(_host, payload.message.customPayload, signer);
139+
m.postCheck(_host, payload.action.actionParams, signer);
152140
return retVal;
153141
}
154142

155143
// TODO: should this exist?
156-
function getTypeDefinition(IUserDefined712Macro m) external view returns (string memory) {
157-
return _getTypeDefinition(m);
144+
function getTypeDefinition(IUserDefined712Macro m, bytes calldata params) external view returns (string memory) {
145+
return _getTypeDefinition(m, params);
158146
}
159147

160148
// TODO: should this exist?
161-
function getTypeHash(IUserDefined712Macro m) public view returns (bytes32) {
162-
return keccak256(abi.encodePacked(_getTypeDefinition(m)));
149+
function getTypeHash(IUserDefined712Macro m, bytes calldata params) public view returns (bytes32) {
150+
return keccak256(abi.encodePacked(_getTypeDefinition(m, params)));
163151
}
164152

165153
// TODO: should this exist?
166154
function getStructHash(IUserDefined712Macro m, bytes calldata params) external view returns (bytes32) {
167-
return _getStructHash(m, abi.decode(params, (Payload)));
155+
return _getStructHash(m, params);
168156
}
169157

170158
function getDigest(IUserDefined712Macro m, bytes calldata params) external view returns (bytes32) {
171-
return _getDigest(m, abi.decode(params, (Payload)));
159+
return _getDigest(m, params);
172160
}
173161

174162
// INTERNAL FUNCTIONS
175163

176-
function _getTypeDefinition(IUserDefined712Macro m) internal view returns (string memory) {
164+
function _getTypeDefinition(IUserDefined712Macro m, bytes calldata params) internal view returns (string memory) {
177165
return string(abi.encodePacked(
178-
m.getPrimaryTypeName(),
179-
"(Meta meta,Message message,Security security)",
166+
m.getPrimaryTypeName(params),
167+
"(Action action,Security security)",
180168
// nested components need to be in alphabetical order
181-
m.getMessageTypeDefinition(),
182-
_TYPEDEF_META,
169+
m.getActionTypeDefinition(params),
183170
_TYPEDEF_SECURITY
184171
));
185172
}
186173

187-
function _getStructHash(IUserDefined712Macro m, Payload memory payload) internal view returns (bytes32) {
188-
bytes32 metaStructHash = _getMetaStructHash(payload.meta);
189-
190-
// the message fragment is handled by the user macro.
191-
bytes32 messageStructHash = m.getMessageStructHash(
192-
abi.encode(payload.message.title, payload.message.customPayload)
193-
);
174+
function _getStructHash(IUserDefined712Macro m, bytes calldata params) internal view returns (bytes32) {
175+
PrimaryType memory payload = abi.decode(params, (PrimaryType));
176+
// the action fragment is handled by the user macro.
177+
bytes32 actionStructHash = m.getActionStructHash(payload.action.actionParams);
194178

195179
bytes32 securityStructHash = _getSecurityStructHash(payload.security);
196180

197181
// get the typehash
198-
bytes32 primaryTypeHash = getTypeHash(m);
182+
bytes32 primaryTypeHash = getTypeHash(m, params);
199183

200184
// calculate the struct hash
201185
bytes32 structHash = keccak256(
202186
abi.encode(
203187
primaryTypeHash,
204-
metaStructHash,
205-
messageStructHash,
188+
actionStructHash,
206189
securityStructHash
207190
)
208191
);
209192
return structHash;
210193
}
211194

212-
function _getDigest(IUserDefined712Macro m, Payload memory payload) internal view returns (bytes32) {
213-
bytes32 structHash = _getStructHash(m, payload);
195+
function _getDigest(IUserDefined712Macro m, bytes calldata params) internal view returns (bytes32) {
196+
bytes32 structHash = _getStructHash(m, params);
214197
return _hashTypedDataV4(structHash);
215198
}
216199

217-
function _getMetaStructHash(PayloadMeta memory meta) internal pure returns (bytes32) {
218-
return keccak256(abi.encode(
219-
_TYPEHASH_META,
220-
keccak256(bytes(meta.domain)),
221-
keccak256(bytes(meta.version))
222-
));
223-
}
224-
225-
function _getSecurityStructHash(PayloadSecurity memory security) internal pure returns (bytes32) {
200+
function _getSecurityStructHash(SecurityType memory security) internal pure returns (bytes32) {
226201
return keccak256(abi.encode(
227202
_TYPEHASH_SECURITY,
203+
keccak256(bytes(security.domain)),
228204
keccak256(bytes(security.provider)),
229205
security.validAfter,
230206
security.validBefore,

0 commit comments

Comments
 (0)