From fe6ac408b28f584e555740ca54cd92b374212b4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matev=C5=BE=20Jekovec?= Date: Tue, 20 Feb 2024 12:04:24 +0100 Subject: [PATCH] contracts: Require message() call to be signed --- backend/contracts/MessageBox.sol | 11 +++++++++-- backend/test/MessageBox.ts | 2 +- frontend/src/views/HomeView.vue | 4 ++-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/backend/contracts/MessageBox.sol b/backend/contracts/MessageBox.sol index 366c734..1229c5d 100644 --- a/backend/contracts/MessageBox.sol +++ b/backend/contracts/MessageBox.sol @@ -2,11 +2,18 @@ pragma solidity ^0.8.0; contract MessageBox { - string public message; + string private _message; address public author; function setMessage(string calldata in_message) external { - message = in_message; + _message = in_message; author = msg.sender; } + + function message() external view returns (string memory) { + if (msg.sender!=author) { + revert("not allowed"); + } + return _message; + } } diff --git a/backend/test/MessageBox.ts b/backend/test/MessageBox.ts index f45e838..c1788cd 100644 --- a/backend/test/MessageBox.ts +++ b/backend/test/MessageBox.ts @@ -9,7 +9,7 @@ describe("MessageBox", function () { return { messageBox }; } - it("Should send message", async function () { + it("Should set message", async function () { const {messageBox} = await deployMessageBox(); await messageBox.setMessage("hello world"); diff --git a/frontend/src/views/HomeView.vue b/frontend/src/views/HomeView.vue index bae0685..d08fe9b 100644 --- a/frontend/src/views/HomeView.vue +++ b/frontend/src/views/HomeView.vue @@ -32,8 +32,8 @@ function handleError(error: Error, errorMessage: string) { } async function fetchMessage(): Promise { - const message = await uwMessageBox.value!.message(); - const author = await uwMessageBox.value!.author(); + const message = await messageBox.value!.message(); + const author = await messageBox.value!.author(); return { message, author }; }