package octez-shell-libs

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

Source file client_context_unix.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com>     *)
(* Copyright (c) 2018 Nomadic Labs, <contact@nomadic-labs.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.                                                 *)
(*                                                                           *)
(*****************************************************************************)

class unix_wallet ~base_dir ~password_filename : Client_context.wallet =
  object (self)
    val lock_mutex = Lwt_mutex.create ()

    method load_passwords =
      match password_filename with
      | None -> None
      | Some filename ->
          if Sys.file_exists filename then Some (Lwt_io.lines_of_file filename)
          else None

    method read_file path =
      let open Lwt_syntax in
      Lwt.catch
        (fun () ->
          let* content = Lwt_io.(with_file ~mode:Input path read) in
          return_ok content)
        (fun exn -> failwith "cannot read file (%s)" (Printexc.to_string exn))

    method private filename alias_name =
      Filename.concat
        base_dir
        (String.map (function ' ' -> '_' | c -> c) alias_name ^ "s")

    method get_base_dir = base_dir

    method with_lock : type a. (unit -> a Lwt.t) -> a Lwt.t =
      fun f ->
        Lwt_mutex.with_lock lock_mutex (fun () ->
            let open Lwt_syntax in
            let unlock fd =
              let fd = Lwt_unix.unix_file_descr fd in
              Unix.lockf fd Unix.F_ULOCK 0 ;
              Unix.close fd
            in
            let lock () =
              let* fd =
                Lwt_unix.openfile
                  (Filename.concat base_dir "wallet_lock")
                  Lwt_unix.[O_CREAT; O_WRONLY]
                  0o644
              in
              let* () = Lwt_unix.lockf fd Unix.F_LOCK 0 in
              let sighandler =
                Lwt_unix.on_signal Sys.sigint (fun _s -> unlock fd)
              in
              Lwt.return (fd, sighandler)
            in
            let* fd, sh = lock () in
            (* catch might be useless if f always uses the error monad *)
            let* res =
              Lwt.finalize f (fun () ->
                  unlock fd ;
                  Lwt.return_unit)
            in
            Lwt_unix.disable_signal_handler sh ;
            Lwt.return res)

    method load : type a.
        string -> default:a -> a Data_encoding.encoding -> a tzresult Lwt.t =
      fun alias_name ~default encoding ->
        let open Lwt_result_syntax in
        let filename = self#filename alias_name in
        if not (Sys.file_exists filename) then return default
        else
          let* json =
            trace_eval
              (fun () ->
                error_of_fmt "could not read the %s alias file" alias_name)
              (Lwt_utils_unix.Json.read_file filename)
          in
          match Data_encoding.Json.destruct encoding json with
          | exception e ->
              failwith
                "did not understand the %s alias file %s : %s"
                alias_name
                filename
                (Printexc.to_string e)
          | data -> return data

    method write : type a.
        string -> a -> a Data_encoding.encoding -> unit tzresult Lwt.t =
      fun alias_name list encoding ->
        let open Lwt_result_syntax in
        trace_eval (fun () ->
            error_of_fmt "could not write the %s alias file." alias_name)
        @@ Error_monad.catch_es (fun () ->
               let*! () = Lwt_utils_unix.create_dir base_dir in
               let filename = self#filename alias_name in
               let json = Data_encoding.Json.construct encoding list in
               let content = Data_encoding.Json.to_string ~minify:false json in
               let*! res =
                 Lwt_utils_unix.with_atomic_open_out ~overwrite:true filename
                 @@ fun chan -> Lwt_utils_unix.write_string chan content
               in
               match res with
               | Ok x -> return x
               | Error {unix_code; caller; arg; _} ->
                   tzfail (Exn (Unix.Unix_error (unix_code, caller, arg))))

    method last_modification_time : string -> float option tzresult Lwt.t =
      let open Lwt_result_syntax in
      fun alias_name ->
        let filename = self#filename alias_name in
        let*! exists = Lwt_unix.file_exists filename in
        if exists then
          let* stat = Error_monad.catch_s (fun () -> Lwt_unix.stat filename) in
          return_some stat.st_mtime
        else return_none
  end

class unix_prompter : Client_context.prompter =
  object
    method prompt : type a. (a, string tzresult) Client_context.lwt_format -> a
        =
      Format.kasprintf (fun msg ->
          print_string msg ;
          let line = read_line () in
          Lwt.return_ok line)

    method prompt_password : type a.
        (a, Bytes.t tzresult) Client_context.lwt_format -> a =
      Format.kasprintf (fun msg ->
          print_string msg ;
          let line =
            if Unix.isatty Unix.stdin then Lwt_utils_unix.getpass ()
            else read_line ()
          in
          Lwt.return_ok (Bytes.of_string line))

    method multiple_password_retries = true
  end

class unix_logger ~base_dir : Client_context.printer =
  let startup = Format.asprintf "%a" Time.System.pp_hum (Time.System.now ()) in
  let log channel msg =
    let open Lwt_syntax in
    match channel with
    | "stdout" ->
        print_endline msg ;
        Lwt.return_unit
    | "stderr" ->
        prerr_endline msg ;
        Lwt.return_unit
    | log ->
        let open Filename.Infix in
        let* () = Lwt_utils_unix.create_dir (base_dir // "logs" // log) in
        Lwt_io.with_file
          ~flags:Unix.[O_APPEND; O_CREAT; O_WRONLY]
          ~mode:Lwt_io.Output
          (base_dir // "logs" // log // startup)
          (fun chan -> Lwt_io.write chan msg)
  in
  object
    inherit Client_context.simple_printer log
  end

class unix_io_wallet ~base_dir ~password_filename : Client_context.io_wallet =
  object
    inherit unix_wallet ~base_dir ~password_filename

    inherit unix_logger ~base_dir

    inherit unix_prompter
  end

class unix_ui : Client_context.ui =
  object
    method sleep f = Lwt_unix.sleep f

    method exit : 'a. int -> 'a = fun i -> Lwt_exit.exit_and_raise i

    method now = Tezos_base.Time.System.now
  end

class unix_full ~base_dir ~chain ~block ~confirmations ~password_filename
  ~rpc_config ~verbose_rpc_error_diagnostics : Client_context.full =
  object
    inherit unix_logger ~base_dir

    inherit unix_prompter

    inherit unix_wallet ~base_dir ~password_filename

    inherit
      Tezos_rpc_http_client_unix.RPC_client_unix.http_ctxt
        rpc_config
        (Media_type.Command_line.of_command_line rpc_config.media_type)

    inherit unix_ui

    method chain = chain

    method block = block

    method confirmations = confirmations

    method verbose_rpc_error_diagnostics = verbose_rpc_error_diagnostics
  end

class unix_mockup ~base_dir ~mem_only ~mockup_env ~chain_id ~rpc_context
  ~protocol_data : Client_context.full =
  object
    inherit unix_logger ~base_dir

    inherit unix_prompter

    inherit unix_wallet ~base_dir ~password_filename:None

    inherit
      Tezos_mockup.RPC_client.mockup_ctxt
        base_dir
        mem_only
        mockup_env
        chain_id
        rpc_context
        protocol_data

    inherit unix_ui

    method chain = `Hash chain_id

    method block = `Head 0

    method confirmations = None

    method verbose_rpc_error_diagnostics = false
  end

class unix_proxy ~base_dir ?protocol ~chain ~block ~confirmations
  ~password_filename ~rpc_config ~mode () : Client_context.full =
  object
    inherit unix_logger ~base_dir

    inherit unix_prompter

    inherit unix_wallet ~base_dir ~password_filename

    inherit
      Tezos_proxy_rpc.RPC_client.http_local_ctxt
        (new unix_logger ~base_dir)
        (new Tezos_rpc_http_client_unix.RPC_client_unix.http_ctxt
           rpc_config
           (Media_type.Command_line.of_command_line rpc_config.media_type))
        mode
        protocol

    inherit unix_ui

    method chain = chain

    method block = block

    method confirmations = confirmations

    method verbose_rpc_error_diagnostics = false
  end
OCaml

Innovation. Community. Security.