@think-and-dev/peaq-gr-sdk
v3.1.0-beta.6
Published
JavaScript/TypeScript SDK for interacting with the get real campaign of Peaq Network
Readme
PEAQ Get Real SDK
A JavaScript/TypeScript SDK designed to interact with PEAQ Network's Get Real campaign, enabling seamless integration with precompiled contracts and decentralized identity (DID) management for machine verification.
Features
- Machine Smart Account management for device authentication
- Decentralized Identity (DID) handling for machine verification
- Blockchain data storage for machine records
- Multi-network support (mainnet and testnet)
- Flexible chainId-based configuration
- Get Real campaign integration for quests verification
Installation
npm install @think-and-dev/peaq-gr-sdkArchitecture
The SDK is organized into modular services, each handling specific aspects of machine verification and DID management:
Core Services
ConfigurationService
- Manages global SDK configuration
- Handles precompiled contract addresses based on chainId
- Provides access to ethers accounts and provider
- Configures network endpoints for Get Real campaign
TransactionService
- Handles blockchain transaction management
- Manages signatures and nonces
- Processes transaction errors and retries
MachineAccountService
- Deploys Smart Accounts for machine verification
- Manages transaction execution through Smart Accounts
- Interacts with MachineStationFactory contract for device registration
IdentityService
- Manages machine DID creation
- Handles email verification signatures
- Prepares DID operations for machine verification
- Integrates with Get Real campaign verification process
StorageService
- Manages on-chain storage of machine verification data
- Prepares storage operations for machine records
- Handles machine verification status updates
Precompiled Contracts
The SDK interacts with two main precompiled contracts:
DID Contract (
0x800)- Manages decentralized identities for machines
- Handles machine attribute registration
Storage Contract (
0x801)- Stores machine verification data
- Manages machine records and verification history
- Handles verification tags and metadata
Usage
Initialization
The SDK can be initialized in three ways:
- Using environment variables:
const sdk = PeaqSDK.fromEnv();- Using partial configuration:
const sdk = PeaqSDK.fromConfig({
machineStationFactoryContractAddress: "0x...",
ownerPrivateKey: "0x...",
machineOwnerPrivateKey: "0x...",
apiKey: "your-api-key",
projectApiKey: "your-project-api-key",
depinSeed: "your-seed"
});- Using complete configuration:
const sdk = new PeaqSDK({
rpcUrl: "https://w3.agung.peaq.network",
chainId: 33333,
machineStationFactoryContractAddress: "0x...",
ownerPrivateKey: "0x...",
machineOwnerPrivateKey: "0x...",
serviceUrl: "https://api.example.com",
apiKey: "your-api-key",
projectApiKey: "your-project-api-key",
depinSeed: "your-seed"
});Usage Examples
1. Deploy a Machine Smart Account for Verification
const machineAddress = await sdk.machineStationFactory.deploySmartAccount();
console.log("Machine Smart Account deployed at:", machineAddress);2. Create a DID for Machine Verification
const didTransactionHash = await sdk.identity.createDID({
didAddress: machineAddress,
email: "[email protected]",
tag: "machine-verification"
});
console.log("Machine DID created with transaction:", didTransactionHash);3. Store Machine Verification Data
const storageTransactionHash = await sdk.storage.storeData({
customTag: "VERIFICATION",
email: "[email protected]",
tag: "machine-verification",
tags: ["verified-machine", "get-real-campaign"]
});
console.log("Verification data stored with transaction:", storageTransactionHash);Network Configuration
The SDK supports different network configurations based on chainId:
// Precompiled addresses by network
const PEAQ_CHAIN_ADDRESSES = {
// PEAQ Mainnet
3338: {
DID_CONTRACT: '0x0000000000000000000000000000000000000800',
STORAGE_CONTRACT: '0x0000000000000000000000000000000000000801'
},
// AGUNG Testnet
9990: {
DID_CONTRACT: '0x0000000000000000000000000000000000000800',
STORAGE_CONTRACT: '0x0000000000000000000000000000000000000801'
}
};Required Environment Variables
When using PeaqSDK.fromEnv(), the following variables are required:
RPC_URL=https://w3.agung.peaq.network
CHAIN_ID=33333
MACHINE_STATION_FACTORY_ADDRESS=0x...
OWNER_PRIVATE_KEY=0x...
MACHINE_OWNER_PRIVATE_KEY=0x...
SERVICE_URL=https://api.example.com
API_KEY=your-api-key
PROJECT_API_KEY=your-project-api-key
DEPIN_SEED=your-seedError Handling
The SDK includes specific error handling for various scenarios:
try {
await sdk.machineStationFactory.deploySmartAccount();
} catch (error) {
if (error.code === "ACTION_REJECTED") {
console.log("Transaction rejected by user");
} else if (error.code === "NETWORK_ERROR") {
console.log("Network error occurred");
} else {
console.log("Unknown error:", error);
}
}License
MIT
