Skip to main content

Simple Example

image

Simple Example​

This guide will walk you through the fundamental steps to integrate Primus's zkTLS-Core-SDK and complete a basic data verification process through your server. You can learn about the integration process through this simple demo.

Prerequisites​

Before you begin, ensure you have:

zkTLS Modes​

We offer two modes in various user scenarios:

  1. proxytls
  2. mpctls

For more details about these two modes, you can refer to the Overview section.

// Set zkTLS mode, default is proxy mode.
primusZKTLS.setAttMode({
algorithmType: "proxytls",
});

Implementation​

You can get the PRIMUS_APP_ID and PRIMUS_APP_SECRET from the Primus Developer Hub.

const { PrimusCoreTLS } = require("@primuslabs/zktls-core-sdk");

async function primusProofTest() {
// Initialize parameters, the init function is recommended to be called when the program is initialized.
const appId = "PRIMUS_APP_ID";
const appSecret= "PRIMUS_APP_SECRET";
const zkTLS = new PrimusCoreTLS();
const initResult = await zkTLS.init(appId, appSecret);
console.log("primusProof initResult=", initResult);

// Set request and responseResolves.
const request ={
url: "YOUR_CUSTOM_URL", // Request endpoint.
method: "REQUEST_METHOD", // Request method.
header: {}, // Request headers.
body: "" // Request body.
};
// The responseResolves is the response structure of the url.
// For example the response of the url is: {"data":[{ ..."instFamily": "","instType":"SPOT",...}]}.
const responseResolves = [
{
keyName: 'CUSTOM_KEY_NAME', // According to the response keyname, such as: instType.
parsePath: 'CUSTOM_PARSE_PATH', // According to the response parsePath, such as: $.data[0].instType.
}
];
// Generate attestation request.
const generateRequest = zkTLS.generateRequestParams(request, responseResolves);

// Set zkTLS mode, default is proxy mode. (This is optional)
generateRequest.setAttMode({
algorithmType: "proxytls"
});

// Start attestation process.
const attestation = await zkTLS.startAttestation(generateRequest);
console.log("attestation=", attestation);

const verifyResult = zkTLS.verifyAttestation(attestation);
console.log("verifyResult=", verifyResult);
if (verifyResult === true) {
// Business logic checks, such as attestation content and timestamp checks
// do your own business logic.
} else {
// If failed, define your own logic.
}
}

Understanding the Attestation Structure​

When a successful data verification process is completed, you will receive a zkTLS attestation (proof) from the attestor.

For more information about the attestation structure, see the section.

Submit Attestation On-chain (optional)​

To submit the verified data result (proof) to the blockchain, you’ll need to invoke the appropriate smart contract method. For detailed instructions, please refer to the onchain interactions.

Error Codes​

We have defined several error codes in the SDK. If an error occurs during the data verification process, you can refer to the error code list for troubleshooting.