@human-0/posh-sdk
Version:
TypeScript SDK for Proof of Sustainable Humanity (PoSH) identity management
436 lines (412 loc) • 11.4 kB
text/typescript
import { Address as Address$1, Hash } from 'viem';
/**
* Common type definitions
*/
type Address = `0x${string}`;
type HumanId = `0x${string}`;
type TransactionHash = `0x${string}`;
/**
* Configuration type definitions
*/
interface PoshConfig {
chainId: number;
rpcUrl?: string;
contracts: {
humanIdentity: Address;
proofRegistry: Address;
poshNFT: Address;
humanScore: Address;
};
cache?: {
enabled: boolean;
ttl: number;
maxSize: number;
};
retry?: {
enabled: boolean;
maxAttempts: number;
backoff: 'linear' | 'exponential';
initialDelay: number;
};
}
/**
* Identity type definitions
*/
interface Identity {
humanId: HumanId;
wallet: Address;
registrationTime: Date;
externalProofs: ExternalProof[];
}
interface ExternalProof {
hash: string;
provider: string;
linkedAt: Date;
}
/**
* Proof type definitions
*/
interface Proof {
proofId: string;
humanId: HumanId;
impactType: ImpactType;
impactValue: bigint;
methodologyHash: string;
verificationHash: string;
timestamp: Date;
tier: ProofTier;
}
declare enum ProofTier {
A = 1,
B = 2,
C = 3
}
type ImpactType = 'renewable_energy' | 'carbon_avoidance' | 'sustainable_transport' | 'waste_reduction' | 'water_conservation';
interface ProofQueryOptions {
impactType?: ImpactType;
tier?: ProofTier;
startDate?: Date;
endDate?: Date;
limit?: number;
offset?: number;
}
interface ProofFilter {
impactTypes?: ImpactType[];
tiers?: ProofTier[];
minValue?: bigint;
maxValue?: bigint;
dateRange?: {
start: Date;
end: Date;
};
}
interface ImpactSummary {
totalImpact: bigint;
byType: Record<ImpactType, bigint>;
byTier: TierBreakdown;
proofCount: number;
}
interface TierBreakdown {
tierA: number;
tierB: number;
tierC: number;
total: number;
}
interface AggregateOptions {
groupBy?: 'type' | 'tier' | 'month';
applyWeighting?: boolean;
applyTimeDecay?: boolean;
}
/**
* Score type definitions
*/
interface ScoreLevel {
level: number;
name: 'None' | 'Bronze' | 'Silver' | 'Gold' | 'Platinum' | 'Diamond';
minScore: number;
maxScore: number | null;
}
/**
* Event type definitions
*/
interface HumanRegisteredEvent {
humanId: HumanId;
wallet: Address;
timestamp: Date;
blockNumber: number;
transactionHash: string;
}
interface ProofRegisteredEvent {
proofId: string;
humanId: HumanId;
impactType: ImpactType;
impactValue: bigint;
tier: ProofTier;
timestamp: Date;
blockNumber: number;
transactionHash: string;
}
interface IdentityLinkedEvent {
humanId: HumanId;
proofHash: string;
provider: string;
timestamp: Date;
blockNumber: number;
transactionHash: string;
}
interface EventFilter {
fromBlock?: number;
toBlock?: number;
humanId?: HumanId;
}
type Unsubscribe = () => void;
/**
* Base provider interface for blockchain interactions
* Abstracts away the underlying Ethereum library (Viem, Wagmi, ethers.js)
*/
/**
* Contract call parameters
*/
interface ContractCallParams {
address: Address$1;
abi: any[];
functionName: string;
args?: any[];
}
/**
* Transaction parameters
*/
interface TransactionParams extends ContractCallParams {
value?: bigint;
gas?: bigint;
}
/**
* Transaction receipt
*/
interface TransactionReceipt {
transactionHash: Hash;
blockNumber: bigint;
status: 'success' | 'reverted';
gasUsed: bigint;
logs: any[];
}
/**
* Provider event filter parameters
*/
interface ProviderEventFilter {
fromBlock?: bigint;
toBlock?: bigint;
address?: Address$1;
topics?: (Hash | Hash[] | null)[];
}
/**
* Provider event log
*/
interface ProviderEventLog {
address: Address$1;
topics: Hash[];
data: Hash;
blockNumber: bigint;
transactionHash: Hash;
logIndex: number;
}
/**
* Provider event subscription callback
*/
type ProviderEventCallback = (log: ProviderEventLog) => void;
/**
* Provider unsubscribe function
*/
type ProviderUnsubscribe = () => void;
/**
* Base provider interface
* All provider adapters must implement this interface
*/
interface BaseProvider {
/**
* Read from a contract (view/pure functions)
*/
readContract<T = any>(params: ContractCallParams): Promise<T>;
/**
* Write to a contract (state-changing functions)
* Returns transaction hash
*/
writeContract(params: TransactionParams): Promise<Hash>;
/**
* Wait for transaction confirmation
*/
waitForTransaction(hash: Hash): Promise<TransactionReceipt>;
/**
* Estimate gas for a transaction
*/
estimateGas(params: TransactionParams): Promise<bigint>;
/**
* Get past events matching filter
*/
getEvents(params: ContractCallParams & {
filter: ProviderEventFilter;
}): Promise<ProviderEventLog[]>;
/**
* Subscribe to contract events
*/
watchEvent(params: ContractCallParams & {
callback: ProviderEventCallback;
}): ProviderUnsubscribe;
/**
* Get current block number
*/
getBlockNumber(): Promise<bigint>;
/**
* Get chain ID
*/
getChainId(): Promise<number>;
}
/**
* IdentityManager - handles identity operations
*/
interface RegisterResult {
hash: Hash;
humanId: HumanId;
}
interface TransactionResult {
hash: Hash;
}
declare class IdentityManager {
private config;
private cache;
private provider?;
constructor(config: PoshConfig, provider?: BaseProvider);
/**
* Check if an address is registered
* Note: Mock implementation - returns false until contracts are deployed
*/
isRegistered(address: Address): Promise<boolean>;
/**
* Get humanId for an address
* Note: Mock implementation - returns null until contracts are deployed
*/
getHumanId(address: Address): Promise<HumanId | null>;
/**
* Get wallet address for a humanId
* Note: Mock implementation
*/
getWallet(_humanId: HumanId): Promise<Address>;
/**
* Get registration time for a humanId
* Note: Mock implementation
*/
getRegistrationTime(_humanId: HumanId): Promise<Date>;
/**
* Register a new identity
* Requires a provider with write capabilities
*/
register(): Promise<RegisterResult>;
/**
* Link an external proof to identity
* Requires a provider with write capabilities
*/
linkExternalProof(_proofHash: string, _provider: string): Promise<TransactionResult>;
/**
* Estimate gas for registration
*/
estimateRegisterGas(): Promise<bigint>;
/**
* Estimate gas for linking external proof
*/
estimateLinkProofGas(): Promise<bigint>;
}
/**
* ProofManager - handles proof queries and aggregations
*/
declare class ProofManager {
private config;
private cache;
constructor(config: PoshConfig, _provider?: BaseProvider);
/**
* Get all proofs for a humanId
* Note: Mock implementation - returns empty array until contracts are deployed
*/
getHumanProofs(humanId: HumanId, options?: ProofQueryOptions): Promise<Proof[]>;
/**
* Get proof count for a humanId
* Note: Mock implementation
*/
getProofCount(humanId: HumanId): Promise<number>;
/**
* Get total impact for a humanId
* Note: Mock implementation - returns 0 until contracts are deployed
*/
getTotalImpact(humanId: HumanId, impactType?: ImpactType): Promise<bigint>;
}
/**
* ScoreManager - handles score calculations
*/
declare class ScoreManager {
private config;
private cache;
constructor(config: PoshConfig, _provider?: BaseProvider);
/**
* Get score for a humanId
* Note: Mock implementation - returns 0 until contracts are deployed
*/
getScore(humanId: HumanId): Promise<number>;
/**
* Get level for a humanId
* Note: Mock implementation
*/
getLevel(humanId: HumanId): Promise<ScoreLevel>;
/**
* Check if score meets threshold
*/
meetsThreshold(humanId: HumanId, threshold: number): Promise<boolean>;
/**
* Get tier breakdown for a humanId
* Note: Mock implementation
*/
getTierBreakdown(_humanId: HumanId): Promise<TierBreakdown>;
/**
* Get level from score value
*/
getLevelFromScore(score: number): ScoreLevel;
}
/**
* EventManager - handles blockchain event subscriptions
*/
declare class EventManager {
constructor(_config: PoshConfig, _provider?: BaseProvider);
/**
* Subscribe to HumanRegistered events
* Note: Mock implementation - no events until contracts are deployed
*/
onHumanRegistered(_callback: (event: HumanRegisteredEvent) => void): Unsubscribe;
/**
* Subscribe to ProofRegistered events
* Note: Mock implementation
*/
onProofRegistered(_callback: (event: ProofRegisteredEvent) => void): Unsubscribe;
/**
* Subscribe to IdentityLinked events
* Note: Mock implementation
*/
onIdentityLinked(_callback: (event: IdentityLinkedEvent) => void): Unsubscribe;
/**
* Get historical HumanRegistered events
* Note: Mock implementation
*/
getHumanRegisteredEvents(_filter?: EventFilter): Promise<HumanRegisteredEvent[]>;
/**
* Get historical ProofRegistered events
* Note: Mock implementation
*/
getProofRegisteredEvents(_filter?: EventFilter): Promise<ProofRegisteredEvent[]>;
/**
* Get historical IdentityLinked events
* Note: Mock implementation
*/
getIdentityLinkedEvents(_filter?: EventFilter): Promise<IdentityLinkedEvent[]>;
}
/**
* Main PoshClient class - entry point for the SDK
*/
interface PoshClientConfig extends PoshConfig {
provider?: BaseProvider;
}
declare class PoshClient {
private config;
private provider?;
readonly identity: IdentityManager;
readonly proofs: ProofManager;
readonly score: ScoreManager;
readonly events: EventManager;
constructor(config: PoshClientConfig);
/**
* Get current configuration
*/
getConfig(): PoshConfig;
/**
* Update configuration
* Note: This creates a new client instance internally
*/
updateConfig(updates: Partial<PoshConfig>): void;
}
export { type Address as A, type BaseProvider as B, type ContractCallParams as C, EventManager as E, type HumanId as H, IdentityManager as I, type ProviderEventFilter as P, type RegisterResult as R, ScoreManager as S, type TransactionParams as T, type Unsubscribe as U, type TransactionReceipt as a, type ProviderEventLog as b, type ProviderEventCallback as c, type ProviderUnsubscribe as d, type PoshConfig as e, type PoshClientConfig as f, PoshClient as g, type TransactionResult as h, ProofManager as i, type Identity as j, type ExternalProof as k, type Proof as l, ProofTier as m, type ImpactType as n, type ProofQueryOptions as o, type ProofFilter as p, type ImpactSummary as q, type TierBreakdown as r, type AggregateOptions as s, type ScoreLevel as t, type HumanRegisteredEvent as u, type ProofRegisteredEvent as v, type IdentityLinkedEvent as w, type EventFilter as x, type TransactionHash as y };