-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPayResolver.sol
More file actions
326 lines (296 loc) · 14.3 KB
/
Copy pathPayResolver.sol
File metadata and controls
326 lines (296 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
import "./lib/data/PbChain.sol";
import "./lib/data/PbEntity.sol";
import "./interfaces/IPayRegistry.sol";
import "./interfaces/IPayResolver.sol";
import "./interfaces/IBooleanCond.sol";
import "./interfaces/INumericCond.sol";
import "./interfaces/IVirtContractResolver.sol";
import "./lib/AgentPayErrors.sol";
import "@openzeppelin/contracts/utils/math/Math.sol";
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import "@openzeppelin/contracts/utils/cryptography/MessageHashUtils.sol";
/**
* @title PayResolver
* @notice On-chain logic for resolving conditional payments. Versioned: each payment
* pins the resolver address it trusts (field 8 of `ConditionalPay`), and the resolver
* address is mixed into the pay id (`payId = keccak256(payHash, resolverAddress)`)
* so a result is bound to the exact resolver version the payment source designated.
* @dev See {IPayResolver} for canonical NatSpec on the external API. Resolution rules:
* HASH_LOCK conditions are present only to gate multi-hop secret reveal — they do not
* affect the transfer amount, and are always required to be true. A payment with no
* condition or only true hash-locks resolves to the max transfer amount.
*/
contract PayResolver is IPayResolver {
using ECDSA for bytes32;
using MessageHashUtils for bytes32;
/// @notice Registry where resolved amounts are recorded.
IPayRegistry public payRegistry;
/// @notice Resolver used to materialize virtual condition contracts on demand.
IVirtContractResolver public virtResolver;
/**
* @notice Construct the resolver and pin its dependencies.
* @param _registryAddr Address of the deployed {IPayRegistry}.
* @param _virtResolverAddr Address of the deployed {IVirtContractResolver}.
*/
constructor(address _registryAddr, address _virtResolverAddr) {
payRegistry = IPayRegistry(_registryAddr);
virtResolver = IVirtContractResolver(_virtResolverAddr);
}
/// @inheritdoc IPayResolver
function resolvePaymentByConditions(bytes calldata _resolvePayRequest) external {
PbChain.ResolvePayByConditionsRequest memory resolvePayRequest =
PbChain.decResolvePayByConditionsRequest(_resolvePayRequest);
PbEntity.ConditionalPay memory pay = PbEntity.decConditionalPay(resolvePayRequest.condPay);
// onchain resolve this payment and get result
uint256 amount;
PbEntity.TransferFunctionType funcType = pay.transferFunc.logicType;
if (funcType == PbEntity.TransferFunctionType.BOOLEAN_AND) {
amount = _calculateBooleanAndPayment(pay, resolvePayRequest.hashPreimages);
} else if (funcType == PbEntity.TransferFunctionType.BOOLEAN_OR) {
amount = _calculateBooleanOrPayment(pay, resolvePayRequest.hashPreimages);
} else if (_isNumericLogic(funcType)) {
amount = _calculateNumericLogicPayment(pay, resolvePayRequest.hashPreimages, funcType);
} else {
// TODO: support more transfer function types
assert(false);
}
bytes32 payHash = keccak256(resolvePayRequest.condPay);
_resolvePayment(pay, payHash, amount);
}
/// @inheritdoc IPayResolver
function resolvePaymentByVouchedResult(bytes calldata _vouchedPayResult) external {
PbEntity.VouchedCondPayResult memory vouchedPayResult = PbEntity.decVouchedCondPayResult(_vouchedPayResult);
PbEntity.CondPayResult memory payResult = PbEntity.decCondPayResult(vouchedPayResult.condPayResult);
PbEntity.ConditionalPay memory pay = PbEntity.decConditionalPay(payResult.condPay);
require(
payResult.amount <= pay.transferFunc.maxTransfer.receiver.amt,
AgentPayErrors.MaxTransferExceeded(payResult.amount, pay.transferFunc.maxTransfer.receiver.amt)
);
// check signatures
bytes32 hash = keccak256(vouchedPayResult.condPayResult).toEthSignedMessageHash();
address recoveredSrc = hash.recover(vouchedPayResult.sigOfSrc);
address recoveredDest = hash.recover(vouchedPayResult.sigOfDest);
require(
recoveredSrc == address(pay.src) && recoveredDest == address(pay.dest), AgentPayErrors.InvalidCoSignatures()
);
bytes32 payHash = keccak256(payResult.condPay);
_resolvePayment(pay, payHash, payResult.amount);
}
/**
* @notice Internal function of resolving a payment with given amount
* @param _pay conditional pay
* @param _payHash hash of serialized condPay
* @param _amount payment amount to resolve
*/
function _resolvePayment(PbEntity.ConditionalPay memory _pay, bytes32 _payHash, uint256 _amount) internal {
// bind the signed pay to its intended (chain, resolver) target
require(_pay.chainId == block.chainid, AgentPayErrors.ChainIdMismatch(block.chainid, _pay.chainId));
require(_pay.payResolver == address(this), AgentPayErrors.ResolverAddressMismatch());
uint256 nowTs = block.timestamp;
require(nowTs <= _pay.resolveDeadline, AgentPayErrors.DeadlinePassed());
bytes32 payId = _calculatePayId(_payHash, address(this));
(uint256 currentAmt, uint256 currentDeadline) = payRegistry.getPayInfo(payId);
// If a prior on-chain resolution exists (`currentDeadline > 0`), updates
// are only accepted while the registry's stored deadline has not yet
// passed. First-time resolution (`currentDeadline == 0`) always passes.
require(currentDeadline == 0 || nowTs <= currentDeadline, AgentPayErrors.ResolveUpdateWindowClosed());
if (currentDeadline > 0) {
// currentDeadline > 0 implies that this pay has been updated
// payment amount must be monotone increasing
require(_amount > currentAmt, AgentPayErrors.AmountNotGreater());
if (_amount == _pay.transferFunc.maxTransfer.receiver.amt) {
// set resolve deadline = current timestamp if amount = max
payRegistry.setPayInfo(_payHash, _amount, nowTs);
emit ResolvePayment(payId, _amount, nowTs);
} else {
// should not update the onchain resolve deadline if not max amount
payRegistry.setPayAmount(_payHash, _amount);
emit ResolvePayment(payId, _amount, currentDeadline);
}
} else {
uint256 newDeadline;
if (_amount == _pay.transferFunc.maxTransfer.receiver.amt) {
newDeadline = nowTs;
} else {
newDeadline = Math.min(nowTs + _pay.resolveTimeout, _pay.resolveDeadline);
// 0 is reserved for unresolved status of a payment
require(newDeadline > 0, AgentPayErrors.ZeroDeadline());
}
payRegistry.setPayInfo(_payHash, _amount, newDeadline);
emit ResolvePayment(payId, _amount, newDeadline);
}
}
/**
* @notice Calculate the result amount of BooleanAnd payment
* @param _pay conditional pay
* @param _preimages preimages for hash lock conditions
* @return pay amount
*/
function _calculateBooleanAndPayment(PbEntity.ConditionalPay memory _pay, bytes[] memory _preimages)
internal
view
returns (uint256)
{
uint256 j = 0;
bool hasFalseContractCond = false;
for (uint256 i = 0; i < _pay.conditions.length; i++) {
PbEntity.Condition memory cond = _pay.conditions[i];
if (cond.conditionType == PbEntity.ConditionType.HASH_LOCK) {
require(keccak256(_preimages[j]) == cond.hashLock, AgentPayErrors.PreimageMismatch());
j++;
} else if (
cond.conditionType == PbEntity.ConditionType.DEPLOYED_CONTRACT
|| cond.conditionType == PbEntity.ConditionType.VIRTUAL_CONTRACT
) {
address addr = _getCondAddress(cond);
IBooleanCond dependent = IBooleanCond(addr);
require(dependent.isFinalized(cond.argsQueryFinalization), AgentPayErrors.ConditionNotFinalized());
if (!dependent.getOutcome(cond.argsQueryOutcome)) {
hasFalseContractCond = true;
}
} else {
assert(false);
}
}
if (hasFalseContractCond) {
return 0;
} else {
return _pay.transferFunc.maxTransfer.receiver.amt;
}
}
/**
* @notice Calculate the result amount of BooleanOr payment
* @param _pay conditional pay
* @param _preimages preimages for hash lock conditions
* @return pay amount
*/
function _calculateBooleanOrPayment(PbEntity.ConditionalPay memory _pay, bytes[] memory _preimages)
internal
view
returns (uint256)
{
uint256 j = 0;
// whether there are any contract based conditions, i.e. DEPLOYED_CONTRACT or VIRTUAL_CONTRACT
bool hasContractCond = false;
bool hasTrueContractCond = false;
for (uint256 i = 0; i < _pay.conditions.length; i++) {
PbEntity.Condition memory cond = _pay.conditions[i];
if (cond.conditionType == PbEntity.ConditionType.HASH_LOCK) {
require(keccak256(_preimages[j]) == cond.hashLock, AgentPayErrors.PreimageMismatch());
j++;
} else if (
cond.conditionType == PbEntity.ConditionType.DEPLOYED_CONTRACT
|| cond.conditionType == PbEntity.ConditionType.VIRTUAL_CONTRACT
) {
address addr = _getCondAddress(cond);
IBooleanCond dependent = IBooleanCond(addr);
require(dependent.isFinalized(cond.argsQueryFinalization), AgentPayErrors.ConditionNotFinalized());
hasContractCond = true;
if (dependent.getOutcome(cond.argsQueryOutcome)) {
hasTrueContractCond = true;
}
} else {
assert(false);
}
}
if (!hasContractCond || hasTrueContractCond) {
return _pay.transferFunc.maxTransfer.receiver.amt;
} else {
return 0;
}
}
/**
* @notice Calculate the result amount of numeric logic payment,
* including NUMERIC_ADD, NUMERIC_MAX and NUMERIC_MIN
* @param _pay conditional pay
* @param _preimages preimages for hash lock conditions
* @param _funcType transfer function type
* @return pay amount
*/
function _calculateNumericLogicPayment(
PbEntity.ConditionalPay memory _pay,
bytes[] memory _preimages,
PbEntity.TransferFunctionType _funcType
) internal view returns (uint256) {
uint256 amount = 0;
uint256 j = 0;
bool hasContractCond = false;
for (uint256 i = 0; i < _pay.conditions.length; i++) {
PbEntity.Condition memory cond = _pay.conditions[i];
if (cond.conditionType == PbEntity.ConditionType.HASH_LOCK) {
require(keccak256(_preimages[j]) == cond.hashLock, AgentPayErrors.PreimageMismatch());
j++;
} else if (
cond.conditionType == PbEntity.ConditionType.DEPLOYED_CONTRACT
|| cond.conditionType == PbEntity.ConditionType.VIRTUAL_CONTRACT
) {
address addr = _getCondAddress(cond);
INumericCond dependent = INumericCond(addr);
require(dependent.isFinalized(cond.argsQueryFinalization), AgentPayErrors.ConditionNotFinalized());
if (_funcType == PbEntity.TransferFunctionType.NUMERIC_ADD) {
amount = amount + dependent.getOutcome(cond.argsQueryOutcome);
} else if (_funcType == PbEntity.TransferFunctionType.NUMERIC_MAX) {
amount = Math.max(amount, dependent.getOutcome(cond.argsQueryOutcome));
} else if (_funcType == PbEntity.TransferFunctionType.NUMERIC_MIN) {
if (hasContractCond) {
amount = Math.min(amount, dependent.getOutcome(cond.argsQueryOutcome));
} else {
amount = dependent.getOutcome(cond.argsQueryOutcome);
}
} else {
assert(false);
}
hasContractCond = true;
} else {
assert(false);
}
}
if (hasContractCond) {
require(
amount <= _pay.transferFunc.maxTransfer.receiver.amt,
AgentPayErrors.MaxTransferExceeded(amount, _pay.transferFunc.maxTransfer.receiver.amt)
);
return amount;
} else {
return _pay.transferFunc.maxTransfer.receiver.amt;
}
}
/**
* @notice Get the contract address of the condition
* @param _cond condition
* @return contract address of the condition
*/
function _getCondAddress(PbEntity.Condition memory _cond) internal view returns (address) {
// We need to take into account that contract may not be deployed.
// However, this is automatically handled for us
// because calling a non-existent function will cause an revert.
if (_cond.conditionType == PbEntity.ConditionType.DEPLOYED_CONTRACT) {
return _cond.deployedContractAddress;
} else if (_cond.conditionType == PbEntity.ConditionType.VIRTUAL_CONTRACT) {
return virtResolver.resolve(_cond.virtualContractAddress);
} else {
revert AgentPayErrors.InvalidConditionType();
}
}
/**
* @notice Check if a function type is numeric logic
* @param _funcType transfer function type
* @return true if it is a numeric logic, otherwise false
*/
function _isNumericLogic(PbEntity.TransferFunctionType _funcType) internal pure returns (bool) {
return _funcType == PbEntity.TransferFunctionType.NUMERIC_ADD
|| _funcType == PbEntity.TransferFunctionType.NUMERIC_MAX
|| _funcType == PbEntity.TransferFunctionType.NUMERIC_MIN;
}
/**
* @notice Calculate pay id
* @param _payHash hash of serialized condPay
* @param _setter payment info setter, i.e. pay resolver
* @return calculated pay id
*/
function _calculatePayId(bytes32 _payHash, address _setter) internal pure returns (bytes32) {
return keccak256(abi.encodePacked(_payHash, _setter));
}
}