mte/src/types.ml

196 lines
5.8 KiB
OCaml
Raw Normal View History

2025-10-01 17:26:50 +02:00
(* https://docs.taler.net/core/api-common.html#tsref-type-ErrorDetail *)
2025-10-07 15:08:35 +02:00
module ErrorDetail = struct
2025-10-01 17:26:50 +02:00
(* TODO GANA error codes
2025-10-01 19:34:42 +02:00
https://git.gnunet.org/gana.git/tree/gnu-taler-error-codes/registry.rec *)
2025-10-03 18:35:21 +02:00
type t = {
code: int;
hint: string option;
2025-10-03 18:35:21 +02:00
}
2025-10-01 17:26:50 +02:00
end
2025-10-01 20:22:32 +02:00
2025-10-03 18:35:21 +02:00
(* TODO number
- number is "float", but we probably want int everywhere instead
- numeric values capped at 2^53 -1 inclusive because json
- have a type for seconds/microseconds/..? *)
2025-10-01 20:22:32 +02:00
module Timestamp = struct
(* Seconds since epoch, or the special
value "never" to represent an event that will
never happen. *)
2025-10-03 18:35:21 +02:00
type t =
| Seconds of float
| Never
end
2025-10-07 15:08:35 +02:00
module RelativeTime = struct
2025-10-03 18:35:21 +02:00
(* Duration in microseconds or "forever"
to represent an infinite duration. Numeric
values are capped at 2^53 - 1 inclusive. *)
type t =
| Microseconds of float
| Forever
2025-10-01 20:22:32 +02:00
end
module Amount = struct
2025-10-03 19:54:18 +02:00
(* TODO
have safe amount arithmetic
make type private *)
(* Amounts of currency, serialized as `<Currency>:<IntegerPart>.<FractionalPart>`
Fixed-precision numbers with 8 decimal places.
- <Currency> must be at most 11 characters long
only consist of ASCII letters (a-zA-Z).
- integer part of <DecimalAmount> may be at most 2^52.
- fractional part of <DecimalAmount> may contain at most 8 decimal digits.
2025-10-01 20:22:32 +02:00
2025-10-03 19:54:18 +02:00
Prefixed with '+' or '-' in certain contexts.
When no sign is present, the amount is assumed to be positive. *)
2025-10-01 20:22:32 +02:00
type t = {
sign: [ `Plus | `Minus ] option;
currency: [ `Eur ];
value: Int64.t;
fraction: Int64.t;
2025-10-01 20:22:32 +02:00
}
2025-10-03 19:54:18 +02:00
let make ~sign ~currency ~value ~fraction =
let open Syntax in
let value_upper_bound = Z.(pow (of_int 2) 52) |> Z.to_int64 in
let fraction_upper_bound = Int64.of_int 99_999_999 in
let* () = if value < Int64.zero then Error "value is negative" else Ok () in
let* () =
if fraction < Int64.zero then Error "fraction is negative" else Ok ()
in
let* () =
if value > value_upper_bound then Error "value is greater than 2^52"
else Ok ()
in
let* () =
if fraction > fraction_upper_bound then
Error "fraction have more than 8 decimal digits"
else Ok ()
in
Ok { sign; currency; value; fraction }
let to_string =
let pp =
let open Fmt in
let pp_sign ppf = function
| `Plus -> char ppf '+'
| `Minus -> char ppf '-'
in
let pp_currency ppf = function `Eur -> string ppf "EUR" in
fun ppf { sign; currency; value; fraction } ->
pf ppf "%a%a:%Ld.%Ld" (Fmt.option pp_sign) sign pp_currency currency
value fraction
in
Fmt.str "%a" pp
let of_string =
let open Angstrom in
let parse_sign =
choice
[
char '+' *> return (Some `Plus); char '-' *> return (Some `Minus);
return None;
2025-10-03 19:54:18 +02:00
]
in
let parse_currency = string "EUR" *> return `Eur in
let parse_int =
take_while1 (function '0' .. '9' -> true | _ -> false)
2025-10-03 22:42:16 +02:00
>>| Int64.of_string_opt
>>= function
| None -> fail "invalid integer"
| Some n -> return n
2025-10-03 19:54:18 +02:00
in
let parse_t =
lift4
(fun sign currency value fraction ->
2025-10-03 22:42:16 +02:00
make ~sign ~currency ~value ~fraction)
2025-10-03 19:54:18 +02:00
parse_sign parse_currency
(char ':' *> parse_int)
(char '.' *> parse_int)
in
2025-10-03 22:42:16 +02:00
fun s -> parse_string ~consume:Consume.All parse_t s |> Result.join
2025-10-01 20:22:32 +02:00
end
module Eddsa = struct
2025-10-03 21:24:02 +02:00
(* TODO test *)
2025-10-01 20:22:32 +02:00
(* EdDSA and ECDHE public keys always point on Curve25519
and represented using the standard 256 bits Ed25519 compact format,
converted to Crockford Base32. *)
2025-10-08 21:17:03 +02:00
(* EdDSA signatures are transmitted as 64-bytes `base32`
binary-encoded objects with just the R and S values (base32_ binary-only). *)
2025-10-03 21:24:02 +02:00
type pub = Mirage_crypto_ec.Ed25519.pub
2025-10-01 20:22:32 +02:00
2025-10-03 21:24:02 +02:00
let pub_of_string s =
let open Mirage_crypto_ec in
match Base_32.decode s with
| Error _ as err -> err
| Ok octets -> (
match Ed25519.pub_of_octets octets with
| Error e -> Fmt.error "%a" pp_error e
| Ok pub -> Ok pub)
let pub_to_string pub =
pub |> Mirage_crypto_ec.Ed25519.pub_to_octets |> Base_32.encode
2025-10-01 20:22:32 +02:00
(* EdDSA signatures are transmitted as 64-bytes base32
2025-10-06 22:20:09 +02:00
binary-encoded objects with just the R and S values (base32_ binary-only).
They are signature over a c-struct like `TALER_xxxPS` + with a purpose *)
type signature = string
2025-10-01 20:22:32 +02:00
end
module Rsa = struct
2025-10-06 19:51:56 +02:00
(* TODO
GNUNET_CRYPTO custom encode/decode *)
2025-10-01 20:22:32 +02:00
(* RSA public key converted to Crockford Base32. *)
2025-10-03 21:24:02 +02:00
type pub = Mirage_crypto_pk.Rsa.pub
let pub_of_string _s = assert false
let pub_to_string _pub = assert false
2025-10-01 20:22:32 +02:00
end
2025-10-07 15:08:35 +02:00
module HashCode = struct
2025-10-01 20:22:32 +02:00
(* 32-byte value representing a point on Curve25519. *)
type cs25519Point = string
end
2025-10-06 19:51:56 +02:00
2025-10-07 15:08:35 +02:00
module RsaDenominationKey = struct
2025-10-06 19:51:56 +02:00
type t = {
age_mask: int;
rsa_pub: string; (* Rsa.pub *)
2025-10-06 19:51:56 +02:00
}
end
2025-10-07 15:08:35 +02:00
module CSDenominationKey = struct
2025-10-06 19:51:56 +02:00
(* Clause Schnorr *)
type t = {
age_mask: int;
cs_pub: string;
2025-10-06 19:51:56 +02:00
}
end
2025-10-07 15:08:35 +02:00
module DenominationKey = struct
2025-10-06 19:51:56 +02:00
type t =
2025-10-07 15:08:35 +02:00
| Rsa of RsaDenominationKey.t
| CS of CSDenominationKey.t
2025-10-06 19:51:56 +02:00
end
2025-10-07 15:08:35 +02:00
module FutureSignKey = struct
type t = {
(* The actual exchange's EdDSA signing public key *)
key: Eddsa.pub;
(* Initial validity date for the signing key. *)
stamp_start: Timestamp.t;
(* Date when the exchange will stop using the signing key, allowed to overlap
slightly with the next signing key's validity to allow for clock skew. *)
stamp_expire: Timestamp.t;
(* Date when all signatures made by the signing key expire and should
henceforth no longer be considered valid in legal disputes. *)
stamp_end: Timestamp.t;
(* Signature over TALER_SigningKeyAnnouncementPS
for this signing key by the signkey security
module using purpose TALER_SIGNATURE_SM_SIGNING_KEY. *)
signkey_secmod_sig: Eddsa.signature;
}
end