Skip to content

Repository files navigation

PolkaMesh SDK

The official TypeScript SDK for interacting with the PolkaMesh confidential compute network.

🚀 Features

  • Confidential Compute: Submit encrypted jobs to Phala TEE workers.
  • MEV Protection: Submit intents for fair batching and sandwich attack protection.
  • Attestation Verification: Verify cryptographic proofs of execution.
  • Type Safety: Full TypeScript support with generated types.

📦 Installation

npm install @polkamesh/sdk
# or
yarn add @polkamesh/sdk

🔧 Configuration

Create a .env file in your project root:

# Polkadot Network
RPC_URL=wss://rpc1.paseo.popnetwork.xyz

# Core Contracts (Ink! v6)
PAYMENT_ESCROW=0x5a86a13ef7fc1c5e58f022be183de015dfb702ae
AI_JOB_QUEUE=0xa44639cd0d0e6c6607491088c9c549e184456122
COMPUTE_PROVIDER_REGISTRY=0x2c6fc00458f198f46ef072e1516b83cd56db7cf5
DATA_NFT_REGISTRY=0x6dc84ddeffccb19ed5285cf3c3d7b03a57a9a4b1

# Advanced Contracts (Ink! v5)
MEV_PROTECTION=5DTPZHSHydkPQZbTFrhnHtZiDER7uoKSzdYHuCUXVAtjajXs
PHALA_JOB_PROCESSOR=5HrKZAiTSAFcuxda89kSD77ZdygRUkufwRnGKgfGFR4NC2np

# Phala Phat Contract (Off-chain TEE)
PHALA_PHAT_CONTRACT_ID=app_4c48fd1fdbcf7495e90758c6b4108faf1205c3a3
PHALA_WORKER_ENDPOINT=https://phala-worker-api.phala.network
PHALA_CLUSTER_ID=poc6-testnet

📖 Usage

Initialization

import { createPolkaMesh } from '@polkamesh/sdk';

const sdk = await createPolkaMesh({
  rpcUrl: process.env.RPC_URL,
  contractAddresses: {
    paymentEscrow: process.env.PAYMENT_ESCROW,
    aiJobQueue: process.env.AI_JOB_QUEUE,
    computeProviderRegistry: process.env.COMPUTE_PROVIDER_REGISTRY,
    dataNFTRegistry: process.env.DATA_NFT_REGISTRY,
    mevProtection: process.env.MEV_PROTECTION,
    phalaJobProcessor: process.env.PHALA_JOB_PROCESSOR,
  },
  phatConfig: {
    contractId: process.env.PHALA_PHAT_CONTRACT_ID,
    workerEndpoint: process.env.PHALA_WORKER_ENDPOINT,
    clusterId: process.env.PHALA_CLUSTER_ID,
  }
});

Submit AI Job

const aiJobQueue = sdk.getAIJobQueue();

const job = await aiJobQueue.submitJob({
  description: 'Train ML model on encrypted dataset',
  budget: '10000000000000000', // 10 DOT
  dataSetId: '123',
  computeType: 'GPU_V100',
});

console.log('Job ID:', job.jobId);

Submit Confidential Job to Phala TEE

const phala = sdk.getPhalaJobProcessor();

const result = await phala.submitConfidentialJob({
  model: 'gpt-2',
  input: 'Sensitive user data',
  requirements: { memory: 16384, gpu: 'T4' }
});

console.log('Job ID:', result.jobId);
console.log('Encrypted:', result.encryptedPayload);

Verify Attestation

const attestation = await phala.getAttestation(jobId);

if (attestation.valid) {
  console.log('Execution verified!', attestation.proof);
}

MEV Protection

const mev = sdk.getMEVProtection();

// Submit a trade intent
const intentId = await mev.submitIntent({
  tokenIn: 'DOT',
  tokenOut: 'USDT',
  amountIn: '10000000000',
  minAmountOut: '50000000',
});

console.log('Intent submitted:', intentId);

🏗️ Architecture

The SDK wraps interactions with three main components:

  1. MEV Protection Contract (Ink!): Handles intent aggregation.
  2. Phala Job Processor (Ink!): Orchestrates off-chain computation.
  3. Phat Contract (Rust/WASM): Executes logic inside TEE.

🔒 Security

  • ECIES Encryption: All confidential data is encrypted using Elliptic Curve Integrated Encryption Scheme.
  • Remote Attestation: Execution results are signed by the TEE hardware.
  • On-Chain Verification: Proofs are verified on-chain before payment release.

📄 License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages