Skip to content

Commit

Permalink
added nounce to randomAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
frank poncelet authored and frank poncelet committed Aug 29, 2024
1 parent b6fe98e commit 9444a5e
Showing 1 changed file with 48 additions and 10 deletions.
58 changes: 48 additions & 10 deletions contracts/utils/AddressUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,56 @@ library AddressUtils {
}

/**
* @dev Generates a pseudo-random address.
*
* This function creates a pseudo-random address by hashing the current block timestamp
* and the previous block's random number (prevrandao). The resulting hash is then
* converted to a uint256, cast to a uint160, and finally to an address.
*
* @return addr A pseudo-randomly generated address.
*/
* @dev Generates a pseudo-random address.
*
* This function creates a pseudo-random address by hashing the current block timestamp
* and the previous block's random number (prevrandao). The resulting hash is then
* converted to a uint256, cast to a uint160, and finally to an address.
*
* @param nonce An integer value to introduce additional variability to the hash.
* @return addr A pseudo-randomly generated address.
*/
function randomAddress(int256 nonce) internal view returns (address addr) {
return
address(
uint160(
uint256(
keccak256(
abi.encodePacked(
block.timestamp,
block.prevrandao,
nonce
)
)
)
)
);
}

/**
* @dev Generates a pseudo-random address.
* This function is deprecated and should not be used. Instead, use the function with the
* `nonce` parameter to introduce additional randomness.
*
* This function creates a pseudo-random address by hashing the current block timestamp
* and the previous block's random number (prevrandao). The resulting hash is then
* converted to a uint256, cast to a uint160, and finally to an address.
*
* @return addr A pseudo-randomly generated address.
*/
function randomAddress() internal view returns (address addr) {
return address(uint160(uint256(keccak256(abi.encodePacked(block.timestamp,block.prevrandao)))));
return
address(
uint160(
uint256(
keccak256(
abi.encodePacked(block.timestamp, block.prevrandao)
)
)
)
);
}

/**
* @notice Calculates an Ethereum address from a given public key.
* @param publicKey The public key as a hex string.
Expand Down

0 comments on commit 9444a5e

Please sign in to comment.