Test Example
Test Example
This guide will walk you through the fundamental steps to integrate Primus's zkTLS SDK and complete a basic data verification process through your application. You can learn about the integration process through this simple demo.
This example demonstrates how developers can create a frontend project and run Primus' data verification process locally for testing purposes. We strongly recommend not using this method in a formal production environment. For guidance on configuring a production environment, please refer to the Production Example
Prerequisites
Before you begin, make sure you have the following:
- A paired appId and appSecret, along with a selected Template ID. These can be obtained from the Primus Developer Hub
- The SDK installed. For installation instructions, refer to the Installation Guide.
Implementation
Here’s a basic example of how to use Primus’ zkTLS SDK from the frontend. Note that this implementation is intended for testing purposes only.
import { PrimusZKTLS } from "@primuslabs/zktls-js-sdk"
// Initialize parameters, the init function is recommended to be called when the page is initialized.
const primusZKTLS = new PrimusZKTLS();
const appId = "YOUR_APPID";
const appSecret= "YOUR_SECRET"; // Just for testing, appSecret cannot be written in the front-end code
const initAttestaionResult = await primusZKTLS.init(appId, appSecret);
console.log("primusProof initAttestaionResult=", initAttestaionResult);
export async function primusProof() {
// Set TemplateID and user address.
const attTemplateID = "YOUR_TEMPLATEID";
const userAddress = "YOUR_USER_ADDRESS";
// Generate attestation request.
const request = primusZKTLS.generateRequestParams(attTemplateID, userAddress);
// Set zkTLS mode, default is proxy model. (This is optional)
const workMode = "proxytls";
request.setAttMode({
algorithmType: workMode,
});
// Set attestation conditions. (These are optional)
// 1. Hashed result.
// const attConditions = JSON.stringify([
// [
// {
// field:'YOUR_CUSTOM_DATA_FIELD',
// op:'SHA256',
// },
// ],
// ]);
// 2. Conditions result.
// const attConditions = JSON.stringify([
// [
// {
// field: "YOUR_CUSTOM_DATA_FIELD",
// op: ">",
// value: "YOUR_CUSTOM_TARGET_DATA_VALUE",
// },
// ],
// ]);
// request.setAttConditions(attConditions);
// Transfer request object to string.
const requestStr = request.toJsonString();
// Sign request.
const signedRequestStr = await primusZKTLS.sign(requestStr);
// Start attestation process.
const attestation = await primusZKTLS.startAttestation(signedRequestStr);
console.log("attestation=", attestation);
// Verify siganture.
const verifyResult = await primusZKTLS.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.
}
}
zkTLS Models
We offer two modes in various user scenarios:
- proxytls
- mpctls
For more details about these two models, you can refer to the Overview section.
// Set zkTLS mode, default is proxy model.
primusZKTLS.setAttMode({
algorithmType: "proxytls",
});
Verification Logics
By default, the zkTLS SDK retrieves a plaintext verification result. We offer two types of verification logic to accommodate different requirements:
- Hashed result
Setting example :
// Set Attestation conditions
request.setAttConditions([
[
{
field: 'YOUR_CUSTOM_DATA_FIELD',
op: 'SHA256',
},
],
]);
- Conditions result
Setting example :
// Set Attestation conditions
request.setAttConditions([
[
{
field: 'YOUR_CUSTOM_DATA_FIELD',
op: '>',
value: 'YOUR_CUSTOM_TARGET_DATA_VALUE',
},
],
]);
For more details about these two verification logics, you can refer to the Verification Logics 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.