draft-ERC4337Utils.test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. const { ethers, predeploy } = 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 ADDRESS_ONE = '0x0000000000000000000000000000000000000001';
  7. const fixture = async () => {
  8. const [authorizer, sender, factory, paymaster] = await ethers.getSigners();
  9. const utils = await ethers.deployContract('$ERC4337Utils');
  10. const SIG_VALIDATION_SUCCESS = await utils.$SIG_VALIDATION_SUCCESS();
  11. const SIG_VALIDATION_FAILED = await utils.$SIG_VALIDATION_FAILED();
  12. return { utils, authorizer, sender, factory, paymaster, SIG_VALIDATION_SUCCESS, SIG_VALIDATION_FAILED };
  13. };
  14. describe('ERC4337Utils', function () {
  15. beforeEach(async function () {
  16. Object.assign(this, await loadFixture(fixture));
  17. });
  18. describe('entrypoint', function () {
  19. it('v0.7.0', async function () {
  20. await expect(this.utils.$ENTRYPOINT_V07()).to.eventually.equal(predeploy.entrypoint.v07);
  21. });
  22. it('v0.8.0', async function () {
  23. await expect(this.utils.$ENTRYPOINT_V08()).to.eventually.equal(predeploy.entrypoint.v08);
  24. });
  25. });
  26. describe('parseValidationData', function () {
  27. it('parses the validation data', async function () {
  28. const authorizer = this.authorizer;
  29. const validUntil = 0x12345678n;
  30. const validAfter = 0x9abcdef0n;
  31. const validationData = packValidationData(validAfter, validUntil, authorizer);
  32. await expect(this.utils.$parseValidationData(validationData)).to.eventually.deep.equal([
  33. authorizer.address,
  34. validAfter,
  35. validUntil,
  36. ]);
  37. });
  38. it('returns an type(uint48).max if until is 0', async function () {
  39. const authorizer = this.authorizer;
  40. const validAfter = 0x12345678n;
  41. const validationData = packValidationData(validAfter, 0, authorizer);
  42. await expect(this.utils.$parseValidationData(validationData)).to.eventually.deep.equal([
  43. authorizer.address,
  44. validAfter,
  45. MAX_UINT48,
  46. ]);
  47. });
  48. it('parse canonical values', async function () {
  49. await expect(this.utils.$parseValidationData(this.SIG_VALIDATION_SUCCESS)).to.eventually.deep.equal([
  50. ethers.ZeroAddress,
  51. 0n,
  52. MAX_UINT48,
  53. ]);
  54. await expect(this.utils.$parseValidationData(this.SIG_VALIDATION_FAILED)).to.eventually.deep.equal([
  55. ADDRESS_ONE,
  56. 0n,
  57. MAX_UINT48,
  58. ]);
  59. });
  60. });
  61. describe('packValidationData', function () {
  62. it('packs the validation data', async function () {
  63. const authorizer = this.authorizer;
  64. const validUntil = 0x12345678n;
  65. const validAfter = 0x9abcdef0n;
  66. const validationData = packValidationData(validAfter, validUntil, authorizer);
  67. await expect(
  68. this.utils.$packValidationData(ethers.Typed.address(authorizer), validAfter, validUntil),
  69. ).to.eventually.equal(validationData);
  70. });
  71. it('packs the validation data (bool)', async function () {
  72. const success = false;
  73. const validUntil = 0x12345678n;
  74. const validAfter = 0x9abcdef0n;
  75. const validationData = packValidationData(validAfter, validUntil, false);
  76. await expect(
  77. this.utils.$packValidationData(ethers.Typed.bool(success), validAfter, validUntil),
  78. ).to.eventually.equal(validationData);
  79. });
  80. it('packing reproduced canonical values', async function () {
  81. await expect(
  82. this.utils.$packValidationData(ethers.Typed.address(ethers.ZeroAddress), 0n, 0n),
  83. ).to.eventually.equal(this.SIG_VALIDATION_SUCCESS);
  84. await expect(this.utils.$packValidationData(ethers.Typed.bool(true), 0n, 0n)).to.eventually.equal(
  85. this.SIG_VALIDATION_SUCCESS,
  86. );
  87. await expect(this.utils.$packValidationData(ethers.Typed.address(ADDRESS_ONE), 0n, 0n)).to.eventually.equal(
  88. this.SIG_VALIDATION_FAILED,
  89. );
  90. await expect(this.utils.$packValidationData(ethers.Typed.bool(false), 0n, 0n)).to.eventually.equal(
  91. this.SIG_VALIDATION_FAILED,
  92. );
  93. });
  94. });
  95. describe('combineValidationData', function () {
  96. const validUntil1 = 0x12345678n;
  97. const validAfter1 = 0x9abcdef0n;
  98. const validUntil2 = 0x87654321n;
  99. const validAfter2 = 0xabcdef90n;
  100. it('combines the validation data', async function () {
  101. const validationData1 = packValidationData(validAfter1, validUntil1, ethers.ZeroAddress);
  102. const validationData2 = packValidationData(validAfter2, validUntil2, ethers.ZeroAddress);
  103. const expected = packValidationData(validAfter2, validUntil1, true);
  104. // check symmetry
  105. await expect(this.utils.$combineValidationData(validationData1, validationData2)).to.eventually.equal(expected);
  106. await expect(this.utils.$combineValidationData(validationData2, validationData1)).to.eventually.equal(expected);
  107. });
  108. for (const [authorizer1, authorizer2] of [
  109. [ethers.ZeroAddress, '0xbf023313b891fd6000544b79e353323aa94a4f29'],
  110. ['0xbf023313b891fd6000544b79e353323aa94a4f29', ethers.ZeroAddress],
  111. ]) {
  112. it('returns SIG_VALIDATION_FAILURE if one of the authorizers is not address(0)', async function () {
  113. const validationData1 = packValidationData(validAfter1, validUntil1, authorizer1);
  114. const validationData2 = packValidationData(validAfter2, validUntil2, authorizer2);
  115. const expected = packValidationData(validAfter2, validUntil1, false);
  116. // check symmetry
  117. await expect(this.utils.$combineValidationData(validationData1, validationData2)).to.eventually.equal(expected);
  118. await expect(this.utils.$combineValidationData(validationData2, validationData1)).to.eventually.equal(expected);
  119. });
  120. }
  121. });
  122. describe('getValidationData', function () {
  123. it('returns the validation data with valid validity range', async function () {
  124. const aggregator = this.authorizer;
  125. const validAfter = 0;
  126. const validUntil = MAX_UINT48;
  127. const validationData = packValidationData(validAfter, validUntil, aggregator);
  128. await expect(this.utils.$getValidationData(validationData)).to.eventually.deep.equal([aggregator.address, false]);
  129. });
  130. it('returns the validation data with invalid validity range (expired)', async function () {
  131. const aggregator = this.authorizer;
  132. const validAfter = 0;
  133. const validUntil = 1;
  134. const validationData = packValidationData(validAfter, validUntil, aggregator);
  135. await expect(this.utils.$getValidationData(validationData)).to.eventually.deep.equal([aggregator.address, true]);
  136. });
  137. it('returns the validation data with invalid validity range (not yet valid)', async function () {
  138. const aggregator = this.authorizer;
  139. const validAfter = MAX_UINT48;
  140. const validUntil = MAX_UINT48;
  141. const validationData = packValidationData(validAfter, validUntil, aggregator);
  142. await expect(this.utils.$getValidationData(validationData)).to.eventually.deep.equal([aggregator.address, true]);
  143. });
  144. it('returns address(0) and false for validationData = 0', async function () {
  145. await expect(this.utils.$getValidationData(0n)).to.eventually.deep.equal([ethers.ZeroAddress, false]);
  146. });
  147. });
  148. describe('hash', function () {
  149. for (const [version, instance] of Object.entries(predeploy.entrypoint)) {
  150. it(`returns the operation hash for entrypoint ${version}`, async function () {
  151. const userOp = new UserOperation({ sender: this.sender, nonce: 1 });
  152. const expected = await userOp.hash(instance);
  153. await expect(this.utils.$hash(userOp.packed, instance)).to.eventually.equal(expected);
  154. });
  155. }
  156. });
  157. describe('userOp values', function () {
  158. describe('intiCode', function () {
  159. beforeEach(async function () {
  160. this.userOp = new UserOperation({
  161. sender: this.sender,
  162. nonce: 1,
  163. verificationGas: 0x12345678n,
  164. factory: this.factory,
  165. factoryData: '0x123456',
  166. });
  167. this.emptyUserOp = new UserOperation({
  168. sender: this.sender,
  169. nonce: 1,
  170. });
  171. });
  172. it('returns factory', async function () {
  173. await expect(this.utils.$factory(this.userOp.packed)).to.eventually.equal(this.factory);
  174. await expect(this.utils.$factory(this.emptyUserOp.packed)).to.eventually.equal(ethers.ZeroAddress);
  175. });
  176. it('returns factoryData', async function () {
  177. await expect(this.utils.$factoryData(this.userOp.packed)).to.eventually.equal('0x123456');
  178. await expect(this.utils.$factoryData(this.emptyUserOp.packed)).to.eventually.equal('0x');
  179. });
  180. });
  181. it('returns verificationGasLimit', async function () {
  182. const userOp = new UserOperation({ sender: this.sender, nonce: 1, verificationGas: 0x12345678n });
  183. await expect(this.utils.$verificationGasLimit(userOp.packed)).to.eventually.equal(userOp.verificationGas);
  184. });
  185. it('returns callGasLimit', async function () {
  186. const userOp = new UserOperation({ sender: this.sender, nonce: 1, callGas: 0x12345678n });
  187. await expect(this.utils.$callGasLimit(userOp.packed)).to.eventually.equal(userOp.callGas);
  188. });
  189. it('returns maxPriorityFeePerGas', async function () {
  190. const userOp = new UserOperation({ sender: this.sender, nonce: 1, maxPriorityFee: 0x12345678n });
  191. await expect(this.utils.$maxPriorityFeePerGas(userOp.packed)).to.eventually.equal(userOp.maxPriorityFee);
  192. });
  193. it('returns maxFeePerGas', async function () {
  194. const userOp = new UserOperation({ sender: this.sender, nonce: 1, maxFeePerGas: 0x12345678n });
  195. await expect(this.utils.$maxFeePerGas(userOp.packed)).to.eventually.equal(userOp.maxFeePerGas);
  196. });
  197. it('returns gasPrice', async function () {
  198. const userOp = new UserOperation({
  199. sender: this.sender,
  200. nonce: 1,
  201. maxPriorityFee: 0x12345678n,
  202. maxFeePerGas: 0x87654321n,
  203. });
  204. await expect(this.utils.$gasPrice(userOp.packed)).to.eventually.equal(userOp.maxPriorityFee);
  205. });
  206. describe('paymasterAndData', function () {
  207. beforeEach(async function () {
  208. this.userOp = new UserOperation({
  209. sender: this.sender,
  210. nonce: 1,
  211. paymaster: this.paymaster,
  212. paymasterVerificationGasLimit: 0x12345678n,
  213. paymasterPostOpGasLimit: 0x87654321n,
  214. paymasterData: '0xbeefcafe',
  215. });
  216. this.emptyUserOp = new UserOperation({
  217. sender: this.sender,
  218. nonce: 1,
  219. });
  220. });
  221. it('returns paymaster', async function () {
  222. await expect(this.utils.$paymaster(this.userOp.packed)).to.eventually.equal(this.userOp.paymaster);
  223. await expect(this.utils.$paymaster(this.emptyUserOp.packed)).to.eventually.equal(ethers.ZeroAddress);
  224. });
  225. it('returns verificationGasLimit', async function () {
  226. await expect(this.utils.$paymasterVerificationGasLimit(this.userOp.packed)).to.eventually.equal(
  227. this.userOp.paymasterVerificationGasLimit,
  228. );
  229. await expect(this.utils.$paymasterVerificationGasLimit(this.emptyUserOp.packed)).to.eventually.equal(0n);
  230. });
  231. it('returns postOpGasLimit', async function () {
  232. await expect(this.utils.$paymasterPostOpGasLimit(this.userOp.packed)).to.eventually.equal(
  233. this.userOp.paymasterPostOpGasLimit,
  234. );
  235. await expect(this.utils.$paymasterPostOpGasLimit(this.emptyUserOp.packed)).to.eventually.equal(0n);
  236. });
  237. it('returns data', async function () {
  238. await expect(this.utils.$paymasterData(this.userOp.packed)).to.eventually.equal(this.userOp.paymasterData);
  239. await expect(this.utils.$paymasterData(this.emptyUserOp.packed)).to.eventually.equal('0x');
  240. });
  241. });
  242. });
  243. });