Skip to main content

Quick Start

image

Frontend Example: Integrating MPC-TLS SDK in a Application

This guide will walk you through the fundamental steps to integrate Primus's proof verification system into your application.

note

We need to register your dApps' domain to maintain the whitelist for production usage. If you are only debugging locally, simply follow the steps below to complete the call. For better tech support, please contact the Primus team through our community.

1. Prerequisites

Before you begin, ensure you have:

2. InitAttestation

To integrate zk-TLS SDK requires initialization

  init(appId: string, appSecret?: string): Promise<string | boolean> {
this.appId = appId
this.appSecret = appSecret
if (appSecret) {
this.isAppServer = true
this.isInitialized = true
return Promise.resolve(true)
} else {
this.isInstalled = !!window.primus
if (this.isInstalled) {
window.postMessage({
target: "padoExtension",
origin: "padoZKAttestationJSSDK",
name: "initAttestation",
});

} else {
const errorCode = '00006'
return Promise.reject(new ZkAttestationError(
errorCode
))
}

console.time('initAttestationCost')
return new Promise((resolve, reject) => {
const eventListener = (event: any) => {
const { target, name, params } = event.data;
if (target === "padoZKAttestationJSSDK") {
if (name === "initAttestationRes") {
console.log('sdk receive initAttestationRes', event.data)
const { result, errorData, data } = params
if (result) {
this.isInitialized = params?.result

if (data?.padoExtensionVersion) {
this.padoExtensionVersion = data.padoExtensionVersion
}
console.timeEnd('initAttestationCost')
window?.removeEventListener('message', eventListener);
resolve(this.padoExtensionVersion);
} else {
window?.removeEventListener('message', eventListener);
// console.log('sdk-initAttestationRes-errorData:',errorData)
if (errorData) {
const { code } = errorData
reject(new ZkAttestationError(code))
}
}
}
}
}
window.addEventListener("message", eventListener);
});
}

}

3. Start zk-TLS process

Note: You can call startAttestation only after the initAttestation method is called.

Before starting the data verification process, a few parameters should be configured and transmitted to the MPC-TLS SDK. This configuration is required regardless of how you set up your users' operation steps in you dApp.

The parameters should be configured in the following order:

  • 1.chainID (must)
  • 2.walletAddress (must)
  • 3.attestationTypeID (must)
  • 4.attestationParameters (must, according to different attestationTypeID)

Parameters detail you can see: Parameters details

try {
const startAttestaionResult = await sdkInstance.startAttestation({
chainID: 56,
walletAddress: '0x',
attestationTypeID: '9',
attestationParameters: ['100'],
});

console.log(startAttestaionResult);
// Output:
// {
// eip712MessageRawDataWithSignature:
// {
// types: {
// Attest: [
// {
// name: "schema",
// type: "bytes32",
// },
// {
// name: "recipient",
// type: "address",
// },
// {
// name: "expirationTime",
// type: "uint64",
// },
// {
// name: "revocable",
// type: "bool",
// },
// {
// name: "refUID",
// type: "bytes32",
// },
// {
// name: "data",
// type: "bytes",
// },
// {
// name: "deadline",
// type: "uint64",
// },
// ],
// },
// primaryType: "Attest",
// message: {
// schema: "0x",
// recipient: "0x",
// expirationTime: 0,
// revocable: true,
// data: "0x",
// refUID:
// "0x0000000000000000000000000000000000000000000000000000000000000000",
// deadline: 0,
// },
// domain: {
// name: "xxx",
// version: "xxx",
// chainId: "xxx",
// verifyingContract: "0x",
// salt: null,
// },
// uid: null,
// signature: {
// v: 28,
// r: "0x",
// s: "0x",
// },
// }
// };
console.log("Attest successfully!");
} catch (e) {
alert(`Attest failed,code: ${e.code} ,message: ${e.message}`);
}

4. Verify attestation result

After receiving the verified result, you need to verify whether the result is trustworthy.

  • Parameters

    • startAttestationReturnParams:StartAttestationReturnParams An object containing the properties of eip712MessageRawDataWithSignature, which is the return value of the startAttestation method.
  • Return:boolean Whether the signature is successfully verified.

  • Example

const verifyAttestationResult = sdkInstance.verifyAttestation(
startAttestaionResult
);
console.log(verifyAttestation);
// Output: true

5. Submit the verified data result (proof) to the blockchain

You can only submit the proof to the chainID associated with the configuration in 3.Start MPC-TLS process.

  • Parameters

    • startAttestationReturnParams:StartAttestationReturnParams An object containing the properties of eip712MessageRawDataWithSignature, which is the return value of the startAttestation method.
  • wallet:any The wallet object

  • Return:string Transaction details URL

  • Example

try {
const sendToChainResult = sdkInstance.sendToChain(
startAttestaionResult,
window.ethereum
);
console.log(sendToChainResult);
// Output: https://bascan.io/attestation/0x
console.log("SendToChain successfully!");
} catch (e) {
alert(`SendToChain failed,code: ${e.code} ,message: ${e.message}`);
}

6.Check the submitted proof on-chain

After completing the submission, you can find the on-chain details using the corresponding blockchain links below.

6.1 Mainnet

6.2 Testnet

Examples of the zkTLS SDK operations

Here's a simple example demonstrating how to perform basic operations with the zkTLS SDK.

import MPCTLSJSSDK from "@padolabs/mpctls-js-sdk";

const sdkInstance = new MPCTLSJSSDK();

try {
const initAttestaionResult = await sdkInstance.initAttestation(
"yourdAppSymbol"
); // Initialize the SDK
console.log(initAttestaionResult); //Output: 0.3.15
console.log(sdkInstance.supportedChainList); // View supported chains
console.log(sdkInstance.supportedAttestationTypeList); // View supported attestation types

// Generate attestation process
const startAttestaionResult = await sdkInstance.startAttestation({
chainID: 56, // Select from the supported chain list
walletAddress: "0x", // User's wallet address
attestationTypeID: "9", // Select from the supported attestation types
attestationParameters: ["100"], // Input the corresponding fields (if needed) according to the selected attestationTypeID
});

// Verify attestation result
const verifyAttestationResult = await sdkInstance.verifyAttestati(startAttestaionResult);

// Upload Proof to Blockchain
const sendToChainResult = await sdkInstance.sendToChain(
startAttestaionResult,
window.ethereum
);

console.log("Generated Proof:", startAttestaionResult);
console.log("Proof on Chain:", sendToChainResult);
console.log("Is Proof Valid:", verifyAttestationResult);
} catch (e) {
alert(`Failed, code: ${e.code} , message: ${e.message}`);
}

Error Codes

We have defined some error codes in the SDK. When an error occurs during the data verification process, you can refer to the following list for troubleshooting.

1. General errors

Error CodeSituation
00001The zkTLS algorithm has not been initialized. Please restart the process.
00002The process did not respond within 5 minutes.
00003A data verification process is currently being generated. Please try again later.
00004The user closes or cancels the attestation process.
00005Wrong parameters!
00006No extension version 0.3.15 or above was detected as installed.
00007Insufficient wallet balance.
00008Failed to submit the proof on-chain. Or other errors in the Wallet operations.
00009Your dApp is not registered. Please contact the Primus team.
99999Undefined error. Contact the Primus team for further support
Error CodeSituation
00102Attestation requirements not met. Insufficient assets balance in Binance Spot Account.
00104Attestation requirements not met.
Error CodeSituation
10001The internet condition is not stable enough to complete the data verification flow. Please try again later.
10002The attestation process has been interrupted due to some unknown network error. Please try again later.
10003Can't connect attestation server due to unstable internet condition. Please try again later.
10004Can't connect data source server due to unstable internet condition. Please try again later.
20005Can't complete the attestation due to some workflow error. Please try again later.
30001 ~ 30004Can't complete the attestation flow due to response error. Please try again later.
50007Can't complete the attestation due to algorithm execution issues.
50008Can't complete the attestation due to abnormal execution results.
50009The algorithm service did not respond within 5 minutes.
50010Can't complete the attestation due to some compatibility issues.
50011Can't complete the attestation due to algorithm version issues.

For any other error codes not mentioned here, please contact our community for further support.