open Types

type t = {
  pub: RsaPublicKey.t;
  section_name: string;
  value: Amount.t;
  stamp_start: Ptime.t;
  stamp_expire_withdraw: Ptime.t;
  stamp_expire_deposit: Ptime.t;
  stamp_expire_legal: Ptime.t;
  fee_withdraw: Amount.t;
  fee_deposit: Amount.t;
  fee_refresh: Amount.t;
  fee_refund: Amount.t;
  h_pub: Binary_formats.DenominationHash.t;
  sign: string -> RsaSignature.t;
  master_sig: EddsaSignature.t option;
}

let hash_pub _pub =
  (* todo public key converted to Crockford Base32 *)
  assert false

let make
    ({
       section_name;
       value;
       duration_withdraw;
       duration_spend;
       duration_legal;
       fee_withdraw;
       fee_deposit;
       fee_refresh;
       fee_refund;
       cipher;
       rsa_keysize;
       age_restricted= _;
     } :
      Config_types.Coin.t) =
  assert (cipher = `RSA);

  let stamp_start = Ptime_clock.now () in
  let stamp_expire_withdraw =
    Util.ptime_add_span_exn stamp_start duration_withdraw
  in
  let stamp_expire_deposit =
    Util.ptime_add_span_exn stamp_start duration_spend
  in
  let stamp_expire_legal = Util.ptime_add_span_exn stamp_start duration_legal in

  let open Mirage_crypto_pk.Rsa in
  let priv = generate ~bits:rsa_keysize () in
  let pub = pub_of_priv priv in
  let h_pub = Binary_formats.DenominationHash.hash pub in
  let sign = RsaSignature.sign ~key:priv in
  let master_sig = None in
  {
    pub;
    section_name;
    value;
    stamp_start;
    stamp_expire_withdraw;
    stamp_expire_deposit;
    stamp_expire_legal;
    fee_withdraw;
    fee_deposit;
    fee_refresh;
    fee_refund;
    h_pub;
    sign;
    master_sig;
  }
