Skip to main content

Simple Example

image

Simple Example​

This guide will walk you through the fundamental steps to integrate Primus's Network-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
let attestParams = {
// ... other parameters
attMode: {
algorithmType: "proxytls", // optional, default is 'proxytls'
},
};
primusNetwork.attest(attestParams);

Verification Logics​

By default, the Primus Network-Core-SDK retrieves a plaintext verification result. We offer two types of verification logic to accommodate different requirements:

  1. Hashed result

Setting example :

// Set Attestation conditions
const responseResolves = [
[
{
...otherParams,
op: "SHA256", // optional, default is 'REVEAL_STRING'
},
],
];
const attestParams = {
...submitTaskParams,
...submitTaskResult,
requests,
responseResolves,
};
let attestResult = await primusNetwork.attest(attestParams);
  1. Conditions result

Setting example :

// Set Attestation conditions
const responseResolves = [
[
{
...otherParams,
op: ">",
value: "YOUR_CUSTOM_TARGET_DATA_VALUE",
},
],
];
const attestParams = {
...submitTaskParams,
...submitTaskResult,
requests,
responseResolves,
};
let attestResult = await primusNetwork.attest(attestParams);

For more details about these two verification logics, you can refer to the Verification Logics section.

Using Attestations in zkVM​

After obtaining the attestation through the Hashed Verification Logic, you may use getAllJsonResponse to collect the associated HTTP response payloads. These two components — the attestation (hashed data) and the HTTP responses (raw data) — can then be jointly submitted to the zkVM for further computation.

This approach of handling the attestation data in another computing module like zkVM, is a powerful verifiable data usage pattern so-called data verification and computation (DVC). You can refer to this part for more details.

const responseResolves = [
[
{
...otherParams,
op: "SHA256", // optional, default is 'REVEAL_STRING'
},
],
];

const attestParams = {
...submitTaskParams,
...submitTaskResult,
requests,
responseResolves,
getAllJsonResponse: "true", // optional, default is 'false', Must be set to true to allow getAllJsonResponse to return the HTTP response.
};

let attestResult = await primusNetwork.attest(attestParams);

const allJsonResponse = await primusNetwork.getAllJsonResponse(
attestResult[0].taskId
);

Implementation​

const PRIVATEKEY = "YOUER_PRIVATEKEY";
const address = "0xYOUER_ADDRESS";
const chainId = 84532; // Base Sepolia (or 8453 for Base mainnet)
const baseSepoliaRpcUrl = "https://sepolia.base.org"; // (or 'https://mainnet.base.org' for Base mainnet)
// The request and response can be customized as needed.
const requests = [
{
url: "https://www.okx.com/api/v5/public/instruments?instType=SPOT&instId=BTC-USD",
method: "GET",
header: {},
body: "",
},
];
const responseResolves = [
[
{
keyName: "instType",
parseType: "json",
parsePath: "$.data[0].instType",
// op: "SHA256", // optional, default is 'REVEAL_STRING'
},
],
];

const provider = new ethers.providers.JsonRpcProvider(baseSepoliaRpcUrl);
const wallet = new ethers.Wallet(PRIVATEKEY, provider);

try {
const primusNetwork = new PrimusNetwork();

// Initialize the network with wallet
const initResult = await primusNetwork.init(wallet, chainId);

// Submit task
const submitTaskParams = {
address,
};
let submitTaskResult = await primusNetwork.submitTask(submitTaskParams);
console.log("Submit task result:", submitTaskResult);

// Compose params for attest
const attestParams = {
...submitTaskParams,
...submitTaskResult,
requests,
responseResolves,
// attMode: {
// algorithmType: "proxytls", // optional, default is 'proxytls'
// },
// getAllJsonResponse: "true", // optional, default is 'false'
};
let attestResult = await primusNetwork.attest(attestParams);
console.log("Attest result:", attestResult);

// Verify and poll task result
const verifyParams = {
taskId: attestResult[0].taskId,
reportTxHash: attestResult[0].reportTxHash,
};
const taskResult = await primusNetwork.verifyAndPollTaskResult(verifyParams);
console.log("Task result:", taskResult);

// const allResponse= await primusNetwork.getAllResponse(attestResult[0].taskId)
// console.log('allResponse:', allResponse)
} catch (error) {
console.error("Unexpected error:", error);
throw error;
}

Understanding the Attestation Structure​

When a successful data verification process is completed, you will receive an attestation from the attestor. For more information about the attestation structure, see the section.

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.