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);
// Set the device parameter to detect the user’s device type when your application supports both PC and mobile. Currently, only Android devices are supported. iOS is coming soon.
// let platformDevice = "pc";
// if (navigator.userAgent.toLocaleLowerCase().includes("android")) {
// platformDevice = "android";
// } else if (navigator.userAgent.toLocaleLowerCase().includes("iphone")) {
// platformDevice = "ios";
// }
// const initAttestaionResult = await primusZKTLS.init(appId, appSecret, {platform: platformDevice});
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 mode. (This is optional)
const workMode = "proxytls";
request.setAttMode({
algorithmType: workMode,
});
// Set attestation conditions. (These are optional)
// 1. Hashed result.
// const attConditions = [
// [
// {
// field:'YOUR_CUSTOM_DATA_FIELD',
// op:'SHA256',
// },
// ],
// ];
// 2. Conditions result.
// const attConditions = [
// [
// {
// 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 Modes
We offer two modes in various user scenarios:
- proxytls
- 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",
});
Device Parameter
If your application is intended for use only on PCs, you do not need to include this parameter.
However, if your application also runs on mobile devices, you must include this parameter. It is used to detect the user’s device type, such as Android, iPhone, or PC. If you want to restrict usage to Android devices only, set the value to 'android'.
Currently, only Android devices are supported. iOS is coming soon.
// Set the device parameter to detect the user’s device type when your application supports both PC and mobile. Currently, only Android devices are supported. iOS is coming soon.
let platformDevice = "pc";
if (navigator.userAgent.toLocaleLowerCase().includes("android")) {
platformDevice = "android";
} else if (navigator.userAgent.toLocaleLowerCase().includes("iphone")) {
platformDevice = "ios";
}
const initAttestaionResult = await primusZKTLS.init(appId, appSecret, {platform: platformDevice});
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.