0

So I need a function that checks if a key fits the hash of that said key. This is what I have:

function checkKey(bytes32 hash, bytes32 privateKey) public returns (bool success) {
    require(keccak256(privateKey) == hash);
    return true;
}

I get the following warnings:

  1. Warning: This function only accepts a single "bytes" argument. Please use "abi.encodePacked(...)" or a similar function to encode the data.

  2. Warning: The provided argument of type bytes32 is not implicitly convertible to expected type bytes memory.

I want to make a game where the user finds private keys and basically gets rewards for it. So I can pretty much choose what kind of strings I hash. What do I need to keep in mind here to make this work?

1
    bytes32 result = keccak256(abi.encodePacked(privateKey));

This converts privateKey to bytes and then the keccak256 function returns a byte32 result which can be compared to the hash (bytes32) as per your question.

| improve this answer | |
0

Chris is right. This is a little PSA - admittedly tangential to your question but possibly useful to others who happen upon this.

A few style comments that might be useful.

  1. It can be helpful to create a safe check function. "Safe" meaning safe to use because it will not revert if the response is "no".
  2. You can use a modifier to strictly require this condition for any state-changing function, so you would get something like this:

-

pragma solidity 0.4.25;

contract CheckKey {

    modifier onlyIfKeyMatches(bytes32 hash, bytes32 key) {
        require(isKey(hash, key));
        _;
    }

    function isKey(bytes32 hash, bytes32 privateKey) public pure returns (bool success) {
        return keccak256(abi.encodePacked(privateKey)) == hash;
    }

    function protected(bytes32 hash, bytes32 privateKey) 
        public 
        onlyIfKeyMatches(hash, privateKey) 
    {
        // carry on 
        // WARN: privateKey is compromised
    }
}

You want to use pure (or view) for the check because it doesn't change the state. This has a number of effects, among which is the local execution. It will not compromise the key because the key won't be broadcast to the network.

On the other hand, there is a non-obvious but potentially serious security issue with the privateKey. protectedFunction() is not view or pure because I presume it does something useful and this will take the form of a transaction. As such, the "private" key will actually be transmitted to the entire network and will remain in plain sight in the blockchain history forever. This is clearly bad news if the key is meant to be a secret.

Use caution with secrets on the blockchain. We have to operate as if everyone can see everything, because they can. If you reveal more about what you're trying to accomplish, possibly someone will chime in with a better pattern.

Hope it helps.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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