draft-ERC4337Utils.test.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. const { packValidationData, UserOperation } = require('../../helpers/erc4337');
  5. const { MAX_UINT48 } = require('../../helpers/constants');
  6. const fixture = async () => {
  7. const [authorizer, sender, entrypoint, factory, paymaster] = await ethers.getSigners();
  8. const utils = await ethers.deployContract('$ERC4337Utils');
  9. return { utils, authorizer, sender, entrypoint, factory, 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. describe('intiCode', function () {
  122. beforeEach(async function () {
  123. this.userOp = new UserOperation({
  124. sender: this.sender,
  125. nonce: 1,
  126. verificationGas: 0x12345678n,
  127. factory: this.factory,
  128. factoryData: '0x123456',
  129. });
  130. this.emptyUserOp = new UserOperation({
  131. sender: this.sender,
  132. nonce: 1,
  133. });
  134. });
  135. it('returns factory', async function () {
  136. expect(this.utils.$factory(this.userOp.packed)).to.eventually.equal(this.factory);
  137. expect(this.utils.$factory(this.emptyUserOp.packed)).to.eventually.equal(ethers.ZeroAddress);
  138. });
  139. it('returns factoryData', async function () {
  140. expect(this.utils.$factoryData(this.userOp.packed)).to.eventually.equal('0x123456');
  141. expect(this.utils.$factoryData(this.emptyUserOp.packed)).to.eventually.equal('0x');
  142. });
  143. });
  144. it('returns verificationGasLimit', async function () {
  145. const userOp = new UserOperation({ sender: this.sender, nonce: 1, verificationGas: 0x12345678n });
  146. expect(this.utils.$verificationGasLimit(userOp.packed)).to.eventually.equal(userOp.verificationGas);
  147. });
  148. it('returns callGasLimit', async function () {
  149. const userOp = new UserOperation({ sender: this.sender, nonce: 1, callGas: 0x12345678n });
  150. expect(this.utils.$callGasLimit(userOp.packed)).to.eventually.equal(userOp.callGas);
  151. });
  152. it('returns maxPriorityFeePerGas', async function () {
  153. const userOp = new UserOperation({ sender: this.sender, nonce: 1, maxPriorityFee: 0x12345678n });
  154. expect(this.utils.$maxPriorityFeePerGas(userOp.packed)).to.eventually.equal(userOp.maxPriorityFee);
  155. });
  156. it('returns maxFeePerGas', async function () {
  157. const userOp = new UserOperation({ sender: this.sender, nonce: 1, maxFeePerGas: 0x12345678n });
  158. expect(this.utils.$maxFeePerGas(userOp.packed)).to.eventually.equal(userOp.maxFeePerGas);
  159. });
  160. it('returns gasPrice', async function () {
  161. const userOp = new UserOperation({
  162. sender: this.sender,
  163. nonce: 1,
  164. maxPriorityFee: 0x12345678n,
  165. maxFeePerGas: 0x87654321n,
  166. });
  167. expect(this.utils.$gasPrice(userOp.packed)).to.eventually.equal(userOp.maxPriorityFee);
  168. });
  169. describe('paymasterAndData', function () {
  170. beforeEach(async function () {
  171. this.userOp = new UserOperation({
  172. sender: this.sender,
  173. nonce: 1,
  174. paymaster: this.paymaster,
  175. paymasterVerificationGasLimit: 0x12345678n,
  176. paymasterPostOpGasLimit: 0x87654321n,
  177. paymasterData: '0xbeefcafe',
  178. });
  179. this.emptyUserOp = new UserOperation({
  180. sender: this.sender,
  181. nonce: 1,
  182. });
  183. });
  184. it('returns paymaster', async function () {
  185. expect(this.utils.$paymaster(this.userOp.packed)).to.eventually.equal(this.userOp.paymaster);
  186. expect(this.utils.$paymaster(this.emptyUserOp.packed)).to.eventually.equal(ethers.ZeroAddress);
  187. });
  188. it('returns verificationGasLimit', async function () {
  189. expect(this.utils.$paymasterVerificationGasLimit(this.userOp.packed)).to.eventually.equal(
  190. this.userOp.paymasterVerificationGasLimit,
  191. );
  192. expect(this.utils.$paymasterVerificationGasLimit(this.emptyUserOp.packed)).to.eventually.equal(0n);
  193. });
  194. it('returns postOpGasLimit', async function () {
  195. expect(this.utils.$paymasterPostOpGasLimit(this.userOp.packed)).to.eventually.equal(
  196. this.userOp.paymasterPostOpGasLimit,
  197. );
  198. expect(this.utils.$paymasterPostOpGasLimit(this.emptyUserOp.packed)).to.eventually.equal(0n);
  199. });
  200. it('returns data', async function () {
  201. expect(this.utils.$paymasterData(this.userOp.packed)).to.eventually.equal(this.userOp.paymasterData);
  202. expect(this.utils.$paymasterData(this.emptyUserOp.packed)).to.eventually.equal('0x');
  203. });
  204. });
  205. });
  206. });