draft-ERC4337Utils.test.js 12 KB

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