draft-ERC4337Utils.test.js 11 KB

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