Tutorial for Setting up a Domain on the ENS and Hosting Decentralized Content on Swarm [Part 2]

Woody Deck
9 min readJan 5, 2018

--

In the first part, we setup a resolver for a .eth domain, and set the wallet/contract address it should point to in the default ENS resolver contract. Now we will get to the fun part, and host a website that is completely decentralized, and also accessible via a normal browser using a .eth domain.

This tutorial assumes you have geth installed to at least version 1.8, and the latest version of Swarm. Both of these things require the command line to run, and require quite a few dependencies and things to make installation easy (e.g. Homebrew, a popular package manager for macOS.). If you don’t understand how to install geth or Swarm, please read the docs on installation. Go step-by-step, and don’t be afraid to ask for help on the forums or gitter if you get stuck. The documentation for many things is a year old, and it might not be your fault if something doesn’t work. After you are done, come back to this tutorial. Installation procedure varies by OS.

One confusing thing you might come across, is that the binaries for geth and Swarm are housed in the /go/folder, while everything else should be in the /go-ethereum/ folder. This includes your accounts, their keys, the blockchain, the swarm data and requisite files, your testnet (e.g. testrpc), and everything else.

It should look like this in your Home folder (the default bash root) if you did things correctly. I had difficulties at first by trying to set a custom Swarm directory. Stick to the install instructions exactly and you will be fine.

If you have used geth or Mist before, you will know that the Ethereum blockchain is a ridiculous size that takes forever to sync, even on first world internet with super-fast Ajit Pai lanes. The blockchain size is currently around 100GB, which might take you over a week to run through, so if you don’t use geth daily with the full chain, please start geth in the terminal with the light flag.

geth --light
Don’t forget to smash the return key.
This is what things look like once the geth in light mode is started.

Now you will need to make an Ethereum account in geth (in a new terminal window), and set the password.

geth account new
Great success, we have an an Ethereum address in geth now!

After you confirm a password, you instantly get an Ethereum address. This is why a hardware wallet is a must, because if you have a keylogger scraping your box up, you will lose not only your ether, but all your tokens and ENS deeds. It’s a scary world. Your private keys are encrypted with the password on your machine. They are not stored in plain text, so entering your password is the weak point.

The good news for this tutorial is that Swarm is in alpha development, and there is no need to incentivize the hosting of your files. This means that there is no need to send ether to this address. It’s completely free for now, and on the honor system. You will not need to submit any transactions to the blockchain to have your files decentrally hosted in the Swarm.

Let’s Do This!

The Ethereum address we just made, and will use with Swarm, has no bearing on the hashes we will create now with out files. People won’t see this ever, only the hash of your files hosted in the Swarm. You can do this as anonymously as you want to in other words.

So It’s Testnet Time?

You can reference this entire process in the official documentation, but it is out of date and doesn’t give you everything you need. If you have experimented with contracts on a testnet, you may think to first try this on the testnet, but the documentation is out of date, and it is far easier just to do this on the mainnet. If you do wish to reference the documentation, please stick to the ‘Simple’ section. You won’t need to understand more than this for some months.

Before starting Swarm, we need to set the variable BZZKEY to the throwaway Ethereum address we just created. In my case the address was e775fe65253ee57a6d6e53922958b98785cc7cff.

BZZKEY=e775fe65253ee57a6d6e53922958b98785cc7cff

Geth in light mode should still be running, if it is not, start it up again, and then open a second window. Now we will set an alias for the Swarm directory our Swarm binary is in.

alias swarm=’$GOPATH/bin/swarm’

Aliases are per window, and per session, so if you close the window you will have to set it again. You could add Swarm to your PATH environmental variable as well if you wanted a permanent solution.

Now We Finally Get to Start Swarm!

Enter the following command:

swarm --bzzaccount e775fe65253ee57a6d6e53922958b98785cc7cff $BZZKEY

It should now make sense why we set BZZKEY to our Ethereum address. This loads the encrypted key when we load our account. You will just need to enter the password set earlier, and Swarm will start! There’s nothing more you need to do in this window, but don’t close it. You should have both the geth and Swarm windows open now. Swarm will also not run without an instance of geth running.

If you did things right, your second terminal window should look like this.

It’s Time To Set History Straight

In Part 1, we setup up a .eth domain so that resolved to a wallet address. I used latvija.eth because I’m still bitter about losing the auction for lithuania.eth, and incensed by the wanton nationalist smugness of my friends in Riga when I last visited them. Not only can I fundraise my revenge on them by receiving ether sent to latvija.eth from patriotic Lithuanians who back me, but I can win the propaganda battle in the meantime by serving a permanent website of alternative history hosted in the decentralized third web.

Some might call this fake news, but not even a world-class übercuck like Justin Trudeau or Emmanuel Macron will be able to take my site down. This is the beauty of decentralization. I don’t have to care what people think of my opinion, because my right to publish is enforced by the blockchain and the Swarm network. There is no pressure a government can use to have my host suspend me, or have a social media company ban my account. If they want to silence me, they will have to kill me. If they want to silence everyone, they have to kill everyone. We are all nodes that prevent acts of aggression just because we look tough all connected together.

Uploading Your First File

Open a third terminal window. Paste in your alias command (if you haven’t added it to your PATH). Next find the path to the file you want to upload. In OSX you can just drag it to the terminal window. This will save you time.

The command to upload one file is up . It’s dead simple. So if you have a lone text file it will look like this:

swarm up /somepath/textfiles/randomfile.txt

Enter this command with any file your choose, and smash the return key again. Like magic you will be given a Swarm file hash.

The hash is that long hashy looking string.

Keep in mind again that Swarm is in alpha, and it may take quite a while to propagate. There are not too many clusters in operation, but you get what you pay for. There is no guarantee your file will stay live, so your best bet is to keep Swarm running on your computer when you can.

I Have a File Hash, Now What?

You can immediately view you file via localhost:

localhost:8500/bzz:/c6190c72240f0ad1129903a3fd4696a67d10141ce8c17c61127d388749259ad8 <--Replace hash with yours. You won't be able to see this one on localhost.

Or you can see if it propagated by using a temporary DNS gateway to the Swarm network: http://swarm-gateways.net/bzz:/c6190c72240f0ad1129903a3fd4696a67d10141ce8c17c61127d388749259ad8/

Why Doesn’t Typing the BZZ Address In a Browser Directly Work?

No browsers (except I think Mist), by default support the BZZ protocol. You can access and download files from the command line, but that’s no fun. To get around this circumstance until browsers introduce support later in 2018, you can install the Chrome plugin “ENS Gateway”, or access it through DNS redirection with http://swarm-gateways.net/bzz:/*hash-or-.eth-domain* .

Time to Make Our .eth Live on Swarm

Like a DNS domain just points to a webserver with its A-record, the ENS can point with a resolver contract to a file hash. To do this, we head back to My Ether Wallet, and the contracts tab to find the ENS Public Resolver contract 0x5FfC014343cd971B7eb70732021E26C35B744cc4.

However, this step is not without confusing fun. You would think you could just paste in the hash, but no, you have to add the prefix0x to your Swarm hash to get it to work. This one had me scratching my head until someone on the Swarm gitter clued me in.

Submit that transaction and wait for it to be confirmed, and boom! You did it. Your .eth domain should now be able to both receive ether, and be accessible in a browser to help serve your decentralized files.

Latvija.eth is live. If you installed the ENS Gateway plugin for Chrome, then you will be able to see the site. You can also use Mist or MEW to send it ether.

The site looks like this. When entering .eth domains with the ENS Gateway plugin, there is no need to enter the “bzz:/”, just type latvija.eth.

Wait, What If I Want to Upload a Full Site?

Yep, that’s just a single file. I wanted after I generated my first hash to use images, JS, and CSS too. This is where I also got stuck, as the command is not in the documentation. The Swarm documentation does list a lot of advanced commands, but not how to set an index to your site.

To set the index, use this:

swarm --recursive --defaultpath /path/to/folder/index.html up /path/to/folder/
The directory we want to upload.
This is what the command looks like successfully done.

Now we have the hash for a full website, 0c9679fc7e42ba9d7579d6345aec07c1851d1aa092b04b18c5235d8a10851a25 . The ENS can only point to one hash, and Swarm hashes cannot be changed or deleted, so you need to set the index and upload all the subfolders recursively. Swarm wraps this all up in one hash that references an index, and you only need to go back to the ENS Public Resolver to point at this hash.

Submit this transaction, and we are done! Latvija.eth is live with a spiffy new design, and one orphaned easter egg.

They are fart smelling scamcoiners!

A Final Note

Swarm is in ongoing development, and it is likely this information could become outdated in the near future. I will keep an addendum here should there be any relevant changes.

If you are happy you learned how to do this, please consider becoming a part of the community. It’s a diverse group of people throughout the world who are helping build this network. We might have vastly different politics (except for those regressive twats in Silicon Valley who think in a hivemind), but we all are working towards the goal of protecting freedom of speech. I love trash talk, and it would be a pitiful world if I couldn’t wind people up or blaspheme the made-up prophet of a stupid religion with memes on the internet.

The tutorial is dedicated to Wolinsky, and Charlie Hebdo. I can’t believe it has been three years. :-( Trump, Trump, Trump!

--

--