better module for BytesXX

This commit is contained in:
swrup 2026-02-26 16:20:20 +01:00
parent 6bd65561f8
commit 9372506c45
5 changed files with 61 additions and 13 deletions

View file

@ -62,6 +62,34 @@ module B32 = struct
~decode:B32.decode Caqti_type.string ~decode:B32.decode Caqti_type.string
end 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 (* TODO error response
- use GANA error codes - use GANA error codes
https://git.gnunet.org/gana.git/tree/gnu-taler-error-codes/registry.rec *) https://git.gnunet.org/gana.git/tree/gnu-taler-error-codes/registry.rec *)
@ -676,7 +704,7 @@ module DrainProfitsMessage = struct
map ~kind:"DrainProfitsMessage" make map ~kind:"DrainProfitsMessage" make
|> mem "debit_account_section" Jsont.string ~enc:debit_account_section |> mem "debit_account_section" Jsont.string ~enc:debit_account_section
|> mem "credit_payto_uri" Jsont.string ~enc:credit_payto_uri |> 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 "master_sig" MasterDrainProfit.jsont ~enc:master_sig
|> mem "date" Timestamp.jsont ~enc:date |> mem "date" Timestamp.jsont ~enc:date
|> mem "amount" Amount.jsont ~enc:amount |> mem "amount" Amount.jsont ~enc:amount

View file

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

View file

@ -265,7 +265,7 @@ let drain_profit_message =
amount; amount;
master_sig; 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 aml_officer_setup =
let master_sig = Signatures.MasterAmlOfficerStatus.caqti in let master_sig = Signatures.MasterAmlOfficerStatus.caqti in

View file

@ -33,16 +33,40 @@ open Aliases
let size_of_int32 = 4 let size_of_int32 = 4
module Bytes32 = struct module type BYTES = sig
type t = string 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 end
module Bytes64 = struct module Bytes32 : BYTES = struct
let n = 32
type t = string 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 end
module TransferSecretP = Bytes64 module TransferSecretP = Bytes64

View file

@ -411,11 +411,7 @@ let disable_wire ~output ~master_key ~payto_uri =
let drain ~output ~master_key ~debit_account_section ~credit_payto_uri ~wtid let drain ~output ~master_key ~debit_account_section ~credit_payto_uri ~wtid
~date ~amount = ~date ~amount =
let* key = read_master_key_file master_key in let* key = read_master_key_file master_key in
let* () = let* wtid = Api.Bytes32.of_octets wtid in
match String.length wtid = 32 with
| false -> Error "invalid wtid: must be 32 bytes"
| true -> Ok ()
in
let master_sig = let master_sig =
let open Signatures.MasterDrainProfit in let open Signatures.MasterDrainProfit in
signf (EddsaSignature.sign ~key) signf (EddsaSignature.sign ~key)