• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MinaProtocol / mina / 2863

05 Nov 2024 06:20PM UTC coverage: 30.754% (-16.6%) from 47.311%
2863

push

buildkite

web-flow
Merge pull request #16296 from MinaProtocol/dkijania/more_multi_jobs

more multi jobs in CI

20276 of 65930 relevant lines covered (30.75%)

8631.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

6.25
/src/lib/secrets/keypair_read_write.ml
1
(* keypair_read_write.ml -- readers, writers for keypairs *)
2

2✔
3
open Core_kernel
4
open Async
5
open Signature_lib
6

7
module Make (Env : sig
8
  val env : string
9

10
  val which : string
11
end) =
12
struct
13
  open Env
14

15
  (* avoid spurious cyclic dependency *)
16
  module Keypair = Signature_lib.Keypair
17

18
  type t = Keypair.t
19

20
  let env = env
21

22
  (** Writes a keypair to [privkey_path] and [privkey_path ^ ".pub"] using [Secret_file] *)
23
  let write_exn { Keypair.private_key; public_key } ~(privkey_path : string)
24
      ~(password : Secret_file.password) : unit Deferred.t =
25
    let privkey_bytes =
×
26
      Private_key.to_bigstring private_key |> Bigstring.to_bytes
×
27
    in
28
    let pubkey_string =
×
29
      Public_key.Compressed.to_base58_check (Public_key.compress public_key)
×
30
    in
31
    match%bind
32
      Secret_file.write ~path:privkey_path ~mkdir:true ~plaintext:privkey_bytes
33
        ~password
34
    with
35
    | Ok () ->
×
36
        (* The hope is that if [Secret_file.write] succeeded then this ought to
37
           as well, letting [handle_open] stay inside [Secret_file]. It might not
38
           if the environment changes underneath us, and we won't have nice errors
39
           in that case. *)
40
        let%bind pubkey_f = Writer.open_file (privkey_path ^ ".pub") in
×
41
        Writer.write_line pubkey_f pubkey_string ;
×
42
        Writer.close pubkey_f
×
43
    | Error e ->
×
44
        Privkey_error.raise ~which e
45

46
  (** Reads a private key from [privkey_path] using [Secret_file] *)
47
  let read ~(privkey_path : string) ~(password : Secret_file.password) :
48
      (Keypair.t, Privkey_error.t) Deferred.Result.t =
49
    let open Deferred.Result.Let_syntax in
×
50
    let%bind pk_bytes = Secret_file.read ~path:privkey_path ~password in
51
    let open Result.Let_syntax in
×
52
    Deferred.return
53
    @@ let%bind sk =
54
         try
55
           return
×
56
             (pk_bytes |> Bigstring.of_bytes |> Private_key.of_bigstring_exn)
×
57
         with exn ->
×
58
           Privkey_error.corrupted_privkey
×
59
             (Error.createf "Error parsing decrypted private key file: %s"
×
60
                (Exn.to_string exn) )
×
61
       in
62
       try return (Keypair.of_private_key_exn sk)
×
63
       with exn ->
×
64
         Privkey_error.corrupted_privkey
65
           (Error.createf
×
66
              "Error computing public key from private, is your keyfile \
67
               corrupt? %s"
68
              (Exn.to_string exn) )
×
69

70
  (** Reads a private key from [privkey_path] using [Secret_file], throws on failure *)
71
  let read_exn ~(privkey_path : string) ~(password : Secret_file.password) :
72
      Keypair.t Deferred.t =
73
    match%map read ~privkey_path ~password with
74
    | Ok keypair ->
×
75
        keypair
76
    | Error priv_key_error ->
×
77
        Privkey_error.raise ~which priv_key_error
78

79
  let read_exn' path =
80
    let password =
×
81
      let env_value = Sys.getenv env in
82
      match env_value with
×
83
      | Some v ->
×
84
          lazy (return @@ Bytes.of_string v)
×
85
      | None ->
×
86
          let error_help_message =
87
            sprintf "Set the %s environment variable to the password" env
88
          in
89
          lazy
×
90
            (Password.read_hidden_line ~error_help_message
×
91
               "Secret key password: " )
92
    in
93
    read_exn ~privkey_path:path ~password
94
end
4✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc