package octez-libs

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

Source file identity_file.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
(*****************************************************************************)
(*                                                                           *)
(* 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.                                                 *)
(*                                                                           *)
(*****************************************************************************)

type error += No_identity_file of string

type error += Insufficient_proof_of_work of {expected : float}

type error +=
  | Identity_mismatch of {
      filename : string;
      peer_id : Tezos_crypto.Crypto_box.Public_key_hash.t;
    }

type error +=
  | Identity_keys_mismatch of {
      filename : string;
      expected_key : Tezos_crypto.Crypto_box.public_key;
    }

let () =
  register_error_kind
    `Permanent
    ~id:"main.identity.no_file"
    ~title:"No identity file"
    ~description:"The node identity file cannot be found"
    ~pp:(fun ppf file ->
      Format.fprintf
        ppf
        "Cannot read the identity file: `%s`. See `%s identity --help` on how \
         to generate an identity."
        file
        Sys.argv.(0))
    Data_encoding.(obj1 (req "file" string))
    (function No_identity_file file -> Some file | _ -> None)
    (fun file -> No_identity_file file)

let () =
  register_error_kind
    `Permanent
    ~id:"main.identity.insufficient_proof_of_work"
    ~title:"Insufficient proof of work"
    ~description:
      "The proof of work embedded by the current identity is not sufficient"
    ~pp:(fun ppf expected ->
      Format.fprintf
        ppf
        "The current identity does not embed a sufficient stamp of \
         proof-of-work. (expected level: %.2f). See `%s identity --help` on \
         how to generate a new identity."
        expected
        Sys.argv.(0))
    Data_encoding.(obj1 (req "expected" float))
    (function
      | Insufficient_proof_of_work {expected} -> Some expected | _ -> None)
    (fun expected -> Insufficient_proof_of_work {expected})

let () =
  register_error_kind
    `Permanent
    ~id:"main.identity.identity_mismatch"
    ~title:"Identity mismatch"
    ~description:
      "The identity (public key hash) does not match the keys provided with it"
    ~pp:(fun ppf (file, public_key_hash) ->
      Format.fprintf
        ppf
        "The current identity (public key hash) does not match the keys in %s.\n\
        \           Expected identity %a."
        file
        Tezos_crypto.Crypto_box.Public_key_hash.pp
        public_key_hash)
    Data_encoding.(
      obj2
        (req "file" string)
        (req "public_key_hash" Tezos_crypto.Crypto_box.Public_key_hash.encoding))
    (function
      | Identity_mismatch {filename; peer_id} -> Some (filename, peer_id)
      | _ -> None)
    (fun (filename, peer_id) -> Identity_mismatch {filename; peer_id})

let () =
  register_error_kind
    `Permanent
    ~id:"main.identity.identity_keys_mismatch"
    ~title:"Identity keys mismatch"
    ~description:
      "The current identity file has non-matching keys (secret key/ public key \
       pair is not valid)"
    ~pp:(fun ppf (file, public_key) ->
      Format.fprintf
        ppf
        "The current identity file %s has non-matching keys (secret key/ \
         public key pair is not valid).\n\
        \           Expected public key %a."
        file
        Tezos_crypto.Crypto_box.pp_pk
        public_key)
    Data_encoding.(
      obj2
        (req "file" string)
        (req "public_key" Tezos_crypto.Crypto_box.public_key_encoding))
    (function
      | Identity_keys_mismatch {filename; expected_key} ->
          Some (filename, expected_key)
      | _ -> None)
    (fun (filename, expected_key) ->
      Identity_keys_mismatch {filename; expected_key})

let read ?expected_pow filename =
  let open Lwt_result_syntax in
  let*! file_exists = Lwt_unix.file_exists filename in
  if not file_exists then tzfail (No_identity_file filename)
  else
    let* json = Lwt_utils_unix.Json.read_file filename in
    let id = Data_encoding.Json.destruct P2p_identity.encoding json in
    let pkh = Tezos_crypto.Crypto_box.hash id.public_key in
    (* check public_key hash *)
    if not (Tezos_crypto.Crypto_box.Public_key_hash.equal pkh id.peer_id) then
      tzfail (Identity_mismatch {filename; peer_id = pkh})
      (* check public/private keys correspondence *)
    else if
      not
        Tezos_crypto.Crypto_box.(equal (neuterize id.secret_key) id.public_key)
    then
      tzfail (Identity_keys_mismatch {filename; expected_key = id.public_key})
    else
      (* check PoW level *)
      match expected_pow with
      | None -> return id
      | Some expected ->
          let target = Tezos_crypto.Crypto_box.make_pow_target expected in
          if
            not
              (Tezos_crypto.Crypto_box.check_proof_of_work
                 id.public_key
                 id.proof_of_work_stamp
                 target)
          then tzfail (Insufficient_proof_of_work {expected})
          else return id

type error += Existent_identity_file of string

let () =
  register_error_kind
    `Permanent
    ~id:"main.identity.existent_file"
    ~title:"Cannot overwrite identity file"
    ~description:"Cannot implicitly overwrite the current identity file"
    ~pp:(fun ppf file ->
      Format.fprintf
        ppf
        "Cannot implicitly overwrite the current identity file: '%s'. See `%s \
         identity --help` on how to generate a new identity."
        file
        Sys.argv.(0))
    Data_encoding.(obj1 (req "file" string))
    (function Existent_identity_file file -> Some file | _ -> None)
    (fun file -> Existent_identity_file file)

let write ~check_data_dir file identity =
  let open Lwt_result_syntax in
  if Sys.file_exists file then tzfail (Existent_identity_file file)
  else
    let* () = check_data_dir ~data_dir:(Filename.dirname file) in
    Lwt_utils_unix.Json.write_file
      file
      (Data_encoding.Json.construct P2p_identity.encoding identity)

let generate_with_animation ppf target =
  let open Lwt_syntax in
  let duration = 1200 / Animation.number_of_frames in
  Animation.make_with_animation
    ppf
    ~make:(fun count ->
      Lwt.catch
        (fun () ->
          let+ id = P2p_identity.generate_with_bound ~max:count target in
          Ok id)
        (function
          | Not_found -> Lwt.return @@ Error count | exc -> Lwt.reraise exc))
    ~on_retry:(fun time count ->
      let ms = Time.Monotonic.Span.to_ms time in
      let count =
        if ms <= 1 then max 10 (count * 10) else count * duration / ms
      in
      let* () = Lwt.pause () in
      Lwt.return count)
    10000

let generate ~check_data_dir identity_file expected_pow =
  let open Lwt_result_syntax in
  if Sys.file_exists identity_file then
    tzfail (Existent_identity_file identity_file)
  else
    let target = Tezos_crypto.Crypto_box.make_pow_target expected_pow in
    Format.eprintf "Generating a new identity... (level: %.2f) " expected_pow ;
    let*! id = generate_with_animation Format.err_formatter target in
    let* () = write ~check_data_dir identity_file id in
    Format.eprintf
      "Stored the new identity (%a) into '%s'.@."
      P2p_peer.Id.pp
      id.peer_id
      identity_file ;
    return id

module Event = struct
  include Internal_event.Simple

  let section = ["node"; "identity"]

  let read_identity =
    declare_1
      ~section
      ~name:"read_identity"
      ~msg:"read identity file"
      ~level:Notice
      ("peer_id", P2p_peer.Id.encoding)

  let generating_identity =
    declare_0
      ~section
      ~name:"generating_identity"
      ~msg:"generating an identity file"
      ~level:Notice
      ()

  let identity_generated =
    declare_1
      ~section
      ~name:"identity_generated"
      ~msg:"identity file generated"
      ~level:Notice
      ("peer_id", P2p_peer.Id.encoding)
end

let init ~check_data_dir ~identity_file ~expected_pow =
  let open Lwt_result_syntax in
  if Sys.file_exists identity_file then
    let* identity = read identity_file in
    let*! () = Event.(emit read_identity) identity.peer_id in
    return identity
  else
    let*! () = Event.(emit generating_identity) () in
    let* identity = generate ~check_data_dir identity_file expected_pow in
    let*! () = Event.(emit identity_generated) identity.peer_id in
    return identity
OCaml

Innovation. Community. Security.