import { KeetaNetAnchorHTTPServer } from './http-server/index.js';
import { SignData, VerifySignedData, objectToSignable } from './utils/signing.js';
/**
 * Namespace tag bound into every service metadata signature.
 */
export const METADATA_SIGNATURE_NAMESPACE = 'keetanet/anchor/service-metadata/v1';
/**
 * Extracts the subset of `metadata` that is covered by the signature.
 * `account` is the public-key string of the claimed signer.
 */
export function extractSignedFields(account, metadata) {
    const fields = {
        namespace: METADATA_SIGNATURE_NAMESPACE,
        account: account,
        operations: metadata.operations
    };
    if (metadata.legal !== undefined) {
        fields.legal = metadata.legal;
    }
    return (fields);
}
/**
 * Verifies that `signed` was produced by `account` over the signed
 * fields of `metadata` (see {@link extractSignedFields}).
 */
export async function verifyMetadataSignature(account, metadata, signed) {
    const signedFields = extractSignedFields(account.publicKeyString.get(), metadata);
    const signable = objectToSignable(signedFields);
    const verifyOptions = { maxSkewMs: Number.POSITIVE_INFINITY };
    return (await VerifySignedData(account, signable, signed, verifyOptions));
}
/**
 * Publishes anchor service metadata, optionally signed by `metadataSigner`.
 * Subclasses implement {@link buildServiceMetadata}.
 */
export class KeetaAnchorMetadataServer extends KeetaNetAnchorHTTPServer {
    #metadataSigner;
    constructor(config) {
        super(config);
        this.#metadataSigner = config.metadataSigner;
    }
    get metadataSigner() {
        return (this.#metadataSigner);
    }
    async serviceMetadata() {
        const built = await this.buildServiceMetadata();
        const signer = this.#metadataSigner;
        if (signer === undefined) {
            return (built);
        }
        const account = signer.publicKeyString.get();
        const signedFields = extractSignedFields(account, built);
        const signable = objectToSignable(signedFields);
        const signed = await SignData(signer, signable);
        return ({
            ...built,
            account,
            signed
        });
    }
}
//# sourceMappingURL=anchor-metadata-server.js.map