Client: fix check signature command

This commit is contained in:
Benjamin Canou 2018-06-20 17:12:19 +02:00
parent 4f3b591e63
commit 76970ffe4d

View File

@ -65,9 +65,18 @@ let commands () =
Clic.parameter (fun _ data -> Clic.parameter (fun _ data ->
Lwt.return (Micheline_parser.no_parsing_error Lwt.return (Micheline_parser.no_parsing_error
@@ Michelson_v1_parser.parse_expression data)) in @@ Michelson_v1_parser.parse_expression data)) in
let hash_parameter = let bytes_parameter ~name ~desc =
Clic.parameter Clic.param ~name ~desc
(fun _cctxt hash -> return @@ MBytes.of_string hash) in (parameter (fun (_cctxt : full) s ->
try
if String.length s < 2
|| s.[0] <> '0' || s.[1] <> 'x' then
raise Exit
else
return (MBytes.of_hex (`Hex (String.sub s 2 (String.length s - 2))))
with _ ->
failwith "Invalid bytes, expecting hexadecimal \
notation (e.g. 0x1234abcd)" )) in
let signature_parameter = let signature_parameter =
Clic.parameter Clic.parameter
(fun _cctxt s -> (fun _cctxt s ->
@ -237,17 +246,7 @@ let commands () =
`CHECK_SIGNATURE`." `CHECK_SIGNATURE`."
no_options no_options
(prefixes [ "sign" ; "bytes" ] (prefixes [ "sign" ; "bytes" ]
@@ Clic.param ~name:"data" ~desc:"the raw data to sign" @@ bytes_parameter ~name:"data" ~desc:"the raw data to sign"
(parameter (fun (_cctxt : full) s ->
try
if String.length s < 2
|| s.[0] <> '0' || s.[1] <> 'x' then
raise Exit
else
return (MBytes.of_hex (`Hex (String.sub s 2 (String.length s - 2))))
with _ ->
failwith "Invalid bytes, expecting hexadecimal \
notation (e.g. 0x1234abcd)" ))
@@ prefixes [ "for" ] @@ prefixes [ "for" ]
@@ Client_keys.Secret_key.source_param @@ Client_keys.Secret_key.source_param
@@ stop) @@ stop)
@ -257,11 +256,11 @@ let commands () =
return ()) ; return ()) ;
command ~group command ~group
~desc: "Ask the node to check the signature of a hashed expression." ~desc: "Check the signature of a byte sequence as per Michelson \
instruction `CHECK_SIGNATURE`."
(args1 (switch ~doc:"Use only exit codes" ~short:'q' ~long:"quiet" ())) (args1 (switch ~doc:"Use only exit codes" ~short:'q' ~long:"quiet" ()))
(prefixes [ "check" ; "that" ] (prefixes [ "check" ; "that" ]
@@ Clic.param ~name:"hash" ~desc:"the hashed data" @@ bytes_parameter ~name:"bytes" ~desc:"the signed data"
hash_parameter
@@ prefixes [ "was" ; "signed" ; "by" ] @@ prefixes [ "was" ; "signed" ; "by" ]
@@ Client_keys.Public_key.alias_param @@ Client_keys.Public_key.alias_param
~name:"key" ~name:"key"
@ -269,18 +268,15 @@ let commands () =
@@ Clic.param ~name:"signature" ~desc:"the signature to check" @@ Clic.param ~name:"signature" ~desc:"the signature to check"
signature_parameter signature_parameter
@@ stop) @@ stop)
(fun quiet hashed (_, (key_locator, _)) signature (cctxt : #Proto_alpha.full) -> (fun quiet bytes (_, (key_locator, _)) signature (cctxt : #Proto_alpha.full) ->
Client_keys.check key_locator signature hashed >>=? fun res -> Client_keys.check key_locator signature bytes >>=? function
begin | false -> cctxt#error "invalid signature"
if quiet | true ->
then if res if quiet then
then return () return ()
else failwith "Not signed" else
else begin if res cctxt#message "Signature check successfull." >>= fun () ->
then cctxt#message "Signed with key" return ()
else cctxt#message "Not signed with key"
end >>= return
end
) ; ) ;
] ]