0

I'm trying to setup a system where you can attach a name and website url to an ethereum address. Then have it get called back in an ethereum block explorer I'm building. What are some ways where a user can attach this data to their own account? And how would one call it back? I know you can attach data when you send out via the HEX values, but how would one do it in this scenario?

  • Users self-register or back-office registers the users? – Rob Hitchens - B9lab Apr 12 '17 at 16:40
2

Here's a simple example with the users maintaining their own information.

For simplicity, I've got the users first registering with a function made for that purpose, and then updating (if needed) with functions made for that purpose.

pragma solidity ^0.4.6;

contract UserRegistry {

    struct UserStruct {
        string name;
        string url;
        uint userListPointer;
    }

    address[] public userList;
    mapping(address => UserStruct) public userStructs;

    function isUser(address user)
        public 
        constant
        returns(bool isIndeed)
    {
        if(userList.length==0) return false;
        return userList[userStructs[user].userListPointer]==user;
    }

    // Assuming user is trusted to maintain information about themselves ... 

    function registerAsUser(string name, string url)
        public
        returns(bool success)
    {
        if(isUser(msg.sender)) throw; // duplicate prohibited
        userStructs[msg.sender].name = name;
        userStructs[msg.sender].url = url;
        userStructs[msg.sender].userListPointer = userList.push(msg.sender) - 1;
        return true;
    }

    function updateUserName(string name)
        public
        returns(bool success)
    {
        if(!isUser(msg.sender)) throw;
        userStructs[msg.sender].name = name;
        return true;
    }

    function updateUserUrl(string url)
        public
        returns(bool success)
    {
        if(!isUser(msg.sender)) throw;
        userStructs[msg.sender].url = url;
        return true;
    }

}

Client side depends on the language. Assuming Web3 JavaScript and Truffle framework, client would do something like this (not tested):

var address = "0x123";
var userRegistry;
UserRegistry.deployed().then(function(instance) { userRegistry = instance; });

// is this address registered?
userRegistry.isUser(address).then(function(isIndeed) { console.log("Is registered", isIndeed); });

// what information do we have about this user?
userRegistry.userStructs(address).then(function(userInfo) { console.log("User Info:", userInfo); });

Also possible to iterate over the list of registered addresses:

// get address from row 0 on the list
userRegistry.userList(0).then(function(addr) { console.log("address:", addr); });

Can add a simple a simple contract function to get a count to help with iteration over the keys

function getUserCount() public constant returns(uint userCount) {
  return userList.length;
}

Function results in Remix to show it working:

enter image description here

Possible to extend this with delete function; omitted for brevity.

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.