import type { MessageRevision } from '../../../../shared-kernel';

export async function attachRevisionSignatureIfPossible(
  revision: MessageRevision,
  signRevision: (value: MessageRevision) => Promise<string>
): Promise<MessageRevision> {
  try {
    const signature = await signRevision(revision);

    return {
      ...revision,
      signature
    };
  } catch {
    return revision;
  }
}

export function shouldAcceptRevisionWithoutRegisteredKey(
  revision: MessageRevision,
  publicKeyJwk: JsonWebKey | null
): boolean {
  return !!revision.signature && !publicKeyJwk;
}
