0

I am using truffle, ganache and web3. I am initializing user id in smart contract while deploying the smart contract. As of now, everything is done by account[0] of Ganache.

Now, I want functionality like, if ## Heading ##I am creating any user id in smart contract then it has to be assigned with unique hash which is given by blockchain.

For that, I have no any idea that how can we integrate nwely created user to the account of ganache.

Is there any other way to do that or is anyone has any reference link for that ?

0

You just need a method addUser(name) which adds a newly created user.

The hash can then be calculated by keccak256() to generate a bytes32 hash. Now it depends on the use case how you would like to store it. Maybe a structure of user details or mapping with uint id => string hash.

As of now, everything is done by account[0] of Ganache.

Yes, that's default. If you are mentioning this, I guess you want only account[0] or the contract deploy-er to be able to add users.In that case you just need to have a require(msg.sender==admin) where admin is the guy assigned during contract deploy.

Edit

I read your question again. If you are thinking each user on contract will directly have a mapped account.. No! You need to explain more what you want to achieve.

Update

After your comment, it looks like you need to allocate address to user. TestRPC/Ganache works differently.

These are the default created accounts under YOUR control.You can easily give away the public address of the accounts but that does not solve any purpose. Why? Because the user should have their own private key to do some kind of transactions.

For this, it totally depends now.The users should and should create their own accounts and give you the public address.This address should now stay with the contract.

But if you still want to do that way and serve as an admin to user, just pick an an account, say account[5]. Place it in a local DB (Optionally) that account[5] is allocated to username 123user. Create a mapping in contract, call the function to assign the address. when you call the function, just pass the address of account[5] and username. Rest everything follows.

A better way will be to generate bip39/bip44 wallets.

  • I have many users in my system, as of now it is for demo purpose, so let's say I have 10 users in my system in which currently I am creating them from constructor and yes everything will be done by account[0]. And Ganache has also 10 accounts. So when I am adding any user to blockchain the address must assigned to the 1st user,then 2nd address to 2nd user. So, in future we can fetch the transaction details of any particular user from that address not by user id which I am currently doing. So, how can we achieve it ? – Riya Soni Jul 23 '18 at 8:24
  • Updated my answer – rohan sethi Jul 23 '18 at 8:41
  • Ok, If I don't want to use the local DB approach, and as of now I just want to do the things like if I add User1 then it has to be mapped account[0] then User2 with account[1] and then we can fetch the transaction by account address,so how to achieve that using address data type in Solidity ? – Riya Soni Jul 24 '18 at 4:07
  • Then just like I said, just get the account address using web3, call contract method which updates a mapping uint userId => address account. That is it. Rest there is a lot to explain. – rohan sethi Jul 24 '18 at 9:15
  • But how can we get the address of account, let's say I am deploying the contract like this "deployer.deploy(AssetContract,[2001014800086] , {from:web3.eth.accounts[1],gas: 6700000})". Means it will be deployed from account[1] so in my contract I can map "2001014800086" user id with msg.sender which is account[1]. Now if I am passing another user id in above code snippet then how to map that id with account[2]? – Riya Soni Jul 25 '18 at 10:17

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.