Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(verify): add support to verify program #199

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/agent/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ import {
multisig_reject_proposal,
multisig_approve_proposal,
multisig_execute_proposal,
verifyProgram,
checkVerificationStatus,
cancelVerification,
VerificationOptions,
} from "../tools";
import {
Config,
Expand Down Expand Up @@ -654,4 +658,36 @@ export class SolanaAgentKit {
): Promise<string> {
return multisig_execute_proposal(this, transactionIndex);
}

async verifyProgram(
programId: string,
repository: string,
commitHash: string,
options?: VerificationOptions,
) {
const processedOptions: VerificationOptions | undefined = options
? {
libName: options.libName ?? null,
bpfFlag: options.bpfFlag ?? null,
cargoArgs: options.cargoArgs ?? null,
verifyProgramId: options.verifyProgramId ?? null,
}
: undefined;

return verifyProgram(
this,
programId,
repository,
commitHash,
processedOptions,
);
}

async checkVerificationStatus(programId: string) {
return checkVerificationStatus(programId);
}

async cancelVerification(programId: PublicKey, verifyProgramId?: PublicKey) {
return cancelVerification(this, programId, verifyProgramId);
}
}
1 change: 1 addition & 0 deletions src/langchain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export * from "./tiplink";
export * from "./sns";
export * from "./lightprotocol";
export * from "./squads";
export * from "./verification";

import { SolanaAgentKit } from "../agent";
import {
Expand Down
1 change: 1 addition & 0 deletions src/langchain/verification/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./verify_program";
164 changes: 164 additions & 0 deletions src/langchain/verification/verify_program.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import { PublicKey } from "@solana/web3.js";
import { Tool } from "langchain/tools";
import { SolanaAgentKit } from "../../agent";

export interface VerificationOptions {
verifyProgramId: PublicKey | null;
libName: string | null;
bpfFlag: boolean | null;
cargoArgs: string[] | null;
}

export interface VerificationResponse {
verificationPda: string;
status: "success" | "error";
message: string;
jobId?: string;
}

export interface VerificationInput {
programId: string;
repository: string;
commitHash: string;
verifyProgramId?: string;
libName?: string;
bpfFlag?: boolean;
cargoArgs?: string[];
}

interface StatusCheckInput {
programId: string;
}

interface CancellationInput {
programId: string;
verifyProgramId?: string;
}

export class SolanaVerifyTool extends Tool {
name = "solana_program_verification";
description = `Verify a Solana program using its source code repository.
Input is a JSON string with:
- programId: string (required) - Solana program ID to verify
- repository: string (required) - GitHub repository URL
- commitHash: string (required) - Git commit hash or branch name
- verifyProgramId: string (optional) - Custom verify program ID
- libName: string (optional) - Library name for multi-program repos
- bpfFlag: boolean (optional) - Use cargo build-bpf instead of build-sbf
- cargoArgs: string[] (optional) - Additional cargo build arguments`;

constructor(private solanaKit: SolanaAgentKit) {
super();
}

protected async _call(input: string): Promise<string> {
try {
const parsedInput: VerificationInput = JSON.parse(input);

this.validateInput(parsedInput);

const options: VerificationOptions = {
verifyProgramId: parsedInput.verifyProgramId
? new PublicKey(parsedInput.verifyProgramId)
: null,
libName: parsedInput.libName ?? null,
bpfFlag: parsedInput.bpfFlag ?? null,
cargoArgs: parsedInput.cargoArgs ?? null,
};

const result = await this.solanaKit.verifyProgram(
parsedInput.programId,
parsedInput.repository,
parsedInput.commitHash,
options,
);

return this.formatSuccessResponse(result);
} catch (error) {
return this.formatErrorResponse(error);
}
}

async cancelVerification(input: string): Promise<string> {
try {
const parsedInput: CancellationInput = JSON.parse(input);

if (!parsedInput.programId) {
throw new Error("Program ID is required");
}

const programId = new PublicKey(parsedInput.programId);
const verifyProgramId = parsedInput.verifyProgramId
? new PublicKey(parsedInput.verifyProgramId)
: undefined;

const signature = await this.solanaKit.cancelVerification(
programId,
verifyProgramId,
);

return JSON.stringify({
status: "success",
message: "Verification cancelled successfully",
signature,
});
} catch (error) {
return this.formatErrorResponse(error);
}
}

async checkStatus(input: string): Promise<string> {
try {
const parsedInput: StatusCheckInput = JSON.parse(input);

if (!parsedInput.programId) {
throw new Error("Program ID is required");
}

const status = await this.solanaKit.checkVerificationStatus(
parsedInput.programId,
);

return JSON.stringify({
status: "success",
...status,
});
} catch (error) {
return this.formatErrorResponse(error);
}
}

private validateInput(input: VerificationInput): void {
if (!input.programId) {
throw new Error("Program ID is required");
}
if (!input.repository) {
throw new Error("Repository URL is required");
}
if (!input.commitHash) {
throw new Error("Commit hash is required");
}
}

private formatSuccessResponse(result: VerificationResponse): string {
return JSON.stringify({
status: "success",
verificationPda: result.verificationPda,
message: result.message,
jobId: result.jobId,
});
}

private formatErrorResponse(error: unknown): string {
const errorMessage = error instanceof Error ? error.message : String(error);

return JSON.stringify({
status: "error",
message: errorMessage,
code:
error instanceof Error && "code" in error
? (error as any).code
: "UNKNOWN_ERROR",
});
}
}
1 change: 1 addition & 0 deletions src/tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ export * from "./3land";
export * from "./tiplink";
export * from "./lightprotocol";
export * from "./squads";
export * from "./verification";
1 change: 1 addition & 0 deletions src/tools/verification/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./verify_program";
Loading