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' proof verification system 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, ensure you have the following:
- An appId, appSecret, and template identity, which can be obtained from the Primus Developer Hub
- The SDK installed. Refer to the Installation Guide for details.
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);
// 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.
}
}
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.