1

in another solidity study, I want to implement a contract to store user links. users must log in using the wallet address and be able to save data by name, description, type, and the link itself. users should also be able to get their links using the same parameters. I managed to write the code below. Tell me why it is bad or good and how it can be secured or optimized.

// SPDX-License-Identifier: AGPL
pragma solidity ^0.8.0;

// Contract Definition
contract DataStorage {

    // Owner address
    address private owner;

    // Addresses that can call contract functions
    mapping(address => bool) private authorizedAddresses;

    // Data structure
    struct Data {
        string dataType;
        string data;
    }

    // Mapping to store data by type
    mapping(string => mapping(address => Data)) private dataByType;

    // Contract constructor
    constructor() {
        owner = msg.sender;
    }

    // Modifier to check that the function is called only by the contract owner
    modifier onlyOwner() {
        require(msg.sender == owner, "Only owner can call this function");
        _;
    }

    // Modifier to check that the function is called only by an authorized address
    modifier onlyAuthorized() {
        require(authorizedAddresses[msg.sender], "Not authorized");
        _;
    }

    // Function to add an authorized address
    function addAuthorizedAddress(address _address) public onlyOwner {
        authorizedAddresses[_address] = true;
    }

    // Function to remove an authorized address
    function removeAuthorizedAddress(address _address) public onlyOwner {
        authorizedAddresses[_address] = false;
    }

    // Function to store data
    function storeData(string memory dataType, string memory data) public onlyAuthorized {
        dataByType[dataType][msg.sender] = Data(dataType, data);
    }

    // Function to retrieve data
    function retrieveData(string memory dataType, address userAddress) public view returns (string memory) {
        return dataByType[dataType][userAddress].data;
    }

}

1 Answer 1

3

First thing first, never store any private information on blockchain.

Another thing is strings are costly you should abi encode the data at the time of storing and abi decode them while retrieving.

2
  • this is more like advice than a complete answer to my question. And according to my idea, the data should be stored openly, and if something private needs to be placed there, then this something will be encrypted before sending the data to the contract Apr 7, 2023 at 7:33
  • Yeah that's an advice ser. Talkin about optimization use external instead of public if the functions are not being called inside the contract. Apr 7, 2023 at 9:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.