Skip to main content

JavaScript

This section describes the different parts of the client code file for the JavaScript/TypeScript postchain-client. See Post a transaction with the Chromia JavaScript client topic for more information about using the client code file.

Key pair

A public and private key pair needs to be set to make transactions. The key pair, shown below, only works when running a dapp locally. Otherwise, the key pair should be unique for you as a user and stored somewhere secure.

const adminPubkey = Buffer.from(
"031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f",
"hex"
);
const adminPrivkey = Buffer.from(
"0101010101010101010101010101010101010101010101010101010101010101",
"hex"
);

Node connection

To connect to a Postchain node you need to know its REST API URL and blockchain identifier.

const nodeApiUrl = "http://localhost:7740/"; //Using default postchain node REST API port
const blockchainRID = "<BlockchainRID>"; //Dapp Blockchain RID
const rest = pcl.restClient.createRestClient([nodeApiUrl], blockchainRID); //REST client connection
note

The Blockchain RID is unique for each setup.

Once the information about the REST client connection is added, you can create the GTX client connection. This needs to receive the previous REST client connection, the Blockchain RID and an array with the names of the operations you want to call.

const gtx = pcl.gtxClient.createClient(rest, blockchainRID, ["set_name"]); //gtx Client connection

Now that the connection is set, you can start to create transactions and queries.

Transaction

You need to create the transaction client side, sign it with one or more keypairs, send it to the node and wait for it to be included into a block.

First, the transaction is created and specified with the public key of the person that signs it.

const request = gtx.newTransaction([adminPubkey]); //Create transaction

Once the transaction has been created, it's possible to call as many operations as you want.

request.set_name("Developer"); //Call operation
request.addOperation("nop", randomInt(12345678)); //A random int to make the hash of the transaction unique

This example calls the operation specified in the Rell file.

operation set_name(name) {
my_name.name = name;
}

Now, the only things left are to sign and post the transaction.

request.sign(adminPrivkey, adminPubkey); //Sign transaction
await request.postAndWaitConfirmation(); //Post to blockchain node
note

The request.postAndWaitConfirmation() returns a promise, and thus can be await-ed.

Query

Queries also make use of the GTX client. The gtx.query accepts the name of a query, as specified in the query module.

const result = await gtx.query("hello_world");

This example works with the query that's specified in the Rell file.

query hello_world() = "Hello %s!".format(my_name.name);

Configure client for Chromia network

To connect to the Chromia network, a URL and a Blockchain RID of a Chromia node running the D1 chain (service chain) have to be given.

const chromiaD1blockchainRID = "<BlockchainRID>"; //Target Blockchain RID
const chromiaD1Url = "<TargetUrl>"; //Target URL

Create an instance of the REST client and configure it for a specific Chromia base URL and Blockchain RID.

const restD1 = pcl.restClient.createRestClient(
[chromiaD1Url],
chromiaD1blockchainRID,
5,
1000
); //REST Client connection

Create an instance of a Chromia client provider. It uses the REST client instance and allows calls to the D1 chain.

const chromiaClient = pcl.chromiaClient.chromiaClientProvider(
chromiaD1blockchainRID,
restD1
); //Chromia Client provider

Thereafter, a connection with a blockchain of the dapp Blockchain RID can be set. It returns a REST client with the addresses of the nodes that run your blockchain.

const dappBlockchainRID = "<BlockchainRID>"; //Dapp Blockchain RID
const rest = await chromiaClient.blockchainConnection(dappBlockchainRID); //Blockchain connection

Now when the REST client to the nodes running the blockchain is created you can initiate a GTX client.

const gtx = gtxClient.createClient(rest, dappBlockchainRID, ["fun1", "fun2"]);