Geth Mobile – How to Interact from React-Native

androidgo-ethereum

I'm working on a react-native dapp to get some experience, but I'm struggling to interact with geth from the javascript side. I'm currently only focused on Android. The Geth client is succesfully executed on the start of the application following this guide: Geth mobile introduction. Since the new mobile packages of Geth don't run a rpc anymore, web3 alone won't do the trick.

I have already tried to wrap Geth inside a native module, but it cannot access the Geth running on the main thread as far as I know.

Status is doing a similar thing, but I couldn't wrap my head around how they do it from looking at their code.

Has anyone figured out how to do this already?

Best Answer

I managed to do this today after some painful debugging. The result is here: https://github.com/zupzup/react-native-ethereum-wallet

It's not a full example yet, but it shows how to interact with the running geth node.

Here's what I did:

First step, add the geth dependency to build.gradle:

compile 'org.ethereum:geth:1.6.7'

Then, in MainActivity.java, try to get the node running. You'll see if it's running correctly in the Android Monitor. This looks something like this:

NodeConfig nc = new NodeConfig();
Node node = Geth.newNode(getFilesDir() + "/.eth1", nc);

Then, you need some way to expose this node to your Wrapping module. I just used a Singleton for this, but I guess there are cleaner ways to share state in Android.

Regarding the wrapping, I just used the official docs at https://facebook.github.io/react-native/docs/native-modules-android.html.

Basically, I created a method test, which fetches the node from the singleton and executes something on it (in my case a simple getBalanceAt query).

And this test method can be called from React-Native with a callback, if you register the wrapping module correctly. Like this:

NativeModules.TestNative.test("tojavandback", (str) => {
    Alert.alert(str);
})

This outputs, in my case, the amount of ether at the given address. It's just a simple example, but the whole end2end interaction works.

Hope this helps.

Related Topic