Min Read

Kaleido and Nethereum

Kaleido and Nethereum
Since this post was written, Hyperledger FireFly has reached 1.0. Learn more here!

Recently, we’ve received questions about connecting to the Kaleido platform via Nethereum, an open source .NET integration library for Ethereum. To test this, I’ve written a short application to connect to a Kaleido node and get the balance of an account. You can check it out here. Before we can run this, we need to set a few things up first.

Set up

Obviously, the most important thing we need when testing connection to a Kaleido node is a Kaleido node itself. If you don’t already have one, you can follow the documentation here on setting up a network. After you have an environment with at least one node, you will need to create app credentials in order to securely communicate with the node. To do so:

  1. Navigate to the environment containing the node you wish to target
  2. Click the Add dropdown in the top right portion of the screen
  3. Select New App Credentials
  4. Select the membership you wish to associate the credentials with
  5. Supply a name for the credentials and click Next
  6. Be sure to save the credentials. The password is ephemeral and cannot be redisplayed
New App Credentials
App credentials illustration

Next we need to fund an account so that we can get a non-zero balance to ensure this actually works. Using the environment-specific Kaleido Ether Pool, you have the ability to send ether to any valid Ethereum address; however it’s probably better that we send ether to an account we actually control. So let’s grab a user account address for one of the nodes in our network. You can find this by clicking on a node to see the node details page. User accounts will have at least one address at the bottom of the list. We also need the RPC endpoint for a node with the same membership as the app credentials. Take note of this so we can use it later.

Node Detail Page

Now that we have an address, we can go to the Ether Pool, which is located at the bottom of the Environment page. Here we can input the account address we just copied and an ether amount. Click Fund and the account will get funded!

Fund account illustration
Test it out!

Now we want to clone the GitHub repo I mentioned earlier and open the Program.cs file under the Application directory. I’ll be using Visual Studio 2015 to edit and run this application.

static string RPC_ENDPOINT = "";
static string USER = "";
static string PASS = "";
static string ACCOUNT_ADDRESS = "";

These are the the only fields you need to change in order to run the program. Here, you’ll add the RPC endpoint of the node in quotes, the username of the application credentials, the password for the credentials, and the account address. Now, you can run the application and see the ether balance print out in the debug console.

Visual Studio with arrows to the start button and ether balance output

To break this down, we have a main function that gets executed initially, calls the getEthBalance function that does most of the work, and then prints out the balance once the getEthBalance function is finished.

In the getEthBalance function, we need a Nethereum.Web3 object to talk to the node and pull info from it. To build this we need an implementation of an IClient object, so we’ll use the RpcClient object since we have the RPC endpoint of our node. First though we need to encode our app credentials in the form “username:password”, into a header value for RPC communication to our node.

var byteArray = Encoding.ASCII.GetBytes(USER + ":" + PASS);
AuthenticationHeaderValue auth = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));

Then, we can create an RpcClient object using our RPC endpoint and the auth object from above.

IClient client = new RpcClient(new Uri(RPC_ENDPOINT), auth, null, null, null);
var web3 = new Web3(client);

Now we can call the web3 function to get the balance of an account. We need to wait on the function call to return, so we use the await keyword here. The returned value is denoted in wei, but we want it in ether so we use another Web3 function to convert it for us.

var some = await web3.Eth.GetBalance.SendRequestAsync(ACCOUNT_ADDRESS);
balance = Web3.Convert.FromWei(some.Value);

And that’s it! Now we have the balance for our address so we can print it out or use it however we want!

Interested in Blockchain?

Start learning blockchain and creating enterprise solutions today with a free Kaleido account!

Create Free Account
Don't forget to share this article!
Interested in Blockchain?

Start learning blockchain and creating enterprise solutions today with a free Kaleido account!

Create Free Account

The Ultimate Enterprise Blockchain Glossary

Your guide to everything from asset tokenization to zero knowledge proofs

Download Now

Swift Utilizes Kaleido in New CBDC Sandbox

Learn how Swift, the world’s leading provider of secure financial messaging services, utilizes Kaleido in its CBDC Sandbox project.

Download Now

Related Posts

Comparing Hyperledger Fabric and Hyperledger Besu: A Deep Dive

Powerhouse Enterprise Protocols: A Comparison of Hyperledger Fabric vs Hyperledger Besu

How to Use the ERC-1400 Standard for Compliant Blockchain Securities

How to Use the ERC-1400 Standard for Compliant Blockchain Securities

Marc Lewis
Managing Editor
How to Manage Digital Asset with Our Next-Gen Asset Manager Service

Asset Manager Service: A Next-Gen Engine for Managing Digital Asset & Tokenization Projects

Marc Lewis
Managing Editor

Blockchain made radically simple for the enterprise

No Credit Card Required
ISO27K & SOC2 Type 2 Compliant
Free Training & Support