draft-ERC4337Utils.test.js 12 KB

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