ScalarDL Node Client SDK
This is a library for Node.js applications by which the applications can interact with a ScalarDL network.
Node version used for development and testing​
This package has been developed and tested using Node LTS v14.16.0. named "fermium". This means we cannot guarantee the package nominal behaviour when using other Node versions.
Install​
We can use any NPM package manager to install this library. For example, to install with NPM:
npm install @scalar-labs/scalardl-node-client-sdk
HOWTO​
Create ClientService instance​
ClientService
class is the main class of this package.
It provides following functions to request ScalarDL network.
Name | use |
---|---|
registerCertificate | To register a client's certificate on a ScalarDL network |
registerContract | To register a contract (of a registered client) on the ScalarDL network |
listContracts | To list the client's registered contracts |
execute and executeContract (deprecated in the feature) | To execute a client's registered contract |
validateLedger | To validate an asset on the ScalarDL network to determine if it has been tampered |
If an error occurs when executing one of the above methods, a ClientError
will be thrown. The
ClientError.code
provides additional error context. Please refer to the Runtime error section below for the status code specification.
Use the code snippet below to create a ClientService instance.
const { ClientService } = require('@scalar-labs/scalardl-node-client-sdk');
const clientService = new ClientService(clientProperties);
The clientProperties
argument is mandatory for the constructor.
This is a properties example that a user foo@example.com
would use to try to connect to the server scalardl-server.example.com:50051
of the ScalarDL network.
{
'scalar.dl.client.server.host': 'scalardl-server.example.com',
'scalar.dl.client.server.port': 50051,
'scalar.dl.client.server.privileged_port': 50052,
'scalar.dl.client.cert_holder_id': 'foo@example.com',
'scalar.dl.client.private_key_pem': '-----BEGIN EC PRIVATE KEY-----\nMHc...',
// scalar.dl.client.private_key_path is applied when scalar.dl.client.private_key_pem is not given
'scalar.dl.client.private_key_path': 'path-to-key-file',
'scalar.dl.client.cert_pem': '-----BEGIN CERTIFICATE-----\nMIICjTCCAj...\n',
// scalar.dl.client.cert_path is applied when scalar.dl.client.cert_pem is not given
'scalar.dl.client.cert_path': 'path-to-certificate-file',
'scalar.dl.client.cert_version': 1,
'scalar.dl.client.tls.enabled': false,
'scalar.dl.client.tls.ca_root_cert_pem': '-----BEGIN CERTIFICATE-----\n...\n',
// scalar.dl.client.tls.ca_root_cert_path is applied when scalar.dl.client.tls.ca_root_cert_pem is not given
'scalar.dl.client.tls.ca_root_cert_path': 'path-to-ca-root-certificate-file',
'scalar.dl.client.authorization.credential': '...',
'scalar.dl.client.proxy.server': '...',
}
If the auditor capability is enabled on the ScalarDL network, specify additional properties like the following example. In this example, the client interacts with the auditor scalardl-auditor.example.com
and detects Byzantine faults including data tampering when executing contracts.
{
'scalar.dl.client.auditor.enabled': true,
'scalar.dl.client.auditor.host': 'scalardl-auditor.example.com',
'scalar.dl.client.auditor.port': 40051,
'scalar.dl.client.auditor.privileged_port': 40052,
}
In what follows assume that we have a clientService instance.
Register the certificate​
Use the registerCertificate
function to register a certificate on the ScalarDL network.
await clientService.registerCertificate();
Register contracts​
Use the registerContract
function to register a contract.
await clientService.registerContract('contractId', 'com.example.contract.contractName', contractUint8Array, propertiesObject);
Register functions​
Use the registerFunction
function to register a function.
await clientService.registerFunction('functionId', 'com.example.function.functionName', functionUint8Array);
List registered contracts​
Use listContracts
function to list all registered contracts.
const constracts = await clientService.listContracts();
Execute a contract​
Use execute
function to execute a registered contract and function (optionally).
const response = await clientService.execute('contractId', argumentObject);
const executionResult = response.getResult();
const proofsList = response.getProofs();
const response = await clientService.execute(
'contractId',
{ 'arg1': 'a' },
'functionId',
{ 'arg2': 'b' }
);
{ 'arg1': 'a' }
will be passed via contractArgument, while { 'arg2': 'b' }
will be passed via functionArgument.
Validate an asset​
Use the validateLedger
function to validate an asset in the ScalarDL network.
const response = await clientService.validateLedger('assetId');
const statusCode = response.getCode();
const proof = response.getProof();