2016-09-08 21:13:10 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Copyright (c) 2014 - 2016. *)
|
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
2016-11-14 18:55:24 +04:00
|
|
|
module Ed25519 = Environment.Ed25519
|
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
module Public_key_hash = Client_aliases.Alias (struct
|
|
|
|
type t = Ed25519.Public_key_hash.t
|
|
|
|
let encoding = Ed25519.Public_key_hash.encoding
|
2017-04-05 11:54:21 +04:00
|
|
|
let of_source _ s = Lwt.return (Ed25519.Public_key_hash.of_b58check s)
|
2017-04-05 03:02:10 +04:00
|
|
|
let to_source _ p = return (Ed25519.Public_key_hash.to_b58check p)
|
2016-09-08 21:13:10 +04:00
|
|
|
let name = "public key hash"
|
|
|
|
end)
|
|
|
|
|
|
|
|
module Public_key = Client_aliases.Alias (struct
|
2017-02-28 05:56:40 +04:00
|
|
|
type t = Ed25519.Public_key.t
|
|
|
|
let encoding = Ed25519.Public_key.encoding
|
2017-04-05 11:54:21 +04:00
|
|
|
let of_source _ s = Lwt.return (Ed25519.Public_key.of_b58check s)
|
2017-04-05 03:02:10 +04:00
|
|
|
let to_source _ p = return (Ed25519.Public_key.to_b58check p)
|
2016-09-08 21:13:10 +04:00
|
|
|
let name = "public key"
|
|
|
|
end)
|
|
|
|
|
|
|
|
module Secret_key = Client_aliases.Alias (struct
|
2017-02-28 05:56:40 +04:00
|
|
|
type t = Ed25519.Secret_key.t
|
|
|
|
let encoding = Ed25519.Secret_key.encoding
|
2017-04-05 11:54:21 +04:00
|
|
|
let of_source _ s = Lwt.return (Ed25519.Secret_key.of_b58check s)
|
2017-04-05 03:02:10 +04:00
|
|
|
let to_source _ p = return (Ed25519.Secret_key.to_b58check p)
|
2016-09-08 21:13:10 +04:00
|
|
|
let name = "secret key"
|
|
|
|
end)
|
|
|
|
|
2017-01-12 19:13:03 +04:00
|
|
|
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
|
2017-04-05 03:02:10 +04:00
|
|
|
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 () ->
|
|
|
|
cctxt.message
|
|
|
|
"I generated a brand new pair of keys under the name '%s'." name >>= fun () ->
|
2017-04-05 01:35:41 +04:00
|
|
|
return ()
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
let check_keys_consistency pk sk =
|
|
|
|
let message = MBytes.of_string "Voulez-vous coucher avec moi, ce soir ?" in
|
|
|
|
let signature = Ed25519.sign sk message in
|
2017-02-28 05:56:40 +04:00
|
|
|
Ed25519.Signature.check pk signature message
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let get_key cctxt pkh =
|
2017-04-05 03:02:10 +04:00
|
|
|
Public_key_hash.rev_find cctxt pkh >>=? function
|
2016-12-03 16:05:02 +04:00
|
|
|
| None -> cctxt.error "no keys for the source contract manager"
|
2016-09-08 21:13:10 +04:00
|
|
|
| Some n ->
|
2017-04-05 03:02:10 +04:00
|
|
|
Public_key.find cctxt n >>=? fun pk ->
|
|
|
|
Secret_key.find cctxt n >>=? fun sk ->
|
2016-09-08 21:13:10 +04:00
|
|
|
return (n, pk, sk)
|
|
|
|
|
2017-02-28 11:18:06 +04:00
|
|
|
let get_keys cctxt =
|
2017-04-05 03:02:10 +04:00
|
|
|
Secret_key.load cctxt >>=? fun sks ->
|
2017-04-11 00:58:36 +04:00
|
|
|
Lwt_list.filter_map_s
|
2017-04-05 03:02:10 +04:00
|
|
|
(fun (name, sk) ->
|
2017-04-11 00:58:36 +04:00
|
|
|
begin
|
2017-04-05 03:02:10 +04:00
|
|
|
Public_key.find cctxt name >>=? fun pk ->
|
|
|
|
Public_key_hash.find cctxt name >>=? fun pkh ->
|
2017-04-11 00:58:36 +04:00
|
|
|
return (name, pkh, pk, sk)
|
|
|
|
end >>= function
|
|
|
|
| Ok r -> Lwt.return (Some r)
|
|
|
|
| Error _ -> Lwt.return_none)
|
|
|
|
sks >>= fun keys ->
|
|
|
|
return keys
|
2017-02-28 11:18:06 +04:00
|
|
|
|
2017-01-12 19:13:03 +04:00
|
|
|
let list_keys cctxt =
|
2017-04-05 03:02:10 +04:00
|
|
|
Public_key_hash.load cctxt >>=? fun l ->
|
|
|
|
map_s
|
|
|
|
(fun (name, pkh) ->
|
|
|
|
Public_key.mem cctxt name >>=? fun pkm ->
|
|
|
|
Secret_key.mem cctxt name >>=? fun pks ->
|
|
|
|
return (name, pkh, pkm, pks))
|
2017-01-12 19:13:03 +04:00
|
|
|
l
|
2017-02-28 11:18:06 +04:00
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let group =
|
|
|
|
{ Cli_entries.name = "keys" ;
|
|
|
|
title = "Commands for managing cryptographic keys" }
|
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
let commands () =
|
|
|
|
let open Cli_entries in
|
2017-03-15 04:17:20 +04:00
|
|
|
let open Client_commands in
|
2017-04-05 03:02:10 +04:00
|
|
|
[
|
|
|
|
|
|
|
|
command ~group ~desc: "generate a pair of keys"
|
2016-09-08 21:13:10 +04:00
|
|
|
(prefixes [ "gen" ; "keys" ]
|
|
|
|
@@ Secret_key.fresh_alias_param
|
|
|
|
@@ stop)
|
2016-12-03 16:05:02 +04:00
|
|
|
(fun name cctxt -> gen_keys cctxt name) ;
|
2017-04-05 03:02:10 +04:00
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
command ~group ~desc: "add a secret key to the wallet"
|
2016-09-08 21:13:10 +04:00
|
|
|
(prefixes [ "add" ; "secret" ; "key" ]
|
|
|
|
@@ Secret_key.fresh_alias_param
|
|
|
|
@@ Secret_key.source_param
|
|
|
|
@@ stop)
|
2016-12-03 16:05:02 +04:00
|
|
|
(fun name sk cctxt ->
|
2017-04-05 03:02:10 +04:00
|
|
|
Public_key.find_opt cctxt name >>=? function
|
|
|
|
| None ->
|
|
|
|
failwith
|
|
|
|
"no public key named '%s', add it before adding the secret key"
|
|
|
|
name
|
|
|
|
| Some pk ->
|
|
|
|
fail_unless
|
|
|
|
(check_keys_consistency pk sk || cctxt.config.force)
|
|
|
|
(failure
|
|
|
|
"public and secret keys '%s' don't correspond, \
|
|
|
|
please don't use -force true" name) >>=? fun () ->
|
|
|
|
Secret_key.add cctxt name sk) ;
|
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
command ~group ~desc: "add a public key to the wallet"
|
2016-09-08 21:13:10 +04:00
|
|
|
(prefixes [ "add" ; "public" ; "key" ]
|
|
|
|
@@ Public_key.fresh_alias_param
|
|
|
|
@@ Public_key.source_param
|
|
|
|
@@ stop)
|
2016-12-03 16:05:02 +04:00
|
|
|
(fun name key cctxt ->
|
2017-04-05 03:02:10 +04:00
|
|
|
Public_key_hash.add cctxt
|
|
|
|
name (Ed25519.Public_key.hash key) >>=? fun () ->
|
|
|
|
Public_key.add cctxt name key) ;
|
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
command ~group ~desc: "add an ID a public key hash to the wallet"
|
2016-09-08 21:13:10 +04:00
|
|
|
(prefixes [ "add" ; "identity" ]
|
|
|
|
@@ Public_key_hash.fresh_alias_param
|
|
|
|
@@ Public_key_hash.source_param
|
|
|
|
@@ stop)
|
2017-04-05 03:02:10 +04:00
|
|
|
(fun name hash cctxt -> Public_key_hash.add cctxt name hash) ;
|
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
command ~group ~desc: "list all public key hashes and associated keys"
|
2016-09-08 21:13:10 +04:00
|
|
|
(fixed [ "list" ; "known" ; "identities" ])
|
2016-12-03 16:05:02 +04:00
|
|
|
(fun cctxt ->
|
2017-04-05 03:02:10 +04:00
|
|
|
list_keys cctxt >>=? fun l ->
|
|
|
|
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 "")
|
|
|
|
(if pks then " (secret key known)" else "") >>= fun () ->
|
|
|
|
return ())
|
|
|
|
l) ;
|
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
command ~group ~desc: "forget all keys"
|
2016-09-08 21:13:10 +04:00
|
|
|
(fixed [ "forget" ; "all" ; "keys" ])
|
2016-12-03 16:05:02 +04:00
|
|
|
(fun cctxt ->
|
2017-04-05 03:02:10 +04:00
|
|
|
fail_unless cctxt.config.force
|
|
|
|
(failure "this can only used with option -force true") >>=? fun () ->
|
|
|
|
Public_key.save cctxt [] >>=? fun () ->
|
|
|
|
Secret_key.save cctxt [] >>=? fun () ->
|
|
|
|
Public_key_hash.save cctxt []) ;
|
|
|
|
|
|
|
|
]
|