import { PublicKey } from '@solana/web3.js' import BN from 'bn.js' /** * Seed for the endpoint. */ export const ENDPOINT_SEED = 'Endpoint' /** * Seed for the message library. */ export const MESSAGE_LIB_SEED = 'MessageLib' /** * Seed for the send library configuration. */ export const SEND_LIBRARY_CONFIG_SEED = 'SendLibraryConfig' /** * Seed for the receive library configuration. */ export const RECEIVE_LIBRARY_CONFIG_SEED = 'ReceiveLibraryConfig' /** * Seed for the nonce. */ export const NONCE_SEED = 'Nonce' /** * Seed for the pending nonce. */ export const PENDING_NONCE_SEED = 'PendingNonce' /** * Seed for the payload hash. */ export const PAYLOAD_HASH_SEED = 'PayloadHash' /** * Seed for the composed message hash. */ export const COMPOSED_MESSAGE_HASH_SEED = 'ComposedMessageHash' /** * Seed for the OApp. */ export const OAPP_SEED = 'OApp' /** * Seed for the count. */ export const COUNT_SEED = 'Count' /** * Seed for the remote. */ export const REMOTE_SEED = 'Remote' /** * Seed for the LzReceive types. */ export const LZ_RECEIVE_TYPES_SEED = 'LzReceiveTypes' /** * Seed for the LzCompose types. */ export const LZ_COMPOSE_TYPES_SEED = 'LzComposeTypes' /** * Seed for the ULN. */ export const ULN_SEED = MESSAGE_LIB_SEED /** * Seed for the ULN configuration. */ export const ULN_CONFIG_SEED = 'UlnConfig' /** * Seed for the send configuration. */ export const SEND_CONFIG_SEED = 'SendConfig' /** * Seed for the receive configuration. */ export const RECEIVE_CONFIG_SEED = 'ReceiveConfig' /** * Seed for the options. */ export const OPTIONS_SEED = 'Options' /** * Seed for the confirmations. */ export const CONFIRMATIONS_SEED = 'Confirmations' /** * Seed for the worker. */ export const WORKER_SEED = 'Worker' /** * Seed for the DVN configuration. */ export const DVN_CONFIG_SEED = 'DvnConfig' /** * Seed for the event authority. */ export const EVENT_SEED = '__event_authority' /** * Seed for the executor configuration. */ export const EXECUTOR_CONFIG_SEED = 'ExecutorConfig' /** * Seed for the price feed. */ export const PRICE_FEED_SEED = 'PriceFeed' /** * Seed for the peer. */ export const PEER_SEED = 'Peer' /** * Seed for the mint. */ export const MINT_SEED = 'Mint' /** * Seed for the enforced options. */ export const ENFORCED_OPTIONS_SEED = 'EnforcedOptions' /** * Class to derive addresses for the endpoint program. */ export class EndpointPDADeriver { program: PublicKey /** * Creates an instance of the EndpointPDADeriver class. * * @param {PublicKey} program - The program public key. */ constructor(program: PublicKey) { this.program = program } /** * Derives the setting address. * * @returns {[PublicKey, number]} The derived address and bump seed. */ setting(): [PublicKey, number] { return PublicKey.findProgramAddressSync([Buffer.from(ENDPOINT_SEED, 'utf8')], this.program) } /** * Derives the default send library configuration address. * * @param {number} dstEndpointId - The destination endpoint ID. * @returns {[PublicKey, number]} The derived address and bump seed. */ defaultSendLibraryConfig(dstEndpointId: number): [PublicKey, number] { return PublicKey.findProgramAddressSync( [ Buffer.from(SEND_LIBRARY_CONFIG_SEED, 'utf8'), // U32 to Uint8Array([0,0,0,0]) new BN(dstEndpointId).toArrayLike(Buffer, 'be', 4), ], this.program ) } /** * Derives the send library configuration address. * * @param {PublicKey} sender - The sender public key. * @param {number} dstEndpointId - The destination endpoint ID. * @returns {[PublicKey, number]} The derived address and bump seed. */ sendLibraryConfig(sender: PublicKey, dstEndpointId: number): [PublicKey, number] { return PublicKey.findProgramAddressSync( [ Buffer.from(SEND_LIBRARY_CONFIG_SEED, 'utf8'), sender.toBytes(), // U32 to Uint8Array([0,0,0,0]) new BN(dstEndpointId).toArrayLike(Buffer, 'be', 4), ], this.program ) } /** * Derives the message library information address. * * @param {PublicKey} messageLibrary - The message library public key, PDA(derive by message lib program) * @returns {[PublicKey, number]} The derived address and bump seed. */ messageLibraryInfo(messageLibrary: PublicKey): [PublicKey, number] { return PublicKey.findProgramAddressSync( [Buffer.from(MESSAGE_LIB_SEED, 'utf8'), messageLibrary.toBytes()], this.program ) } /** * Derives the default receive library configuration address. * * @param {number} srcEndpointId - The source endpoint ID. * @returns {[PublicKey, number]} The derived address and bump seed. */ defaultReceiveLibraryConfig(srcEndpointId: number): [PublicKey, number] { return PublicKey.findProgramAddressSync( [ Buffer.from(RECEIVE_LIBRARY_CONFIG_SEED, 'utf8'), // U32 to Uint8Array([0,0,0,0]) new BN(srcEndpointId).toArrayLike(Buffer, 'be', 4), ], this.program ) } /** * Derives the receive library configuration address. * * @param {PublicKey} receiver - The receiver public key. * @param {number} srcEndpointId - The source endpoint ID. * @returns {[PublicKey, number]} The derived address and bump seed. */ receiveLibraryConfig(receiver: PublicKey, srcEndpointId: number): [PublicKey, number] { return PublicKey.findProgramAddressSync( [ Buffer.from(RECEIVE_LIBRARY_CONFIG_SEED, 'utf8'), receiver.toBytes(), // U32 to Uint8Array([0,0,0,0]) new BN(srcEndpointId).toArrayLike(Buffer, 'be', 4), ], this.program ) } receiveLibraryTimeout(_dstEndpointId: number): [PublicKey, number] { // todo: @zzz find the address of receive library timeout config // if it is default config, then get the address of default timeout config // otherwise, get the address of the custom one return [this.program, 0] } defaultMessageLib(): [PublicKey, number] { return this.messageLibraryInfo(PublicKey.default) } blockMessageLib(blockMsgLib: PublicKey): [PublicKey, number] { return PublicKey.findProgramAddressSync( [Buffer.from(MESSAGE_LIB_SEED, 'utf8'), blockMsgLib.toBytes()], this.program ) } /** * Derives the nonce address. * * @param {PublicKey} localOapp - The local OApp public key. * @param {number} remoteChainId - The remote chain ID. * @param {Uint8Array} remoteOapp - The remote OApp address. * @returns {[PublicKey, number]} The derived address and bump seed. */ nonce(localOapp: PublicKey, remoteChainId: number, remoteOapp: Uint8Array): [PublicKey, number] { return PublicKey.findProgramAddressSync( [ Buffer.from(NONCE_SEED, 'utf8'), localOapp.toBytes(), // U32 to Uint8Array([0,0,0,0]) new BN(remoteChainId).toArrayLike(Buffer, 'be', 4), remoteOapp, ], this.program ) } /** * Derives the pending nonce address. * * @param {PublicKey} localOapp - The local OApp public key. * @param {number} remoteChainId - The remote chain ID. * @param {Uint8Array} remoteOapp - The remote OApp address. * @returns {[PublicKey, number]} The derived address and bump seed. */ pendingNonce(localOapp: PublicKey, remoteChainId: number, remoteOapp: Uint8Array): [PublicKey, number] { return PublicKey.findProgramAddressSync( [ Buffer.from(PENDING_NONCE_SEED, 'utf8'), localOapp.toBytes(), new BN(remoteChainId).toArrayLike(Buffer, 'be', 4), remoteOapp, ], this.program ) } /** * Derives the OApp registry address. * * @param {PublicKey} localOapp - The local OApp public key. * @returns {[PublicKey, number]} The derived address and bump seed. */ oappRegistry(localOapp: PublicKey): [PublicKey, number] { return PublicKey.findProgramAddressSync([Buffer.from(OAPP_SEED, 'utf8'), localOapp.toBytes()], this.program) } /** * Derives the payload hash address. * * @param {PublicKey} receiver - The receiver public key. * @param {number} srcEid - The source endpoint ID. * @param {Uint8Array} sender - The sender address. * @param {number} nonce - The nonce. * @returns {[PublicKey, number]} The derived address and bump seed. */ payloadHash(receiver: PublicKey, srcEid: number, sender: Uint8Array, nonce: number): [PublicKey, number] { return PublicKey.findProgramAddressSync( [ Buffer.from(PAYLOAD_HASH_SEED, 'utf8'), receiver.toBytes(), new BN(srcEid).toArrayLike(Buffer, 'be', 4), sender, new BN(nonce).toArrayLike(Buffer, 'be', 8), ], this.program ) } /** * Derives the composed message address. * * @param {PublicKey} from - The sender public key. * @param {Uint8Array} guid - The GUID. * @param {number} index - The index. * @param {PublicKey} to - The receiver public key. * @param {Uint8Array} messageHash - The message hash. * @returns {[PublicKey, number]} The derived address and bump seed. */ composedMessage( from: PublicKey, guid: Uint8Array, index: number, to: PublicKey, messageHash: Uint8Array ): [PublicKey, number] { return PublicKey.findProgramAddressSync( [ Buffer.from(COMPOSED_MESSAGE_HASH_SEED, 'utf8'), from.toBytes(), to.toBytes(), guid, new BN(index).toArrayLike(Buffer, 'be', 2), messageHash, ], this.program ) } } /** * Class to derive addresses by message library program. */ export class MessageLibPDADeriver { program: PublicKey /** * Creates an instance of the MessageLibPDADeriver class. * * @param {PublicKey} program - The program public key. */ constructor(program: PublicKey) { this.program = program } /** * Derives the message library address. * * @returns {[PublicKey, number]} The derived address and bump seed. */ messageLib(): [PublicKey, number] { return PublicKey.findProgramAddressSync([Buffer.from(MESSAGE_LIB_SEED, 'utf8')], this.program) } /** * Derives the send configuration address. * * @param {number} eid - The endpoint ID. * @param {PublicKey} oapp - The OApp public key. * @returns {[PublicKey, number]} The derived address and bump seed. */ sendConfig(eid: number, oapp: PublicKey): [PublicKey, number] { return PublicKey.findProgramAddressSync( [Buffer.from(SEND_CONFIG_SEED, 'utf8'), new BN(eid).toArrayLike(Buffer, 'be', 4), oapp.toBuffer()], this.program ) } /** * Derives the receive configuration address. * * @param {number} eid - The endpoint ID. * @param {PublicKey} oapp - The OApp public key. * @returns {[PublicKey, number]} The derived address and bump seed. */ receiveConfig(eid: number, oapp: PublicKey): [PublicKey, number] { return PublicKey.findProgramAddressSync( [Buffer.from(RECEIVE_CONFIG_SEED, 'utf8'), new BN(eid).toArrayLike(Buffer, 'be', 4), oapp.toBuffer()], this.program ) } } /** * Class to derive addresses for the ULN program. * @see {@link MessageLibPDADeriver} */ export class UlnPDADeriver extends MessageLibPDADeriver { /** * Derives the setting address. * * @returns {[PublicKey, number]} The derived address and bump seed. */ setting(): [PublicKey, number] { return PublicKey.findProgramAddressSync([Buffer.from(ULN_SEED, 'utf8')], this.program) } /** * Derives the configuration address. * * @param {number} eid - The endpoint ID. * @returns {[PublicKey, number]} The derived address and bump seed. */ config(eid: number): [PublicKey, number] { return PublicKey.findProgramAddressSync( [Buffer.from(ULN_CONFIG_SEED, 'utf8'), new BN(eid).toArrayLike(Buffer, 'be', 4)], this.program ) } /** * Derives the default send configuration address. * * @param {number} eid - The endpoint ID. * @returns {[PublicKey, number]} The derived address and bump seed. */ defaultSendConfig(eid: number): [PublicKey, number] { return PublicKey.findProgramAddressSync( [Buffer.from(SEND_CONFIG_SEED, 'utf8'), new BN(eid).toArrayLike(Buffer, 'be', 4)], this.program ) } /** * Derives the default receive configuration address. * * @param {number} eid - The endpoint ID. * @returns {[PublicKey, number]} The derived address and bump seed. */ defaultReceiveConfig(eid: number): [PublicKey, number] { return PublicKey.findProgramAddressSync( [Buffer.from(RECEIVE_CONFIG_SEED, 'utf8'), new BN(eid).toArrayLike(Buffer, 'be', 4)], this.program ) } /** * Derives the options address. * * @param {number} eit - The endpoint ID. * @returns {[PublicKey, number]} The derived address and bump seed. */ options(eit: number): [PublicKey, number] { return PublicKey.findProgramAddressSync( [Buffer.from(OPTIONS_SEED, 'utf8'), new BN(eit).toArrayLike(Buffer, 'be', 4)], this.program ) } /** * Derives the worker configuration address. * * @param {PublicKey} worker - The worker public key. * @returns {[PublicKey, number]} The derived address and bump seed. */ workerConfig(worker: PublicKey): [PublicKey, number] { return PublicKey.findProgramAddressSync([Buffer.from(WORKER_SEED, 'utf8'), worker.toBuffer()], this.program) } /** * Derives the confirmations address. * * @param {Uint8Array} headerHash - The header hash. * @param {Uint8Array} payloadHash - The payload hash. * @param {PublicKey} dvn - The DVN public key. * @returns {[PublicKey, number]} The derived address and bump seed. */ confirmations(headerHash: Uint8Array, payloadHash: Uint8Array, dvn: PublicKey): [PublicKey, number] { return PublicKey.findProgramAddressSync( [Buffer.from(CONFIRMATIONS_SEED, 'utf8'), headerHash, payloadHash, dvn.toBytes()], this.program ) } } /** * Class to derive addresses by base OApp program. */ export class OAppBasePDADeriver { program: PublicKey /** * Creates an instance of the OAppBasePDADeriver class. * * @param {PublicKey} program - The program public key. */ constructor(program: PublicKey) { this.program = program } /** * Derives the remote address. * * @param {number} dstChainId - The destination chain ID. * @returns {[PublicKey, number]} The derived address and bump seed. */ remote(dstChainId: number): [PublicKey, number] { return PublicKey.findProgramAddressSync( [Buffer.from(REMOTE_SEED), new BN(dstChainId).toArrayLike(Buffer, 'be', 4)], this.program ) } /** * Derives the LzReceive types accounts address. * * @returns {[PublicKey, number]} The derived address and bump seed. */ lzReceiveTypesAccounts(): [PublicKey, number] { return PublicKey.findProgramAddressSync([Buffer.from(LZ_RECEIVE_TYPES_SEED, 'utf8')], this.program) } /** * Derives the LzCompose types accounts address. * * @returns {[PublicKey, number]} The derived address and bump seed. */ lzComposeTypesAccounts(): [PublicKey, number] { return PublicKey.findProgramAddressSync([Buffer.from(LZ_COMPOSE_TYPES_SEED, 'utf8')], this.program) } } /** * Class to derive addresses for the DVN program. */ export class DVNDeriver { constructor(public program: PublicKey) {} /** * Derives the authority address. * * @returns {[PublicKey, number]} The derived address and bump seed. */ authority(): [PublicKey, number] { return PublicKey.findProgramAddressSync([Buffer.from('dvn', 'utf8')], this.program) } /** * Derives the configuration address. * * @returns {[PublicKey, number]} The derived address and bump seed. */ config(): [PublicKey, number] { return PublicKey.findProgramAddressSync([Buffer.from(DVN_CONFIG_SEED, 'utf8')], this.program) } /** * Derives the execute hash address. * * @param {Buffer} digestHash - The digest hash. * @returns {[PublicKey, number]} The derived address and bump seed. */ executeHash(digestHash: Buffer): [PublicKey, number] { return PublicKey.findProgramAddressSync([Buffer.from('ExecuteHash', 'utf8'), digestHash], this.program) } } /** * Class for deriving PDA (Program Derived Address) for events. */ export class EventPDADeriver { /** * Creates an instance of the EventPDADeriver class. * * @param {PublicKey} program - The program public key. */ constructor(public program: PublicKey) {} /** * Derives the event authority PDA. * * @returns {[PublicKey, number]} The event authority PDA and bump seed. */ eventAuthority(): [PublicKey, number] { return PublicKey.findProgramAddressSync([Buffer.from(EVENT_SEED, 'utf8')], this.program) } } /** * Class for deriving PDA (Program Derived Address) for executors. */ export class ExecutorPDADeriver { /** * Creates an instance of the ExecutorPDADeriver class. * * @param {PublicKey} program - The program public key. */ constructor(public program: PublicKey) {} /** * Derives the executor configuration PDA. * * @returns {[PublicKey, number]} The executor configuration PDA and bump seed. */ config(): [PublicKey, number] { return PublicKey.findProgramAddressSync([Buffer.from(EXECUTOR_CONFIG_SEED, 'utf8')], this.program) } } /** * Class for deriving PDA (Program Derived Address) for price feeds. */ export class PriceFeedPDADeriver { /** * Creates an instance of the PriceFeedPDADeriver class. * * @param {PublicKey} program - The program public key. */ constructor(public program: PublicKey) {} /** * Derives the price feed PDA. * * @returns {[PublicKey, number]} The price feed PDA and bump seed. */ priceFeed(): [PublicKey, number] { return PublicKey.findProgramAddressSync([Buffer.from(PRICE_FEED_SEED, 'utf8')], this.program) } }