import { cryptly } from "cryptly" import type { Operation } from "./index" export class Signer { private readonly signer: cryptly.Signer private constructor(private readonly keys: { public?: string; private?: string }) { this.signer = cryptly.Signer.create("RSA", "SHA-256", this.keys.public, this.keys.private) } async sign(operation: Operation & { previous: string }): Promise { return this.signer.sign(JSON.stringify(operation)) } async verify(operation: Operation): Promise { return this.signer.verify(JSON.stringify({ ...operation, signature: undefined }), operation.signature ?? "") } static open(keys: { public?: string; private?: string }): Signer { return new Signer(keys) } } export namespace Signer {}