瀏覽代碼

Add signer constructors (#5757)

Co-authored-by: ernestognw <ernestognw@gmail.com>
Hadrien Croubois 3 月之前
父節點
當前提交
6079eb3f01

+ 5 - 0
.changeset/all-geese-stand.md

@@ -0,0 +1,5 @@
+---
+'openzeppelin-solidity': patch
+---
+
+Add constructors to the different signers.

+ 0 - 27
contracts/mocks/account/AccountMock.sol

@@ -38,10 +38,6 @@ abstract contract AccountMock is Account, ERC7739, ERC7821, ERC721Holder, ERC115
 }
 
 abstract contract AccountECDSAMock is Account, SignerECDSA, ERC7739, ERC7821, ERC721Holder, ERC1155Holder {
-    constructor(address signerAddr) {
-        _setSigner(signerAddr);
-    }
-
     /// @inheritdoc ERC7821
     function _erc7821AuthorizedExecutor(
         address caller,
@@ -53,10 +49,6 @@ abstract contract AccountECDSAMock is Account, SignerECDSA, ERC7739, ERC7821, ER
 }
 
 abstract contract AccountP256Mock is Account, SignerP256, ERC7739, ERC7821, ERC721Holder, ERC1155Holder {
-    constructor(bytes32 qx, bytes32 qy) {
-        _setSigner(qx, qy);
-    }
-
     /// @inheritdoc ERC7821
     function _erc7821AuthorizedExecutor(
         address caller,
@@ -68,10 +60,6 @@ abstract contract AccountP256Mock is Account, SignerP256, ERC7739, ERC7821, ERC7
 }
 
 abstract contract AccountRSAMock is Account, SignerRSA, ERC7739, ERC7821, ERC721Holder, ERC1155Holder {
-    constructor(bytes memory e, bytes memory n) {
-        _setSigner(e, n);
-    }
-
     /// @inheritdoc ERC7821
     function _erc7821AuthorizedExecutor(
         address caller,
@@ -141,10 +129,6 @@ abstract contract AccountERC7579HookedMock is AccountERC7579Hooked {
 }
 
 abstract contract AccountERC7913Mock is Account, SignerERC7913, ERC7739, ERC7821, ERC721Holder, ERC1155Holder {
-    constructor(bytes memory _signer) {
-        _setSigner(_signer);
-    }
-
     /// @inheritdoc ERC7821
     function _erc7821AuthorizedExecutor(
         address caller,
@@ -156,11 +140,6 @@ abstract contract AccountERC7913Mock is Account, SignerERC7913, ERC7739, ERC7821
 }
 
 abstract contract AccountMultiSignerMock is Account, MultiSignerERC7913, ERC7739, ERC7821, ERC721Holder, ERC1155Holder {
-    constructor(bytes[] memory signers, uint64 threshold) {
-        _addSigners(signers);
-        _setThreshold(threshold);
-    }
-
     /// @inheritdoc ERC7821
     function _erc7821AuthorizedExecutor(
         address caller,
@@ -179,12 +158,6 @@ abstract contract AccountMultiSignerWeightedMock is
     ERC721Holder,
     ERC1155Holder
 {
-    constructor(bytes[] memory signers, uint64[] memory weights, uint64 threshold) {
-        _addSigners(signers);
-        _setSignerWeights(signers, weights);
-        _setThreshold(threshold);
-    }
-
     /// @inheritdoc ERC7821
     function _erc7821AuthorizedExecutor(
         address caller,

+ 3 - 18
contracts/mocks/utils/cryptography/ERC7739Mock.sol

@@ -3,26 +3,11 @@
 pragma solidity ^0.8.20;
 
 import {ECDSA} from "../../../utils/cryptography/ECDSA.sol";
-import {EIP712} from "../../../utils/cryptography/EIP712.sol";
 import {ERC7739} from "../../../utils/cryptography/signers/draft-ERC7739.sol";
 import {SignerECDSA} from "../../../utils/cryptography/signers/SignerECDSA.sol";
 import {SignerP256} from "../../../utils/cryptography/signers/SignerP256.sol";
 import {SignerRSA} from "../../../utils/cryptography/signers/SignerRSA.sol";
 
-contract ERC7739ECDSAMock is ERC7739, SignerECDSA {
-    constructor(address signerAddr) EIP712("ERC7739ECDSA", "1") {
-        _setSigner(signerAddr);
-    }
-}
-
-contract ERC7739P256Mock is ERC7739, SignerP256 {
-    constructor(bytes32 qx, bytes32 qy) EIP712("ERC7739P256", "1") {
-        _setSigner(qx, qy);
-    }
-}
-
-contract ERC7739RSAMock is ERC7739, SignerRSA {
-    constructor(bytes memory e, bytes memory n) EIP712("ERC7739RSA", "1") {
-        _setSigner(e, n);
-    }
-}
+abstract contract ERC7739ECDSAMock is ERC7739, SignerECDSA {}
+abstract contract ERC7739P256Mock is ERC7739, SignerP256 {}
+abstract contract ERC7739RSAMock is ERC7739, SignerRSA {}

+ 5 - 0
contracts/utils/cryptography/signers/MultiSignerERC7913.sol

@@ -69,6 +69,11 @@ abstract contract MultiSignerERC7913 is AbstractSigner {
     /// @dev The `threshold` is unreachable given the number of `signers`.
     error MultiSignerERC7913UnreachableThreshold(uint64 signers, uint64 threshold);
 
+    constructor(bytes[] memory signers_, uint64 threshold_) {
+        _addSigners(signers_);
+        _setThreshold(threshold_);
+    }
+
     /**
      * @dev Returns a slice of the set of authorized signers.
      *

+ 5 - 0
contracts/utils/cryptography/signers/MultiSignerERC7913Weighted.sol

@@ -67,6 +67,11 @@ abstract contract MultiSignerERC7913Weighted is MultiSignerERC7913 {
     /// @dev Thrown when the arrays lengths don't match. See {_setSignerWeights}.
     error MultiSignerERC7913WeightedMismatchedLength();
 
+    constructor(bytes[] memory signers_, uint64[] memory weights_, uint64 threshold_) MultiSignerERC7913(signers_, 1) {
+        _setSignerWeights(signers_, weights_);
+        _setThreshold(threshold_);
+    }
+
     /// @dev Gets the weight of a signer. Returns 0 if the signer is not authorized.
     function signerWeight(bytes memory signer) public view virtual returns (uint64) {
         unchecked {

+ 4 - 0
contracts/utils/cryptography/signers/SignerECDSA.sol

@@ -27,6 +27,10 @@ import {ECDSA} from "../ECDSA.sol";
 abstract contract SignerECDSA is AbstractSigner {
     address private _signer;
 
+    constructor(address signerAddr) {
+        _setSigner(signerAddr);
+    }
+
     /**
      * @dev Sets the signer with the address of the native signer. This function should be called during construction
      * or through an initializer.

+ 4 - 0
contracts/utils/cryptography/signers/SignerERC7913.sol

@@ -31,6 +31,10 @@ import {SignatureChecker} from "../SignatureChecker.sol";
 abstract contract SignerERC7913 is AbstractSigner {
     bytes private _signer;
 
+    constructor(bytes memory signer_) {
+        _setSigner(signer_);
+    }
+
     /// @dev Return the ERC-7913 signer (i.e. `verifier || key`).
     function signer() public view virtual returns (bytes memory) {
         return _signer;

+ 4 - 0
contracts/utils/cryptography/signers/SignerP256.sol

@@ -30,6 +30,10 @@ abstract contract SignerP256 is AbstractSigner {
 
     error SignerP256InvalidPublicKey(bytes32 qx, bytes32 qy);
 
+    constructor(bytes32 qx, bytes32 qy) {
+        _setSigner(qx, qy);
+    }
+
     /**
      * @dev Sets the signer with a P256 public key. This function should be called during construction
      * or through an initializer.

+ 4 - 0
contracts/utils/cryptography/signers/SignerRSA.sol

@@ -28,6 +28,10 @@ abstract contract SignerRSA is AbstractSigner {
     bytes private _e;
     bytes private _n;
 
+    constructor(bytes memory e, bytes memory n) {
+        _setSigner(e, n);
+    }
+
     /**
      * @dev Sets the signer with a RSA public key. This function should be called during construction
      * or through an initializer.

+ 1 - 1
test/account/AccountECDSA.test.js

@@ -19,7 +19,7 @@ async function fixture() {
 
   // ERC-4337 account
   const helper = new ERC4337Helper();
-  const mock = await helper.newAccount('$AccountECDSAMock', ['AccountECDSA', '1', signer]);
+  const mock = await helper.newAccount('$AccountECDSAMock', [signer, 'AccountECDSA', '1']);
 
   // ERC-4337 Entrypoint domain
   const entrypointDomain = await getDomain(entrypoint.v08);

+ 1 - 1
test/account/AccountERC7913.test.js

@@ -32,7 +32,7 @@ async function fixture() {
   const domain = { name: 'AccountERC7913', version: '1', chainId: entrypointDomain.chainId }; // Missing verifyingContract,
 
   const makeMock = signer =>
-    helper.newAccount('$AccountERC7913Mock', ['AccountERC7913', '1', signer]).then(mock => {
+    helper.newAccount('$AccountERC7913Mock', [signer, 'AccountERC7913', '1']).then(mock => {
       domain.verifyingContract = mock.address;
       return mock;
     });

+ 1 - 1
test/account/AccountMultiSigner.test.js

@@ -37,7 +37,7 @@ async function fixture() {
   const domain = { name: 'AccountMultiSigner', version: '1', chainId: entrypointDomain.chainId }; // Missing verifyingContract
 
   const makeMock = (signers, threshold) =>
-    helper.newAccount('$AccountMultiSignerMock', ['AccountMultiSigner', '1', signers, threshold]).then(mock => {
+    helper.newAccount('$AccountMultiSignerMock', [signers, threshold, 'AccountMultiSigner', '1']).then(mock => {
       domain.verifyingContract = mock.address;
       return mock;
     });

+ 1 - 1
test/account/AccountMultiSignerWeighted.test.js

@@ -37,7 +37,7 @@ async function fixture() {
 
   const makeMock = (signers, weights, threshold) =>
     helper
-      .newAccount('$AccountMultiSignerWeightedMock', ['AccountMultiSignerWeighted', '1', signers, weights, threshold])
+      .newAccount('$AccountMultiSignerWeightedMock', [signers, weights, threshold, 'AccountMultiSignerWeighted', '1'])
       .then(mock => {
         domain.verifyingContract = mock.address;
         return mock;

+ 2 - 2
test/account/AccountP256.test.js

@@ -21,10 +21,10 @@ async function fixture() {
   // ERC-4337 account
   const helper = new ERC4337Helper();
   const mock = await helper.newAccount('$AccountP256Mock', [
-    'AccountP256',
-    '1',
     signer.signingKey.publicKey.qx,
     signer.signingKey.publicKey.qy,
+    'AccountP256',
+    '1',
   ]);
 
   // ERC-4337 Entrypoint domain

+ 2 - 2
test/account/AccountRSA.test.js

@@ -21,10 +21,10 @@ async function fixture() {
   // ERC-4337 account
   const helper = new ERC4337Helper();
   const mock = await helper.newAccount('$AccountRSAMock', [
-    'AccountRSA',
-    '1',
     signer.signingKey.publicKey.e,
     signer.signingKey.publicKey.n,
+    'AccountRSA',
+    '1',
   ]);
 
   // ERC-4337 Entrypoint domain

+ 7 - 3
test/utils/cryptography/ERC7739.test.js

@@ -6,7 +6,7 @@ describe('ERC7739', function () {
   describe('for an ECDSA signer', function () {
     before(async function () {
       this.signer = ethers.Wallet.createRandom();
-      this.mock = await ethers.deployContract('ERC7739ECDSAMock', [this.signer.address]);
+      this.mock = await ethers.deployContract('$ERC7739ECDSAMock', ['ERC7739ECDSA', '1', this.signer.address]);
     });
 
     shouldBehaveLikeERC1271({ erc7739: true });
@@ -15,7 +15,9 @@ describe('ERC7739', function () {
   describe('for a P256 signer', function () {
     before(async function () {
       this.signer = new NonNativeSigner(P256SigningKey.random());
-      this.mock = await ethers.deployContract('ERC7739P256Mock', [
+      this.mock = await ethers.deployContract('$ERC7739P256Mock', [
+        'ERC7739P256',
+        '1',
         this.signer.signingKey.publicKey.qx,
         this.signer.signingKey.publicKey.qy,
       ]);
@@ -27,7 +29,9 @@ describe('ERC7739', function () {
   describe('for an RSA signer', function () {
     before(async function () {
       this.signer = new NonNativeSigner(RSASHA256SigningKey.random());
-      this.mock = await ethers.deployContract('ERC7739RSAMock', [
+      this.mock = await ethers.deployContract('$ERC7739RSAMock', [
+        'ERC7739RSA',
+        '1',
         this.signer.signingKey.publicKey.e,
         this.signer.signingKey.publicKey.n,
       ]);