import { ParsedAccountData , AccountInfo , PublicKey } from '@solana/web3.js' ; import { type , number , literal , nullable , Infer , create } from 'superstruct' ; import { PublicKeyFromString } from '../pubkey' ; export type ProgramAccountInfo = Infer < typeof ProgramAccountInfo > ; export const ProgramAccountInfo = type ( { programData : PublicKeyFromString , } ) ; export type ProgramAccount = Infer < typeof ProgramDataAccount > ; export const ProgramAccount = type ( { type : literal ( 'program' ) , info : ProgramAccountInfo , } ) ; export type ProgramDataAccountInfo = Infer < typeof ProgramDataAccountInfo > ; export const ProgramDataAccountInfo = type ( { authority : nullable ( PublicKeyFromString ) , slot : number ( ) , } ) ; export type ProgramDataAccount = Infer < typeof ProgramDataAccount > ; export const ProgramDataAccount = type ( { type : literal ( 'programData' ) , info : ProgramDataAccountInfo , } ) ; export type ProgramBufferAccountInfo = Infer < typeof ProgramBufferAccountInfo > ; export const ProgramBufferAccountInfo = type ( { authority : nullable ( PublicKeyFromString ) , } ) ; export type ProgramBufferAccount = Infer < typeof ProgramBufferAccount > ; export const ProgramBufferAccount = type ( { type : literal ( 'buffer' ) , info : ProgramBufferAccountInfo , } ) ; export function validateProgramBufferAccount ( info , bufferAuthority ) { if ( ! ( 'parsed' in info . data && info . data . program === 'bpf-upgradeable-loader' ) ) { throw new Error ( 'Invalid program buffer account' ) ; } let buffer ; try { buffer = create ( info . data . parsed , ProgramBufferAccount ) ; } catch { throw new Error ( 'Invalid program buffer account' ) ; } if ( buffer . info . authority ?. toBase58 ( ) !== bufferAuthority . toBase58 ( ) ) { throw new Error ( `Buffer authority must be set to governance account 
        ${ bufferAuthority . toBase58 ( ) }` ) ; } }