|
| 1 | +import BN from "bn.js"; |
| 2 | +import fetch from "cross-fetch"; |
| 3 | +import * as borsh from "@project-serum/borsh"; |
| 4 | +import { Connection, PublicKey } from "@solana/web3.js"; |
| 5 | + |
| 6 | +/** |
| 7 | + * Returns a verified build from the anchor registry. null if no such |
| 8 | + * verified build exists, e.g., if the program has been upgraded since the |
| 9 | + * last verified build. |
| 10 | + */ |
| 11 | +export async function verifiedBuild( |
| 12 | + connection: Connection, |
| 13 | + programId: PublicKey, |
| 14 | + limit: number = 5 |
| 15 | +): Promise<Build | null> { |
| 16 | + const url = `https://anchor.projectserum.com/api/v0/program/${programId.toString()}/latest?limit=${limit}`; |
| 17 | + const [programData, latestBuildsResp] = await Promise.all([ |
| 18 | + fetchData(connection, programId), |
| 19 | + fetch(url), |
| 20 | + ]); |
| 21 | + |
| 22 | + // Filter out all non successful builds. |
| 23 | + const latestBuilds = (await latestBuildsResp.json()).filter( |
| 24 | + (b: Build) => !b.aborted && b.state === "Built" && b.verified === "Verified" |
| 25 | + ); |
| 26 | + if (latestBuilds.length === 0) { |
| 27 | + return null; |
| 28 | + } |
| 29 | + |
| 30 | + // Get the latest build. |
| 31 | + const build = latestBuilds[0]; |
| 32 | + |
| 33 | + // Has the program been upgraded since the last build? |
| 34 | + if (programData.slot.toNumber() !== build.verified_slot) { |
| 35 | + return null; |
| 36 | + } |
| 37 | + |
| 38 | + // Success. |
| 39 | + return build; |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Returns the program data account for this program, containing the |
| 44 | + * metadata for this program, e.g., the upgrade authority. |
| 45 | + */ |
| 46 | +export async function fetchData( |
| 47 | + connection: Connection, |
| 48 | + programId: PublicKey |
| 49 | +): Promise<ProgramData> { |
| 50 | + const accountInfo = await connection.getAccountInfo(programId); |
| 51 | + if (accountInfo === null) { |
| 52 | + throw new Error("program account not found"); |
| 53 | + } |
| 54 | + const { program } = decodeUpgradeableLoaderState(accountInfo.data); |
| 55 | + const programdataAccountInfo = await connection.getAccountInfo( |
| 56 | + program.programdataAddress |
| 57 | + ); |
| 58 | + if (programdataAccountInfo === null) { |
| 59 | + throw new Error("program data account not found"); |
| 60 | + } |
| 61 | + const { programData } = decodeUpgradeableLoaderState( |
| 62 | + programdataAccountInfo.data |
| 63 | + ); |
| 64 | + return programData; |
| 65 | +} |
| 66 | + |
| 67 | +const UPGRADEABLE_LOADER_STATE_LAYOUT = borsh.rustEnum( |
| 68 | + [ |
| 69 | + borsh.struct([], "uninitialized"), |
| 70 | + borsh.struct( |
| 71 | + [borsh.option(borsh.publicKey(), "authorityAddress")], |
| 72 | + "buffer" |
| 73 | + ), |
| 74 | + borsh.struct([borsh.publicKey("programdataAddress")], "program"), |
| 75 | + borsh.struct( |
| 76 | + [ |
| 77 | + borsh.u64("slot"), |
| 78 | + borsh.option(borsh.publicKey(), "upgradeAuthorityAddress"), |
| 79 | + ], |
| 80 | + "programData" |
| 81 | + ), |
| 82 | + ], |
| 83 | + undefined, |
| 84 | + borsh.u32() |
| 85 | +); |
| 86 | + |
| 87 | +export function decodeUpgradeableLoaderState(data: Buffer): any { |
| 88 | + return UPGRADEABLE_LOADER_STATE_LAYOUT.decode(data); |
| 89 | +} |
| 90 | + |
| 91 | +export type ProgramData = { |
| 92 | + slot: BN; |
| 93 | + upgradeAuthorityAddress: PublicKey | null; |
| 94 | +}; |
| 95 | + |
| 96 | +export type Build = { |
| 97 | + aborted: boolean; |
| 98 | + address: string; |
| 99 | + created_at: string; |
| 100 | + updated_at: string; |
| 101 | + descriptor: string[]; |
| 102 | + docker: string; |
| 103 | + id: number; |
| 104 | + name: string; |
| 105 | + sha256: string; |
| 106 | + upgrade_authority: string; |
| 107 | + verified: string; |
| 108 | + verified_slot: number; |
| 109 | + state: string; |
| 110 | +}; |
0 commit comments