package octez-libs

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file chain_id.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com>     *)
(*                                                                           *)
(* Permission is hereby granted, free of charge, to any person obtaining a   *)
(* copy of this software and associated documentation files (the "Software"),*)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense,  *)
(* and/or sell copies of the Software, and to permit persons to whom the     *)
(* Software is furnished to do so, subject to the following conditions:      *)
(*                                                                           *)
(* The above copyright notice and this permission notice shall be included   *)
(* in all copies or substantial portions of the Software.                    *)
(*                                                                           *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL   *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING   *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER       *)
(* DEALINGS IN THE SOFTWARE.                                                 *)
(*                                                                           *)
(*****************************************************************************)

open Error_monad

type t = string

let name = "Chain_id"

let title = "Network identifier"

let extract bh = Bytes.sub_string (Block_hash.to_bytes bh) 0 4

let hash_bytes ?key l = extract (Block_hash.hash_bytes ?key l)

let hash_string ?key l = extract (Block_hash.hash_string ?key l)

let size = 4

let of_string_opt s = if String.length s <> size then None else Some s

let of_string s =
  match of_string_opt s with
  | None ->
      error_with
        "%s.of_string: wrong string size (%d instead of %d)"
        name
        (String.length s)
        size
  | Some h -> Ok h

let of_string_exn s =
  match of_string_opt s with
  | None ->
      Format.kasprintf
        invalid_arg
        "%s.of_string_exn: wrong string size (%d instead of %d)"
        name
        (String.length s)
        size
  | Some h -> h

let to_string s = s

let of_hex s =
  match Hex.to_string s with
  | None -> error_with "%s.of_hex: invalid hex string (%a)" name Hex.pp s
  | Some s -> of_string s

let of_hex_opt s = Option.bind (Hex.to_string s) of_string_opt

let of_hex_exn s =
  match Hex.to_string s with
  | None ->
      Format.kasprintf
        invalid_arg
        "%s.of_hex_exn: invalid hex string (%a)"
        name
        Hex.pp
        s
  | Some s -> of_string_exn s

let to_hex s = Hex.of_string (to_string s)

let of_bytes_opt b =
  if Bytes.length b <> size then None else Some (Bytes.to_string b)

let of_bytes_exn b =
  match of_bytes_opt b with
  | None ->
      let msg =
        Printf.sprintf
          "%s.of_bytes_exn: wrong string size (%d instead of %d)"
          name
          (Bytes.length b)
          size
      in
      raise (Invalid_argument msg)
  | Some h -> h

let of_bytes s =
  match of_bytes_opt s with
  | Some x -> Ok x
  | None -> error_with "Failed to deserialize a hash (%s)" name

let to_bytes = Bytes.of_string

let path_length = 1

let to_path key l =
  let (`Hex h) = to_hex key in
  h :: l

let of_path path =
  let path = String.concat "" path in
  of_hex_opt (`Hex path)

let of_path_exn path =
  let path = String.concat "" path in
  of_hex_exn (`Hex path)

let prefix_path p =
  let (`Hex p) = Hex.of_string p in
  [p]

let zero = of_hex_exn (`Hex (String.make (size * 2) '0'))

type Base58.data += Data of t

let b58check_encoding =
  Base58.register_encoding
    ~prefix:Base58.Prefix.chain_id
    ~length:size
    ~wrap:(fun s -> Data s)
    ~of_raw:of_string_opt
    ~to_raw:to_string

let raw_encoding =
  let open Data_encoding in
  conv to_bytes of_bytes_exn (Fixed.bytes size)

let of_block_hash bh = hash_bytes [Block_hash.to_bytes bh]

include (Compare.String : Compare.S with type t := t)

let hash = Stdlib.Hashtbl.hash

let seeded_hash = Stdlib.Hashtbl.seeded_hash

include Helpers.Make (struct
  type nonrec t = t

  let title = title

  let name = name

  let b58check_encoding = b58check_encoding

  let raw_encoding = raw_encoding

  let compare = compare

  let equal = equal

  let hash = hash

  let seeded_hash = seeded_hash
end)
OCaml

Innovation. Community. Security.