@wuwei-labs/srsly
Version:
TypeScript SDK for SRSLY
58 lines • 2.13 kB
JavaScript
;
/**
* Address Lookup Table (ALT) derivation utilities
* @module pda/lookupTable
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.deriveLookupTable = deriveLookupTable;
const kit_1 = require("@solana/kit");
const signer_1 = require("../utils/signer");
/**
* Address Lookup Table program address
*/
const ALT_PROGRAM = 'AddressLookupTab1e1111111111111111111111111';
const addressEncoder = (0, kit_1.getAddressEncoder)();
/**
* Convert a BigInt or number slot to little-endian bytes (u64)
*/
function slotToBytes(slot) {
const buffer = new ArrayBuffer(8);
const view = new DataView(buffer);
view.setBigUint64(0, BigInt(slot), true); // little-endian
return new Uint8Array(buffer);
}
/**
* Derive an Address Lookup Table (ALT) address
*
* ALT addresses are deterministically derived from an authority and a recent slot.
* The slot is typically obtained from getSlot() RPC call.
*
* @param authority - The authority that will own the lookup table
* @param recentSlot - A recent slot number (from getSlot RPC)
* @param altProgramAddress - Optional ALT program address (defaults to mainnet)
* @returns The derived lookup table address
*
* @example
* ```typescript
* import { deriveLookupTable } from './lookupTable';
*
* // Get recent slot from RPC
* const slot = await rpc.getSlot().send();
*
* // Derive the lookup table address
* const lookupTable = await deriveLookupTable(authorityAddress, slot);
* ```
*/
async function deriveLookupTable(authority, recentSlot, altProgramAddress) {
const authorityAddress = (0, signer_1.getSignerAddress)(authority);
const altProgram = altProgramAddress ? (0, signer_1.getSignerAddress)(altProgramAddress) : (0, kit_1.address)(ALT_PROGRAM);
// Encode authority and slot to bytes
const authorityBytes = addressEncoder.encode(authorityAddress);
const slotBytes = slotToBytes(recentSlot);
const [pda] = await (0, kit_1.getProgramDerivedAddress)({
programAddress: altProgram,
seeds: [authorityBytes, slotBytes],
});
return pda;
}
//# sourceMappingURL=lookupTable.js.map