SDK

Phi SDK provides tools and libraries for programmatic interaction with the Phi Protocol. Through an intuitive and consistent API, developers can create credentials, manage NFTs, and interact with multiple blockchain networks.

Installation

To install the Phi SDK, run the following command:

bun add @phi-hub/lib

Usage

Importing the SDK

First, import the necessary modules from the Phi SDK:

import { CredManager, ArtManager, VerifierManager } from "@phi-hub/lib";

Creating a Merkle-based Cred

Use the CredManager to create a Merkle-based credential:

const credManager = new CredManager(privateKey, chainId);

const input = {
  creator: "0xYourAddress",
  requirement: "YourRequirement",
  description: "YourDescription",
  imageData: "Base64EncodedImageData",
  networks: [1, 4],
  tags: ["tag1", "tag2"],
  relatedLinks: ["https://example.com"],
  signalRoyalty: 0.01,
  unSignalRoyalty: 0.02,
  verificationSource: "https://verificationsource.com",
  addressList: "YourAddressListCSVData",
};

const credId = await credManager.createMerkleCred(input, chainId);
console.log(`Credential created successfully with ID: \${credId}`);

Signaling and Unsignaling Cred

Use the CredManager to signal or unsignal a credential:

await credManager.signalCred(credId, amount);
await credManager.unsignalCred(credId, amount);

Uploading an art for a cred

Arts are of two types:

  1. IMAGE - static image

  2. API_ENDPOINT - dynamic image which can be fetched from an external source

Use the ArtManager to create and upload art. The inputs will vary according to the artType

const artManager = new ArtManager(privateKey, artChainId, credChainId);

let inputs = {
      artType: "IMAGE", // IMAGE | API_ENDPOINT
      title: "Awesome Title",
      network: 84532,
      artist: "0xYourAddress",
      receiver: "0xReceiverAddress",
      description: "YourDescription",
      externalURL: "https://example.com",
      start: 1724681692,
      end: 4880355292,
      maxSupply: 100,
      price: parseEther(new Big(1).toString()).toString(),
      soulbound: false,
      tags: ["tag1", "tag2"],
};

if(inputs.artType === 'IMAGE') {
    inputs.imageData = "Base64EncodedImageData"
} else if(inputs.artType === 'API_ENDPOINT') {
    inputs.endpoint = "https://api.example.com"
    inputs.previewInput = {
        address: "0xAnyAddress",
        data: "AnyStringifiedData"
    }
}

const artId = await artManager.createArt(inputs, credId);
console.log(`art created successfully with ID: \${artId}`);

Creating a verifier for a cred

Use the VerifierManager to create a verifier.

const verifierManager = new VerifierManager(credChainId as CredChainId);

let inputs: AddVerifierInput = {
  address,
  endpoint,
  verificationSource,
};

const arweaveId = await verifierManager.createVerifier(inputs, credId)
console.log(`verifier added successfully: \${arweaveId}`);

API Reference

CredManager

  • Constructor: new CredManager(privateKey: string, chainId: CredentialChainId)

    • privateKey: The private key of the user's account.

    • chainId: The ID of the blockchain network.

  • Methods:

    • createMerkleCred(input: MerkleCredCreateInput, chainId: CredentialChainId): Promise<string>

      • Creates a Merkle-based credential.

      • input: An object containing credential details.

      • chainId: The ID of the blockchain network.

    • signalCred(credId: string, amount: number): Promise<void>

      • Signals a credential.

      • credId: The ID of the credential.

      • amount: The amount to signal.

    • unsignalCred(credId: string, amount: number): Promise<void>

      • Unsignals a credential.

      • credId: The ID of the credential.

      • amount: The amount to unsignal.

ArtManager

  • Constructor: new ArtManager(privateKey: Hex, artChainId: ArtChainId, credChainId: CredChainId)

    • privateKey: The private key of the user's account.

    • artChainId: The ID of the blockchain network to upload the art to.

    • credChainId: The ID of the blockchain network where the cred is.

  • Methods:

    • createArt(input: ArtCreateInput, credId: string | number): Promise<string | undefined>

      • Creates and upload art and its metadata

      • input: An object containing art details.

      • credId: The ID of the cred.

VerifierManager

  • Constructor: new VerifierManager(credChainId as CredChainId)

    • credChainId: The ID of the blockchain network where the cred is.

  • Methods:

    • createVerifier(input: AddVerifierInput, credId: string | number): Promise<string | undefined>

      • Creates verifier for a cred

      • input: An object containing verification details.

      • credId: The ID of the cred.

config

  • Methods:

    • setChain(chain: string): void

      • Sets the default blockchain network.

    • setPrivateKey(privateKey: string): void

      • Sets the private key for transactions.

Type Definitions

MerkleCredCreateInput

export interface MerkleCredCreateInput {
  creator: string;
  requirement: string;
  description?: string;
  imageData: string;
  networks: number[];
  tags?: string[];
  relatedLinks?: string[];
  signalRoyalty: number;
  unSignalRoyalty: number;
  verificationSource: string;
  addressList: string;
}

ArtCreateInput

export interface BaseArtCreateInput {
  artType: ArtType;
  title: string;
  network: ArtChainId;
  artist: `0x${string}`;
  receiver?: `0x${string}`;
  description?: string;
  externalURL?: string;
  start?: number;
  end?: number;
  maxSupply?: number;
  price?: string;
  soulbound?: boolean;
  tags?: string[];
}

export type ArtCreateInput =
  | (BaseArtCreateInput & {
      artType: "API_ENDPOINT";
      endpoint: string;
      previewInput: { address: string; data: string };
    })
  | (BaseArtCreateInput & { artType: "IMAGE"; imageData: string });

AddVerifierInput

export type AddVerifierInput = {
  address: Address;
  endpoint: string;
  verificationSource: string;
}

Last updated