better module for BytesXX

This commit is contained in:
swrup 2026-02-26 16:20:20 +01:00 committed by Swrup
parent 7c989e590a
commit f9ebdb0a96
5 changed files with 61 additions and 13 deletions

View file

@ -62,6 +62,34 @@ module B32 = struct
~decode:B32.decode Caqti_type.string
end
module Bytes32 = struct
include Signatures.Bytes32
let jsont =
let decode s = Result.bind (B32.decode s) of_octets in
let encode b = to_octets b |> B32.encode in
Jsont.of_of_string ~kind:"Bytes32" decode ~enc:encode
let caqti =
Caqti_type.custom
~encode:(fun v -> Ok (to_octets v))
~decode:of_octets Caqti_type.octets
end
module Bytes64 = struct
include Signatures.Bytes64
let jsont =
let decode s = Result.bind (B32.decode s) of_octets in
let encode b = to_octets b |> B32.encode in
Jsont.of_of_string ~kind:"Bytes64" decode ~enc:encode
let caqti =
Caqti_type.custom
~encode:(fun v -> Ok (to_octets v))
~decode:of_octets Caqti_type.octets
end
(* TODO error response
- use GANA error codes
https://git.gnunet.org/gana.git/tree/gnu-taler-error-codes/registry.rec *)
@ -676,7 +704,7 @@ module DrainProfitsMessage = struct
map ~kind:"DrainProfitsMessage" make
|> mem "debit_account_section" Jsont.string ~enc:debit_account_section
|> mem "credit_payto_uri" Jsont.string ~enc:credit_payto_uri
|> mem "wtid" B32.jsont ~enc:wtid
|> mem "wtid" Bytes32.jsont ~enc:wtid
|> mem "master_sig" MasterDrainProfit.jsont ~enc:master_sig
|> mem "date" Timestamp.jsont ~enc:date
|> mem "amount" Amount.jsont ~enc:amount

View file

@ -290,7 +290,7 @@ let get_wire_accounts =
let find_drain_profit =
let req =
Caqti_type.(octets ->? drain_profit_message)
Caqti_type.(Bytes32.caqti ->? drain_profit_message)
"SELECT wtid, account_section, payto_uri, trigger_date, (amount).*, \
master_sig FROM profit_drains WHERE wtid=$1"
in

View file

@ -265,7 +265,7 @@ let drain_profit_message =
amount;
master_sig;
})
Caqti_type.(t6 octets string string time amount master_sig)
Caqti_type.(t6 Bytes32.caqti string string time amount master_sig)
let aml_officer_setup =
let master_sig = Signatures.MasterAmlOfficerStatus.caqti in

View file

@ -33,16 +33,40 @@ open Aliases
let size_of_int32 = 4
module Bytes32 = struct
type t = string
module type BYTES = sig
type t
let bin = Bin.bytes 32
val of_octets : string -> (t, string) result
val to_octets : t -> string
val bin : t Bin.t
end
module Bytes64 = struct
module Bytes32 : BYTES = struct
let n = 32
type t = string
let bin = Bin.bytes 64
let of_octets s =
match String.length s = n with
| false -> Error "invalid bytes length"
| true -> Ok s
let to_octets = Fun.id
let bin = Bin.bytes n
end
module Bytes64 : BYTES = struct
let n = 64
type t = string
let of_octets s =
match String.length s = n with
| false -> Error "invalid bytes length"
| true -> Ok s
let to_octets = Fun.id
let bin = Bin.bytes n
end
module TransferSecretP = Bytes64