Ethereum: Create Deterministic address without create2

Creating Deterministic Ethereum Addresses Without create2

In Solidity, creating a deterministic (i.e. unique) address is crucial for managing private keys and accounts. However, Truffle’s create2 function provides an alternative way to create addresses, especially when you don’t plan on deploying new contracts. In this article, we’ll learn how to create deterministic Ethereum addresses without using create2.

Understanding Deterministic Addresses

In Solidity, a deterministic address is an address that always produces the same output from the same input (private key). This ensures that users can trust their private keys and reduces the risk of account compromise.

create2 Feature

Truffle’s create2 function allows you to create addresses with a fixed set of prefixes, such as 0x, 0xA, or 0xB. However, when using create2, you are limited to the available prefixes and cannot generate custom prefix combinations.

Creating Deterministic Addresses without create2

To create deterministic addresses without create2, you will need to use a different approach. One solution is to use a library like ethers-polyfill-accounts or truffle-ethers. These libraries allow you to work with Ethereum accounts and generate custom prefixes.

Here is an example of how you can create a deterministic address without using create2:

Install the required library

First, install the required library:

npm install ethers-polyfill-accounts

Create a custom address function

Ethereum: Create Deterministic address without create2

Create a new Solidity file (e.g., addressFunction.sol) and add the following code:

pragma solidity ^0.8.0;

import "

contract CustomAddress {

function createDeterministicAddress() public returns (address) {

// Generate a random number to ensure uniqueness

uint256 random = uint256(keccak256(abi.encodePacked(block.timestamp)));

// Create a custom prefix

address newPrefix = 0x...; // Replace with the desired prefix

// Return the generated address

return keccak256(abi.encodePacked(newPrefix, random));

}

}

Use the custom address function

You can now call the createDeterministicAddress function to generate a custom deterministic address:

CustomAddress memory address = CustomAddress(addressFunction);

addressAddress = address.createDeterministicAddress();

In this example, we define a custom contract called CustomAddress. The createDeterministicAddress function generates a random number and uses it as a seed for a custom prefix. This ensures that all generated addresses will be unique.

Conclusion

While using create2 can simplify your workflow in some cases, there are scenarios where creating deterministic addresses without it is necessary. By following this guide, you have learned how to create custom deterministic addresses in Solidity without resorting to the create2 function. Don’t forget to choose a library like ethers-polyfill-accounts or truffle-ethers to work with Ethereum accounts and generate custom prefixes.