From 9405b702e919ea8adfa7cc228acc7ee208b44031 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Mon, 15 Jan 2018 15:03:13 +0100 Subject: [PATCH] OPAM: use the `hex` package It replaces our own `Hex_encode` module. --- lib_base/fitness.ml | 4 +- lib_client_base/client_debug.ml | 9 ++-- lib_crypto/blake2B.ml | 10 ++-- lib_crypto/ed25519.ml | 21 +++++--- lib_crypto/ed25519.mli | 4 ++ lib_crypto/net_id.ml | 10 ++-- lib_data_encoding/data_encoding.ml | 9 ++-- lib_data_encoding/hex_encode.ml | 49 ------------------- lib_data_encoding/hex_encode.mli | 24 --------- .../client_proto_programs.ml | 3 +- .../src/constants_repr.ml | 3 +- .../src/script_ir_translator.ml | 8 +-- lib_embedded_protocol_genesis/src/data.ml | 3 +- .../tezos_protocol_environment.ml | 1 - lib_protocol_environment_sigs/jbuild | 1 - lib_protocol_environment_sigs/v1/ed25519.mli | 4 ++ .../v1/hex_encode.mli | 24 --------- lib_protocol_environment_sigs/v1/mBytes.mli | 3 ++ lib_stdlib/jbuild | 2 +- lib_stdlib/mBytes.ml | 3 ++ lib_stdlib/mBytes.mli | 3 ++ lib_stdlib/tezos-stdlib.opam | 1 + test/utils/test_mbytes_buffer.ml | 4 +- 23 files changed, 67 insertions(+), 136 deletions(-) delete mode 100644 lib_data_encoding/hex_encode.ml delete mode 100644 lib_data_encoding/hex_encode.mli delete mode 100644 lib_protocol_environment_sigs/v1/hex_encode.mli diff --git a/lib_base/fitness.ml b/lib_base/fitness.ml index a16ed70b5..1b030f8e4 100644 --- a/lib_base/fitness.ml +++ b/lib_base/fitness.ml @@ -53,8 +53,8 @@ let max x y = if x <= y then y else x let rec pp fmt = function | [] -> () - | [f] -> Format.fprintf fmt "%s" (Hex_encode.hex_of_bytes f) - | f1 :: f -> Format.fprintf fmt "%s::%a" (Hex_encode.hex_of_bytes f1) pp f + | [f] -> Format.fprintf fmt "%a" Hex.pp (MBytes.to_hex f) + | f1 :: f -> Format.fprintf fmt "%a::%a" Hex.pp (MBytes.to_hex f1) pp f let encoding = let open Data_encoding in diff --git a/lib_client_base/client_debug.ml b/lib_client_base/client_debug.ml index 22e4bf21e..bb707a0fc 100644 --- a/lib_client_base/client_debug.ml +++ b/lib_client_base/client_debug.ml @@ -26,7 +26,7 @@ let pp_block ppf @ Fitness: @[%a@]\ @ Operations hash: %a\ @ Operations: @[%a@]\ - @ Data (hex encoded): \"%s\"@]" + @ Data (hex encoded): \"%a\"@]" Block_hash.pp hash Test_network_status.pp test_network level @@ -35,10 +35,7 @@ let pp_block ppf Protocol_hash.pp protocol Net_id.pp net_id Time.pp_hum timestamp - (Format.pp_print_list - ~pp_sep:Format.pp_print_space - Format.pp_print_string) - (List.map Hex_encode.hex_of_bytes fitness) + Fitness.pp fitness Operation_list_list_hash.pp operations_hash (fun ppf -> function | None -> Format.fprintf ppf "None" @@ -48,7 +45,7 @@ let pp_block ppf (fun ppf (oph, _) -> Operation_hash.pp ppf oph)) ppf operations) operations - (Hex_encode.hex_of_bytes data) + Hex.pp (MBytes.to_hex data) let stuck_node_report cctxt file = let ppf = Format.formatter_of_out_channel (open_out file) in diff --git a/lib_crypto/blake2B.ml b/lib_crypto/blake2B.ml index 304ccb4a1..0cbf665d3 100644 --- a/lib_crypto/blake2B.ml +++ b/lib_crypto/blake2B.ml @@ -46,9 +46,11 @@ module Make_minimal (K : S.Name) = struct | Some h -> h let to_string s = Bytes.to_string (Sodium.Generichash.Bytes.of_hash s) - let of_hex s = of_string (Hex_encode.hex_decode s) - let of_hex_exn s = of_string_exn (Hex_encode.hex_decode s) - let to_hex s = Hex_encode.hex_encode (to_string s) + let of_hex s = of_string (Hex.to_string (`Hex s)) + let of_hex_exn s = of_string_exn (Hex.to_string (`Hex s)) + let to_hex s = + let `Hex s = Hex.of_string (to_string s) in + s let compare = Sodium.Generichash.compare let equal x y = compare x y = 0 @@ -99,7 +101,7 @@ module Make_minimal (K : S.Name) = struct of_hex_exn path let prefix_path p = - let p = Hex_encode.hex_encode p in + let `Hex p = Hex.of_string p in let len = String.length p in let p1 = if len >= 2 then String.sub p 0 2 else "" and p2 = if len >= 4 then String.sub p 2 2 else "" diff --git a/lib_crypto/ed25519.ml b/lib_crypto/ed25519.ml index f9d842694..33bfd410b 100644 --- a/lib_crypto/ed25519.ml +++ b/lib_crypto/ed25519.ml @@ -33,14 +33,18 @@ module Public_key = struct type Base58.data += | Public_key of t + let to_string s = Bytes.to_string (Sodium.Sign.Bytes.of_public_key s) + let of_string_exn x = Sodium.Sign.Bytes.to_public_key (Bytes.of_string x) + let of_string x = + try Some (of_string_exn x) + with _ -> None + let b58check_encoding = Base58.register_encoding ~prefix: Base58.Prefix.ed25519_public_key ~length:Sodium.Sign.public_key_size - ~to_raw:(fun x -> Bytes.to_string (Sodium.Sign.Bytes.of_public_key x)) - ~of_raw:(fun x -> - try Some (Sodium.Sign.Bytes.to_public_key (Bytes.of_string x)) - with _ -> None) + ~to_raw:to_string + ~of_raw:of_string ~wrap:(fun x -> Public_key x) let of_b58check_opt s = Base58.simple_decode b58check_encoding s @@ -54,6 +58,10 @@ module Public_key = struct | None -> generic_error "Unexpected hash (ed25519 public key)" let to_b58check s = Base58.simple_encode b58check_encoding s + let of_hex s = of_string (Hex.to_string s) + let of_hex_exn s = of_string_exn (Hex.to_string s) + let to_hex s = Hex.of_string (to_string s) + let of_bytes s = Sodium.Sign.Bytes.to_public_key s let param ?(name="ed25519-public") ?(desc="Ed25519 public key (b58check-encoded)") t = @@ -222,10 +230,11 @@ module Seed = struct let to_hex s = Sodium.Sign.Bytes.of_seed s |> Bytes.to_string - |> Hex_encode.hex_encode + |> Hex.of_string + |> (fun (`Hex s) -> s) let of_hex s = - Hex_encode.hex_decode s + Hex.to_string (`Hex s) |> Bytes.of_string |> Sodium.Sign.Bytes.to_seed diff --git a/lib_crypto/ed25519.mli b/lib_crypto/ed25519.mli index be1fa0e8b..82b0ad2f7 100644 --- a/lib_crypto/ed25519.mli +++ b/lib_crypto/ed25519.mli @@ -36,6 +36,10 @@ module Public_key : sig val of_b58check_opt: string -> t option val to_b58check: t -> string + val to_hex: t -> Hex.t + val of_hex: Hex.t -> t option + val of_hex_exn: Hex.t -> t + val of_bytes: Bytes.t -> t end diff --git a/lib_crypto/net_id.ml b/lib_crypto/net_id.ml index fa8a4ebc7..1173b9f17 100644 --- a/lib_crypto/net_id.ml +++ b/lib_crypto/net_id.ml @@ -36,9 +36,11 @@ let of_string_exn s = | Some h -> h let to_string s = s -let of_hex s = of_string (Hex_encode.hex_decode s) -let of_hex_exn s = of_string_exn (Hex_encode.hex_decode s) -let to_hex s = Hex_encode.hex_encode (to_string s) +let of_hex s = of_string (Hex.to_string (`Hex s)) +let of_hex_exn s = of_string_exn (Hex.to_string (`Hex s)) +let to_hex s = + let `Hex s = Hex.of_string (to_string s) in + s let compare = String.compare let equal = String.equal @@ -135,7 +137,7 @@ let of_path_exn path = of_hex_exn path let prefix_path p = - let p = Hex_encode.hex_encode p in + let `Hex p = Hex.of_string p in [ p ] module Table = struct diff --git a/lib_data_encoding/data_encoding.ml b/lib_data_encoding/data_encoding.ml index a4c368d9b..8f27c916a 100644 --- a/lib_data_encoding/data_encoding.ml +++ b/lib_data_encoding/data_encoding.ml @@ -315,9 +315,12 @@ module Json = struct format = None ; id = None } in conv ~schema - Hex_encode.hex_of_bytes - (wrap_error Hex_encode.bytes_of_hex) - string + MBytes.to_hex + (wrap_error MBytes.of_hex) + (conv + (fun (`Hex h) -> h) + (fun h -> `Hex h) + string) let rec lift_union : type a. a t -> a t = fun e -> match e.encoding with diff --git a/lib_data_encoding/hex_encode.ml b/lib_data_encoding/hex_encode.ml deleted file mode 100644 index 74da36daf..000000000 --- a/lib_data_encoding/hex_encode.ml +++ /dev/null @@ -1,49 +0,0 @@ -(**************************************************************************) -(* *) -(* Copyright (c) 2014 - 2017. *) -(* Dynamic Ledger Solutions, Inc. *) -(* *) -(* All rights reserved. No warranty, explicit or implicit, provided. *) -(* *) -(**************************************************************************) - -(* Tezos Utility library - Hexadecimal encoding *) - -(* From OCaml's stdlib. See [Digest.to_hex], and [hex_of_bytes], [hex_encode] - below for examples. *) -let gen_encode length get s = - let n = length s in - let result = Bytes.create (n*2) in - for i = 0 to n-1 do - Bytes.blit_string (Printf.sprintf "%02x" (get s i)) 0 result (2*i) 2; - done; - Bytes.unsafe_to_string result - -let hex_of_bytes = gen_encode MBytes.length MBytes.get_uint8 -let hex_encode = gen_encode String.length (fun s i -> int_of_char s.[i]) - -(* From OCaml's stdlib. See [Digest.from_hex], and [hex_decode], [bytes_of_hex] - below for examples. *) -let gen_decode create set h = - let n = String.length h in - if n mod 2 <> 0 then invalid_arg ("hex_decode: " ^ h); - let digit c = - match c with - | '0'..'9' -> int_of_char c - int_of_char '0' - | 'A'..'F' -> int_of_char c - int_of_char 'A' + 10 - | 'a'..'f' -> int_of_char c - int_of_char 'a' + 10 - | _c -> invalid_arg ("hex_decode: " ^ h) - in - let byte i = digit h.[i] lsl 4 + digit h.[i+1] in - let result = create (n / 2) in - for i = 0 to n/2 - 1 do - set result i (byte (2 * i)); - done; - result - -let hex_decode s = - gen_decode Bytes.create (fun s i c -> Bytes.set s i (char_of_int c)) s |> - Bytes.unsafe_to_string - -let bytes_of_hex s = - gen_decode MBytes.create MBytes.set_int8 s diff --git a/lib_data_encoding/hex_encode.mli b/lib_data_encoding/hex_encode.mli deleted file mode 100644 index da96d7993..000000000 --- a/lib_data_encoding/hex_encode.mli +++ /dev/null @@ -1,24 +0,0 @@ -(**************************************************************************) -(* *) -(* Copyright (c) 2014 - 2017. *) -(* Dynamic Ledger Solutions, Inc. *) -(* *) -(* All rights reserved. No warranty, explicit or implicit, provided. *) -(* *) -(**************************************************************************) - -(** Tezos Utility library - Hexadecimal encoding *) - -(** Parses a sequence of hexadecimal characters pairs as bytes *) -val hex_of_bytes: MBytes.t -> string - -(** Prints a sequence of bytes as hexadecimal characters pairs *) -val bytes_of_hex: string -> MBytes.t - -(** Interprets a sequence of hexadecimal characters pairs representing - bytes as the characters codes of an OCaml string. *) -val hex_decode: string -> string - -(** Formats the codes of the characters of an OCaml string as a - sequence of hexadecimal character pairs. *) -val hex_encode: string -> string diff --git a/lib_embedded_client_alpha/client_proto_programs.ml b/lib_embedded_client_alpha/client_proto_programs.ml index d407b4473..af23b3ee6 100644 --- a/lib_embedded_client_alpha/client_proto_programs.ml +++ b/lib_embedded_client_alpha/client_proto_programs.ml @@ -89,7 +89,8 @@ let hash_and_sign (data : Michelson_v1_parser.parsed) (typ : Michelson_v1_parser return (hash, signature |> Data_encoding.Binary.to_bytes Ed25519.Signature.encoding |> - Hex_encode.hex_of_bytes) + MBytes.to_hex |> + (fun (`Hex s) -> s)) let typecheck_data ~(data : Michelson_v1_parser.parsed) diff --git a/lib_embedded_protocol_alpha/src/constants_repr.ml b/lib_embedded_protocol_alpha/src/constants_repr.ml index 0ca4888f9..3f6441784 100644 --- a/lib_embedded_protocol_alpha/src/constants_repr.ml +++ b/lib_embedded_protocol_alpha/src/constants_repr.ml @@ -57,8 +57,7 @@ type constants = { michelson_maximum_type_size: int; } -let read_public_key s = - Ed25519.Public_key.of_bytes (Bytes.of_string (Hex_encode.hex_decode s)) +let read_public_key s = Ed25519.Public_key.of_hex_exn (`Hex s) let default = { cycle_length = 2048l ; diff --git a/lib_embedded_protocol_alpha/src/script_ir_translator.ml b/lib_embedded_protocol_alpha/src/script_ir_translator.ml index 9d95646e0..68791d76f 100644 --- a/lib_embedded_protocol_alpha/src/script_ir_translator.ml +++ b/lib_embedded_protocol_alpha/src/script_ir_translator.ml @@ -601,9 +601,9 @@ let rec unparse_data | Contract_t _, (_, _, c) -> String (-1, Contract.to_b58check c) | Signature_t, s -> - let text = - Hex_encode.hex_encode - (MBytes.to_string (Data_encoding.Binary.to_bytes Ed25519.Signature.encoding s)) in + let `Hex text = + MBytes.to_hex + (Data_encoding.Binary.to_bytes Ed25519.Signature.encoding s) in String (-1, text) | Tez_t, v -> String (-1, Tez.to_string v) @@ -1075,7 +1075,7 @@ let rec parse_data | Signature_t, String (_, s) -> begin try match Data_encoding.Binary.of_bytes Ed25519.Signature.encoding - (MBytes.of_string (Hex_encode.hex_decode s)) with + (MBytes.of_hex (`Hex s)) with | Some s -> return s | None -> raise Not_found with _ -> diff --git a/lib_embedded_protocol_genesis/src/data.ml b/lib_embedded_protocol_genesis/src/data.ml index 977c5d316..b843c468a 100644 --- a/lib_embedded_protocol_genesis/src/data.ml +++ b/lib_embedded_protocol_genesis/src/data.ml @@ -81,8 +81,7 @@ module Pubkey = struct let default = let pubkey = "4d5373455738070434f214826d301a1c206780d7f789fcbf94c2149b2e0718cc" in - Ed25519.Public_key.of_bytes - (Bytes.of_string (Hex_encode.hex_decode pubkey)) + Ed25519.Public_key.of_hex_exn (`Hex pubkey) let get_pubkey ctxt = Context.get ctxt pubkey_key >>= function diff --git a/lib_node_updater/tezos_protocol_environment.ml b/lib_node_updater/tezos_protocol_environment.ml index 50450a723..854cbef72 100644 --- a/lib_node_updater/tezos_protocol_environment.ml +++ b/lib_node_updater/tezos_protocol_environment.ml @@ -31,7 +31,6 @@ module Make(Param : sig val name: string end)() = struct module Nativeint = Nativeint module Buffer = Buffer module Format = Format - module Hex_encode = Hex_encode module Z = Z module Lwt_sequence = Lwt_sequence module Lwt = Lwt diff --git a/lib_protocol_environment_sigs/jbuild b/lib_protocol_environment_sigs/jbuild index 9a2b324ea..adf1c6960 100644 --- a/lib_protocol_environment_sigs/jbuild +++ b/lib_protocol_environment_sigs/jbuild @@ -25,7 +25,6 @@ ;; Tezos extended stdlib v1/mBytes.mli - v1/hex_encode.mli v1/compare.mli v1/data_encoding.mli v1/error_monad.mli diff --git a/lib_protocol_environment_sigs/v1/ed25519.mli b/lib_protocol_environment_sigs/v1/ed25519.mli index e54117ffa..36faed593 100644 --- a/lib_protocol_environment_sigs/v1/ed25519.mli +++ b/lib_protocol_environment_sigs/v1/ed25519.mli @@ -32,6 +32,10 @@ module Public_key : sig val of_bytes: Bytes.t -> t + val to_hex: t -> [ `Hex of string ] + val of_hex: [ `Hex of string ] -> t option + val of_hex_exn: [ `Hex of string ] -> t + end module Secret_key : sig diff --git a/lib_protocol_environment_sigs/v1/hex_encode.mli b/lib_protocol_environment_sigs/v1/hex_encode.mli deleted file mode 100644 index da96d7993..000000000 --- a/lib_protocol_environment_sigs/v1/hex_encode.mli +++ /dev/null @@ -1,24 +0,0 @@ -(**************************************************************************) -(* *) -(* Copyright (c) 2014 - 2017. *) -(* Dynamic Ledger Solutions, Inc. *) -(* *) -(* All rights reserved. No warranty, explicit or implicit, provided. *) -(* *) -(**************************************************************************) - -(** Tezos Utility library - Hexadecimal encoding *) - -(** Parses a sequence of hexadecimal characters pairs as bytes *) -val hex_of_bytes: MBytes.t -> string - -(** Prints a sequence of bytes as hexadecimal characters pairs *) -val bytes_of_hex: string -> MBytes.t - -(** Interprets a sequence of hexadecimal characters pairs representing - bytes as the characters codes of an OCaml string. *) -val hex_decode: string -> string - -(** Formats the codes of the characters of an OCaml string as a - sequence of hexadecimal character pairs. *) -val hex_encode: string -> string diff --git a/lib_protocol_environment_sigs/v1/mBytes.mli b/lib_protocol_environment_sigs/v1/mBytes.mli index 430d4f3c8..ff26d9935 100644 --- a/lib_protocol_environment_sigs/v1/mBytes.mli +++ b/lib_protocol_environment_sigs/v1/mBytes.mli @@ -130,3 +130,6 @@ val (>) : t -> t -> bool val compare : t -> t -> int val concat: t -> t -> t + +val to_hex: t -> [ `Hex of string ] +val of_hex: [ `Hex of string ] -> t diff --git a/lib_stdlib/jbuild b/lib_stdlib/jbuild index 369503a54..a306b19df 100644 --- a/lib_stdlib/jbuild +++ b/lib_stdlib/jbuild @@ -3,7 +3,7 @@ (library ((name tezos_stdlib) (public_name tezos-stdlib) - (libraries (ocplib-endian.bigstring cstruct stringext)) + (libraries (ocplib-endian.bigstring cstruct stringext hex)) (flags (:standard -safe-string)))) (alias diff --git a/lib_stdlib/mBytes.ml b/lib_stdlib/mBytes.ml index cc0c2444c..032bc30fd 100644 --- a/lib_stdlib/mBytes.ml +++ b/lib_stdlib/mBytes.ml @@ -64,6 +64,9 @@ let of_string buf = unsafe_blit_string_to_bigstring buf 0 c 0 buflen; c +let to_hex s = Hex.of_cstruct (Cstruct.of_bigarray s) +let of_hex s = Cstruct.to_bigarray (Hex.to_cstruct s) + let substring src srcoff len = if len < 0 || srcoff < 0 || length src - srcoff < len then raise (Invalid_argument (invalid_bounds srcoff len)); diff --git a/lib_stdlib/mBytes.mli b/lib_stdlib/mBytes.mli index d4c9bcab9..845bd846e 100644 --- a/lib_stdlib/mBytes.mli +++ b/lib_stdlib/mBytes.mli @@ -149,3 +149,6 @@ val compare : t -> t -> int val concat: t -> t -> t (** Returns a new array with adjacent copies of the two input arrays **) + +val to_hex: t -> Hex.t +val of_hex: Hex.t -> t diff --git a/lib_stdlib/tezos-stdlib.opam b/lib_stdlib/tezos-stdlib.opam index b910895be..b52a3ceb9 100644 --- a/lib_stdlib/tezos-stdlib.opam +++ b/lib_stdlib/tezos-stdlib.opam @@ -10,6 +10,7 @@ depends: [ "ocamlfind" { build } "jbuilder" { build & >= "1.0+beta15" } "cstruct" + "hex" "ocplib-endian" "stringext" ] diff --git a/test/utils/test_mbytes_buffer.ml b/test/utils/test_mbytes_buffer.ml index 2f7e33679..b7852cb80 100644 --- a/test/utils/test_mbytes_buffer.ml +++ b/test/utils/test_mbytes_buffer.ml @@ -8,8 +8,8 @@ (**************************************************************************) let hex_of_buffer buf = - Hex_encode.hex_of_bytes (MBytes_buffer.to_mbytes buf) - + let `Hex s = MBytes.to_hex (MBytes_buffer.to_mbytes buf) in + s let assert_hex_eq buf = Assert.equal ~prn:(fun x -> x) (hex_of_buffer buf)