CodeChain SDK in Golang

Seongjae Choi
CodeChain
Published in
5 min readFeb 23, 2020
Image from https://github.com/egonelbre/gophers

We introduce the first version of CodeChain SDK Go! CodeChain SDK, which was available only in JavaScript, is now also available in the Go programming language. You are now able to develop Go language applications that use CodeChain.

The Go language is easy to read due to its simple syntax. It was developed to combine the ease of interpreter languages and the stability of compiled languages. Compilation results come out in a single binary, and compilation time is faster compared to other languages. It’s loved by many programmers for its advantages including garbage collection, memory safety, strongly typed, etc.

With the current version of Go-SDK, you can use all the RPC functions provided by CodeChain, meaning you can use all the features provided by CodeChain. However, this alone is insufficient for a comfortable development process for CodeChain applications. You have to read the documentation carefully to find out through which types of objects the RPC functions communicate with and how the signing process works. Go-SDK provides a number of helper functions to provide a comfortable interface for developers.

Package Description

Go-SDK consists of 6 packages, and each package provides the following features:

1. rpc

The rpc package provides the ability to call any RPC function defined in CodeChain. Depending on the type of RPC, each function is divided into Account, Chain, Devel, Engine, Mempool, and Net.

2. primitives

The primitives package defines the basic types and functions used in CodeChain. 64, 128 and 256 bit integers and 128, 160, 256 and 512 bit data types are provided. In addition, bech32 encoding/decoding functions are defined, and functions that can calculate the PlatformAddress and AssetAddress with bech32 encoding are also provided.

3. vm

The vm package provides the encoding/decoding functions of the CodeChain virtual machine script. This package facilitates writing CodeChain scripts.

4. crypto

The crypto package provides features related to cryptography such as ECDSA, Blake, Ripemd functions, etc. that can be used in CodeChain. Key functions include hash functions, signing and verification functions. This package is crucial when creating transactions.

5. key

The key package provides functions related to the keys used in CodeChain. You can get the public key from the ECDSA private key or the P2PKH LockScript and UnlockScript from this package, as well as generate AssetAddresses and PlatformAddresses.

6. core

The core package provides various functions and objects for each transaction type to enable sending transactions to CodeChain. Currently only three transactions are supported: Pay, MintAsset, and TransferAsset; however, we will bring updates to support all types of transactions in the future.

Tutorial

Let’s go over Go-SDK with a simple tutorial. To demonstrate how to use the Go-SDK, I will show an example of sending CCC to another account in CodeChain in a testing environment.

First, get the CodeChain binaries released from the CodeChain repository. Next, run CodeChain with the solo option. The solo option creates a single machine of CodeChain solo testnet. The RPC node address for the solo testnet is http: // localhost: 8080.

Run CodeChain in solo

Change the settings of the Go language environment to use Go-SDK. Download the latest version of the Go language binary (1.13) from the Golang homepage and download CodeChain Go-SDK with the following command:

go get -u github.com/CodeChain-io/codechain-sdk-go

The environment is now ready for Go-SDK. Now let’s create a Pay transaction. Pay transactions are transactions that send CCC to another account. The PlatformAddress and secret key below are listed in the genesis block of CodeChain solo and are provided with 10000000000000000000CCC by default. Let’s move CCC from this PlatformAddress to another PlatformAddress.

PlatformAddress: tccq9h7vnl68frvqapzv3tujrxtxtwqdnxw6yamrrgd

Secret Key: ede1d4ccb4ec9a8bbbae9a13db3f4a7b56ea04189be86ac3a6a439d9a0a1addd

Let’s create a new PlatformAddress that will receive the CCC. This is done by using the createPlatformAddress function in the key package. If you write the Go code by referring to using ‘tc’ for networkID of solo networks, it is as shown below. This will print out the ECDSA key pair and the PlatformAddress.

Generating PlatformAddress

In order to create a Pay transaction that can be registered in the chain, you need to set up the recipients and the amount you want to send and sign it with the sending account. When signing, you need the secret key and sequence number of the sending account. The sequence number can be obtained through the Chain.GetLatestSeq function of the CodeChain RPC. Send the RLP encoding of the signed transaction to solo’s RPC to include the Pay transaction in the chain. The code below illustrates this process:

Create and send pay transaction

Line 15 initializes rpcClient with solo’s RPC address. Line 18 is the address and secret key of the account mentioned above. On line 21, write down the address you created earlier. Then retrieve the quantity to send, fee, and sender’s sequence number (seq).

In line 27, we create a Pay transaction using the recipient and the receiving amount. Next, sign the transaction by using the sender’s private key, seq and fee. In line 30, we use the Mempool.SendSignedTransaction function to send a Pay transaction to the RPC node. If the transaction fails, line 33 will print the reason for the error. If the transaction is successful, the transaction hash will be printed.

After running your Go code, if you see the message ‘Imported sealed block’ in the terminal running CodeChain solo, you have successfully created a new block containing transactions! If you want to check the block information, you can use the Chain.GetBlockByNumber function from the RPC. The code is shown below:

Printing information of the first block

Conclusion

The first release version of the Go-SDK is missing a lot of implementation compared to the JavaScript version. The keystore is not implemented so the user must store the private key on their own. In addition, in Go-SDK, only three transactions can currently be created: Pay, MintAsset, and TransferAsset. These three transactions were supported first because they are the minimum conditions for issuing and sending assets in CodeChain. After that, we will add helper functions to create and send many kinds of transactions to the node, such as ChangeAssetScheme, IncreaseAssetSupply, WrapCCC and so on.

--

--