SDK - Typescript

Chain Connector for Typescript

Git repository:

The Typescript SDK for accessing and operating on blockchain platform.


The project is managed based on yarn.

Build a JavaScript file

$yarn build

The file “dist/chain_connector.js” was generated when compiling success.

Run testing

Before running the test, save the private key data allocated by the platform in “access_key.json”, and then configure the parameters and chain.port of “package.json” to the service address of the gateway.

$yarn test


Connect the chain

Initial the connector using the private key data allocated by the chain platform, then connect to the gateway module.

const filePath = path.join(process.cwd(), 'access_key.json');
const content = await fs.readFile(filePath, 'utf8');
let conn = NewConnectorFromAccess(JSON.parse(content));
await conn.connect(gatewayHost, gatewayPort);

Build and manage digital assets

Define a data schema for digital assets, and then you can add, update, delete, and query documents (digital assets) under the schema. All changes are automatically persistently stored using blockchain and could be queried using getSchemaLog and getDocumentLog.

//create new schema
const schemaName = 'sample';
let properties: DocumentProperty[] = [
        name: 'name',
        type: PropertyType.String
        name: 'age',
        type: PropertyType.Integer
        name: 'available',
        type: PropertyType.Boolean
await conn.createSchema(schemaName, properties);

//get schema define    
let current = await conn.getSchema(schemaName);
console.log('test schema created ok:\n' + JSON.stringify(current))

//add a document
let content = {
    name: 'hello',
    age: 20,
    available: true,
let docID = await conn.addDocument(schemaName, '', JSON.stringify(content));
//check a document
if (await conn.hasDocument(schemaName, docID)){
    //update a existed document
    let updatedContent = "{\"name\": \"alice\", \"age\": 18, \"available\": false}";
    await conn.updateDocument(schemaName, docID, content);

//get change trace of a document
let logs = await conn.getDocumentLogs(schemaName, docID);

//query documents
let condition = new QueryBuilder()
let records = await conn.queryDocuments(schemaName, condition);

//remove document
await conn.removeDocument(schemaName, docID);

Deploy and invoke the Smart Contract

It is necessary to assign a name and execute steps to deploy a Smart Contract. Then initiate execution using the contract name and call parameters. The system can enable the trace option for a contract, which allows the user to review the contract’s execution plan and steps.

const contractName = "contract_create";
let contractDefine: ContractDefine = {
    steps: [
            action: "create_doc",
            params: ["$s", "@1", "@2"],
            action: "set_property",
            params: ["$s", "catalog", "@3"],
            action: "set_property",
            params: ["$s", "balance", "@4"],
            action: "set_property",
            params: ["$s", "number", "@5"],
            action: "set_property",
            params: ["$s", "available", "@6"],
            action: "set_property",
            params: ["$s", "weight", "@7"],
            action: "update_doc",
            params: ["@1", "$s"],
            action: "submit",

//check existed contract
if (await conn.hasContract(contractName)) {
    //withdraw existed contract
    await conn.withdrawContract(contractName);
    console.log('previous contract %s removed', createContractName)

//deploy contact
await conn.deployContract(contractName, contractDefine);

//enable trace option
let info = await conn.getContractInfo(contractName);
if (!info.isEnabled()) {
    await conn.enableContractTrace(contractName);

const docID = "contract-doc";
let parameters: string[] = {
    Math.floor(Math.random() * 1000).toString(),
    Math.random() > 0.5 ? 'true' : 'false',
    (Math.random() * 200).toFixed(2),

//call contract with parameters
await conn.callContract(createContractName, parameters);

Audit the block chain and transaction

Through the SDK, you can obtain and check all the details of chains, blocks, and transactions, which can be used to audit data security and monitor the background operation.

//check chain status
let status = await conn.getStatus();

//query blocks from height 1 to 10
let blockRecords = await conn.queryBlocks(1, 10);
for (let blockID of blockRecords.blocks) {
    //get block data
    let blockData = await conn.getBlock(blockID);
    //query transactions in a block
    let transactionRecords = await conn.queryTransactions(blockID, 0, 20);
    for (let transID of transactionRecords.transactions) {
        //get transaction data
        let transactionData = await conn.getTransaction(blockID, transID);