draft-ERC4337Utils.test.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. const { packValidationData, packPaymasterData, UserOperation } = require('../../helpers/erc4337');
  5. const { MAX_UINT48 } = require('../../helpers/constants');
  6. const fixture = async () => {
  7. const [authorizer, sender, entrypoint, paymaster] = await ethers.getSigners();
  8. const utils = await ethers.deployContract('$ERC4337Utils');
  9. return { utils, authorizer, sender, entrypoint, paymaster };
  10. };
  11. describe('ERC4337Utils', function () {
  12. beforeEach(async function () {
  13. Object.assign(this, await loadFixture(fixture));
  14. });
  15. describe('parseValidationData', function () {
  16. it('parses the validation data', async function () {
  17. const authorizer = this.authorizer;
  18. const validUntil = 0x12345678n;
  19. const validAfter = 0x9abcdef0n;
  20. const validationData = packValidationData(validAfter, validUntil, authorizer);
  21. expect(this.utils.$parseValidationData(validationData)).to.eventually.deep.equal([
  22. authorizer.address,
  23. validAfter,
  24. validUntil,
  25. ]);
  26. });
  27. it('returns an type(uint48).max if until is 0', async function () {
  28. const authorizer = this.authorizer;
  29. const validAfter = 0x12345678n;
  30. const validationData = packValidationData(validAfter, 0, authorizer);
  31. expect(this.utils.$parseValidationData(validationData)).to.eventually.deep.equal([
  32. authorizer.address,
  33. validAfter,
  34. MAX_UINT48,
  35. ]);
  36. });
  37. });
  38. describe('packValidationData', function () {
  39. it('packs the validation data', async function () {
  40. const authorizer = this.authorizer;
  41. const validUntil = 0x12345678n;
  42. const validAfter = 0x9abcdef0n;
  43. const validationData = packValidationData(validAfter, validUntil, authorizer);
  44. expect(
  45. this.utils.$packValidationData(ethers.Typed.address(authorizer), validAfter, validUntil),
  46. ).to.eventually.equal(validationData);
  47. });
  48. it('packs the validation data (bool)', async function () {
  49. const success = false;
  50. const validUntil = 0x12345678n;
  51. const validAfter = 0x9abcdef0n;
  52. const validationData = packValidationData(validAfter, validUntil, false);
  53. expect(this.utils.$packValidationData(ethers.Typed.bool(success), validAfter, validUntil)).to.eventually.equal(
  54. validationData,
  55. );
  56. });
  57. });
  58. describe('combineValidationData', function () {
  59. const validUntil1 = 0x12345678n;
  60. const validAfter1 = 0x9abcdef0n;
  61. const validUntil2 = 0x87654321n;
  62. const validAfter2 = 0xabcdef90n;
  63. it('combines the validation data', async function () {
  64. const validationData1 = packValidationData(validAfter1, validUntil1, ethers.ZeroAddress);
  65. const validationData2 = packValidationData(validAfter2, validUntil2, ethers.ZeroAddress);
  66. const expected = packValidationData(validAfter2, validUntil1, true);
  67. // check symmetry
  68. expect(this.utils.$combineValidationData(validationData1, validationData2)).to.eventually.equal(expected);
  69. expect(this.utils.$combineValidationData(validationData2, validationData1)).to.eventually.equal(expected);
  70. });
  71. for (const [authorizer1, authorizer2] of [
  72. [ethers.ZeroAddress, '0xbf023313b891fd6000544b79e353323aa94a4f29'],
  73. ['0xbf023313b891fd6000544b79e353323aa94a4f29', ethers.ZeroAddress],
  74. ]) {
  75. it('returns SIG_VALIDATION_FAILURE if one of the authorizers is not address(0)', async function () {
  76. const validationData1 = packValidationData(validAfter1, validUntil1, authorizer1);
  77. const validationData2 = packValidationData(validAfter2, validUntil2, authorizer2);
  78. const expected = packValidationData(validAfter2, validUntil1, false);
  79. // check symmetry
  80. expect(this.utils.$combineValidationData(validationData1, validationData2)).to.eventually.equal(expected);
  81. expect(this.utils.$combineValidationData(validationData2, validationData1)).to.eventually.equal(expected);
  82. });
  83. }
  84. });
  85. describe('getValidationData', function () {
  86. it('returns the validation data with valid validity range', async function () {
  87. const aggregator = this.authorizer;
  88. const validAfter = 0;
  89. const validUntil = MAX_UINT48;
  90. const validationData = packValidationData(validAfter, validUntil, aggregator);
  91. expect(this.utils.$getValidationData(validationData)).to.eventually.deep.equal([aggregator.address, false]);
  92. });
  93. it('returns the validation data with invalid validity range (expired)', async function () {
  94. const aggregator = this.authorizer;
  95. const validAfter = 0;
  96. const validUntil = 1;
  97. const validationData = packValidationData(validAfter, validUntil, aggregator);
  98. expect(this.utils.$getValidationData(validationData)).to.eventually.deep.equal([aggregator.address, true]);
  99. });
  100. it('returns the validation data with invalid validity range (not yet valid)', async function () {
  101. const aggregator = this.authorizer;
  102. const validAfter = MAX_UINT48;
  103. const validUntil = MAX_UINT48;
  104. const validationData = packValidationData(validAfter, validUntil, aggregator);
  105. expect(this.utils.$getValidationData(validationData)).to.eventually.deep.equal([aggregator.address, true]);
  106. });
  107. it('returns address(0) and false for validationData = 0', function () {
  108. expect(this.utils.$getValidationData(0n)).to.eventually.deep.equal([ethers.ZeroAddress, false]);
  109. });
  110. });
  111. describe('hash', function () {
  112. it('returns the operation hash with specified entrypoint and chainId', async function () {
  113. const userOp = new UserOperation({ sender: this.sender, nonce: 1 });
  114. const chainId = 0xdeadbeef;
  115. expect(this.utils.$hash(userOp.packed, this.entrypoint, chainId)).to.eventually.equal(
  116. userOp.hash(this.entrypoint, chainId),
  117. );
  118. });
  119. });
  120. describe('userOp values', function () {
  121. it('returns verificationGasLimit', async function () {
  122. const userOp = new UserOperation({ sender: this.sender, nonce: 1, verificationGas: 0x12345678n });
  123. expect(this.utils.$verificationGasLimit(userOp.packed)).to.eventually.equal(userOp.verificationGas);
  124. });
  125. it('returns callGasLimit', async function () {
  126. const userOp = new UserOperation({ sender: this.sender, nonce: 1, callGas: 0x12345678n });
  127. expect(this.utils.$callGasLimit(userOp.packed)).to.eventually.equal(userOp.callGas);
  128. });
  129. it('returns maxPriorityFeePerGas', async function () {
  130. const userOp = new UserOperation({ sender: this.sender, nonce: 1, maxPriorityFee: 0x12345678n });
  131. expect(this.utils.$maxPriorityFeePerGas(userOp.packed)).to.eventually.equal(userOp.maxPriorityFee);
  132. });
  133. it('returns maxFeePerGas', async function () {
  134. const userOp = new UserOperation({ sender: this.sender, nonce: 1, maxFeePerGas: 0x12345678n });
  135. expect(this.utils.$maxFeePerGas(userOp.packed)).to.eventually.equal(userOp.maxFeePerGas);
  136. });
  137. it('returns gasPrice', async function () {
  138. const userOp = new UserOperation({
  139. sender: this.sender,
  140. nonce: 1,
  141. maxPriorityFee: 0x12345678n,
  142. maxFeePerGas: 0x87654321n,
  143. });
  144. expect(this.utils.$gasPrice(userOp.packed)).to.eventually.equal(userOp.maxPriorityFee);
  145. });
  146. describe('paymasterAndData', function () {
  147. beforeEach(async function () {
  148. this.verificationGasLimit = 0x12345678n;
  149. this.postOpGasLimit = 0x87654321n;
  150. this.paymasterAndData = packPaymasterData(this.paymaster, this.verificationGasLimit, this.postOpGasLimit);
  151. this.userOp = new UserOperation({
  152. sender: this.sender,
  153. nonce: 1,
  154. paymasterAndData: this.paymasterAndData,
  155. });
  156. });
  157. it('returns paymaster', async function () {
  158. expect(this.utils.$paymaster(this.userOp.packed)).to.eventually.equal(this.paymaster);
  159. });
  160. it('returns verificationGasLimit', async function () {
  161. expect(this.utils.$paymasterVerificationGasLimit(this.userOp.packed)).to.eventually.equal(
  162. this.verificationGasLimit,
  163. );
  164. });
  165. it('returns postOpGasLimit', async function () {
  166. expect(this.utils.$paymasterPostOpGasLimit(this.userOp.packed)).to.eventually.equal(this.postOpGasLimit);
  167. });
  168. });
  169. });
  170. });