AccountMultiSignerWeighted.test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. const { ethers, entrypoint } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. const { getDomain } = require('../helpers/eip712');
  5. const { ERC4337Helper } = require('../helpers/erc4337');
  6. const { NonNativeSigner, P256SigningKey, RSASHA256SigningKey, MultiERC7913SigningKey } = require('../helpers/signers');
  7. const { PackedUserOperation } = require('../helpers/eip712-types');
  8. const { shouldBehaveLikeAccountCore, shouldBehaveLikeAccountHolder } = require('./Account.behavior');
  9. const { shouldBehaveLikeERC1271 } = require('../utils/cryptography/ERC1271.behavior');
  10. const { shouldBehaveLikeERC7821 } = require('./extensions/ERC7821.behavior');
  11. // Prepare signers in advance (RSA are long to initialize)
  12. const signerECDSA1 = ethers.Wallet.createRandom();
  13. const signerECDSA2 = ethers.Wallet.createRandom();
  14. const signerECDSA3 = ethers.Wallet.createRandom();
  15. const signerECDSA4 = ethers.Wallet.createRandom();
  16. const signerP256 = new NonNativeSigner(P256SigningKey.random());
  17. const signerRSA = new NonNativeSigner(RSASHA256SigningKey.random());
  18. // Minimal fixture common to the different signer verifiers
  19. async function fixture() {
  20. // EOAs and environment
  21. const [beneficiary, other] = await ethers.getSigners();
  22. const target = await ethers.deployContract('CallReceiverMock');
  23. // ERC-7913 verifiers
  24. const verifierP256 = await ethers.deployContract('ERC7913P256Verifier');
  25. const verifierRSA = await ethers.deployContract('ERC7913RSAVerifier');
  26. // ERC-4337 env
  27. const helper = new ERC4337Helper();
  28. await helper.wait();
  29. const entrypointDomain = await getDomain(entrypoint.v08);
  30. const domain = { name: 'AccountMultiSignerWeighted', version: '1', chainId: entrypointDomain.chainId }; // Missing verifyingContract
  31. const makeMock = (signers, weights, threshold) =>
  32. helper
  33. .newAccount('$AccountMultiSignerWeightedMock', [signers, weights, threshold, 'AccountMultiSignerWeighted', '1'])
  34. .then(mock => {
  35. domain.verifyingContract = mock.address;
  36. return mock;
  37. });
  38. // Sign user operations using NonNativeSigner with MultiERC7913SigningKey
  39. const signUserOp = function (userOp) {
  40. return this.signer
  41. .signTypedData(entrypointDomain, { PackedUserOperation }, userOp.packed)
  42. .then(signature => Object.assign(userOp, { signature }));
  43. };
  44. const invalidSig = function () {
  45. return this.signer.signMessage('invalid');
  46. };
  47. return {
  48. helper,
  49. verifierP256,
  50. verifierRSA,
  51. domain,
  52. target,
  53. beneficiary,
  54. other,
  55. makeMock,
  56. signUserOp,
  57. invalidSig,
  58. };
  59. }
  60. describe('AccountMultiSignerWeighted', function () {
  61. beforeEach(async function () {
  62. Object.assign(this, await loadFixture(fixture));
  63. });
  64. describe('Weighted signers with equal weights (1, 1, 1) and threshold=2', function () {
  65. beforeEach(async function () {
  66. this.signer = new NonNativeSigner(new MultiERC7913SigningKey([signerECDSA1, signerECDSA3])); // 2 accounts, weight 1+1=2
  67. this.mock = await this.makeMock([signerECDSA1.address, signerECDSA2.address, signerECDSA3.address], [1, 1, 1], 2);
  68. });
  69. shouldBehaveLikeAccountCore();
  70. shouldBehaveLikeAccountHolder();
  71. shouldBehaveLikeERC1271({ erc7739: true });
  72. shouldBehaveLikeERC7821();
  73. });
  74. describe('Weighted signers with varying weights (1, 2, 3) and threshold=3', function () {
  75. beforeEach(async function () {
  76. this.signer = new NonNativeSigner(new MultiERC7913SigningKey([signerECDSA1, signerECDSA2])); // 2 accounts, weight 1+2=3
  77. this.mock = await this.makeMock([signerECDSA1.address, signerECDSA2.address, signerECDSA3.address], [1, 2, 3], 3);
  78. });
  79. shouldBehaveLikeAccountCore();
  80. shouldBehaveLikeAccountHolder();
  81. shouldBehaveLikeERC1271({ erc7739: true });
  82. shouldBehaveLikeERC7821();
  83. });
  84. describe('Mixed weighted signers with threshold=4', function () {
  85. beforeEach(async function () {
  86. // Create signers array with all three types
  87. signerP256.bytes = ethers.concat([
  88. this.verifierP256.target,
  89. signerP256.signingKey.publicKey.qx,
  90. signerP256.signingKey.publicKey.qy,
  91. ]);
  92. signerRSA.bytes = ethers.concat([
  93. this.verifierRSA.target,
  94. ethers.AbiCoder.defaultAbiCoder().encode(
  95. ['bytes', 'bytes'],
  96. [signerRSA.signingKey.publicKey.e, signerRSA.signingKey.publicKey.n],
  97. ),
  98. ]);
  99. this.signer = new NonNativeSigner(new MultiERC7913SigningKey([signerP256, signerRSA])); // 2 accounts, weight 2+3=5
  100. this.mock = await this.makeMock(
  101. [signerECDSA1.address, signerP256.bytes, signerRSA.bytes],
  102. [1, 2, 3],
  103. 4, // Requires at least signer2 + signer3, or all three signers
  104. );
  105. });
  106. shouldBehaveLikeAccountCore();
  107. shouldBehaveLikeAccountHolder();
  108. shouldBehaveLikeERC1271({ erc7739: true });
  109. shouldBehaveLikeERC7821();
  110. });
  111. describe('Weight management', function () {
  112. const signer1 = signerECDSA1.address;
  113. const signer2 = signerECDSA2.address;
  114. const signer3 = signerECDSA3.address;
  115. const signer4 = signerECDSA4.address;
  116. beforeEach(async function () {
  117. this.mock = await this.makeMock([signer1, signer2, signer3], [1, 2, 3], 4);
  118. await this.mock.deploy();
  119. });
  120. it('can get signer weights', async function () {
  121. await expect(this.mock.signerWeight(signer1)).to.eventually.equal(1);
  122. await expect(this.mock.signerWeight(signer2)).to.eventually.equal(2);
  123. await expect(this.mock.signerWeight(signer3)).to.eventually.equal(3);
  124. });
  125. it('can update signer weights', async function () {
  126. // Successfully updates weights and emits event
  127. await expect(this.mock.$_setSignerWeights([signer1, signer2], [5, 6]))
  128. .to.emit(this.mock, 'ERC7913SignerWeightChanged')
  129. .withArgs(signer1, 5)
  130. .to.emit(this.mock, 'ERC7913SignerWeightChanged')
  131. .withArgs(signer2, 6);
  132. await expect(this.mock.signerWeight(signer1)).to.eventually.equal(5);
  133. await expect(this.mock.signerWeight(signer2)).to.eventually.equal(6);
  134. await expect(this.mock.signerWeight(signer3)).to.eventually.equal(3); // unchanged
  135. });
  136. it('cannot set weight to non-existent signer', async function () {
  137. // Reverts when setting weight for non-existent signer
  138. await expect(this.mock.$_setSignerWeights([signer4], [1]))
  139. .to.be.revertedWithCustomError(this.mock, 'MultiSignerERC7913NonexistentSigner')
  140. .withArgs(signer4.toLowerCase());
  141. });
  142. it('cannot set weight to 0', async function () {
  143. // Reverts when setting weight to 0
  144. await expect(this.mock.$_setSignerWeights([signer1], [0]))
  145. .to.be.revertedWithCustomError(this.mock, 'MultiSignerERC7913WeightedInvalidWeight')
  146. .withArgs(signer1.toLowerCase(), 0);
  147. });
  148. it('requires signers and weights arrays to have same length', async function () {
  149. // Reverts when arrays have different lengths
  150. await expect(this.mock.$_setSignerWeights([signer1, signer2], [1])).to.be.revertedWithCustomError(
  151. this.mock,
  152. 'MultiSignerERC7913WeightedMismatchedLength',
  153. );
  154. await expect(this.mock.$_setSignerWeights([signer1], [1, 2])).to.be.revertedWithCustomError(
  155. this.mock,
  156. 'MultiSignerERC7913WeightedMismatchedLength',
  157. );
  158. });
  159. it('validates threshold is reachable when updating weights', async function () {
  160. // First, lower the weights so the sum is exactly 6 (just enough for threshold=6)
  161. await expect(this.mock.$_setSignerWeights([signer1, signer2, signer3], [1, 2, 3]))
  162. .to.emit(this.mock, 'ERC7913SignerWeightChanged')
  163. .withArgs(signer1, 1)
  164. .to.emit(this.mock, 'ERC7913SignerWeightChanged')
  165. .withArgs(signer2, 2)
  166. .to.emit(this.mock, 'ERC7913SignerWeightChanged')
  167. .withArgs(signer3, 3);
  168. // Increase threshold to 6
  169. await expect(this.mock.$_setThreshold(6)).to.emit(this.mock, 'ERC7913ThresholdSet').withArgs(6);
  170. // Now try to lower weights so their sum is less than the threshold
  171. await expect(this.mock.$_setSignerWeights([signer1, signer2, signer3], [1, 1, 1])).to.be.revertedWithCustomError(
  172. this.mock,
  173. 'MultiSignerERC7913UnreachableThreshold',
  174. );
  175. // Try to increase threshold to be larger than the total weight
  176. await expect(this.mock.$_setThreshold(7))
  177. .to.be.revertedWithCustomError(this.mock, 'MultiSignerERC7913UnreachableThreshold')
  178. .withArgs(6, 7);
  179. });
  180. it('reports default weight of 1 for signers without explicit weight', async function () {
  181. // Add a new signer without setting weight
  182. await this.mock.$_addSigners([signer4]);
  183. // Should have default weight of 1
  184. await expect(this.mock.signerWeight(signer4)).to.eventually.equal(1);
  185. });
  186. it('reports weight of 0 for invalid signers', async function () {
  187. // not authorized
  188. await expect(this.mock.signerWeight(signer4)).to.eventually.equal(0);
  189. });
  190. it('can get total weight of all signers', async function () {
  191. await expect(this.mock.totalWeight()).to.eventually.equal(6); // 1+2+3=6
  192. });
  193. it('totalWeight returns correct value when all signers have default weight of 1', async function () {
  194. // Deploy a new mock with all signers having default weight (1)
  195. const signers = [signerECDSA1.address, signerECDSA2.address, signerECDSA3.address];
  196. const defaultWeights = [1, 1, 1]; // All weights are 1 (default)
  197. const newMock = await this.makeMock(signers, defaultWeights, 2);
  198. await newMock.deploy();
  199. // totalWeight should return max(3, 3) = 3 when all weights are default
  200. await expect(newMock.totalWeight()).to.eventually.equal(3);
  201. // Clear custom weights to ensure we're using default weights
  202. await newMock.$_setSignerWeights(signers, [1, 1, 1]);
  203. // totalWeight should still be max(3, 3) = 3
  204. await expect(newMock.totalWeight()).to.eventually.equal(3);
  205. });
  206. it('_setSignerWeights correctly handles default weights when updating', async function () {
  207. await expect(this.mock.totalWeight()).to.eventually.equal(6); // 1+2+3=6
  208. // Set weight for signer1 from 1 (default) to 5
  209. await this.mock.$_setSignerWeights([signer1], [5]);
  210. await expect(this.mock.totalWeight()).to.eventually.equal(10); // 5+2+3=10
  211. // Reset signer1 to default weight (1)
  212. await this.mock.$_setSignerWeights([signer1], [1]);
  213. await expect(this.mock.totalWeight()).to.eventually.equal(6); // 1+2+3=6
  214. });
  215. it('updates total weight when adding and removing signers', async function () {
  216. await expect(this.mock.totalWeight()).to.eventually.equal(6); // 1+2+3=6
  217. // Add a new signer - should increase total weight by default weight (1)
  218. await this.mock.$_addSigners([signer4]);
  219. await expect(this.mock.totalWeight()).to.eventually.equal(7); // 1+2+3+1=7
  220. // Set weight to 5 - should increase total weight by 4
  221. await this.mock.$_setSignerWeights([signer4], [5]);
  222. await expect(this.mock.totalWeight()).to.eventually.equal(11); // 1+2+3+5=11
  223. // Remove signer - should decrease total weight by current weight (5)
  224. await this.mock.$_removeSigners([signer4]);
  225. await expect(this.mock.totalWeight()).to.eventually.equal(6); // 1+2+3=6
  226. });
  227. it('removing signers should not make threshold unreachable', async function () {
  228. // current threshold = 4, totalWeight = 1+2+3 = 6
  229. // After removing signer3, the threshold is unreachable because totalWeight = 1+2 = 3 but threshold = 4
  230. // [reverts]
  231. await expect(this.mock.$_removeSigners([signer3]))
  232. .to.be.revertedWithCustomError(this.mock, 'MultiSignerERC7913UnreachableThreshold')
  233. .withArgs(3, 4);
  234. // After removing signer1, the threshold is still reachable because totalWeight = 2+3 = 5 and threshold = 4
  235. // [does not revert]
  236. await expect(this.mock.$_removeSigners([signer1]))
  237. .to.emit(this.mock, 'ERC7913SignerRemoved')
  238. .withArgs(signer1)
  239. .to.not.emit(this.mock, 'ERC7913SignerWeightChanged');
  240. });
  241. });
  242. });