From 04ef832ad3b2d52dfb53b03144007381bcf6c6ad Mon Sep 17 00:00:00 2001 From: Guillem Rieu Date: Thu, 12 Jan 2017 16:13:03 +0100 Subject: [PATCH] Client: add optional seed argument to Client_keys.gen_keys --- src/client/client_keys.ml | 42 ++++++++++++++++++++++++++++++++------ src/client/client_keys.mli | 17 +++++++++++++++ 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/src/client/client_keys.ml b/src/client/client_keys.ml index 4c370223d..622a0fb64 100644 --- a/src/client/client_keys.ml +++ b/src/client/client_keys.ml @@ -33,8 +33,33 @@ module Secret_key = Client_aliases.Alias (struct let name = "secret key" end) -let gen_keys cctxt name = - let secret_key, public_key = Sodium.Sign.random_keypair () in +module Seed = struct + + let to_hex s = + Sodium.Sign.Bytes.of_seed s + |> Bytes.to_string + |> Hex_encode.hex_encode + + let of_hex s = + Hex_encode.hex_decode s + |> Bytes.of_string + |> Sodium.Sign.Bytes.to_seed + + let generate () = + (* Seed is 32 bytes long *) + Sodium.Random.Bytes.generate Sodium.Sign.seed_size + |> Sodium.Sign.Bytes.to_seed + + let extract = + Sodium.Sign.secret_key_to_seed +end + +let gen_keys ?seed cctxt name = + let seed = + match seed with + | None -> Seed.generate () + | Some s -> s in + let secret_key, public_key = Sodium.Sign.seed_keypair seed in Secret_key.add cctxt name secret_key >>= fun () -> Public_key.add cctxt name public_key >>= fun () -> Public_key_hash.add cctxt name (Ed25519.Public_key.hash public_key) >>= fun () -> @@ -65,6 +90,13 @@ let get_keys cctxt = end end +let list_keys cctxt = + Public_key_hash.load cctxt >>= fun l -> + Lwt_list.map_s (fun (name, pkh) -> + Public_key.mem cctxt name >>= fun pkm -> + Secret_key.mem cctxt name >>= fun pks -> + Lwt.return (name, pkh, pkm, pks)) + l let group = { Cli_entries.name = "keys" ; @@ -114,10 +146,8 @@ let commands () = command ~group ~desc: "list all public key hashes and associated keys" (fixed [ "list" ; "known" ; "identities" ]) (fun cctxt -> - Public_key_hash.load cctxt >>= fun l -> - Lwt_list.iter_s (fun (name, pkh) -> - Public_key.mem cctxt name >>= fun pkm -> - Secret_key.mem cctxt name >>= fun pks -> + list_keys cctxt >>= fun l -> + Lwt_list.iter_s (fun (name, pkh, pkm, pks) -> Public_key_hash.to_source cctxt pkh >>= fun v -> cctxt.message "%s: %s%s%s" name v (if pkm then " (public key known)" else "") diff --git a/src/client/client_keys.mli b/src/client/client_keys.mli index ecdc9f48e..2f4423878 100644 --- a/src/client/client_keys.mli +++ b/src/client/client_keys.mli @@ -14,6 +14,13 @@ module Public_key_hash : module Public_key : Client_aliases.Alias with type t = Ed25519.Public_key.t module Secret_key : Client_aliases.Alias with type t = Ed25519.Secret_key.t +module Seed : sig + val to_hex : Sodium.Sign.seed -> string + val of_hex : string -> Sodium.Sign.seed + val generate : unit -> Sodium.Sign.seed + val extract : Secret_key.t -> Sodium.Sign.seed +end + val get_key: Client_commands.context -> Public_key_hash.t -> @@ -23,4 +30,14 @@ val get_keys: Client_commands.context -> ( string * Public_key_hash.t * Public_key.t * Secret_key.t ) list Lwt.t +val list_keys: + Client_commands.context -> + (string * Public_key_hash.t * bool * bool) list Lwt.t + +val gen_keys: + ?seed: Sodium.Sign.seed -> + Client_commands.context -> + string -> + unit Lwt.t + val commands: unit -> Client_commands.command list