Skip to content

Commit

Permalink
wip 2
Browse files Browse the repository at this point in the history
  • Loading branch information
corbanbrook committed Feb 28, 2024
1 parent d2d30a6 commit a6a250e
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 93 deletions.
4 changes: 2 additions & 2 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { HardhatUserConfig, task } from 'hardhat/config'
import { networkConfig } from './utils/config-loader'

import '@nomiclabs/hardhat-truffle5'
import '@nomicfoundation/hardhat-ethers'
import '@nomicfoundation/hardhat-verify'
import '@nomiclabs/hardhat-truffle5'
import '@nomiclabs/hardhat-web3'
import '@nomiclabs/hardhat-etherscan'
import '@tenderly/hardhat-tenderly'

import 'hardhat-gas-reporter'
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
},
"devDependencies": {
"@nomicfoundation/hardhat-ethers": "^3.0.5",
"@nomiclabs/hardhat-etherscan": "^3.1.8",
"@nomicfoundation/hardhat-verify": "^2.0.4",
"@nomiclabs/hardhat-truffle5": "^2.0.7",
"@nomiclabs/hardhat-web3": "^2.0.0",
"@tenderly/hardhat-tenderly": "^1.0.11",
Expand Down
50 changes: 26 additions & 24 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion test/GuestModule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ contract('GuestModule', () => {
await expectToBeRejected(tx, 'DelegateCallNotAllowed(0)')
})
it('Should not accept ETH', async () => {
const tx = hethers.provider.getSigner().sendTransaction({ value: 1, to: await guestModule.getAddress() })
const signer = await hethers.provider.getSigner()
const tx = signer.sendTransaction({ value: 1, to: await guestModule.getAddress() })
await expect(tx).to.be.rejected
})
it('Should not implement hooks', async () => {
Expand Down
65 changes: 34 additions & 31 deletions test/MainModule.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ethers } from 'ethers'
import { ethers as hethers } from 'hardhat'

import { bytes32toAddress, CHAIN_ID, expect, expectToBeRejected, randomHex } from './utils'
import { bytes32toAddress, getChainId, expect, expectToBeRejected, randomHex } from './utils'

import {
CallReceiverMock,
Expand Down Expand Up @@ -243,7 +243,7 @@ contract('MainModule', (accounts: string[]) => {
data: callReceiver.interface.encodeFunctionData('testCall', [5423, randomHex(32)])
}

const subdigest = subdigestOf(wallet.address, digestOf([transaction], await wallet.getNonce()))
const subdigest = await subdigestOf(wallet.address, digestOf([transaction], await wallet.getNonce()))
const badNestedSignature = await wallet_b.signDigest(subdigest).then(s => s + '03')

const tx = wallet.sendTransactions([transaction])
Expand All @@ -261,7 +261,7 @@ contract('MainModule', (accounts: string[]) => {
await wallet.deploy()

const signauture = await wallet.signTransactions([{}])
const subdigest = subdigestOf(wallet.address, digestOf([{}], await wallet.getNonce()))
const subdigest = await subdigestOf(wallet.address, digestOf([{}], await wallet.getNonce()))

const tx = wallet.relayTransactions([{}], signauture)
await expect(tx).to.be.rejected
Expand Down Expand Up @@ -310,14 +310,14 @@ contract('MainModule', (accounts: string[]) => {

describe('Network ID', () => {
it('Should reject a transaction of another network id', async () => {
wallet = wallet.useChainId(CHAIN_ID() + 1)
wallet = wallet.useChainId((await getChainId()) + 1n)
const tx = wallet.sendTransactions([{}])
await expectToBeRejected(tx, 'InvalidSignature')
})

describe('Universal network signatures', async () => {
it('Should reject signature for another network id, even if encoded as universal', async () => {
wallet = wallet.useChainId(CHAIN_ID() + 1)
wallet = wallet.useChainId((await getChainId()) + 1n)
const tx = wallet.sendTransactions([{}])
await expectToBeRejected(tx, 'InvalidSignature')
})
Expand Down Expand Up @@ -438,7 +438,7 @@ contract('MainModule', (accounts: string[]) => {

await wallet.sendTransactions([{}], encodeNonce(space, 0))

const storageValue = await hethers.provider.getStorageAt(wallet.address, storageKey)
const storageValue = await hethers.provider.getStorage(wallet.address, storageKey)
expect(BigInt(storageValue)).to.equal(1)
})
})
Expand Down Expand Up @@ -501,7 +501,7 @@ contract('MainModule', (accounts: string[]) => {

it('Should reject signature with bad encoding type', async () => {
const tx = wallet.relayTransactions([{}], '0x2012')
const subdigest = subdigestOf(wallet.address, digestOf([{}], 0))
const subdigest = await subdigestOf(wallet.address, digestOf([{}], 0))
await expectToBeRejected(tx, `InvalidSignatureType("0x20")`)
})

Expand Down Expand Up @@ -559,7 +559,7 @@ contract('MainModule', (accounts: string[]) => {
it('Should use implementation storage key', async () => {
await wallet.updateImageHash(ethers.randomBytes(32))

const storageValue = await hethers.provider.getStorageAt(wallet.address, wallet.address)
const storageValue = await hethers.provider.getStorage(wallet.address, wallet.address)
expect(bytes32toAddress(storageValue)).to.equal(await context.mainModuleUpgradable.getAddress())
})
})
Expand Down Expand Up @@ -677,7 +677,7 @@ contract('MainModule', (accounts: string[]) => {
describe(c.name, () => {
it('Should reject proof for another subdigest', async () => {
const digests = new Array(2).fill(0).map(() => ethers.hexlify(ethers.randomBytes(32)))
const subdigests = digests.map(d => ({ subdigest: subdigestOf(wallet.address, d, 0) }))
const subdigests = await Promise.all(digests.map(async d => ({ subdigest: await subdigestOf(wallet.address, d, 0) })))
const subdigestsMerkle = merkleTopology([...subdigests])

const prevLeaves = leavesOf(wallet.config.topology)
Expand All @@ -700,7 +700,7 @@ contract('MainModule', (accounts: string[]) => {
wallet = SequenceWallet.basicWallet(context, { signing: 10, idle: 11 })

const digests = new Array(33).fill(0).map(() => ethers.hexlify(ethers.randomBytes(32)))
const subdigests = digests.map(d => ({ subdigest: subdigestOf(wallet.address, d, 0) }))
const subdigests = await Promise.all(digests.map(async d => ({ subdigest: await subdigestOf(wallet.address, d, 0) })))
const subdigestsMerkle = merkleTopology([...subdigests])

const prevLeaves = leavesOf(wallet.config.topology)
Expand Down Expand Up @@ -891,7 +891,8 @@ contract('MainModule', (accounts: string[]) => {
})

it('Should perform call a contract and transfer eth in one tx', async () => {
await hethers.provider.getSigner().sendTransaction({ to: wallet.address, value: 100 })
const signer = await hethers.provider.getSigner()
await signer.sendTransaction({ to: wallet.address, value: 100 })
const receiver = new ethers.Wallet(ethers.hexlify(ethers.randomBytes(32)))

const transactions = [
Expand All @@ -913,7 +914,8 @@ contract('MainModule', (accounts: string[]) => {
})

it('Should fail if one transaction fails', async () => {
await hethers.provider.getSigner().sendTransaction({ to: wallet.address, value: 100 })
const signer = await hethers.provider.getSigner()
await signer.sendTransaction({ to: wallet.address, value: 100 })

await callReceiver.setRevertFlag(true)

Expand Down Expand Up @@ -1004,11 +1006,13 @@ contract('MainModule', (accounts: string[]) => {

describe('Handle ETH', () => {
it('Should receive ETH', async () => {
hethers.provider.getSigner().sendTransaction({ to: wallet.address, value: 1 })
const signer = await hethers.provider.getSigner()
signer.sendTransaction({ to: wallet.address, value: 1 })
})

it('Should transfer ETH', async () => {
hethers.provider.getSigner().sendTransaction({ to: wallet.address, value: 100 })
const signer = await hethers.provider.getSigner()
signer.sendTransaction({ to: wallet.address, value: 100 })

const receiver = ethers.Wallet.createRandom().address

Expand All @@ -1022,7 +1026,8 @@ contract('MainModule', (accounts: string[]) => {
})

it('Should call payable function', async () => {
hethers.provider.getSigner().sendTransaction({ to: wallet.address, value: 100 })
const signer = await hethers.provider.getSigner()
signer.sendTransaction({ to: wallet.address, value: 100 })

const valA = 63129
const valB = randomHex(120)
Expand Down Expand Up @@ -1124,7 +1129,7 @@ contract('MainModule', (accounts: string[]) => {
}
]

const txHash = subdigestOf(wallet.address, digestOf(transactions, await wallet.getNonce()))
const txHash = await subdigestOf(wallet.address, digestOf(transactions, await wallet.getNonce()))
const receipt = await wallet.sendTransactions(transactions).then(r => r.wait())

if (!receipt) {
Expand Down Expand Up @@ -1286,7 +1291,7 @@ contract('MainModule', (accounts: string[]) => {
const subkey = ethers.AbiCoder.defaultAbiCoder().encode(['bytes4'], [hookSelector])
const storageKey = computeStorageKey('org.arcadeum.module.hooks.hooks', subkey)

const storageValue = await hethers.provider.getStorageAt(wallet.address, storageKey)
const storageValue = await hethers.provider.getStorage(wallet.address, storageKey)

const addr = (() => {
try {
Expand All @@ -1302,7 +1307,8 @@ contract('MainModule', (accounts: string[]) => {

it('Should pass calling a non registered hook', async () => {
const data = ethers.AbiCoder.defaultAbiCoder().encode(['bytes4'], [hookSelector])
await hethers.provider.getSigner().sendTransaction({ to: wallet.address, data: data })
const signer = await hethers.provider.getSigner()
await signer.sendTransaction({ to: wallet.address, data: data })
})
})

Expand All @@ -1317,18 +1323,14 @@ contract('MainModule', (accounts: string[]) => {

await wallet.sendTransactions([transaction])

const signer = await hethers.provider.getSigner()

// Calling the wallet with '0x112233' should not forward the call to the hook
const tx = hethers.provider
.getSigner()
.sendTransaction({ to: wallet.address, data: '0x112233' })
.then(t => t.wait())
const tx = signer.sendTransaction({ to: wallet.address, data: '0x112233' }).then(t => t.wait())
await expect(tx).to.be.fulfilled

// Calling the wallet with '0x11223300' should forward the call to the hook (and thus revert)
const tx2 = hethers.provider
.getSigner()
.sendTransaction({ to: wallet.address, data: '0x11223300' })
.then(t => t.wait())
const tx2 = signer.sendTransaction({ to: wallet.address, data: '0x11223300' }).then(t => t.wait())
await expect(tx2).to.be.rejected
})

Expand Down Expand Up @@ -1431,7 +1433,7 @@ contract('MainModule', (accounts: string[]) => {

it('Should use image hash storage key', async () => {
const storageKey = computeStorageKey('org.arcadeum.module.auth.upgradable.image.hash')
const storageValue = await hethers.provider.getStorageAt(wallet.address, storageKey)
const storageValue = await hethers.provider.getStorage(wallet.address, storageKey)
expect(ethers.AbiCoder.defaultAbiCoder().encode(['bytes32'], [storageValue])).to.equal(walletb.imageHash)
})

Expand Down Expand Up @@ -1463,7 +1465,7 @@ contract('MainModule', (accounts: string[]) => {

it('Should use image hash storage key', async () => {
const storageKey = computeStorageKey('org.arcadeum.module.auth.upgradable.image.hash')
const storageValue = await hethers.provider.getStorageAt(walletb.address, storageKey)
const storageValue = await hethers.provider.getStorage(walletb.address, storageKey)
expect(ethers.AbiCoder.defaultAbiCoder().encode(['bytes32'], [storageValue])).to.equal(walletc.imageHash)
})
})
Expand Down Expand Up @@ -1950,7 +1952,8 @@ contract('MainModule', (accounts: string[]) => {
it('Should create a contract with value', async () => {
const deployCode = CallReceiverMock.factory().bytecode

await hethers.provider.getSigner().sendTransaction({ to: wallet.address, value: 100 })
const signer = await hethers.provider.getSigner()
await signer.sendTransaction({ to: wallet.address, value: 100 })

const transaction = {
target: wallet.address,
Expand Down Expand Up @@ -1979,7 +1982,7 @@ contract('MainModule', (accounts: string[]) => {

describe('Transaction events', () => {
it('Should emit TxExecuted event', async () => {
const txHash = subdigestOf(wallet.address, digestOf([{}], await wallet.getNonce()))
const txHash = await subdigestOf(wallet.address, digestOf([{}], await wallet.getNonce()))
const res = await wallet.sendTransactions([{}])
const receipt = await res.wait()

Expand All @@ -1997,7 +2000,7 @@ contract('MainModule', (accounts: string[]) => {
})

it('Should emit multiple TxExecuted events', async () => {
const txHash = subdigestOf(wallet.address, digestOf([{}, {}], await wallet.getNonce()))
const txHash = await subdigestOf(wallet.address, digestOf([{}, {}], await wallet.getNonce()))
const receipt = await wallet.sendTransactions([{}, {}]).then(t => t.wait())

if (!receipt) {
Expand Down
10 changes: 5 additions & 5 deletions test/MultiCallUtils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ethers } from 'ethers'

import { ethers as hethers } from 'hardhat'
import { CHAIN_ID, encodeError, expect, expectStaticToBeRejected } from './utils'
import { getChainId, encodeError, expect, expectStaticToBeRejected } from './utils'
import { CallReceiverMock, ContractType, MultiCallUtils } from './utils/contracts'
import { applyTxDefault, applyTxDefaults } from './utils/sequence'

Expand Down Expand Up @@ -210,7 +210,7 @@ contract('Multi call utils', (accounts: string[]) => {

const txs = await Promise.all(
[
i.encodeFunctionData('callBlockhash', [lastBlock.number - 1]),
i.encodeFunctionData('callBlockhash', [lastBlock!.number - 1]),
i.encodeFunctionData('callCoinbase'),
i.encodeFunctionData('callDifficulty'),
i.encodeFunctionData('callGasLimit'),
Expand Down Expand Up @@ -258,7 +258,7 @@ contract('Multi call utils', (accounts: string[]) => {
expect(res[1][4]).to.not.equal(emptyBytes32, 'return block number')
expect(res[1][5]).to.not.equal(emptyBytes32, 'return timestamp')
expect(res[1][6]).to.not.equal(emptyBytes32, 'return gas left')
expect(BigInt(res[1][7])).to.equal(1, 'return gas price')
expect(BigInt(res[1][7])).to.equal(1n, 'return gas price')
expect(res[1][8]).to.not.equal(emptyBytes32, 'return origin')
expect(res[1][9]).to.not.equal(emptyBytes32, 'return balance of 0x')
expect(res[1][10]).to.equal(emptyBytes32, 'return balance of empty account')
Expand All @@ -269,14 +269,14 @@ contract('Multi call utils', (accounts: string[]) => {

const codeSize = ethers.AbiCoder.defaultAbiCoder().decode(['uint256'], res[1][12])[0]
expect(ethers.AbiCoder.defaultAbiCoder().decode(['bytes'], res[1][14])[0].length).to.equal(
2 + codeSize * 2,
2 + Number(codeSize) * 2,
'return code of correct size'
)

expect(res[1][15]).to.not.equal(emptyBytes32)
expect(res[1][16]).to.not.equal(emptyBytes32)

expect(ethers.AbiCoder.defaultAbiCoder().decode(['uint256'], Number(res[1][17])[0])).to.equal(CHAIN_ID(), 'return chain id')
expect(ethers.AbiCoder.defaultAbiCoder().decode(['uint256'], res[1][17])[0]).to.equal(await getChainId(), 'return chain id')
})
})
})
2 changes: 1 addition & 1 deletion test/utils/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const cachedFactories: { [name: string]: ethers.ContractFactory } = {}
async function deploy<Y extends ethers.BaseContract>(name: string, ...args: any[]) {
const factory = await hethers.getContractFactory(name)
cachedFactories[name] = factory
return (await factory.deploy(...args)) as Y
return (await factory.deploy(...args)) as unknown as Y
}

function attach<Y extends ethers.BaseContract>(name: string, address: string) {
Expand Down
Loading

0 comments on commit a6a250e

Please sign in to comment.