paint-brush
wn your Identity in Blockchainby@achiever.hitesh
325 reads
325 reads

wn your Identity in Blockchain

by Hitesh JoshiApril 1st, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Have you tried transferring dough(Ether) on <a href="https://hackernoon.com/tagged/ethereum" target="_blank">Ethereum</a>?
featured image - wn your Identity in Blockchain
Hitesh Joshi HackerNoon profile picture

Have you tried transferring dough(Ether) on Ethereum?

If yes, then you would know that what a pain it is to remember your creditors wallet address. How cool it would be if you can transfer the Ether just using her name.

If you could do something like this

dnsContractInstace.sendEtherToName(“Hitesh”, 10 Ether) and whichever address is the owner of Hitesh will get the 10 Ether credited to his wallet.

So in this post, we will try to learn the building blocks of solidity and DApp development while building a Naming Service in Ethereum.

We are going to write few Smart contracts today that would allow us to reserve our names as our identity in Ethereum and then transact using those names. That said, this article is not a getting started intro on Ethereum or solidity or Truffle.

That’s it? Nope… We will also do the following -

See how you can bid on a name of your liking.

Get the highest bid on a name. Bids are stored in the contract.

Accept bids if you are the name owner.

Withdraw bids that you have placed — withdraw pattern

Send ether to a Name instead of an address.

Is that it???

Nope not yet, we will also do a lot of peripheral functions , namely -

Send initial reservation fee to the contract creator.

Check name characters and limit the name size you can reserve.

Check that the malicious contract code cannot call your withdraw functions over and over again to empty your contract balances.

And finally, we will -

Write JS test cases with truffle to test contract code.

Write an Angular 4 DApp that is integrated with Metamask to use the contract code that we have written.

High level arch diagram

Architecture Diagram

What you would need to get started -

Your own private Geth node or Ganache or ability to connect to Ropsten

Node Js

Angular cli

Truffle

Remix or Ethereum wallet for quick and dirty testing

Enough prelude, lets get down to code…

First a library with state enums and basic Struct that we will use in our contracts

Now comes the design question, how will you store all the bids in a contract. Using an array is limiting and gas costs are insane. We will create a bid container for every name which will hold a mapping for all the bids that are submitted for a name and a owner.

We will represent a bid as

And to hold all bids against a particular name, the container

The code is heavily commented in Github do check it out, I have removed most of it in the gist.

We also a util contract which allows you to convert a string to bytes32. We also have ProtectReentry contract that tries to stop contract code calling your code over and over again. I have omitted them in this writeup for brevity.

Finally, the main contract itself.

Now let’s write a test case , testing all this…

This is just 1 of the test cases,you will find quite a few in the repository.

It basically reserves Beatles using account[2](nameOwner). Then a bidding war ensues between bidderOne and bidderTwo. Finally bidderOne caves in and withdraws his bid. We finally check that the total ether set in the contract at this point of test execution. You can easily check individual balances or accept a bid and see the outcome after Name ownership is transferred.

Finally we also build a very basic Angular Dapp to interact with the deployed smart contract. The Dapp integrates with Metamask to allow you to sign the transactions that are going to ethereum network. Any operations such as Reserving a Name, Bidding on Name etc would have to be sanctioned by the wallet.

You need to create or import your accounts in Metamask to use this. Also, if like me you are using a private node, you need to start it with a special flag

 --rpccorsdomain="chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn

One open issue in the code is that bidding as of now has no end. So any bidder who was out-bidden can take out his funds from the contract, but if you are the topmost bidder for a name and the owner has not accepted your bid your funds are locked into the contract :(

We will try to fix this in our next post. I am thinking of adding a bidPeriod to the bids. On expiry of the bidding period even the highest bidder can take his funds out. So there will be a lock in , but once you are out-bidden or the bidding period expires you are free to take your Ether out.

Complete source code is hosted in my github repo here. Please check it out and play with it. Star it if you like it. Cheers !

Considering that folks have made commercial software along these lines, see this. If you wanna take it further, pull requests and collaboration requests are always welcome.

That’s it folks !

Connect with me on LinkedIn or follow me on Twitter.