import { ETHEREUM_KEY_LENGTH } from '../instructions/secp256k1';
import { utils } from '@wormhole-foundation/connect-sdk-solana';
export function deriveGuardianSetKey(wormholeProgramId, index) {
    return utils.deriveAddress([
        Buffer.from('GuardianSet'),
        (() => {
            const buf = Buffer.alloc(4);
            buf.writeUInt32BE(index);
            return buf;
        })(),
    ], wormholeProgramId);
}
export async function getGuardianSet(connection, wormholeProgramId, index, commitment) {
    return connection
        .getAccountInfo(deriveGuardianSetKey(wormholeProgramId, index), commitment)
        .then((info) => GuardianSetData.deserialize(utils.getAccountData(info)));
}
export class GuardianSetData {
    index;
    keys;
    creationTime;
    expirationTime;
    constructor(index, keys, creationTime, expirationTime) {
        this.index = index;
        this.keys = keys;
        this.creationTime = creationTime;
        this.expirationTime = expirationTime;
    }
    static deserialize(data) {
        const index = data.readUInt32LE(0);
        const keysLen = data.readUInt32LE(4);
        const keysEnd = 8 + keysLen * ETHEREUM_KEY_LENGTH;
        const creationTime = data.readUInt32LE(keysEnd);
        const expirationTime = data.readUInt32LE(4 + keysEnd);
        const keys = [];
        for (let i = 0; i < keysLen; ++i) {
            const start = 8 + i * ETHEREUM_KEY_LENGTH;
            keys.push(data.subarray(start, start + ETHEREUM_KEY_LENGTH));
        }
        return new GuardianSetData(index, keys, creationTime, expirationTime);
    }
}
//# sourceMappingURL=guardianSet.js.map