add mtls for network core sdk
This commit is contained in:
41
README.md
Normal file
41
README.md
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
# network-core-sdk-mtls-example
|
||||||
|
|
||||||
|
## Install
|
||||||
|
```bash
|
||||||
|
npm install
|
||||||
|
```
|
||||||
|
|
||||||
|
## Configure
|
||||||
|
Create a `.env` file in the project root:
|
||||||
|
```
|
||||||
|
PRIVATE_KEY=0xYOUR_PRIVATE_KEY
|
||||||
|
```
|
||||||
|
|
||||||
|
Optional: if you want to enable mTLS, uncomment the `mTLS` block in `index.js` and provide your client cert/key strings:
|
||||||
|
```js
|
||||||
|
const mTLS = {
|
||||||
|
clientCrt: "YourClientCrtString",
|
||||||
|
clientKey: "YourClientKeyString",
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
## Run
|
||||||
|
```bash
|
||||||
|
node index.js
|
||||||
|
```
|
||||||
|
|
||||||
|
You should see logs for:
|
||||||
|
- submit task result
|
||||||
|
- attest result
|
||||||
|
- task result
|
||||||
|
|
||||||
|
## Customize
|
||||||
|
Edit these sections in `index.js`:
|
||||||
|
- `address`: your wallet address
|
||||||
|
- `chainId` and `baseSepoliaRpcUrl`: switch to Base mainnet if needed
|
||||||
|
- `requests`: request params for your mTLS endpoint
|
||||||
|
- `responseResolves`: JSON parse paths for the response fields you want to attest
|
||||||
|
|
||||||
|
## Notes
|
||||||
|
- Keep your `.env` out of version control.
|
||||||
|
- The example uses a public RPC; for reliability, use your own provider endpoint.
|
||||||
78
index.js
Normal file
78
index.js
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
const { PrimusNetwork } = require("@primuslabs/network-core-sdk");
|
||||||
|
const { ethers } = require("ethers");
|
||||||
|
require("dotenv/config");
|
||||||
|
|
||||||
|
async function primusProofTest() {
|
||||||
|
const PRIVATEKEY = process.env.PRIVATE_KEY;
|
||||||
|
const address = "0x8F0D4188307496926d785fB00E08Ed772f3be890"; // change to your 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.
|
||||||
|
|
||||||
|
// change to your request params
|
||||||
|
const requests = [
|
||||||
|
{
|
||||||
|
url: "https://checkout.mtls.api.hm.bb.com.br/",
|
||||||
|
method: "GET",
|
||||||
|
header: {},
|
||||||
|
body: "",
|
||||||
|
}
|
||||||
|
];
|
||||||
|
const responseResolves = [
|
||||||
|
[
|
||||||
|
{
|
||||||
|
keyName: "detail", // change to your response key name
|
||||||
|
parseType: "json",
|
||||||
|
parsePath: "$.detail", // change to your response json path
|
||||||
|
},
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
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 mTLS = {
|
||||||
|
// clientCrt: "YourClientCrtString", // Please replace with your ownner client crt string
|
||||||
|
// clientKey: "YourClientKeyString", // Please replace with your ownner client key string
|
||||||
|
// }
|
||||||
|
const attestParams = {
|
||||||
|
...submitTaskParams,
|
||||||
|
...submitTaskResult,
|
||||||
|
requests,
|
||||||
|
responseResolves,
|
||||||
|
// mTLS
|
||||||
|
};
|
||||||
|
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);
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Unexpected error:", error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
primusProofTest();
|
||||||
1520
package-lock.json
generated
Normal file
1520
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
16
package.json
Normal file
16
package.json
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
"name": "network-core-sdk-example",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"author": "",
|
||||||
|
"license": "ISC",
|
||||||
|
"dependencies": {
|
||||||
|
"@primuslabs/network-core-sdk": "^0.1.5",
|
||||||
|
"dotenv": "^17.2.3",
|
||||||
|
"ethers": "^5.7.2"
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user