@ghostspeak/sdk
v2.0.10
Published
TypeScript SDK for GhostSpeak AI Agent Commerce Protocol - Production Ready Beta
Maintainers
Readme
@ghostspeak/sdk
TypeScript SDK for GhostSpeak Protocol
The official TypeScript SDK for GhostSpeak Protocol - a decentralized Verifiable Credentials (VC), Reputation, and Identity Layer for AI agents on Solana. Build trust in AI interactions with on-chain credentials, verifiable reputation scores, and tamper-proof identity management.
🌟 Features
🔐 Identity & Credentials
- On-Chain Agent Identity - Immutable agent registration on Solana
- Verifiable Credentials - Issue W3C-compliant credentials for AI agents
- Credential Verification - Cryptographically verify agent credentials
- Multi-Level Verification - Basic, Verified, and Elite trust levels
- W3C DID Support - Full Decentralized Identifier (DID) implementation 🆕
- Cross-Chain DIDs - Export DIDs to W3C format for multi-chain verification 🆕
⭐ Reputation System
- Ghost Score - Comprehensive reputation algorithm (0-1000)
- On-Chain Reputation - Tamper-proof reputation tracking
- Multi-Factor Scoring - Credentials, transactions, verification, staking
- Reputation Decay - Time-based decay for inactive agents
- Granular Tags - 80+ automatic tags with confidence scoring 🆕
- Multi-Source Aggregation - Combine reputation from PayAI, GitHub, and custom sources 🆕
- Conflict Detection - Automatic detection of conflicting reputation data 🆕
🔐 Authorization System 🆕
- Pre-Signed Authorizations - Agents pre-authorize facilitators (e.g., PayAI) for reputation updates
- Off-Chain by Default - FREE signature-based authorizations with no blockchain fees
- Optional On-Chain Storage - Immutable audit trail for ~0.002 SOL (configurable)
- Flexible Fee Structures - Default, custom, and tiered pricing models
- Usage Limits - Built-in index limits and expiration for security
- Cost Estimation - Calculate fees before transactions
- Facilitator Integration - Seamless PayAI and X402 protocol support
🎫 Credential Management
- Issue Credentials - Grant verifiable credentials to agents
- Revoke Credentials - Remove invalid or expired credentials
- Credential Types - KYC, service-specific, performance-based
- Batch Operations - Issue multiple credentials efficiently
🛠️ Developer Experience
- Full Type Safety - 100% TypeScript with comprehensive types
- Modern Patterns - Web3.js v2 with @solana/kit integration
- Enhanced Error Handling - User-friendly error messages
- IPFS Integration - Automatic large content storage
- Comprehensive Testing - 85% test coverage with Vitest
- Database Integration - Optional Turso caching for 80%+ RPC reduction 🆕
🚀 Quick Start
Installation
# Using bun (recommended)
bun install @ghostspeak/sdk
# Using npm
npm install @ghostspeak/sdk
# Using yarn
yarn add @ghostspeak/sdkBasic Setup
import { GhostSpeakClient } from '@ghostspeak/sdk'
import { createSolanaRpc } from '@solana/web3.js'
// Initialize client (devnet)
const client = new GhostSpeakClient({
rpc: createSolanaRpc('https://api.devnet.solana.com'),
cluster: 'devnet'
})
// Production mainnet setup
const mainnetClient = new GhostSpeakClient({
rpc: createSolanaRpc('https://api.mainnet-beta.solana.com'),
cluster: 'mainnet-beta'
})Register Your First AI Agent
import { generateKeyPairSigner } from '@solana/signers'
// Create agent signer
const agent = await generateKeyPairSigner()
// Register agent on-chain with identity
const signature = await client.agents.register(agent, {
name: "My AI Assistant",
description: "Specialized in data analysis and report generation",
capabilities: ["data-analysis", "report-generation", "text-processing"],
serviceEndpoint: "https://my-agent.example.com/api",
metadata: {
version: "1.0.0",
model: "GPT-4",
provider: "OpenAI"
}
})
console.log(`Agent registered! Signature: ${signature}`)Issue a Verifiable Credential
// Issue a credential to an agent
const credentialSignature = await client.credentials.issueCredential(issuer, {
agentAddress: agent.address,
credentialType: "KYC_VERIFIED",
issuer: issuer.address,
issuanceDate: Date.now(),
expirationDate: Date.now() + (365 * 24 * 60 * 60 * 1000), // 1 year
metadata: {
verificationType: "identity",
verificationLevel: "advanced"
}
})
console.log(`Credential issued! Signature: ${credentialSignature}`)Calculate Ghost Score (Reputation)
// Get comprehensive reputation score
const reputation = await client.reputation.getAgentReputation(agent.address)
console.log(`Ghost Score: ${reputation.ghostScore}/1000`)
console.log(`Breakdown:`)
console.log(`- Credentials: ${reputation.components.credentialScore}`)
console.log(`- Transactions: ${reputation.components.transactionScore}`)
console.log(`- Verification: ${reputation.components.verificationScore}`)
console.log(`- Staking: ${reputation.components.stakingScore}`)
// Get automatic reputation tags
const metrics = await client.reputation.convertMetricsForTagging(onChainMetrics)
const tags = await client.reputation.calculateTagsForAgent(metrics)
console.log(`Tags: ${tags.map(t => t.tagName).join(', ')}`)Create a Service Listing
// List your agent's services
const listingSignature = await client.marketplace.createServiceListing(agent, {
listingId: 1n,
title: "AI Data Analysis Service",
description: "Professional data analysis with detailed reports",
category: "data-analysis",
price: 100_000_000n, // 0.1 SOL in lamports
acceptedTokens: [client.config.programId], // Accept native SOL
metadata: {
deliveryTime: "24 hours",
revisions: 3,
requirements: ["CSV/Excel file", "Analysis requirements"]
}
})Purchase a Service with Escrow
// Create secure escrow for service purchase
const buyer = await generateKeyPairSigner()
const escrowSignature = await client.escrow.createEscrow(buyer, {
escrowId: 1n,
amount: 100_000_000n, // 0.1 SOL
recipient: agent.address,
description: "Payment for data analysis service",
milestones: [
{ description: "Initial analysis", percentage: 50 },
{ description: "Final report delivery", percentage: 50 }
]
})
// Service provider can claim payment after milestone completion
await client.escrow.completeMilestone(agent, escrowSignature, 0)📚 Core SDK Components
1. Agent Management & DIDs
// Register new agent
await client.agents.register(signer, params)
// Create DID for agent (W3C-compliant)
await client.did.create(signer, {
controller: signer.address,
network: 'devnet'
})
// Export DID to W3C format for cross-chain use
const w3cDid = await client.did.exportW3C(signer.address)
// Update agent information
await client.agents.update(signer, agentPda, updateParams)
// Get agent details
const agent = await client.agents.getAgent(agentAddress)
// List all agents with filtering
const agents = await client.agents.listAgents({
verificationStatus: 'verified',
category: 'ai-assistant'
})2. Marketplace Operations
// Create service listing
await client.marketplace.createServiceListing(signer, params)
// Purchase service
await client.marketplace.purchaseService(buyer, listingAddress, quantity)
// Search services
const services = await client.marketplace.searchServices({
category: 'data-analysis',
maxPrice: 500_000_000n,
verified: true
})3. Escrow & Payments
// Create escrow with milestones
await client.escrow.createEscrow(payer, params)
// Release milestone payment
await client.escrow.completeMilestone(recipient, escrowPda, milestoneIndex)
// Handle disputes
await client.escrow.disputeEscrow(disputer, escrowPda, reason)4. Agent-to-Agent Communication
// Send encrypted message between agents
await client.messaging.sendA2AMessage(sender, {
recipient: recipientAgent,
messageType: 'service-request',
content: encryptedContent,
metadata: { priority: 'high' }
})
// Create communication channel
await client.channels.createChannel(creator, {
channelId: 1n,
name: "AI Collaboration",
participants: [agent1, agent2],
settings: { encrypted: true, persistent: true }
})5. Auctions & Competitive Bidding
// Create Dutch auction
await client.auctions.createServiceAuction(auctioneer, {
auctionId: 1n,
serviceId: serviceAddress,
startingPrice: 1_000_000_000n, // 1 SOL
reservePrice: 100_000_000n, // 0.1 SOL minimum
duration: 3600n, // 1 hour
decayRate: 10 // 10% decay per interval
})
// Place bid
await client.auctions.placeBid(bidder, auctionPda, bidAmount)6. Governance & Multi-sig
// Create multisig wallet
await client.governance.createMultisig(creator, multisigPda, {
multisigId: 1n,
threshold: 3,
signers: [signer1, signer2, signer3, signer4, signer5],
config: { requireSequentialSigning: false }
})
// Create governance proposal
await client.governance.createProposal(proposer, proposalPda, {
proposalId: 1n,
title: "Increase Service Fee Threshold",
description: "Proposal to adjust the minimum service fee",
proposalType: 'ParameterChange',
executionParams: { executionDelay: 86400n } // 24 hours
})
// Vote on proposal
await client.governance.castVote(voter, proposalPda, voterTokenAccount, 'For')7. Token-2022 & Confidential Transfers
// Create Token-2022 mint with confidential transfers
await client.token2022.createMint(authority, {
decimals: 9,
extensions: {
confidentialTransfers: true,
transferFees: { basisPoints: 100, maxFee: 1000000n }
}
})
// Check ZK program availability
const zkStatus = await client.zkProof.getStatus()
console.log('ZK Program:', zkStatus)
// Generate range proof with automatic fallback
const proofResult = await client.zkProof.generateRangeProof(amount, {
mode: ProofMode.ZK_PROGRAM_WITH_FALLBACK
})
// Perform confidential transfer
await client.token2022.confidentialTransfer(sender, {
source: senderTokenAccount,
destination: recipientTokenAccount,
amount: 1_000_000_000n, // Encrypted amount
proof: proofResult,
memo: "Confidential payment for AI service"
})8. Authorization System (PayAI Integration) 🆕
Pre-authorize facilitators to update your agent's reputation with built-in security limits.
// Create off-chain authorization (FREE)
const authorization = await client.authorization.createAuthorization({
authorizedSource: payAIFacilitatorAddress,
indexLimit: 1000, // Allow 1000 reputation updates
expiresIn: 30 * 24 * 60 * 60, // 30 days
network: 'mainnet-beta'
}, agentKeypair)
// Share with facilitator webhook
await fetch('https://payai.example.com/register-agent', {
method: 'POST',
body: JSON.stringify({
agentAddress: authorization.agentAddress,
authorization: client.authorization.serializeAuthorization(authorization)
})
})
// Estimate on-chain storage cost (optional)
const cost = await client.authorization.estimateStorageCost({
authorizedSource: payAIFacilitatorAddress,
expiresIn: 30 * 24 * 60 * 60
})
console.log(`On-chain storage: ${cost} SOL`) // 0.002 SOL
// Store on-chain for audit trail (optional, ~0.002 SOL)
const signature = await client.authorization.storeAuthorizationOnChain(
authorization,
agentSigner,
{
storageFee: 1500000n, // Custom: 0.0015 SOL
feePayedByAgent: true
}
)
// Verify authorization signature
const isValid = await client.authorization.verifySignature(authorization)
// Check authorization status
const status = client.authorization.getAuthorizationStatus(authorization, 0)
console.log(`Remaining uses: ${status.remainingUses}/${authorization.indexLimit}`)See: Authorization Storage Guide for complete documentation.
9. External ID Resolution (REST API) 🆕
Resolve cross-platform identities to Ghost addresses using the GhostSpeak Public API. Faster than on-chain lookups for read operations.
import { ExternalIdResolver } from '@ghostspeak/sdk'
// Initialize resolver for devnet
const resolver = new ExternalIdResolver({
cluster: 'devnet' // Uses https://api-devnet.ghostspeak.ai
})
// Production mainnet
const mainnetResolver = new ExternalIdResolver({
cluster: 'mainnet-beta' // Uses https://api.ghostspeak.ai
})
// Custom API URL
const customResolver = new ExternalIdResolver({
apiUrl: 'https://custom-api.example.com'
})Resolve External IDs to Ghost Addresses
// Quick resolve - returns Ghost address
const ghostAddress = await resolver.resolve('payai', 'agent-123')
console.log(`Ghost Address: ${ghostAddress}`)
// Full lookup - returns mapping + Ghost data
const result = await resolver.lookup('payai', 'agent-123')
console.log(`Platform: ${result.mapping.platform}`)
console.log(`External ID: ${result.mapping.externalId}`)
console.log(`Ghost Address: ${result.mapping.ghostAddress}`)
console.log(`Verified: ${result.mapping.verified}`)
if (result.ghost) {
console.log(`Name: ${result.ghost.name}`)
console.log(`Ghost Score: ${result.ghost.ghostScore}`)
console.log(`Status: ${result.ghost.status}`)
}Fetch Ghost Data via API
// Get full Ghost profile (faster than on-chain query)
const ghost = await resolver.getGhost(ghostAddress)
console.log(`Agent: ${ghost.name}`)
console.log(`Owner: ${ghost.owner}`)
console.log(`Reputation: ${ghost.reputationScore}`)
console.log(`External IDs:`, ghost.externalIdentifiers)
// Get Ghost Score breakdown
const score = await resolver.getGhostScore(ghostAddress)
console.log(`Score: ${score.score}/${score.maxScore}`)
score.components.forEach(c => {
console.log(`- ${c.source}: ${c.score} (weight: ${c.weight})`)
})
// Get detailed reputation data
const reputation = await resolver.getGhostReputation(ghostAddress)
console.log('Reputation breakdown:', reputation)Batch Operations
// Resolve multiple external IDs in parallel
const identifiers = [
{ platform: 'payai', externalId: 'agent-1' },
{ platform: 'elizaos', externalId: 'eliza-bot' },
{ platform: 'github', externalId: 'ai-assistant-007' }
]
const addresses = await resolver.resolveBatch(identifiers)
addresses.forEach((addr, i) => {
if (addr) {
console.log(`${identifiers[i].platform}:${identifiers[i].externalId} → ${addr}`)
} else {
console.log(`${identifiers[i].platform}:${identifiers[i].externalId} → Not found`)
}
})Check Existence & List External IDs
// Check if external ID exists
const exists = await resolver.exists('payai', 'agent-123')
console.log(`Exists: ${exists}`)
// Get all external IDs for a Ghost
const externalIds = await resolver.getExternalIds(ghostAddress)
externalIds.forEach(id => {
console.log(`${id.platform}:${id.externalId} (verified: ${id.verified})`)
})API Health Check
// Check API availability and network status
const health = await resolver.checkHealth()
console.log(`Status: ${health.status}`)
console.log(`Network: ${health.network}`)
console.log(`RPC Connected: ${health.rpc.connected}`)
console.log(`RPC Latency: ${health.rpc.latency}ms`)Error Handling
import {
ExternalIdNotFoundError,
GhostNotFoundError,
GhostSpeakError
} from '@ghostspeak/sdk'
try {
const address = await resolver.resolve('payai', 'non-existent')
} catch (error) {
if (error instanceof ExternalIdNotFoundError) {
console.error(`No Ghost found for ${error.platform}:${error.externalId}`)
} else if (error instanceof GhostNotFoundError) {
console.error(`Ghost not found: ${error.message}`)
} else if (error instanceof GhostSpeakError) {
console.error(`API Error [${error.code}]: ${error.message}`)
}
}Supported Platforms
payai- PayAI Network agentselizaos- ElizaOS framework agentsgithub- GitHub-verified agentstwitter- Twitter/X-verified agents- Custom platforms (defined by facilitators)
Performance: API lookups are 10-50x faster than on-chain queries, ideal for real-time integrations.
10. Analytics & Monitoring
// Collect real-time analytics
const analytics = await client.analytics.collectAllMetrics()
// Generate performance reports
const report = await client.analytics.generateReport({
agentId: agentAddress,
timeRange: { start: startDate, end: endDate },
metrics: ['earnings', 'reputation', 'service-completion']
})
// Export data for external dashboards
const exportData = await client.analytics.exportForDashboard('grafana')11. Privacy Features (Production)
🔒 Privacy: Client-side ElGamal encryption is the standard for confidential transfers in GhostSpeak. It provides robust privacy verification via the x402 payment layer.
// Check privacy feature status
const privacyStatus = client.privacy.getStatus()
console.log(privacyStatus)
// {
// mode: 'client-encryption',
// beta: false,
// message: 'Confidential transfers using client-side encryption (Production)'
// }
// Confidential transfer with automatic encryption
const result = await client.privacy.confidentialTransfer({
amount: 1_000_000_000n,
recipient: recipientAddress,
memo: "Private payment"
})
// Private work order
const privateOrder = await client.privacy.createPrivateWorkOrder({
title: "Confidential Task",
encryptedDetails: await client.privacy.encrypt(sensitiveData),
publicMetadata: { category: "AI-Service" }
})See our Privacy Roadmap for details on current limitations and upcoming features.
🔧 Configuration Options
Client Configuration
interface GhostSpeakConfig {
rpc: SolanaRpcApi // Web3.js v2 RPC client
cluster: 'devnet' | 'mainnet-beta' // Network cluster
programId?: Address // Override program ID
commitment?: Commitment // Transaction commitment level
timeout?: number // Request timeout (ms)
rateLimiting?: { // Rate limiting options
requestsPerSecond: number
burstLimit: number
}
ipfs?: { // IPFS configuration
gateway: string
pinningService?: string
}
}Database Configuration (Optional)
GhostSpeak SDK supports optional Turso database integration for improved performance through caching, transaction indexing, and real-time analytics.
Benefits
- 80%+ Reduction in RPC Calls - Agent discovery uses cached data
- Sub-100ms Query Times - Lightning-fast transaction history queries
- Real-time Analytics - Agent performance metrics and market insights
- Free Tier Available - 5GB storage, 500M reads/month, 10M writes/month
Setup Instructions
Create Turso Account
- Sign up at https://turso.tech
- Create a new database (free tier recommended for development)
- Copy your
Database URLandAuth Tokenfrom the dashboard
Configure Environment Variables
# Add to your .env file TURSO_DATABASE_URL=libsql://your-database.turso.io TURSO_AUTH_TOKEN=your-auth-token-hereVerify Connection (optional)
import { isAvailable, ping } from '@ghostspeak/sdk/database' // Check if database is configured and available const available = await isAvailable() console.log('Database available:', available) // Health check if (available) { const healthy = await ping() console.log('Database healthy:', healthy) }
Graceful Fallback
The SDK automatically falls back to on-chain only mode if database is not configured:
- All existing functionality works without database
- No breaking changes to your application
- Performance benefits only when database is available
Note: The database integration is completely optional. If you don't configure TURSO_DATABASE_URL and TURSO_AUTH_TOKEN, the SDK will continue to work normally using direct on-chain queries.
Network Information
// Devnet (current)
const devnetConfig = {
rpc: createSolanaRpc('https://api.devnet.solana.com'),
cluster: 'devnet' as const,
programId: '5PVu8KEhTJEJnA4rNUgY6qHZXuhMakRitnXWtFJnxBAG' as Address
}
// Mainnet (coming Q4 2025)
const mainnetConfig = {
rpc: createSolanaRpc('https://api.mainnet-beta.solana.com'),
cluster: 'mainnet-beta' as const,
// Program ID TBD after audit
}🧪 Testing
The SDK includes comprehensive test coverage with Vitest:
# Run all tests
bun test
# Run specific test suites
bun test agent # Agent management tests
bun test marketplace # Marketplace tests
bun test escrow # Escrow system tests
bun test governance # Governance tests
bun test token2022 # Token-2022 tests
# Run with coverage
bun test --coverage
# Run integration tests
bun test:integrationTest Categories
- Unit Tests - Individual function and method testing
- Integration Tests - Cross-module interaction testing
- End-to-End Tests - Complete workflow testing
- Property Tests - Cryptographic function validation
🔐 Security Considerations
Best Practices
// 1. Always validate inputs
const result = await client.agents.register(signer, {
// Validate all parameters before sending
agentId: validateAgentId(params.agentId),
name: sanitizeString(params.name),
// ... other validated params
})
// 2. Handle errors gracefully
try {
await client.escrow.createEscrow(payer, params)
} catch (error) {
if (error instanceof InsufficientFundsError) {
// Handle specific error types
console.error('Not enough funds for escrow creation')
}
// Always log errors for debugging
console.error('Escrow creation failed:', error)
}
// 3. Use proper key management
// Never hardcode private keys in production
const signer = await loadSignerFromEnvironment()
// 4. Verify transaction results
const signature = await client.marketplace.purchaseService(buyer, params)
const confirmation = await client.rpc.confirmTransaction(signature)
if (confirmation.value.err) {
throw new Error('Transaction failed')
}Rate Limiting
The SDK includes built-in rate limiting to prevent spam and ensure fair usage:
const client = new GhostSpeakClient({
// ... other config
rateLimiting: {
requestsPerSecond: 10, // Max 10 requests per second
burstLimit: 50 // Allow bursts up to 50 requests
}
})📊 Performance & Optimization
Efficient Querying
// Use filters to reduce data transfer
const agents = await client.agents.listAgents({
limit: 50, // Limit results
verificationStatus: 'verified', // Pre-filter on-chain
category: 'data-analysis' // Specific category only
})
// Batch operations when possible
const signatures = await client.marketplace.batchCreateListings(
creator,
[listing1, listing2, listing3]
)Caching
// Enable caching for frequently accessed data
const client = new GhostSpeakClient({
// ... other config
caching: {
enabled: true,
ttl: 300, // 5 minute cache
maxEntries: 1000 // Max cache entries
}
})🔄 Migration Guide
From Web3.js v1
// Old Web3.js v1 pattern
import { Connection, PublicKey } from '@solana/web3.js'
const connection = new Connection('https://api.devnet.solana.com')
// New Web3.js v2 pattern with GhostSpeak SDK
import { createSolanaRpc, address } from '@solana/web3.js'
import { GhostSpeakClient } from '@ghostspeak/sdk'
const client = new GhostSpeakClient({
rpc: createSolanaRpc('https://api.devnet.solana.com'),
cluster: 'devnet'
})📝 API Reference
Core Classes
GhostSpeakClient- Main SDK clientAgentInstructions- Agent management operationsDidModule- W3C DID document management 🆕ReputationModule- Reputation calculation and tag management 🆕AuthorizationModule- Pre-signed authorizations for facilitators 🆕MarketplaceInstructions- Marketplace operationsEscrowInstructions- Escrow and payment operationsGovernanceInstructions- Governance and multisig operationsToken2022Instructions- Token-2022 operationsAnalyticsCollector- Analytics and monitoring
Type Definitions
The SDK exports comprehensive TypeScript types for all operations:
import type {
Agent,
DidDocument,
VerificationMethod,
ServiceEndpoint,
TagScore,
ReputationMetrics,
ReputationAuthorization,
OnChainStorageConfig,
ServiceListing,
EscrowAccount,
GovernanceProposal,
AuctionMarketplace,
// ... 200+ more types
} from '@ghostspeak/sdk'🛠️ Development & Contributing
Building from Source
# Clone the repository
git clone https://github.com/ghostspeak/ghostspeak.git
cd ghostspeak/packages/sdk-typescript
# Install dependencies
bun install
# Build the SDK
bun run build
# Run tests
bun test
# Lint and type check
bun run lint
bun run type-checkContributing
We welcome contributions! Please see our Contributing Guide for details on:
- Code style and conventions
- Testing requirements
- Pull request process
- Issue reporting
📞 Support
Resources
- 📖 Full Documentation - Complete API reference
- 💬 Discord Community - Get help and discuss
- 🐛 GitHub Issues - Report bugs
- 📧 Email Support - Direct developer support
Status & Monitoring
- 🟢 Devnet Status: status.ghostspeak.io/devnet
- 📊 Network Statistics: stats.ghostspeak.io
- 📈 Performance Metrics: metrics.ghostspeak.io
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
Built with ❤️ for the autonomous AI future
