1

I want to map names(string) with a wallet address in solidity but solidity throws an error saying that its not allowed!

mapping(string => address) public users;

And this is the error I get:

    TypeError: Dynamically-sized keys for public mappings are not supported.
        mapping(string => address) public users;
        ^-------------------------------------^

Is there any other way to do it?

New here! Suggestions would be appreciated! Thanks in advance!

  • Yeah, change the public to internal or private. – goodvibration Nov 13 '19 at 11:05
3

Change this:

mapping(string => address) public users;

To this:

mapping(string => address) private users;

Or to this:

mapping(string => address) internal users;

And add this if you must:

function getUser(string str) public view returns (address) {
    return users[str];
}
  • Today morning even I was thinking of doing the same and then opened stackexchange to see this. Anyways thanks! – vteja711 Nov 14 '19 at 5:05
  • @vteja711: You're welcome :) – goodvibration Nov 14 '19 at 7:35
1

Just to add to goodvibration's answer, it has to do with Solidity's inability to automatically construct the inspection function requested with public - hence the suggestion to drop it.

It also has to do with the compiler version. Here you can see it works fine with 0.5.11.

pragma solidity 0.5.11;

contract StringMap {
    mapping(string => address) public users;
}

So, actually you can.

Subject to gas cost testing, I would consider using a bytes32 instead, and a hash function over any string that might be in play.

string x;
bytes32 key = keccak256(bytes(x));

Lastly, as a side note, in most cases it would be better to look up users up by their address instead of relying on another input to discover it. msg.sender is usually used to reliably authenticate/positively identify the signer.

Hope it helps.

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.