CodeChain SDK in Python
By: Geunwoo Kim
Tags: Codechain, Blockchain, Python, Sdk

The alpha version of CodeChain-sdk-python(hereafter sdk-python) has been released, and now it’s possible to use CodeChain’s SDK in Python projects. Currently, only Python version 3.6 and above are provided. sdk-python ported the existing CodeChain-sdk-js to Python and provides most of the functionality supported by sdk-js except for the browser-specific functions.

Sub Module

Unlike sub libraries in the existing sdk-js, which are managed in different repositories, sdk-python manages the sdk-js modules’ primitives, keystore, rpc, crypto, and sdk as submodules. This makes each submodule both easier to manage and access. Each sub module can be used as follows:

Primitive

The main data types used in the SDK are defined in this module, and most submodules have dependencies on this module.

For example, the unsigned integer and hex string types are provided in the module. Fundamentally, it converts to a string and provides the function that checks whether an arbitrary data is of that type or not. In addition, it also provides diverse methods according to each characteristic of data. Unsigned integers provide arithmetic operations, RLP encoding, JSON encoding, etc. Hex strings also provide RLP encoding and JSON encoding.

In addition, AssetAddress and PlatformAddress, which are used by the CodeChain, are provided in this module. Address classes provide the ability to receive keys and generate addresses.

Keystore

This module is for managing CodeChain’s keystore. The CodeChain keystore uses the same format as Ethereum’s web3 secret storage definition.

CodeChain keystore basically manages three types of addresses. Currently, python-sdk only supports PlatformAddress and AssetAddress, and functionalities related to hdwseed will be added later. The keystore module allows users to more efficiently and easily manage the keys required to use CodeChain.

RPC

This module makes it easy to use CodeChain RPCs. Basically, it is a module composed of functions that send RPC calls and return values when data is given as an argument. It is classified into 6 groups: account, chain, devel, engine, mempool and net.

These functions are called thin rpc functions, which allow users to create applications that use CodeChain’s rpc suited for their own purposes. The SDK submodule, which provides a number of features, also uses the thin rpc functions of the RPC module.

Crypto

This module provides the crypto functions used in the CodeChain. Typically, ECDSA functions are provided in this module. In addition, the hash functions used in CodeChain, as well as crypto functions related to addresses, including bech32, are defined in this module.

SDK

This module provides a top-level class of sdk-python. This module allows users to create applications that use CodeChain more effortlessly and conveniently. The classes provided by the SDK module are called helper classes. The alpha version only provides the functionality of importing accounts and creating assets. All other features provided by sdk-js will be added in the near future.

Usage

The main module for developers using sdk-python is the sdk module. The sdk module contains definitions of the SDK class, which is a helper class. Users can use the SDK helper classes appropriately for their needs, and can import and customize submodules (primitive, key, rpc, crypto) if necessary.

Now let’s take a quick look at the example code, focusing on the features currently supported by the helper class. First, install the codechain module in your Python environment with the following command:

For convenience, let’s say you are running a CodeChain node in your local environment. Below is the code to import the private key of the platform address whose address is “tccq9h7vnl68frvqapzv3tujrxtxtwqdnxw6yamrrgd” to the node and create an asset using it. The above address is on the genesis block when the CodeChain node is initialized in solo mode and has 10000000000000000000CCC.

If you import the account’s private key to the node as follows, you only need to provide the passphrase without signing anything when sending a transaction to that account.

After importing the account, this code creates a mint transaction and sends it to the node. Since you have imported the private key before, you can successfully execute the mint transaction on the node by providing only the platform account and passphrase to mint the asset through the mint transaction.

Conclusion

The sdk-python alpha version manages all the sub-libraries within a single module, unlike the js version, allowing various function access much simpler. Currently, only the submodules, except for the sdk module, are fully supported, and only the ability to import accounts to nodes and send mint transactions through the helper class is implemented. Other features will be completed soon.