From 983d6c36e345aa07f49e3035cdbcba07a4d2b468 Mon Sep 17 00:00:00 2001 From: Mouran Antoine <32228897+L0GYKAL@users.noreply.github.com> Date: Mon, 28 Oct 2024 09:47:52 +0100 Subject: [PATCH 1/4] refactor: change "requestID" to "requestId" --- contracts/IRandomnessReceiver.sol | 4 ++-- contracts/IRandomnessRequester.sol | 6 +++--- contracts/TypesLib.sol | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) 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/IRandomnessRequester.sol b/contracts/IRandomnessRequester.sol index 83d3b91..79bea0c 100644 --- a/contracts/IRandomnessRequester.sol +++ b/contracts/IRandomnessRequester.sol @@ -7,10 +7,10 @@ interface IRandomnessRequester { /** * @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/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; } From 1de6d11c6a4be67b98cbba1da97b28638d1654f1 Mon Sep 17 00:00:00 2001 From: Mouran Antoine <32228897+L0GYKAL@users.noreply.github.com> Date: Mon, 28 Oct 2024 10:05:09 +0100 Subject: [PATCH 2/4] feat: MockRandomnessRequester --- contracts/helper/MockRandomnessRequester.sol | 42 ++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 contracts/helper/MockRandomnessRequester.sol diff --git a/contracts/helper/MockRandomnessRequester.sol b/contracts/helper/MockRandomnessRequester.sol new file mode 100644 index 0000000..da4ed31 --- /dev/null +++ b/contracts/helper/MockRandomnessRequester.sol @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.8.13; + +import "../IRandomnessReceiver.sol"; +import "../IRandomnessRequester.sol"; + +contract MockRandomnessRequester is IRandomnessRequester { + 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 From b8a8f46b1a56399bada753c1f3d863f2518c3c80 Mon Sep 17 00:00:00 2001 From: Mouran Antoine <32228897+L0GYKAL@users.noreply.github.com> Date: Mon, 28 Oct 2024 13:48:21 +0100 Subject: [PATCH 3/4] feat: AbstractRandomnessReceiver.sol --- contracts/AbstractRandomnessReceiver.sol | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 contracts/AbstractRandomnessReceiver.sol 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 From e0175da7c2256d13015c7bf49a1e01fb95406a15 Mon Sep 17 00:00:00 2001 From: Mouran Antoine <32228897+L0GYKAL@users.noreply.github.com> Date: Mon, 28 Oct 2024 14:02:33 +0100 Subject: [PATCH 4/4] Rename "IRandomnessRequester.sol" to "IRandomnessProvider.sol" --- .../{IRandomnessRequester.sol => IRandomnessProvider.sol} | 2 +- contracts/helper/MockRandomnessRequester.sol | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) rename contracts/{IRandomnessRequester.sol => IRandomnessProvider.sol} (98%) diff --git a/contracts/IRandomnessRequester.sol b/contracts/IRandomnessProvider.sol similarity index 98% rename from contracts/IRandomnessRequester.sol rename to contracts/IRandomnessProvider.sol index 79bea0c..58b784c 100644 --- a/contracts/IRandomnessRequester.sol +++ b/contracts/IRandomnessProvider.sol @@ -3,7 +3,7 @@ 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. diff --git a/contracts/helper/MockRandomnessRequester.sol b/contracts/helper/MockRandomnessRequester.sol index da4ed31..c1b026d 100644 --- a/contracts/helper/MockRandomnessRequester.sol +++ b/contracts/helper/MockRandomnessRequester.sol @@ -2,9 +2,9 @@ pragma solidity ^0.8.13; import "../IRandomnessReceiver.sol"; -import "../IRandomnessRequester.sol"; +import "../IRandomnessProvider.sol"; -contract MockRandomnessRequester is IRandomnessRequester { +contract MockRandomnessRequester is IRandomnessProvider { mapping(uint256 => address) requestIdToReceiver; uint256 public lastRequestId; uint256[] requests;