Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Predict safe address util #415

Merged
merged 58 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
7f4862a
Extract Protocol Kit Safe types
germartinez Mar 27, 2023
62416b4
Extract Protocol Kit Safe Factory types
germartinez Mar 27, 2023
934a228
Move AA logic to Protocol Kit
germartinez Mar 27, 2023
e2b5857
Refactor contractManager
germartinez Mar 27, 2023
de25af8
Refactor guardManager
germartinez Mar 27, 2023
b25296b
Refactor fallbackHandlerManager
germartinez Mar 27, 2023
4f8b4c2
Refactor moduleManager
germartinez Mar 27, 2023
4f32a30
Refactor ownerManager
germartinez Mar 27, 2023
92ec68d
Allow instantiation of undeployed Safes
germartinez Mar 29, 2023
0283bdd
Add method to check if a Safe is deployed
germartinez Mar 29, 2023
9c88cba
Adjust safe-ethers-adapters async instantiation
germartinez Mar 31, 2023
623b4fd
Merge branch 'protocol-kit-split' into protocol-kit-undeployed-safe
germartinez Mar 31, 2023
b6c2af7
Fix imports
germartinez Mar 31, 2023
a145fc8
Code formatting
germartinez Mar 31, 2023
4a49c03
Fix tests
germartinez Mar 31, 2023
98dabc5
Initialize the protocol-kit with a Safe that is not deployed
germartinez Apr 13, 2023
662e4a1
Add tests
germartinez Apr 13, 2023
1c34d1f
Merge branch 'development' into protocol-kit-undeployed-safe
germartinez Apr 13, 2023
0580384
Fix not deployed Safe version
germartinez Apr 13, 2023
d550b34
Update READMEs
germartinez Apr 13, 2023
92c63d0
Fix connect test
germartinez Apr 13, 2023
eaacc78
Support v1.0.0 in setup encoding
germartinez Apr 13, 2023
f3a3a09
Replace signer with predictecSafe in AA-Kit
germartinez Apr 13, 2023
1c30e99
Small adjustments
germartinez Apr 13, 2023
6e96206
chore: update safe-deployments version number (#395)
germartinez Apr 14, 2023
15e2395
Add predictedSafe to A-A-Kit
germartinez Apr 14, 2023
fea60eb
Fix default payment type in setup encoding
germartinez Apr 14, 2023
6a0cefa
Add safeVersion to PredictedSafeProps
germartinez Apr 14, 2023
5dcc08c
Update tests with predictedSafe.safeVersion
germartinez Apr 14, 2023
5427e77
Merge branch 'development' into protocol-kit-undeployed-safe
germartinez Apr 18, 2023
4a2a2a3
WIP
germartinez Apr 20, 2023
d1b94cd
Adjustments
germartinez Apr 20, 2023
eeb29cb
Add predictSafeAddress util function
DaniSomoza Apr 20, 2023
921bbaa
Merge branch 'development' into protocol-kit-undeployed-safe
germartinez Apr 20, 2023
f5fb586
Merge branch 'protocol-kit-undeployed-safe' into predict-safe-address…
DaniSomoza Apr 20, 2023
19927d4
removed chainId from predictSafeAddress
DaniSomoza Apr 20, 2023
367b7c1
Fix issues after merge
germartinez Apr 20, 2023
33d4de0
Merge branch 'protocol-kit-undeployed-safe' into predict-safe-address…
DaniSomoza Apr 21, 2023
44e5e35
fix merge issues
DaniSomoza Apr 21, 2023
5f37825
Fix AA SDK imports
germartinez Apr 21, 2023
b1803c8
Merge branch 'protocol-kit-undeployed-safe' into predict-safe-address…
DaniSomoza Apr 21, 2023
5f80b49
Added predictSafeAddress tests
DaniSomoza Apr 21, 2023
9565a63
Fix import
germartinez Apr 24, 2023
c3ed0e6
Add AA as a v1.3.0 feature
germartinez Apr 24, 2023
b1b45c5
Fix AA tests
germartinez Apr 24, 2023
8caab50
Add comments
germartinez Apr 24, 2023
c3bf35f
Fix naming convention
germartinez Apr 25, 2023
90fd29b
Fix fallbackHandler AA tests
germartinez Apr 25, 2023
d1601f6
Fix off-chain AA signatures tests
germartinez Apr 25, 2023
9377caa
Improve tests
germartinez Apr 25, 2023
e7de76d
Remove .only() in tests
germartinez Apr 26, 2023
9ee5d6d
Merge branch 'protocol-kit-undeployed-safe' into predict-safe-address…
dasanra Apr 27, 2023
960d1db
Fix validateSafeDeploymentConfig
dasanra Apr 27, 2023
cea8147
Merge branch 'development' into predict-safe-address-util
dasanra Apr 27, 2023
f9f00b4
chore avoid same function twice with only one param difference
dasanra Apr 27, 2023
942d4b3
add checks into the utils tests
dasanra Apr 27, 2023
573b326
Merge branch 'development' into predict-safe-address-util
dasanra Apr 28, 2023
c23c48f
Set explicit dependency on @ethersproject subpackage
dasanra Apr 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move AA logic to Protocol Kit
  • Loading branch information
germartinez committed Mar 27, 2023
commit 934a2282a44b7517ba43a7835d100738b110b9c8
15 changes: 6 additions & 9 deletions packages/account-abstraction-kit/src/AccountAbstraction.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { RelayAdapter } from '@safe-global/relay-kit'
import Safe, {
calculateChainSpecificProxyAddress,
EthersAdapter,
getMultiSendCallOnlyContract,
getProxyFactoryContract,
getSafeContract
getSafeContract,
getSafeInitializer
} from '@safe-global/protocol-kit'
import { RelayAdapter } from '@safe-global/relay-kit'
import {
GnosisSafeContract,
GnosisSafeProxyFactoryContract,
Expand All @@ -15,12 +17,7 @@ import {
} from '@safe-global/safe-core-sdk-types'
import { ethers } from 'ethers'
import { AccountAbstractionConfig, OperationType } from './types'
import {
calculateChainSpecificProxyAddress,
encodeCreateProxyWithNonce,
encodeMultiSendData,
getSafeInitializer
} from './utils/contracts'
import { encodeCreateProxyWithNonce, encodeMultiSendData } from './utils/contracts'

const safeVersion: SafeVersion = '1.3.0'

Expand Down Expand Up @@ -56,7 +53,7 @@ class AccountAbstraction {
this.#ethAdapter,
safeVersion,
this.#safeProxyFactoryContract,
this.#signer
await this.getSignerAddress()
)
this.#safeContract = await getSafeContract({
ethAdapter: this.#ethAdapter,
Expand Down
5 changes: 0 additions & 5 deletions packages/account-abstraction-kit/src/constants/index.ts

This file was deleted.

85 changes: 3 additions & 82 deletions packages/account-abstraction-kit/src/utils/contracts.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import { BigNumber } from '@ethersproject/bignumber'
import { arrayify, BytesLike } from '@ethersproject/bytes'
import { arrayify } from '@ethersproject/bytes'
import { pack as solidityPack } from '@ethersproject/solidity'
import { getCompatibilityFallbackHandlerContract, getSafeContract } from '@safe-global/protocol-kit'
import { PREDETERMINED_SALT_NONCE } from '@safe-global/protocol-kit'
import {
EthAdapter,
GnosisSafeContract,
GnosisSafeProxyFactoryContract,
MetaTransactionData,
SafeTransactionData,
SafeVersion
SafeTransactionData
} from '@safe-global/safe-core-sdk-types'
import { BigNumberish, ethers, Signer } from 'ethers'
import { PREDETERMINED_SALT_NONCE, ZERO_ADDRESS } from '../constants'

export function encodeCreateProxyWithNonce(
safeProxyFactoryContract: GnosisSafeProxyFactoryContract,
Expand All @@ -25,30 +20,6 @@ export function encodeCreateProxyWithNonce(
])
}

export async function encodeSetupCallData(
ethAdapter: EthAdapter,
safeVersion: SafeVersion,
safeContract: GnosisSafeContract,
owners: string[],
chainId: number
): Promise<string> {
const compatibilityFallbackhandlerAddress = await getCompatibilityFallbackHandlerContract({
ethAdapter,
safeVersion,
chainId
})
return safeContract.encode('setup', [
owners,
BigNumber.from(1) as BigNumberish,
ZERO_ADDRESS,
'0x' as BytesLike,
compatibilityFallbackhandlerAddress,
ZERO_ADDRESS,
BigNumber.from(0) as BigNumberish,
ZERO_ADDRESS
])
}

export function encodeExecTransaction(
safeContract: GnosisSafeContract,
transaction: SafeTransactionData,
Expand Down Expand Up @@ -80,53 +51,3 @@ function encodeMetaTransaction(tx: MetaTransactionData): string {
export function encodeMultiSendData(txs: MetaTransactionData[]): string {
return '0x' + txs.map((tx) => encodeMetaTransaction(tx)).join('')
}

export async function getSafeInitializer(
ethAdapter: EthAdapter,
safeContract: GnosisSafeContract,
signerAddress: string,
chainId: number
): Promise<string> {
const safeVersion = await safeContract.getVersion()
const initializer = await encodeSetupCallData(
ethAdapter,
safeVersion,
safeContract,
[signerAddress],
chainId
)
return initializer
}

export async function calculateChainSpecificProxyAddress(
ethAdapter: EthAdapter,
safeVersion: SafeVersion,
safeProxyFactoryContract: GnosisSafeProxyFactoryContract,
signer: Signer
): Promise<string> {
const chainId = await ethAdapter.getChainId()
const safeSingletonContract = await getSafeContract({ ethAdapter, safeVersion, chainId })
const deployer = safeProxyFactoryContract.getAddress()
const signerAddress = await signer.getAddress()

const deploymentCode = ethers.utils.solidityPack(
['bytes', 'uint256'],
[safeProxyFactoryContract.proxyCreationCode(), safeSingletonContract.getAddress()]
)
const salt = ethers.utils.solidityKeccak256(
['bytes32', 'uint256'],
[
ethers.utils.solidityKeccak256(
['bytes'],
[await getSafeInitializer(ethAdapter, safeSingletonContract, signerAddress, chainId)]
),
PREDETERMINED_SALT_NONCE
]
)
const derivedAddress = ethers.utils.getCreate2Address(
deployer,
salt,
ethers.utils.keccak256(deploymentCode)
)
return derivedAddress
}
91 changes: 91 additions & 0 deletions packages/protocol-kit/src/contracts/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import {
EthAdapter,
GnosisSafeContract,
GnosisSafeProxyFactoryContract,
SafeVersion
} from '@safe-global/safe-core-sdk-types'
import { BigNumber, BigNumberish, BytesLike, ethers } from 'ethers'
import {
getCompatibilityFallbackHandlerContract,
getSafeContract
} from '../contracts/safeDeploymentContracts'
import { ZERO_ADDRESS } from '../utils/constants'


// keccak256(toUtf8Bytes('Safe Account Abstraction'))
export const PREDETERMINED_SALT_NONCE =
'0xb1073742015cbcf5a3a4d9d1ae33ecf619439710b89475f92e2abd2117e90f90'

// TO-DO: Merge with encodeSetupCallData from the SafeFactory class
export async function encodeDefaultSetupCallData(
ethAdapter: EthAdapter,
safeVersion: SafeVersion,
safeContract: GnosisSafeContract,
owners: string[],
chainId: number
): Promise<string> {
const compatibilityFallbackhandlerAddress = await getCompatibilityFallbackHandlerContract({
ethAdapter,
safeVersion,
chainId
})
return safeContract.encode('setup', [
owners,
BigNumber.from(1) as BigNumberish,
ZERO_ADDRESS,
'0x' as BytesLike,
compatibilityFallbackhandlerAddress,
ZERO_ADDRESS,
BigNumber.from(0) as BigNumberish,
ZERO_ADDRESS
])
}

export async function getSafeInitializer(
ethAdapter: EthAdapter,
safeContract: GnosisSafeContract,
signerAddress: string,
chainId: number
): Promise<string> {
const safeVersion = await safeContract.getVersion()
const initializer = await encodeDefaultSetupCallData(
ethAdapter,
safeVersion,
safeContract,
[signerAddress],
chainId
)
return initializer
}

export async function calculateChainSpecificProxyAddress(
ethAdapter: EthAdapter,
safeVersion: SafeVersion,
safeProxyFactoryContract: GnosisSafeProxyFactoryContract,
signerAddress: string
): Promise<string> {
const chainId = await ethAdapter.getChainId()
const safeSingletonContract = await getSafeContract({ ethAdapter, safeVersion, chainId })
const deployer = safeProxyFactoryContract.getAddress()

const deploymentCode = ethers.utils.solidityPack(
['bytes', 'uint256'],
[safeProxyFactoryContract.proxyCreationCode(), safeSingletonContract.getAddress()]
)
const salt = ethers.utils.solidityKeccak256(
['bytes32', 'uint256'],
[
ethers.utils.solidityKeccak256(
['bytes'],
[await getSafeInitializer(ethAdapter, safeSingletonContract, signerAddress, chainId)]
),
PREDETERMINED_SALT_NONCE
]
)
const derivedAddress = ethers.utils.getCreate2Address(
deployer,
salt,
ethers.utils.keccak256(deploymentCode)
)
return derivedAddress
}
13 changes: 10 additions & 3 deletions packages/protocol-kit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,18 @@ import {
getSafeContract,
getSignMessageLibContract
} from './contracts/safeDeploymentContracts'
import {
calculateChainSpecificProxyAddress,
getSafeInitializer,
PREDETERMINED_SALT_NONCE
} from './contracts/utils'
import ContractManager from './managers/contractManager'
import Safe from './Safe'
import SafeFactory, { DeploySafeProps, SafeFactoryConfig } from './safeFactory'
import {
AddOwnerTxParams,
ConnectSafeConfig,
ContractNetworksConfig,
CreateTransactionProps,
PredictSafeProps,
RemoveOwnerTxParams,
Expand All @@ -47,8 +53,6 @@ import {
SafeDeploymentConfig,
SwapOwnerTxParams
} from './types'

import { ContractNetworksConfig } from './types'
import { SafeTransactionOptionalProps } from './utils/transactions/types'
import { standardizeSafeTransactionData } from './utils/transactions/utils'

Expand Down Expand Up @@ -97,7 +101,10 @@ export {
getMultiSendContract,
getMultiSendCallOnlyContract,
getSignMessageLibContract,
getCreateCallContract
getCreateCallContract,
getSafeInitializer,
calculateChainSpecificProxyAddress,
PREDETERMINED_SALT_NONCE
}

export default Safe
2 changes: 1 addition & 1 deletion packages/protocol-kit/src/safeFactory/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BigNumber } from '@ethersproject/bignumber'
import { SafeAccountConfig, SafeDeploymentConfig } from './'
import { SafeAccountConfig, SafeDeploymentConfig } from '../types'

export const validateSafeAccountConfig = ({ owners, threshold }: SafeAccountConfig): void => {
if (owners.length <= 0) throw new Error('Owner list must have at least one owner')
Expand Down