Skip to content

Client SDK with Receipt Verification #45

@AnkanMisra

Description

@AnkanMisra

Problem

There's no official way for clients to integrate with MicroAI-Paygate. Users need a TypeScript SDK to sign payments and verify receipts.

Architecture

flowchart LR
    subgraph Client
        A[User App] --> B[Paygate SDK]
        B --> C[ethers.js]
    end
    
    subgraph Server
        D[Gateway]
        E[Verifier]
    end
    
    B -->|1. Sign Payment| D
    D -->|2. Verify| E
    E -->|3. Valid| D
    D -->|4. Receipt + Response| B
    B -->|5. Verify Receipt| B
Loading

SDK Flow

sequenceDiagram
    participant App
    participant SDK
    participant Gateway
    
    App->>SDK: request("/api/ai/summarize", body)
    SDK->>Gateway: POST (no signature)
    Gateway-->>SDK: 402 + PaymentContext
    SDK->>SDK: Sign with ethers.js
    SDK->>Gateway: POST + X-402-Signature
    Gateway-->>SDK: 200 + Receipt
    SDK->>SDK: Verify receipt signature
    SDK-->>App: Response + verified receipt
Loading

Implementation

New directory: sdk/typescript/

// sdk/typescript/src/index.ts
export class PaygateClient {
  constructor(private serverUrl: string, private signer: ethers.Signer) {}
  
  async signPayment(context: PaymentContext): Promise<string>
  async verifyReceipt(receipt: SignedReceipt): Promise<boolean>
  async request<T>(endpoint: string, body: any): Promise<T>
}

Acceptance Criteria

  • Package structure with package.json
  • signPayment() creates valid EIP-712 signatures
  • verifyReceipt() validates server signatures (Keccak256)
  • Works with receipts from Go gateway
  • Unit tests with real receipt fixtures
  • Example usage in README

Testing

cd sdk/typescript && bun test

Must test against actual receipts from running server

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions