Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
This library provides a simple and efficient way of pseudo-random number generation for Solidity smart contracts. It ensures that users commit to a given block in the future, making it impossible to know the outcome beforehand or manipulate it after. The package operates in two key phases:
Request randomness (Commit): Users initiate a request by committing to a future block number, nonce (allowing multiple requests within the same transaction) and some custom data that can be chose by the protocol developer. This request is stored on-chain and remains unpredictable until the reveal phase. A future block number (e.g., current block + 5) is selected to ensure the result remains unpredictable.
Fulfill randomness (Reveal): The actual randomness is determined by hashing the future block number hash and other committed parameters. This produces a bytes32 hash that can be cast to a number. In order to generate a number from range you can use the modulo operation. A safety check ensures the committed block number is not older than 256 blocks, as the EVM only retains block hashes for the last 256 blocks. If the block is too old, the transaction reverts with an "EXPIRED" status.
If you plan to use this library to generate random traits on sellable items, make sure that users pay when requesting randomness. This way, they cannot cancel the request and try again if they don't like the result.
npm i solrand
You can inherit from the base SolrandConsumer
contract and use _requestRandomness
and _fullfillRandomness
functions to request and fullfill randomness:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import { SolrandConsumer } from "solrand/contracts/Solrand.sol";
contract SolrandConsumerExample is SolrandConsumer {
uint256 public number;
/**
* Request a random number from the Solrand library and wait at least 5 blocks for it to be
* fullfillable.
* @return requestId The ID of the request
*/
function requestRandomNumber() public returns (bytes32 requestId) {
return
_requestRandomness(
block.number + 5,
abi.encodePacked("Counter", msg.sender)
);
}
/**
*
* @param requestId The ID of the request to fullfill
* @return The random number
*/
function fullfillRandomNumber(bytes32 requestId) public returns (uint256) {
bytes32 randomValue = _fullfillRandomness(requestId);
number = uint256(randomValue);
return number;
}
}
SolrandConsumer uses ERC7201 storage pattern, so you can safely use it in upgradeable contracts.
This is the underlying library used by the SolrandConsumer base contract. You can use the library directly to generate randomness in your contracts, just make sure to keep the parameters in your contract.
uint requestBlockNumber = block.number;
uint targetBlockNumber = block.number + 10;
uint nonce = 1;
bytes memory requesterIdentifier = abi.encodePacked(
// arbitrary data like user address, NFT ID they hold, in game points amount, etc.
"unique requester identifier",
address(0x1337),
timestamp
);
bytes32 requestId = SolrandLib.requestRandomness(
targetBlockNumber,
nonce,
requesterIdentifier
);
// Wait until the target block +1 is reached (+1 because the blockhash of current block is not
// available, only the last 256 block hashes are available)
// In foundry you can use the following cheatcode to wait for the block:
// vm.roll(targetBlockNumber + 1);
// Consumer contract should hold this data like shown in the SolrandConsumer
uint256 randomNumber = uint256(
SolrandLib.fullfillRandomness(
requestBlockNumber,
targetBlockNumber,
nonce,
requesterIdentifier
)
);
pnpm build
pnpm test
pnpm prettier:write
forge script script/SolrandConsumerDeployment.s.sol:SolrandConsumerDeploymentScript --rpc-url <your_rpc_url> --private-key <your_private_key> --broadcast
FAQs
Simple randomness library for Solidity
The npm package solrand receives a total of 2 weekly downloads. As such, solrand popularity was classified as not popular.
We found that solrand demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.