0.1.0 β’ Published 5 months ago
agentkit-witnesschain v0.1.0
Witnesschain Adapter
π Overview
The Witnesschain Adapter is a TypeScript-based utility that enables image verification and blockchain-based campaign creation by sending requests to an API. It handles file uploads using axios
and form-data
, and integrates Ethereum-based authentication using ethers
.
π Features
- π· Upload multiple images for verification
- β‘ Uses
axios
for efficient HTTP requests - π€ Handles file streams properly to prevent memory overload
- π οΈ TypeScript support for better development experience
- π Ethereum-based authentication using private keys
- ποΈ Create and manage blockchain campaigns
π οΈ Usage
Import and Initialize the Adapter
import { WitnesschainAdapter } from "witnesschain-adapter";
const adapter = new WitnesschainAdapter();
Authenticate with Ethereum Wallet
export WITNESSCHAIN_PRIVATE_KEY="<Your-Private-Key>"
const privateKey = process.env.WITNESSCHAIN_PRIVATE_KEY;
const latitude = 12.9;
const longitude = 77.5;
const logged_in = await adapter.authenticate(privateKey, latitude, longitude);
Create a Blockchain Campaign
const r = await witnesschain_client.createCampaign ({
campaign : MY_CAMPAIGN,
description : "my-campaign-description",
type : "individual", // "group", "individual", OR "task"
// ---- Group campaigns may require 2 values ---
// location_limit_in_meters : 100, // how far can people in a group can be
// time_limit_in_minutes : 60, // how long the referral link is valid
tags : [
"campaign",
"tags"
],
// lat, long, and radius is not mandatory
latitude : LONGITUDE,
longitude : LATITUDE,
radius : 100, // in kms the radius of circle within which the campaign is valid
banner_url : "https://www.google.com/x.png", // images shown to user
poster_url : "https://www.google.com/x.png",
currency : "POINTS", // What currency will be rewarded to participants
total_rewards : 10.0, // The MAX/total rewards the campaign can give
reward_per_task : 2.0, // rewards per task
fuel_required : 1.0, // Fuel that will be spent by the user for this task
starts_at : starts_at, // When campaign starts and ends
ends_at : ends_at,
max_submissions : 10000,// Max submissions that this campaign can accept
is_active : true // true makes it immediately available to all users
});
Example scripts
cd adapter-witnesschain
export WITNESSCHAIN_PRIVATE_KEY="<Your-Private-Key>"
npm install
npm run build
node dist/examples/campaign.js
Verify Images
To verify a set of images for a specific task:
const imagePaths = ["IMG_1347.jpeg", "IMG_1348.jpeg"];
const task = "Reduce electricity consumption";
adapter.verifyPhotos(imagePaths, task)
.then(response => {
console.log("Verification Response:", response.data);
})
.catch(error => {
console.error("Error verifying photos:", error);
});
π Class Documentation
WitnesschainAdapter
The main adapter class for verifying images and interacting with the blockchain.
Constructor
constructor(apiUrl: string = "http://localhost:8000/verify-photos/", blockchainApiUrl: string = "https://testnet.witnesschain.com/proof/v1/pol")
apiUrl
(optional): The endpoint to send image verification requests (default:http://localhost:8000/verify-photos/
).blockchainApiUrl
(optional): The Witnesschain blockchain API endpoint (default:https://testnet.witnesschain.com/proof/v1/pol
).
Method: authenticate(privateKey: string, latitude: number, longitude: number)
authenticate(privateKey: string, latitude: number, longitude: number): Promise<boolean>
privateKey
(string): The Ethereum private key for authentication.latitude
,longitude
(number): Userβs geographical location.- Returns:
Promise<boolean>
βtrue
if authentication is successful,false
otherwise.
Method: createCampaign(...)
createCampaign(privateKey: string, campaignName: string, description: string, createdBy: string, latitude: number, longitude: number, radius: number, totalRewards: number, rewardPerTask: number, fuelRequired: number): Promise<any>
- Parameters: Customizable campaign details.
- Returns: Campaign creation response.
Method: verifyPhotos(imagePaths: string[], task: string)
verifyPhotos(imagePaths: string[], task: string): Promise<AxiosResponse | null>
imagePaths
(string array): Paths to images that need verification.task
(string): The task description (e.g.,"Reduce electricity consumption"
).- Returns:
Promise<AxiosResponse | null>
β API response ornull
if an error occurs.
πΊ Project Structure
witnesschain-adapter/
βββ src/
β βββ index.ts
β βββ WitnesschainAdapter.ts
βββ dist/ # Compiled JavaScript files
βββ package.json
βββ tsconfig.json # TypeScript configuration
βββ .gitignore
βββ README.md
β Troubleshooting
1. TypeScript Error: esModuleInterop
If you see this error:
Module 'form-data' can only be default-imported using the 'esModuleInterop' flag
Enable esModuleInterop
in tsconfig.json
:
{
"compilerOptions": {
"esModuleInterop": true
}
}
Then recompile:
npm install
npm run build
2. Missing Dependencies
Ensure all dependencies are installed:
npm install
π License
This project is licensed under the MIT License.
π¨βπ» Author
Developed by Witnesschain.
For inquiries or contributions, feel free to open an issue! π
0.1.0
5 months ago