From f644d8f332c3bab0ddc3ca2a684f72c3e14e39f3 Mon Sep 17 00:00:00 2001 From: Milo Davis Date: Mon, 9 Apr 2018 10:13:19 -0400 Subject: [PATCH] Client: adds check signature command --- src/lib_client_base/client_keys.ml | 4 +++ src/lib_client_base/client_keys.mli | 5 +++ .../client_proto_programs_commands.ml | 36 +++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/src/lib_client_base/client_keys.ml b/src/lib_client_base/client_keys.ml index 4d9fe3605..c7649c584 100644 --- a/src/lib_client_base/client_keys.ml +++ b/src/lib_client_base/client_keys.ml @@ -134,6 +134,10 @@ let append ?watermark loc buf = sign ?watermark loc buf >>|? fun signature -> Signature.concat buf signature +let check ?watermark pk_uri signature buf = + public_key pk_uri >>=? fun pk -> + return (Signature.check ?watermark pk signature buf) + let register_key cctxt ?(force=false) (public_key_hash, pk_uri, sk_uri) name = Public_key.add ~force cctxt name pk_uri >>=? fun () -> Secret_key.add ~force cctxt name sk_uri >>=? fun () -> diff --git a/src/lib_client_base/client_keys.mli b/src/lib_client_base/client_keys.mli index be7b0a840..7aaaaa40a 100644 --- a/src/lib_client_base/client_keys.mli +++ b/src/lib_client_base/client_keys.mli @@ -48,6 +48,7 @@ module type SIGNER = sig sk_uri -> MBytes.t -> Signature.t tzresult Lwt.t (** [sign ?watermark sk data] is signature obtained by signing [data] with [sk]. *) + end val register_signer : (module SIGNER) -> unit @@ -70,6 +71,10 @@ val append : ?watermark:Signature.watermark -> sk_uri -> MBytes.t -> MBytes.t tzresult Lwt.t +val check : + ?watermark:Signature.watermark -> + pk_uri -> Signature.t -> MBytes.t -> bool tzresult Lwt.t + val register_key : #Client_context.wallet -> ?force:bool -> diff --git a/src/proto_alpha/lib_client_commands/client_proto_programs_commands.ml b/src/proto_alpha/lib_client_commands/client_proto_programs_commands.ml index 25c1ae9d1..25c98af99 100644 --- a/src/proto_alpha/lib_client_commands/client_proto_programs_commands.ml +++ b/src/proto_alpha/lib_client_commands/client_proto_programs_commands.ml @@ -62,6 +62,15 @@ let commands () = Clic.parameter (fun _ data -> Lwt.return (Micheline_parser.no_parsing_error @@ Michelson_v1_parser.parse_expression data)) in + let hash_parameter = + Clic.parameter + (fun _cctxt hash -> return @@ MBytes.of_string hash) in + let signature_parameter = + Clic.parameter + (fun _cctxt s -> + match Signature.of_b58check_opt s with + | Some s -> return s + | None -> failwith "Not given a valid signature") in [ command ~group ~desc: "Lists all programs in the library." @@ -237,4 +246,31 @@ let commands () = cctxt#error "ill-formed data" end >>= return) ; + command ~group + ~desc: "Ask the node to check the signature of a hashed expression." + (args1 (switch ~doc:"Use only exit codes" ~short:'q' ~long:"quiet" ())) + (prefixes [ "check" ; "that" ] + @@ Clic.param ~name:"hash" ~desc:"the hashed data" + hash_parameter + @@ prefixes [ "was" ; "signed" ; "by" ] + @@ Client_keys.Public_key.alias_param + ~name:"key" + @@ prefixes [ "to" ; "produce" ] + @@ Clic.param ~name:"signature" ~desc:"the signature to check" + signature_parameter + @@ stop) + (fun quiet hashed (_, key_locator) signature (cctxt : #Proto_alpha.full) -> + Client_keys.check key_locator signature hashed >>=? fun res -> + begin + if quiet + then if res + then return () + else failwith "Not signed" + else begin if res + then cctxt#message "Signed with key" + else cctxt#message "Not signed with key" + end >>= return + end + ) ; + ]