diff --git a/test/factory/CustomSmartWalletFactory.test.ts b/test/factory/customSmartWalletFactory.test.ts similarity index 81% rename from test/factory/CustomSmartWalletFactory.test.ts rename to test/factory/customSmartWalletFactory.test.ts index 4e4482a7..38cca468 100644 --- a/test/factory/CustomSmartWalletFactory.test.ts +++ b/test/factory/customSmartWalletFactory.test.ts @@ -8,27 +8,20 @@ import { UtilToken, } from 'typechain-types'; import { deployContract } from '../../utils/deployment/deployment.utils'; -import seedrandom from 'seedrandom'; import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers'; -import { - getLocalEip712DeploySignature, - TypedDeployRequestData, -} from '../utils/EIP712Utils'; -import { getSuffixData, HARDHAT_CHAIN_ID } from '../smartwallet/utils'; -import { createDeployRequest } from './utils'; - -const random = seedrandom('rif'); -const minIndex = 0; -const maxIndex = 1000000000; - -const nextIndex = () => - Math.floor(random() * (maxIndex - minIndex + 1) + minIndex); +import { createDeployRequest, randomNumber, signDeployRequest } from './utils'; type CustomSmartWalletFactoryOptions = Parameters< CustomSmartWalletFactory__factory['deploy'] >; describe('CustomSmartWalletFactory', function () { + let chainId: number; + + before(async function () { + ({ chainId } = await ethers.provider.getNetwork()); + }); + describe('constructor', function () { let customSmartWalletFactory: CustomSmartWalletFactory; let template: Wallet; @@ -76,7 +69,7 @@ describe('CustomSmartWalletFactory', function () { beforeEach(function () { recoverer = constants.AddressZero; - index = nextIndex(); + index = randomNumber(); logicAddress = constants.AddressZero; }); @@ -215,7 +208,7 @@ describe('CustomSmartWalletFactory', function () { beforeEach(async function () { recoverer = constants.AddressZero; logicAddress = constants.AddressZero; - index = nextIndex(); + index = randomNumber(); smartWalletAddress = await customSmartWalletFactory.getSmartWalletAddress( owner.address, @@ -248,21 +241,11 @@ describe('CustomSmartWalletFactory', function () { } ); - const typedDeployData = new TypedDeployRequestData( - HARDHAT_CHAIN_ID, + const { suffixData, signature } = signDeployRequest( + owner, + deployRequest, customSmartWalletFactory.address, - deployRequest - ); - - const suffixData = getSuffixData(typedDeployData); - - const privateKey = Buffer.from( - owner.privateKey.substring(2, 66), - 'hex' - ); - const signature = getLocalEip712DeploySignature( - typedDeployData, - privateKey + chainId ); const initialWorkerBalance = await token.balanceOf(worker.address); @@ -305,22 +288,11 @@ describe('CustomSmartWalletFactory', function () { } ); - const typedDeployData = new TypedDeployRequestData( - HARDHAT_CHAIN_ID, + const { suffixData, signature } = signDeployRequest( + owner, + deployRequest, customSmartWalletFactory.address, - deployRequest - ); - - const suffixData = getSuffixData(typedDeployData); - - const privateKey = Buffer.from( - owner.privateKey.substring(2, 66), - 'hex' - ); - - const signature = getLocalEip712DeploySignature( - typedDeployData, - privateKey + chainId ); const initialWorkerBalance = await token.balanceOf(worker.address); @@ -365,21 +337,11 @@ describe('CustomSmartWalletFactory', function () { } ); - const typedDeployData = new TypedDeployRequestData( - HARDHAT_CHAIN_ID, + const { suffixData, signature } = signDeployRequest( + owner, + deployRequest, customSmartWalletFactory.address, - deployRequest - ); - - const suffixData = getSuffixData(typedDeployData); - - const privateKey = Buffer.from( - owner.privateKey.substring(2, 66), - 'hex' - ); - const signature = getLocalEip712DeploySignature( - typedDeployData, - privateKey + chainId ); const initialWorkerBalance = await token.balanceOf(worker.address); @@ -418,21 +380,11 @@ describe('CustomSmartWalletFactory', function () { } ); - const typedDeployData = new TypedDeployRequestData( - HARDHAT_CHAIN_ID, + const { suffixData, signature } = signDeployRequest( + owner, + deployRequest, customSmartWalletFactory.address, - deployRequest - ); - - const suffixData = getSuffixData(typedDeployData); - - const privateKey = Buffer.from( - owner.privateKey.substring(2, 66), - 'hex' - ); - const signature = getLocalEip712DeploySignature( - typedDeployData, - privateKey + chainId ); const initialWorkerBalance = await token.balanceOf(worker.address); @@ -469,21 +421,11 @@ describe('CustomSmartWalletFactory', function () { } ); - const typedDeployData = new TypedDeployRequestData( - HARDHAT_CHAIN_ID, + const { suffixData, signature } = signDeployRequest( + owner, + deployRequest, customSmartWalletFactory.address, - deployRequest - ); - - const suffixData = getSuffixData(typedDeployData); - - const privateKey = Buffer.from( - owner.privateKey.substring(2, 66), - 'hex' - ); - const signature = getLocalEip712DeploySignature( - typedDeployData, - privateKey + chainId ); const initialWorkerBalance = await token.balanceOf(worker.address); @@ -521,22 +463,11 @@ describe('CustomSmartWalletFactory', function () { } ); - const typedDeployData = new TypedDeployRequestData( - HARDHAT_CHAIN_ID, + const { suffixData, signature } = signDeployRequest( + owner, + deployRequest, customSmartWalletFactory.address, - deployRequest - ); - - const suffixData = getSuffixData(typedDeployData); - - const privateKey = Buffer.from( - owner.privateKey.substring(2, 66), - 'hex' - ); - - const signature = getLocalEip712DeploySignature( - typedDeployData, - privateKey + chainId ); const initialWorkerBalance = await token.balanceOf(worker.address); @@ -573,21 +504,11 @@ describe('CustomSmartWalletFactory', function () { } ); - const typedDeployData = new TypedDeployRequestData( - HARDHAT_CHAIN_ID, + const { suffixData, signature } = signDeployRequest( + owner, + deployRequest, customSmartWalletFactory.address, - deployRequest - ); - - const suffixData = getSuffixData(typedDeployData); - - const privateKey = Buffer.from( - owner.privateKey.substring(2, 66), - 'hex' - ); - const signature = getLocalEip712DeploySignature( - typedDeployData, - privateKey + chainId ); const otherAccount = Wallet.createRandom(); diff --git a/test/factory/smartWalletFactory.test.ts b/test/factory/smartWalletFactory.test.ts index 6c57e07d..a6d775ac 100644 --- a/test/factory/smartWalletFactory.test.ts +++ b/test/factory/smartWalletFactory.test.ts @@ -8,28 +8,21 @@ import { import { ethers } from 'hardhat'; import { constants, utils, Wallet } from 'ethers'; import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers'; -import seedrandom from 'seedrandom'; import { createValidPersonalSignSignature } from '../utils/createValidPersonalSignSignature'; -import { createDeployRequest } from './utils'; -import { - getLocalEip712DeploySignature, - TypedDeployRequestData, -} from '../utils/EIP712Utils'; -import { getSuffixData, HARDHAT_CHAIN_ID } from '../smartwallet/utils'; +import { createDeployRequest, randomNumber, signDeployRequest } from './utils'; import { deployContract } from '../../utils/deployment/deployment.utils'; -const random = seedrandom('rif'); -const minIndex = 0; -const maxIndex = 1000000000; - -const nextIndex = () => - Math.floor(random() * (maxIndex - minIndex + 1) + minIndex); - type SmartWalletFactoryOptions = Parameters< SmartWalletFactory__factory['deploy'] >; describe('SmartWalletFactory', function () { + let chainId: number; + + before(async function () { + ({ chainId } = await ethers.provider.getNetwork()); + }); + describe('constructor', function () { let smartWalletFactory: SmartWalletFactory; let template: Wallet; @@ -77,7 +70,7 @@ describe('SmartWalletFactory', function () { beforeEach(function () { recoverer = constants.AddressZero; - index = nextIndex(); + index = randomNumber(); }); it('should initiate the smart wallet in the expected address', async function () { @@ -183,7 +176,7 @@ describe('SmartWalletFactory', function () { beforeEach(async function () { recoverer = constants.AddressZero; - index = nextIndex(); + index = randomNumber(); smartWalletAddress = await smartWalletFactory.getSmartWalletAddress( owner.address, recoverer, @@ -213,21 +206,11 @@ describe('SmartWalletFactory', function () { } ); - const typedDeployData = new TypedDeployRequestData( - HARDHAT_CHAIN_ID, + const { suffixData, signature } = signDeployRequest( + owner, + deployRequest, smartWalletFactory.address, - deployRequest - ); - - const suffixData = getSuffixData(typedDeployData); - - const privateKey = Buffer.from( - owner.privateKey.substring(2, 66), - 'hex' - ); - const signature = getLocalEip712DeploySignature( - typedDeployData, - privateKey + chainId ); const initialWorkerBalance = await token.balanceOf(worker.address); @@ -270,22 +253,11 @@ describe('SmartWalletFactory', function () { } ); - const typedDeployData = new TypedDeployRequestData( - HARDHAT_CHAIN_ID, + const { suffixData, signature } = signDeployRequest( + owner, + deployRequest, smartWalletFactory.address, - deployRequest - ); - - const suffixData = getSuffixData(typedDeployData); - - const privateKey = Buffer.from( - owner.privateKey.substring(2, 66), - 'hex' - ); - - const signature = getLocalEip712DeploySignature( - typedDeployData, - privateKey + chainId ); const initialWorkerBalance = await token.balanceOf(worker.address); @@ -330,21 +302,11 @@ describe('SmartWalletFactory', function () { } ); - const typedDeployData = new TypedDeployRequestData( - HARDHAT_CHAIN_ID, + const { suffixData, signature } = signDeployRequest( + owner, + deployRequest, smartWalletFactory.address, - deployRequest - ); - - const suffixData = getSuffixData(typedDeployData); - - const privateKey = Buffer.from( - owner.privateKey.substring(2, 66), - 'hex' - ); - const signature = getLocalEip712DeploySignature( - typedDeployData, - privateKey + chainId ); const initialWorkerBalance = await token.balanceOf(worker.address); @@ -383,21 +345,11 @@ describe('SmartWalletFactory', function () { } ); - const typedDeployData = new TypedDeployRequestData( - HARDHAT_CHAIN_ID, + const { suffixData, signature } = signDeployRequest( + owner, + deployRequest, smartWalletFactory.address, - deployRequest - ); - - const suffixData = getSuffixData(typedDeployData); - - const privateKey = Buffer.from( - owner.privateKey.substring(2, 66), - 'hex' - ); - const signature = getLocalEip712DeploySignature( - typedDeployData, - privateKey + chainId ); const initialWorkerBalance = await token.balanceOf(worker.address); @@ -434,21 +386,11 @@ describe('SmartWalletFactory', function () { } ); - const typedDeployData = new TypedDeployRequestData( - HARDHAT_CHAIN_ID, + const { suffixData, signature } = signDeployRequest( + owner, + deployRequest, smartWalletFactory.address, - deployRequest - ); - - const suffixData = getSuffixData(typedDeployData); - - const privateKey = Buffer.from( - owner.privateKey.substring(2, 66), - 'hex' - ); - const signature = getLocalEip712DeploySignature( - typedDeployData, - privateKey + chainId ); const initialWorkerBalance = await token.balanceOf(worker.address); @@ -486,22 +428,11 @@ describe('SmartWalletFactory', function () { } ); - const typedDeployData = new TypedDeployRequestData( - HARDHAT_CHAIN_ID, + const { suffixData, signature } = signDeployRequest( + owner, + deployRequest, smartWalletFactory.address, - deployRequest - ); - - const suffixData = getSuffixData(typedDeployData); - - const privateKey = Buffer.from( - owner.privateKey.substring(2, 66), - 'hex' - ); - - const signature = getLocalEip712DeploySignature( - typedDeployData, - privateKey + chainId ); const initialWorkerBalance = await token.balanceOf(worker.address); @@ -538,21 +469,11 @@ describe('SmartWalletFactory', function () { } ); - const typedDeployData = new TypedDeployRequestData( - HARDHAT_CHAIN_ID, + const { suffixData, signature } = signDeployRequest( + owner, + deployRequest, smartWalletFactory.address, - deployRequest - ); - - const suffixData = getSuffixData(typedDeployData); - - const privateKey = Buffer.from( - owner.privateKey.substring(2, 66), - 'hex' - ); - const signature = getLocalEip712DeploySignature( - typedDeployData, - privateKey + chainId ); const otherAccount = Wallet.createRandom(); diff --git a/test/factory/utils.ts b/test/factory/utils.ts index 0bd85137..2a451d53 100644 --- a/test/factory/utils.ts +++ b/test/factory/utils.ts @@ -1,10 +1,24 @@ -import { constants } from 'ethers'; +import { constants, Wallet } from 'ethers'; +import seedrandom from 'seedrandom'; +import { getSuffixData } from '../smartwallet/utils'; +import { + getLocalEip712DeploySignature, + TypedDeployRequestData, +} from '../utils/EIP712Utils'; + import { EnvelopingTypes, IForwarder, } from 'typechain-types/contracts/RelayHub'; -export function createDeployRequest( +const random = seedrandom('rif'); +const minIndex = 0; +const maxIndex = 1000000000; + +const randomNumber = (min = minIndex, max = maxIndex) => + Math.floor(random() * (max - min + 1) + min); + +function createDeployRequest( request: Partial, relayData?: Partial ): EnvelopingTypes.DeployRequestStruct { @@ -42,3 +56,29 @@ export function createDeployRequest( }, }; } + +const signDeployRequest = ( + signer: Wallet, + deployRequest: EnvelopingTypes.DeployRequestStruct, + verifier: string, + chainId: number +) => { + const typedDeployData = new TypedDeployRequestData( + chainId, + verifier, + deployRequest + ); + + const suffixData = getSuffixData(typedDeployData); + + const privateKey = Buffer.from(signer.privateKey.substring(2, 66), 'hex'); + + const signature = getLocalEip712DeploySignature(typedDeployData, privateKey); + + return { + suffixData, + signature, + }; +}; + +export { randomNumber, createDeployRequest, signDeployRequest };