First Steps With Go Ethereum

Oren Zakay
Kin Blog
Published in
4 min readNov 16, 2017

As part of our new journey at Kik, we started working on the Kin wallet SDK for mobile. This is a great opportunity to take a deeper step into the world of cryptocurrency. We had to research and decide if the wallet SDK should be native Android and iOS or Javascript, and whether it should be deployed via binary or WebView. The options were:

  1. go-ethereum — Go library supported by Ethereum
  2. web3.js — Javascript library supported by Ethereum
  3. web3.j — Lightweight Java and Android library

We explored and created a simple integration with those options. I explored go-ethereum right away, reading their docs and taking a few online courses, but I wanted to do more. I had to build something with go-ethereum myself in order to better understand how everything works. The integration with Geth (go-ethereum) is pretty simple. I started running a Geth node on my Mac and synced the block history on Mainnet. It took a while to sync, but eventually I had a fully running Geth on my Mac.

My next goal was to set up an Ethereum node on a mobile device. It took hours, but it was just a POC so I thought, “Why bother having a full node on my mobile device?” I consulted with some of our blockchain specialists and asked if we had to have a full or light node on the mobile device. They told me it is not a requirement at the moment and suggested to use Infura as a node provider.

The experience was smooth, and I had a connection to Testnet quickly. I created an account and started sending tokens to the world ;) You can send to yourself test tokens here on Ropsten. Enjoy!

With go-ethereum, I created a simple app that allows users to create as many accounts as they wish with a public address and then added a feature that checks the balance. Then, I played with the ability to send transactions passing ETH from one account to another. It was a really great feeling building the app and see how it all fits together.

But now, how do we spend KIN and not ETH? I connected with our Kin token contract, which is already deployed on Ethereum. At first glance, it looked easy: create a BoundContract object and call any function in the contract. While playing with the BoundContract I encountered a problem that apparently many developers experienced: "abi: cannot unmarshal *big.Int in to []interface {}". I did a lot of searches for a solution for this issue, which all led to the same conclusion: it wasn’t resolved yet, and I had to solve it by myself.

I forked go-ethereum and went to work on the problem. It took time to get familiar with the Go code as I’m not familiar with that language (I’m experienced in Java), but I finally found a solution and submitted a pull request (PR).

The problem: On mobile you should pass a Slice (analogous to arrays in other languages) of interface{} (Go type) that will contain the results. It is used as a pointer to an object referenced from Java to Go. For example, the method balanceOf() returns single output *big.Int. So while passing a slice of interface{}, the code was trying to put the single output into a slice of interface{} and not to a single interface{}. Therefore, it can’t unmarshal the single output into the slice of results interface{}.

The solution: In account/abi.go there is a method being called unpack.singleUnpack(v, output). It is implemented for two objects types: method.go and event.go. However, the current implementation does not handle the case of receiving slice, and on mobile, the BoundContract.call(..) signature is always for a slice. So I added the case for receiving a slice, such that it would take the first element of that slice and copy the output to it. Otherwise — keep the old implementation as it was. It’s working (tests are passing), and I got the balance correct.

I’ve submitted the PR to go-ethereum and am now awaiting for feedback.

I had fun with my first experience with go-ethereum and can’t wait to continue to the next task — building a Kin mobile SDK. Feel free to contact me with any questions about Geth on Android.

--

--