diff --git a/contracts/AbstractRandomnessReceiver.sol b/contracts/AbstractRandomnessReceiver.sol new file mode 100644 index 0000000..fae431b --- /dev/null +++ b/contracts/AbstractRandomnessReceiver.sol @@ -0,0 +1,19 @@ +// SPDX-License-Identifier: MIT +pragma solidity 0.8.24; + +import {IRandomnessReceiver} from "./IRandomnessReceiver.sol"; + +abstract contract AbstractRandomnessReceiver { + address randomnessRequester; + + error NotAuthorizedRandomnessProvider(); + + modifier onlyRandomnessProvider(){ + if (msg.sender != randomnessRequester) revert NotAuthorizedRandomnessProvider(); + _; + } + + constructor(address _randomnessRequester) public { + randomnessRequester = _randomnessRequester; + } +} \ No newline at end of file diff --git a/contracts/IRandomnessRequester.sol b/contracts/IRandomnessProvider.sol similarity index 92% rename from contracts/IRandomnessRequester.sol rename to contracts/IRandomnessProvider.sol index 83d3b91..58b784c 100644 --- a/contracts/IRandomnessRequester.sol +++ b/contracts/IRandomnessProvider.sol @@ -3,14 +3,14 @@ pragma solidity 0.8.24; import "./TypesLib.sol"; -interface IRandomnessRequester { +interface IRandomnessProvider { /** * @notice Requests the generation of a random value for a specified blockchain height. * @dev Initiates a randomness request. - * The generated randomness will be associated with the returned `requestID`. - * @return requestID The unique identifier assigned to this randomness request. + * The generated randomness will be associated with the returned `requestId`. + * @return requestId The unique identifier assigned to this randomness request. */ - function requestRandomness() external returns (uint256 requestID); + function requestRandomness() external returns (uint256 requestId); /** * @notice Retrieves a specific request by its ID. diff --git a/contracts/IRandomnessReceiver.sol b/contracts/IRandomnessReceiver.sol index 7546ea1..2a3f187 100644 --- a/contracts/IRandomnessReceiver.sol +++ b/contracts/IRandomnessReceiver.sol @@ -6,8 +6,8 @@ interface IRandomnessReceiver { * @notice Receives a random value associated with a specific request. * @dev This function is called to provide the randomness generated for a given request ID. * It is intended to be called by a trusted source that provides randomness. - * @param requestID The unique identifier of the randomness request. + * @param requestId The unique identifier of the randomness request. * @param randomness The generated random value, provided as a `bytes32` type. */ - function receiveRandomness(uint256 requestID, bytes32 randomness) external; + function receiveRandomness(uint256 requestId, bytes32 randomness) external; } diff --git a/contracts/TypesLib.sol b/contracts/TypesLib.sol index a8a582f..114162d 100644 --- a/contracts/TypesLib.sol +++ b/contracts/TypesLib.sol @@ -5,7 +5,7 @@ library TypesLib { // RandomnessRequest stores details needed to verify the signature struct RandomnessRequest { - uint256 requestID; + uint256 requestId; address callback; } diff --git a/contracts/helper/MockRandomnessRequester.sol b/contracts/helper/MockRandomnessRequester.sol new file mode 100644 index 0000000..c1b026d --- /dev/null +++ b/contracts/helper/MockRandomnessRequester.sol @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.8.13; + +import "../IRandomnessReceiver.sol"; +import "../IRandomnessProvider.sol"; + +contract MockRandomnessRequester is IRandomnessProvider { + mapping(uint256 => address) requestIdToReceiver; + uint256 public lastRequestId; + uint256[] requests; + mapping(address => uint256[]) public receiverToRequestIds; + + function requestRandomness() external returns (uint256) { + lastRequestId++; + requestIdToReceiver[lastRequestId] = msg.sender; + requests.push(lastRequestId); + return lastRequestId; + } + + function getRequest(uint256 requestId) external view returns (TypesLib.RandomnessRequest memory){ + return TypesLib.RandomnessRequest(requestId, requestIdToReceiver[requestId]); + } + + function getAllRequests() external view returns (TypesLib.RandomnessRequest[] memory){ + TypesLib.RandomnessRequest[] memory allRequests = new TypesLib.RandomnessRequest[](15); + for(uint i; i<15; i++){ + address callBack = requestIdToReceiver[requests[i]]; + allRequests[i] = TypesLib.RandomnessRequest(requests[i], callBack); + } + return allRequests; + } + + function messageFrom(TypesLib.RandomnessRequest memory r) external pure returns (bytes memory){ + return bytes(""); + } + + function answerRequest(uint256 requestId, bytes32 randomness) external { + address callback = requestIdToReceiver[requestId]; + IRandomnessReceiver receiver = IRandomnessReceiver(callback); + receiver.receiveRandomness(requestId, randomness); + } +} \ No newline at end of file