Skip to content

Commit 82448c8

Browse files
committed
feat: InvoiceManager creates invoices
1 parent fd83278 commit 82448c8

File tree

4 files changed

+57
-49
lines changed

4 files changed

+57
-49
lines changed

src/core/InvoiceManager.sol

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ contract InvoiceManager is UUPSUpgradeable, OwnableUpgradeable, ReentrancyGuardU
3636

3737
function _authorizeUpgrade(address) internal override onlyOwner {}
3838

39+
modifier onlyPaymaster(address account) {
40+
require(cabPaymasters[account].paymaster == msg.sender, "InvoiceManager: caller is not the paymaster");
41+
_;
42+
}
43+
3944
/// @inheritdoc IInvoiceManager
4045
function registerPaymaster(address paymaster, IPaymasterVerifier paymasterVerifier, uint256 expiry)
4146
external
@@ -61,14 +66,17 @@ contract InvoiceManager is UUPSUpgradeable, OwnableUpgradeable, ReentrancyGuardU
6166
}
6267

6368
/// @inheritdoc IInvoiceManager
64-
function createInvoice(uint256 nonce, address paymaster, bytes32 invoiceId) external override {
69+
function createInvoice(uint256 nonce, address account, bytes32 invoiceId)
70+
external
71+
override
72+
onlyPaymaster(account)
73+
{
6574
// check if the invoice already exists
6675
require(invoices[invoiceId].account == address(0), "InvoiceManager: invoice already exists");
67-
6876
// store the invoice
69-
invoices[invoiceId] = Invoice(msg.sender, nonce, paymaster, block.chainid);
77+
invoices[invoiceId] = Invoice(account, nonce, msg.sender, block.chainid);
7078

71-
emit InvoiceCreated(invoiceId, msg.sender, paymaster);
79+
emit InvoiceCreated(invoiceId, account, msg.sender);
7280
}
7381

7482
/// @inheritdoc IInvoiceManager

src/interfaces/IPaymasterVerifier.sol

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ import {IInvoiceManager} from "./IInvoiceManager.sol";
88
* @title Interface for the PaymasterVerifier contract.
99
*/
1010
interface IPaymasterVerifier {
11-
/// @notice Emitted when an invoice is created.
12-
event InvoiceCreated(bytes32 indexed invoiceId);
13-
14-
/// @notice Emitted when an invoice is verified.
15-
event InvoiceVerified(bytes32 indexed invoiceId);
16-
1711
/// @notice The struct of the sponsor token.
1812
struct SponsorToken {
1913
address token;

src/paymasters/CABPaymaster.sol

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,10 @@ contract CABPaymaster is IPaymasterVerifier, BasePaymaster {
6060

6161
(uint256 logIndex, bytes memory proof) = abi.decode(_proof, (uint256, bytes));
6262
(,, bytes[] memory topics,) = crossL2Prover.validateEvent(logIndex, proof);
63-
bytes[] memory expectedTopics = new bytes[](2);
64-
expectedTopics[0] = abi.encode(InvoiceCreated.selector);
65-
expectedTopics[1] = abi.encode(invoiceId);
6663

67-
if (!LibBytes.eq(abi.encode(topics), abi.encode(expectedTopics))) {
68-
return false;
69-
}
70-
emit InvoiceVerified(invoiceId);
71-
return true;
64+
return LibBytes.eq(
65+
abi.encode(topics[0], topics[1]), abi.encode(IInvoiceManager.InvoiceCreated.selector, invoiceId)
66+
);
7267
}
7368

7469
function withdraw(address token, uint256 amount) external override onlyOwner {
@@ -144,13 +139,17 @@ contract CABPaymaster is IPaymasterVerifier, BasePaymaster {
144139
// don't revert on signature failure: return SIG_VALIDATION_FAILED
145140
if (verifyingSigner != ECDSA.recover(hash, paymasterSignature)) {
146141
return (
147-
abi.encodePacked(invoiceId, sponsorTokenData[0:1 + sponsorTokenLength * 72]),
142+
abi.encodePacked(
143+
invoiceId, userOp.getSender(), userOp.nonce, sponsorTokenData[0:1 + sponsorTokenLength * 72]
144+
),
148145
_packValidationData(true, validUntil, validAfter)
149146
);
150147
}
151148

152149
return (
153-
abi.encodePacked(invoiceId, sponsorTokenData[0:1 + sponsorTokenLength * 72]),
150+
abi.encodePacked(
151+
invoiceId, userOp.getSender(), userOp.nonce, sponsorTokenData[0:1 + sponsorTokenLength * 72]
152+
),
154153
_packValidationData(false, validUntil, validAfter)
155154
);
156155
}
@@ -160,8 +159,7 @@ contract CABPaymaster is IPaymasterVerifier, BasePaymaster {
160159
virtual
161160
override
162161
{
163-
bytes32 invoiceId = bytes32(context[:32]);
164-
bytes calldata sponsorTokenData = context[32:];
162+
bytes calldata sponsorTokenData = context[84:];
165163

166164
(uint8 sponsorTokenLength, SponsorToken[] memory sponsorTokens) = parseSponsorTokenData(sponsorTokenData);
167165
for (uint8 i = 0; i < sponsorTokenLength; i++) {
@@ -170,7 +168,10 @@ contract CABPaymaster is IPaymasterVerifier, BasePaymaster {
170168
}
171169
// TODO: Batch Proving Optimistation -> write in settlement contract on `opSucceeded`
172170
if (mode == PostOpMode.opSucceeded) {
173-
emit InvoiceCreated(invoiceId);
171+
bytes32 invoiceId = bytes32(context[:32]);
172+
address account = address(bytes20(context[32:52]));
173+
uint256 nonce = uint256(bytes32(context[52:84]));
174+
invoiceManager.createInvoice(nonce, account, invoiceId);
174175
}
175176
}
176177

0 commit comments

Comments
 (0)