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

Chain-specific Safe addresses #560

Merged
merged 4 commits into from
Oct 31, 2023
Merged

Conversation

tmjssz
Copy link
Contributor

@tmjssz tmjssz commented Oct 23, 2023

What it solves

Resolves #464

How this PR fixes it

Adding the getChainSpecificDefaultSaltNonce function which appends the given chainId to the PREDETERMINED_SALT_NONCE. This results in different default addresses for the Safe contract on different chains.

Example

Deployed two Safes with identical parameters on two different chains without defining a custom salt nonce. Received different addresses:

@github-actions
Copy link

github-actions bot commented Oct 23, 2023

CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅

@coveralls
Copy link

coveralls commented Oct 23, 2023

Pull Request Test Coverage Report for Build 6627146939

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 76.975%

Totals Coverage Status
Change from base Build 6262615050: 0.0%
Covered Lines: 451
Relevant Lines: 543

💛 - Coveralls

@tmjssz
Copy link
Contributor Author

tmjssz commented Oct 23, 2023

I have read the CLA Document and I hereby sign the CLA

@tmjssz tmjssz force-pushed the chain-specific-safe-addresses branch from 172daf7 to 6602eba Compare October 23, 2023 14:56
@@ -1,5 +1,6 @@
import chai from 'chai'
import chaiAsPromised from 'chai-as-promised'
import { keccak256 } from 'ethereumjs-util'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DaniSomoza this lib is deprecated at your PR, could you recommend an alternative here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx, I updated the PR to use @noble/hashes now.

* Provides a default salt nonce with appended chainId to generate different addresses for the
* same Safe in different networks.
*/
export function getChainSpecificDefaultSaltNonce(chainId: number): string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is your opinion about to provide a full JSDoc for this function?. Here's a suggested format:

/**
 * Provides a chain-specific default salt nonce for generating unique addresses 
 * for the same Safe configuration across different chains.
 * 
 * @param {number} chainId - The chain ID associated with the chain.
 * @returns {string} The chain-specific salt nonce in hexadecimal format.
 */

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes makes total sense! Added it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤓 : the largest number in javascript is (2^53) – 1, but chain IDs in ethereum theoretically can be 256 bits numbers

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From here:

Adds a new opcode CHAINID at 0x46, which uses 0 stack arguments. It pushes the current chain ID onto the stack. Chain ID is a 256-bit value. The operation costs G_base to execute.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok, so BigInt should be used? But would it not be better then to change it everywhere in the repo, instead of only in this function? Because the ethAdapter.getChainId() returns a simple number value and that is where the chainId comes from when being passed to this function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, then it is irrelevant for this PR. Thanks for the explanation

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense! I will update the type to BigInt in the PR of the Ethers v6 migration.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BigInt or string both should work. Ethers also has a BigNumberish type

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that now we have BigInt available in Javascript is the way to go. Also most libraries I expect they will use BigInt as for example Ethers v6 already does.

We created a separate ticket to handle this as there are quite some changes needed. Thank you for your input @mmv08

#564

@dasanra dasanra merged commit 447197c into development Oct 31, 2023
15 checks passed
@dasanra dasanra deleted the chain-specific-safe-addresses branch October 31, 2023 08:40
@github-actions github-actions bot locked and limited conversation to collaborators Oct 31, 2023
@dasanra dasanra linked an issue Nov 6, 2023 that may be closed by this pull request
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Force the deployment of different Safe addresses in different networks
5 participants