Client: add optional seed argument to Client_keys.gen_keys

This commit is contained in:
Guillem Rieu 2017-01-12 16:13:03 +01:00 committed by Benjamin Canou
parent c6c81345a8
commit 04ef832ad3
2 changed files with 53 additions and 6 deletions

View File

@ -33,8 +33,33 @@ module Secret_key = Client_aliases.Alias (struct
let name = "secret key" let name = "secret key"
end) end)
let gen_keys cctxt name = module Seed = struct
let secret_key, public_key = Sodium.Sign.random_keypair () in
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 () -> Secret_key.add cctxt name secret_key >>= fun () ->
Public_key.add cctxt name public_key >>= fun () -> Public_key.add cctxt name public_key >>= fun () ->
Public_key_hash.add cctxt name (Ed25519.Public_key.hash 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
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 = let group =
{ Cli_entries.name = "keys" ; { Cli_entries.name = "keys" ;
@ -114,10 +146,8 @@ let commands () =
command ~group ~desc: "list all public key hashes and associated keys" command ~group ~desc: "list all public key hashes and associated keys"
(fixed [ "list" ; "known" ; "identities" ]) (fixed [ "list" ; "known" ; "identities" ])
(fun cctxt -> (fun cctxt ->
Public_key_hash.load cctxt >>= fun l -> list_keys cctxt >>= fun l ->
Lwt_list.iter_s (fun (name, pkh) -> Lwt_list.iter_s (fun (name, pkh, pkm, pks) ->
Public_key.mem cctxt name >>= fun pkm ->
Secret_key.mem cctxt name >>= fun pks ->
Public_key_hash.to_source cctxt pkh >>= fun v -> Public_key_hash.to_source cctxt pkh >>= fun v ->
cctxt.message "%s: %s%s%s" name v cctxt.message "%s: %s%s%s" name v
(if pkm then " (public key known)" else "") (if pkm then " (public key known)" else "")

View File

@ -14,6 +14,13 @@ module Public_key_hash :
module Public_key : Client_aliases.Alias with type t = Ed25519.Public_key.t 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 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: val get_key:
Client_commands.context -> Client_commands.context ->
Public_key_hash.t -> Public_key_hash.t ->
@ -23,4 +30,14 @@ val get_keys:
Client_commands.context -> Client_commands.context ->
( string * Public_key_hash.t * Public_key.t * Secret_key.t ) list Lwt.t ( 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 val commands: unit -> Client_commands.command list