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 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 HTTP Responses Logics

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

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 a structure with the following details:

    [
{
"attestor": "ATTESTOR_ADDRESS", // attestor's address
"taskId": "TASK_ID", // the task id
"attestation": {
"recipient": "YOUR_USER_ADDRESS", // user's wallet address
"request": [
{
"url": "REQUEST_URL", // request url
"header": "REQUEST_HEADER", // request header
"method": "REQUEST_METHOD", // request method
"body": "REQUEST_BODY" // request body
}
],
"responseResolve": [
{
"oneUrlResponseResolve": [
{
"keyName": "VERIFY_DATA_ITEMS", // the "verify data items" you set in the template
"parseType": "",
"parsePath": "DARA_ITEM_PATH" // json path of the data for verification
}
]
}
],
"data": "{ACTUAL_DATA}", // actual data items in the request, stringified JSON object
"attConditions": "[RESPONSE_CONDITIONS]", // response conditions, stringified JSON object
"timestamp": TIMESTAMP_OF_VERIFICATION_EXECUTION, // timestamp of execution
"additionParams": "", // additionParams from zkTLS sdk
}
}
]

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.