From 1f662dd53bf82318cffeb7c4b17403e23c6c0db0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Sun, 27 May 2018 12:31:52 +0200 Subject: [PATCH] Signer: add proper documentation to signer --- src/lib_client_base/client_keys.ml | 13 ++++++- src/lib_client_base/client_keys.mli | 3 ++ src/lib_signer_backends/https.ml | 28 ++++++++++----- src/lib_signer_backends/remote.ml | 26 ++++++-------- src/lib_signer_backends/socket.ml | 40 ++++++++++++--------- src/lib_signer_services/signer_messages.ml | 13 ------- src/lib_signer_services/signer_messages.mli | 2 -- 7 files changed, 67 insertions(+), 58 deletions(-) diff --git a/src/lib_client_base/client_keys.ml b/src/lib_client_base/client_keys.ml index c7649c584..2768a8498 100644 --- a/src/lib_client_base/client_keys.ml +++ b/src/lib_client_base/client_keys.ml @@ -8,6 +8,7 @@ (**************************************************************************) type error += Unregistered_key_scheme of string +type error += Invalid_uri of Uri.t let () = register_error_kind `Permanent @@ -20,7 +21,17 @@ let () = Format.fprintf ppf "No matching plugin for key scheme %s" s) Data_encoding.(obj1 (req "value" string)) (function Unregistered_key_scheme s -> Some s | _ -> None) - (fun s -> Unregistered_key_scheme s) + (fun s -> Unregistered_key_scheme s) ; + register_error_kind `Permanent + ~id: "cli.key.invalid_uri" + ~title: "Invalid key uri" + ~description: "A key has been provided with an invalid uri." + ~pp: + (fun ppf s -> + Format.fprintf ppf "Cannot parse the key uri: %s" s) + Data_encoding.(obj1 (req "value" string)) + (function Invalid_uri s -> Some (Uri.to_string s) | _ -> None) + (fun s -> Invalid_uri (Uri.of_string s)) module Public_key_hash = Client_aliases.Alias (struct type t = Signature.Public_key_hash.t diff --git a/src/lib_client_base/client_keys.mli b/src/lib_client_base/client_keys.mli index 7aaaaa40a..48a12c63c 100644 --- a/src/lib_client_base/client_keys.mli +++ b/src/lib_client_base/client_keys.mli @@ -12,6 +12,9 @@ type pk_uri = private Uri.t type sk_uri = private Uri.t +type error += Unregistered_key_scheme of string +type error += Invalid_uri of Uri.t + module Public_key_hash : Client_aliases.Alias with type t = Signature.Public_key_hash.t module Public_key : diff --git a/src/lib_signer_backends/https.ml b/src/lib_signer_backends/https.ml index c9634258f..3e9cbd767 100644 --- a/src/lib_signer_backends/https.ml +++ b/src/lib_signer_backends/https.ml @@ -11,18 +11,28 @@ open Client_keys let scheme = "https" -let title = "..." +let title = + "Built-in tezos-signer using remote signer through hardcoded https requests." -let description = "..." +let description = + "Valid locators are of this form:\n\ + \ - https://host/tz1...\n\ + \ - https://host:port/path/to/service/tz1...\n" let parse uri = - let path = String.split '/' (Uri.path uri) in - match List.rev path with - | [] -> invalid_arg "..." - | key :: rev_path -> - Lwt.return (Signature.Public_key_hash.of_b58check key) >>=? fun key -> - return (Uri.with_path uri (String.concat "/" (List.rev rev_path)), - key) + (* extract `tz1..` from the last component of the path *) + assert (Uri.scheme uri = Some scheme) ; + let path = Uri.path uri in + let base, pkh = + match String.rindex_opt path '/' with + | None -> + Uri.with_path uri "", path + | Some i -> + let pkh = String.sub path i (String.length path - i) in + let path = String.sub path 0 i in + Uri.with_path uri path, pkh in + Lwt.return (Signature.Public_key_hash.of_b58check pkh) >>=? fun pkh -> + return (base, pkh) let public_key uri = parse (uri : pk_uri :> Uri.t) >>=? fun (base, pkh) -> diff --git a/src/lib_signer_backends/remote.ml b/src/lib_signer_backends/remote.ml index 66f4cdc75..af2522260 100644 --- a/src/lib_signer_backends/remote.ml +++ b/src/lib_signer_backends/remote.ml @@ -19,18 +19,9 @@ module Make(S : sig val default : Uri.t end) = struct "Built-in tezos-signer using remote wallet." let description = - "Valid locators are one of these two forms:\n\ - \ - unix [path to local signer socket] \n\ - \ - tcp [host] [port] \n\ - \ - https [host] [port] \n\ - All fields except the key can be of the form '$VAR', \ - in which case their value is taken from environment variable \ - VAR each time the key is accessed.\n\ - Not specifiyng fields sets them to $TEZOS_SIGNER_UNIX_PATH, \ - $TEZOS_SIGNER_TCP_HOST and $TEZOS_SIGNER_TCP_PORT, \ - $TEZOS_SIGNER_HTTPS_HOST and $TEZOS_SIGNER_HTTPS_PORT, \ - that get evaluated to default values '$HOME/.tezos-signer-socket', \ - localhost and 6732, and can be set later on." + "Valid locators are of this form: remote://tz1...\n\ + The key will be queried to current remote signer, which can be \ + configured with the `--remote-signer` or `-R` options" let get_remote () = match Uri.scheme S.default with @@ -42,10 +33,14 @@ module Make(S : sig val default : Uri.t end) = struct module Remote = (val get_remote () : SIGNER) let key = match Uri.scheme S.default with - | Some "unix" | Some "tcp" -> + | Some "unix" -> (fun uri -> let key = Uri.path uri in - Uri.add_query_param S.default ("key", [key])) + Uri.add_query_param' S.default ("pkh", key)) + | Some "tcp" -> + (fun uri -> + let key = Uri.path uri in + Uri.with_path S.default key) | Some "https" -> (fun uri -> let key = Uri.path uri in @@ -63,8 +58,7 @@ module Make(S : sig val default : Uri.t end) = struct (Client_keys.make_pk_uri (key (pk_uri : pk_uri :> Uri.t))) let neuterize sk_uri = - Remote.neuterize - (Client_keys.make_sk_uri (key (sk_uri : sk_uri :> Uri.t))) + return (Client_keys.make_pk_uri (sk_uri : sk_uri :> Uri.t)) let sign ?watermark sk_uri msg = Remote.sign diff --git a/src/lib_signer_backends/socket.ml b/src/lib_signer_backends/socket.ml index d9826a38f..38f4b04f7 100644 --- a/src/lib_signer_backends/socket.ml +++ b/src/lib_signer_backends/socket.ml @@ -38,13 +38,17 @@ module Unix = struct let scheme = "unix" - let title = "..." + let title = + "Built-in tezos-signer using remote signer through hardcoded unix socket." - let description = "..." + let description = + "Valid locators are of this form: unix:///path/to/socket?pkh=tz1..." let parse uri = - match Uri.get_query_param uri "key" with - | None -> invalid_arg "... FIXME ... B" + assert (Uri.scheme uri = Some scheme) ; + trace (Invalid_uri uri) @@ + match Uri.get_query_param uri "pkh" with + | None -> failwith "Missing the query parameter: 'pkh=tz1...'" | Some key -> Lwt.return (Signature.Public_key_hash.of_b58check key) >>=? fun key -> return (Lwt_utils_unix.Socket.Unix (Uri.path uri), key) @@ -70,22 +74,24 @@ module Tcp = struct let scheme = "tcp" - let title = "..." + let title = + "Built-in tezos-signer using remote signer through hardcoded tcp socket." - let description = "..." - - (* let init _cctxt = return () *) + let description = + "Valid locators are of this form: tcp://host:port/tz1..." let parse uri = - match Uri.get_query_param uri "key" with - | None -> invalid_arg "... FIXME ... C" - | Some key -> - Lwt.return (Signature.Public_key_hash.of_b58check key) >>=? fun key -> - match Uri.host uri, Uri.port uri with - | None, _ | _, None -> - invalid_arg "... FIXME ... C2" - | Some path, Some port -> - return (Lwt_utils_unix.Socket.Tcp (path, port), key) + assert (Uri.scheme uri = Some scheme) ; + trace (Invalid_uri uri) @@ + match Uri.host uri, Uri.port uri with + | None, _ -> + failwith "Missing host address" + | _, None -> + failwith "Missing host port" + | Some path, Some port -> + Lwt.return + (Signature.Public_key_hash.of_b58check (Uri.path uri)) >>=? fun pkh -> + return (Lwt_utils_unix.Socket.Tcp (path, port), pkh) let public_key uri = parse (uri : pk_uri :> Uri.t) >>=? fun (path, pkh) -> diff --git a/src/lib_signer_services/signer_messages.ml b/src/lib_signer_services/signer_messages.ml index 9c18e3efb..57ad5f305 100644 --- a/src/lib_signer_services/signer_messages.ml +++ b/src/lib_signer_services/signer_messages.ml @@ -7,19 +7,6 @@ (* *) (**************************************************************************) -type error += Unknown_alias_key of string - -let () = - register_error_kind `Permanent - ~id: "signer.unknown_alias_key" - ~title: "Unkwnon_alias_key" - ~description: "A remote key does not exists" - ~pp: (fun ppf s -> - Format.fprintf ppf "The key %s does not is not known on the remote signer" s) - Data_encoding.(obj1 (req "value" string)) - (function Unknown_alias_key s -> Some s | _ -> None) - (fun s -> Unknown_alias_key s) - module Sign = struct module Request = struct diff --git a/src/lib_signer_services/signer_messages.mli b/src/lib_signer_services/signer_messages.mli index f07324558..529b40f62 100644 --- a/src/lib_signer_services/signer_messages.mli +++ b/src/lib_signer_services/signer_messages.mli @@ -7,8 +7,6 @@ (* *) (**************************************************************************) -type error += Unknown_alias_key of string - module Sign : sig module Request : sig