Connection Utility

The remainder of the documentation will be making use of the following utility to connect the SDK in an effort to keep the example usage excerpts brief.

The utility file found in the example usage repo handles:

  • Building the Viem client from a private key
  • Initializing the Mesh SDK
  • Logging in with SiWe challenge
import { config as dotEnvConfig } from "dotenv";
import { createWalletClient, http, createPublicClient, Chain, PublicClient, WalletClient } from 'viem';
import { privateKeyToAccount } from 'viem/accounts';
import { sepolia } from 'viem/chains';
import { MeshSDK, Mesh, MeshSDKOptions, UnifiedClient } from "@instruxi-io/mesh-sdk-core";
import fs from 'fs';
import path from 'path';

dotEnvConfig();

const API_URI = "https://gateway-staging.instruxi.dev";
const SDK_VERSION = "0.0.1";
const TOKEN_FILE = path.join(__dirname, '.jwt_token.json');

export interface MeshSDKConfig {
  apiKey: string;
  privateKey?: string;
  chain?: Chain;
}

// We construct the client outside of the SDK 
async function createUnifiedClient(privateKey: string, chain: Chain = sepolia): Promise<UnifiedClient> {
	...
  return {
    account: viemAccount,
    open: publicClient,
    private: {
      ...walletClient,
      chain,
    },
  };
}


export async function initializeMeshSDK(config: MeshSDKConfig): Promise<Mesh> {
	...
  return mesh;
}

async function login(mesh: Mesh, unifiedClient: UnifiedClient): Promise<void> {
	...
  mesh.setJwtToken(authResponse.token);
}