-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
315 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
|
||
contract PriceOracle { | ||
uint256 public currentPrice; | ||
address private owner; | ||
address public authorizedSigner; | ||
|
||
event PriceUpdated(uint256 newPrice); | ||
|
||
constructor(address _authorizedSigner) { | ||
owner = msg.sender; | ||
authorizedSigner = _authorizedSigner; | ||
} | ||
|
||
modifier onlyOwner() { | ||
require(msg.sender == owner, "Only owner can call this."); | ||
_; | ||
} | ||
|
||
|
||
function updateAuthorizedSigner(address _newSigner) public onlyOwner { | ||
authorizedSigner = _newSigner; | ||
} | ||
|
||
|
||
function updatePrice(uint256 _newPrice, uint256 _nonce, bytes memory _signature) public { | ||
require(recoverSigner(_newPrice, _nonce, _signature) == authorizedSigner, "Invalid or unauthorized signature"); | ||
currentPrice = _newPrice; | ||
emit PriceUpdated(_newPrice); | ||
} | ||
|
||
|
||
function recoverSigner(uint256 _newPrice, uint256 _nonce, bytes memory _signature) internal pure returns (address) { | ||
bytes32 dataHash = keccak256(abi.encodePacked(_newPrice, _nonce)); | ||
bytes32 messageHash = prefixed(dataHash); | ||
|
||
// Split the signature into r, s, and v variables | ||
(bytes32 r, bytes32 s, uint8 v) = splitSignature(_signature); | ||
|
||
// Perform the ecrecover operation to determine the signer | ||
return ecrecover(messageHash, v, r, s); | ||
} | ||
|
||
// Internal function to split a signature into r, s, and v | ||
function splitSignature(bytes memory sig) | ||
internal | ||
pure | ||
returns (bytes32 r, bytes32 s, uint8 v) | ||
{ | ||
require(sig.length == 65, "Invalid signature length"); | ||
|
||
assembly { | ||
// First 32 bytes, after the length prefix | ||
r := mload(add(sig, 32)) | ||
// Second 32 bytes | ||
s := mload(add(sig, 64)) | ||
// Final byte (first byte of the next 32 bytes) | ||
v := byte(0, mload(add(sig, 96))) | ||
} | ||
|
||
if (v < 27) { | ||
v += 27; | ||
} | ||
} | ||
|
||
|
||
function prefixed(bytes32 hash) internal pure returns (bytes32) { | ||
return keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", hash)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.