Managing game assets with CodeChain

JinGyeong Jeong
CodeChain
Published in
7 min readApr 24, 2019

--

Minting Assets on the Corgi Testnet

In this post, we will use the Corgi testnet, Keystore CLI, and JavaScript SDK to mint assets on CodeChain. The Corgi testnet can be used for testing purposes during CodeChain application development, and CCC is available free of charge through the faucet site. The Keystore CLI allows you to create and store CodeChain addresses.

CodeChain provides JavaScript SDK, making it easy to create and transfer various assets. Let’s try using the SDK to actually mint some assets. To make the blog more interesting, I will reference items from the popular game, World of Warcraft(WoW). The API manual is also a great source of information, so be sure to check it out.

CodeChain’s Transaction Types

Transactions are contained within blocks and different transactions do different things. Transaction types that will be used in this tutorial are as follows:

MintAsset

This is the transaction used to issue assets. It contains metadata such as the asset’s name, description, and icon. Data that characterize these assets are written on the assetScheme.

TransferAsset

This is used to transfer assets between users. Similar to blockchains that use the UTXO standard, TransferAsset contains a list of Inputs and Outputs. The Input contains the hash and index of the previous Output, and also contains the LockScript and UnlockScript, which are used to prove ownership. Both Input and Output contain AssetType and the amount.

In addition, even if an asset has already been minted through MintAsset, it is possible to change that asset’s important information through ChangeAssetScheme, or issue more of those assets with the IncreaseAssetSupply transaction. More details can be found in the CodeChain Transaction Specification document.

Creating an Account

The Keystore CLI provides commands for creating and storing addresses and can be installed with the following NPM command:

npm install -g codechain-keystore-cli

You can create a new account by using the Keystore CLI.

cckey create --network-id wc -t platform

When creating an address, you must specify the network in which the address will be used. The default value is cc, which is the network ID of the mainnet. The Corgi testnet’s network ID is wc.

A keystore.db file is created in the working directory where the CLI command is executed, and the addresses are encrypted and stored in this file. Be careful not to lose the key file and the passphrase you entered when creating the address. This key file can be read and used by the SDK.

The generated address can be verified through the following command:

cckey list --network-id wc -t platform

Once you successfully create an account, you will have access to a CCC address. You can then go to CodeChain Faucet to get some free CCC into your newly created test account.

Setting up the Address that Owns the Assets

Asset addresses are different from the address that we learned to create above. This address shows ownership over assets. Asset addresses are generated by using the standard LockScript and the parameters containing the public key. By using this address, users can exchange assets with one another.

Like Bitcoin scripts, CodeChain uses the output’s LockScript to indicate the owner. Since the LockScriptHash is stored in the Output, you can’t understand what kind of script it is just by looking at the Output. If you use the standard LockScript, LockScript stores the parameters separately, and thus, you can understand what kind of script it is just by taking a look at the LockScriptHash.

The former and the latter both have advantages and disadvantages, but using standard LockScript has the advantage of recognizing asset transfers in apps, such as block explorers and wallets. Furthermore, in the case of an address created using a standard LockScript, users can recognize the address just by taking a glance at the address.

More details on Asset addresses can be found on the CodeChain Address Specification document.

cckey create --network-id wc -t asset

The above command creates an P2PKH(Pay to Public Key Hash) address.

Likewise, it is possible to verify the generated address through the list command.

cckey list --network-id wc -t asset

CodeChain JavaScript SDK Installation

Let’s try using the SDK to create new assets. The CodeChain SDK can be installed easily using the Node Package Manager (NPM).

npm install codechain-sdk

Initialization

To create an object of SDK, you need the URL of the CodeChain RPC server. The RPC server that runs on the Corgi network is “corgi-rpc.codechain.io”.

Creating WoW Tokens

Each asset has an attribute called an AssetScheme. AssetSchemes include the quantity of the asset, name of the asset, description of the asset, and the icon URL of the asset. To publish an asset, you should first create an AssetScheme, and the AssetScheme created will be included later in the MintAsset transaction.

Metadata records the description of game items. In each game, the information of the game item can be known by using the corresponding metadata. The CodeChain explorer defines a standard metadata specification. When metadata is created according to the standard, the explorer displays the item accordingly.

The code above creates an AssetScheme for 100,000 WoW tokens. Once you have created an AssetScheme for an item, you can create a MintAsset Transaction with createMintAssetTransaction, which takes AssetScheme and the recipient as parameters. The recipient is the entity that issued the asset and is the first owner. We will use the previously issued asset address of the game company.

In order to actually include a previously created transaction in a block, you must sign it, and propagate it over the network. At this point, you need an account to sign and pay the fee, so you must pass the CCC address and passphrase. In order to sign, you need to access the keystore. To do so, use the createLocalKeyStore function to read the keystore.db file that’s generated by the CLI command.

When signing a transaction, you must specify the seq and fee. In order to perform MintAsset, you must specify at least 100000 as fee. seq is a field that acts like a nonce in an Ethereum account. You should increment it by 1 every time you send a transaction. The getSeq RPC can be used to retrieve the seq value of the account.

You can check whether a sent transaction is included in a block through the containsTransactions function. If a transaction is rejected due to it containing invalid content, the transaction will not be included in the block. In this case, you can see why the transaction was rejected with the getErrorHint function. However, the reason for the transaction rejection may not exist on the CodeChain node due to various reasons, such as it not being the node that processed that specific transaction, a long time having passed, etc. In cases like these, the reason for rejection would be difficult or impossible to obtain.

In addition, by using CodeChain explorer, you can check whether a transaction is ‘Pending’ or has been added to the block.

Transferring WoW Tokens

Assets can be transferred to other addresses through the TransferAsset transaction. You can create a transaction using the SDK’s createTransferAssetTransaction function.

To transfer an asset, you must insert the UTXO(Unspent Transaction Output) of the asset you want to transfer as the input of TransferAsset. UTXOs can be obtained through the getAsset RPC function, which requires a transaction’s tracker and index. The code below retrieves the UTXO created by MintAsset explained above and inserts the retrieved UTXO as the input of TransferAsset.

Next, add the destination address through the addOutputs function. The output should include an address, asset type, and quantity. Note that in order for TransferAsset to be valid, the sum of inputs and outputs must match. Therefore, if you want to send only 3,000 tokens out of 100,000 to other addresses, you must send the remaining 97,000 tokens back to your address.

Lastly, all inputs require a signature for the TransferAsset transaction to be valid.

The code above signs the 0th input of transferTx. It finds the corresponding address of the UTXO in the keystore and signs it. Similarly to MintAsset, once the transaction is signed and sent with signTransaction, TransferAsset is processed.

Once the TransferAsset above has been processed, 3,000 tokens are sent to the gameUser address, and the gameUser can fetch the UTXO through the Tracker and Index (in this case, 0) of TransferAsset and send the received tokens to other addresses.

Conclusion

We’ve looked at the transactions and addresses of CodeChain and used the SDK to create new assets. In CodeChain, account and asset creation becomes simple. I hope this will be of help to those who are new to CodeChain. The following blog will introduce a more complex scenario that publishes assets and sends them to other users.

--

--