From 585838c16d145f4b2ab013e141b6c2482539ae70 Mon Sep 17 00:00:00 2001 From: swrup Date: Fri, 28 Nov 2025 17:18:17 +0100 Subject: [PATCH] --- src/bin_type.ml | 8 ++++++-- src/denomination.ml | 9 ++++----- src/management.ml | 10 ++-------- src/signkey.ml | 9 +++------ src/timestamp.ml | 23 +++++++++++++++++++++++ src/timestamp.mli | 6 ++++++ src/util.ml | 14 -------------- 7 files changed, 44 insertions(+), 35 deletions(-) diff --git a/src/bin_type.ml b/src/bin_type.ml index 8d9f758d..652a0eab 100644 --- a/src/bin_type.ml +++ b/src/bin_type.ml @@ -81,8 +81,6 @@ module TimeAbsolute : Time_S = TIME module TimeAbsoluteNBO : Time_S = TIME_NBO module TimeRelative : Time_S = TIME module TimeRelativeNBO : Time_S = TIME_NBO -module Timestamp : Time_S = TIME -module TimestampNBO : Time_S = TIME_NBO (* -- Cryptographic primitives -- *) @@ -230,6 +228,12 @@ end (* TODO keep this? some of those are actuall ecdhe, or union of eddsa|ecdhe *) module Aliases = struct + module TimestampNBO : Time_S = struct + type t = Timestamp.t + + let bin = Timestamp.bin_nbo + end + module AmountNBO = struct type t = Amount.t diff --git a/src/denomination.ml b/src/denomination.ml index 7c0d8832..68aca301 100644 --- a/src/denomination.ml +++ b/src/denomination.ml @@ -36,17 +36,16 @@ let make Config.Coin.t) = assert (cipher = `RSA); - let stamp_start = Ptime_clock.now () in + let stamp_start = Ptime_clock.now () |> Option.some in let stamp_expire_withdraw = - Util.ptime_add_span_exn stamp_start duration_withdraw |> Option.some + Timestamp.add_span_exn stamp_start (Some duration_withdraw) in let stamp_expire_deposit = - Util.ptime_add_span_exn stamp_start duration_spend |> Option.some + Timestamp.add_span_exn stamp_start (Some duration_spend) in let stamp_expire_legal = - Util.ptime_add_span_exn stamp_start duration_legal |> Option.some + Timestamp.add_span_exn stamp_start (Some duration_legal) in - let stamp_start = Some stamp_start in let open Mirage_crypto_pk.Rsa in let priv = generate ~bits:rsa_keysize () in diff --git a/src/management.ml b/src/management.ml index 9255ae44..b89fed1c 100644 --- a/src/management.ml +++ b/src/management.ml @@ -30,9 +30,7 @@ let mk_future_denom denom_key_signf let h_section_name = Hash_64_cstr.hash section_name in let anchor_time = stamp_start in let duration_withdraw = - None - (* TODO time - Ptime.diff stamp_start stamp_expire_withdraw |> Util.ptime_of_span_exn*) + Timestamp.diff stamp_start stamp_expire_withdraw |> Timestamp.of_span_exn in let ps = { h_denom_pub; h_section_name; anchor_time; duration_withdraw } in ps |> Bin.to_string bin |> denom_key_signf @@ -61,11 +59,7 @@ let mk_future_signkey signkey_signf let exchange_pub = pub in let anchor_time = stamp_start in let duration = - None - (* TODO time - Ptime.diff stamp_start stamp_expire - |> Util.ptime_of_span_exn - *) + Timestamp.diff stamp_start stamp_expire |> Timestamp.of_span_exn in let ps = { exchange_pub; anchor_time; duration } in ps |> Bin.to_string bin |> signkey_signf diff --git a/src/signkey.ml b/src/signkey.ml index 696c73ea..2b29662b 100644 --- a/src/signkey.ml +++ b/src/signkey.ml @@ -17,15 +17,12 @@ let generate () = - look if it exists - if not, create it (TOFU initialization scheme) - write it *) - (* TODO time - correctly set those: *) - let stamp_start = Ptime_clock.now () in + let stamp_start = Ptime_clock.now () |> Option.some in let stamp_expire = - Util.ptime_add_span_exn stamp_start Config.Exchange.signkey_legal_duration - |> Option.some + Timestamp.add_span_exn stamp_start + (Some Config.Exchange.signkey_legal_duration) in let stamp_end = stamp_expire in - let stamp_start = Some stamp_start in let priv, pub = Mirage_crypto_ec.Ed25519.generate () in let master_sig = None in diff --git a/src/timestamp.ml b/src/timestamp.ml index 6f964586..710ac992 100644 --- a/src/timestamp.ml +++ b/src/timestamp.ml @@ -1,4 +1,27 @@ type t = Ptime.t option +type span = Ptime.Span.t option + +let diff a b = + match (a, b) with + | None, _ | _, None -> None + | Some a, Some b -> Some (Ptime.diff a b) + +let add_span_exn t span = + match (t, span) with + | None, _ | _, None -> None + | Some t, Some span -> ( + match Ptime.add_span t span with + | None -> Fmt.failwith "add_span_exn: not in the range [min;max]" + | Some v -> Some v) + +let of_span_exn = function + | None -> None + | Some span -> ( + match Ptime.of_span span with + | None -> Fmt.failwith "of_span_exn: not in the range [min;max]" + | Some p -> Some p) + +(* -- *) (* microseconds since the UNIX Epoch, or "never" if None *) let jsont = diff --git a/src/timestamp.mli b/src/timestamp.mli index c3bc216a..c1ac00ab 100644 --- a/src/timestamp.mli +++ b/src/timestamp.mli @@ -2,7 +2,13 @@ not sure about this *) type t = Ptime.t option +type span = Ptime.Span.t option +val diff : t -> t -> span +val add_span_exn : t -> span -> t +val of_span_exn : span -> t + +(* - *) val jsont : t Jsont.t val bin : t Bin.t val bin_nbo : t Bin.t diff --git a/src/util.ml b/src/util.ml index 3dc0d55d..59ab2c3c 100644 --- a/src/util.ml +++ b/src/util.ml @@ -1,17 +1,3 @@ -(* -- Ptime -- *) - -let ptime_of_span_exn span = - match Ptime.of_span span with - | None -> Fmt.failwith "Ptime.of_span: not in the range [min;max]" - | Some p -> p - -let ptime_add_span_exn p span = - match Ptime.add_span p span with - | None -> Fmt.failwith "Ptime.add_span: not in the range [min;max]" - | Some v -> v - -(* -- *** -- *) - (* TODO bin - no [Bin.of_string] ? *) let bin_of_string bin s =