Signer/Socket: fix some glitches
This commit is contained in:
parent
357a4827e8
commit
5e8e6347e2
@ -43,7 +43,6 @@ let run (cctxt : #Client_context.wallet) path ?magic_bytes ~require_auth =
|
||||
end ;
|
||||
loop ()
|
||||
in
|
||||
Lwt_unix.listen fd 10 ;
|
||||
begin
|
||||
match path with
|
||||
| Tcp (host, port) ->
|
||||
|
@ -57,7 +57,9 @@ module Make(N : sig val scheme : string end) = struct
|
||||
| None ->
|
||||
failwith "Invalid locator %a" Uri.pp_hum uri
|
||||
| Some i ->
|
||||
let pkh = String.sub path (i + 1) (String.length path - i - 1) in
|
||||
let pkh =
|
||||
try String.sub path (i + 1) (String.length path - i - 1)
|
||||
with _ -> "" in
|
||||
let path = String.sub path 0 i in
|
||||
return (Uri.with_path uri path, pkh)
|
||||
end >>=? fun (base, pkh) ->
|
||||
|
@ -23,19 +23,24 @@ module Make(P : sig
|
||||
| None -> msg
|
||||
| Some watermark ->
|
||||
MBytes.concat "" [ Signature.bytes_of_watermark watermark ; msg ] in
|
||||
Lwt_utils_unix.Socket.connect path >>=? fun conn ->
|
||||
Lwt_utils_unix.Socket.send
|
||||
conn Request.encoding Request.Authorized_keys >>=? fun () ->
|
||||
Lwt_utils_unix.Socket.recv conn
|
||||
Authorized_keys.Response.encoding >>=? fun authorized_keys ->
|
||||
begin match authorized_keys with
|
||||
| No_authentication -> return None
|
||||
| Authorized_keys authorized_keys ->
|
||||
P.authenticate authorized_keys
|
||||
(Sign.Request.to_sign ~pkh ~data:msg) >>=? fun signature ->
|
||||
return (Some signature)
|
||||
begin
|
||||
Lwt_utils_unix.Socket.connect path >>=? fun conn ->
|
||||
Lwt_utils_unix.Socket.send
|
||||
conn Request.encoding Request.Authorized_keys >>=? fun () ->
|
||||
Lwt_utils_unix.Socket.recv conn
|
||||
(result_encoding Authorized_keys.Response.encoding) >>=? fun authorized_keys ->
|
||||
Lwt.return authorized_keys >>=? fun authorized_keys ->
|
||||
Lwt_unix.close conn >>= fun () ->
|
||||
begin match authorized_keys with
|
||||
| No_authentication -> return None
|
||||
| Authorized_keys authorized_keys ->
|
||||
P.authenticate authorized_keys
|
||||
(Sign.Request.to_sign ~pkh ~data:msg) >>=? fun signature ->
|
||||
return (Some signature)
|
||||
end
|
||||
end >>=? fun signature ->
|
||||
let req = { Sign.Request.pkh ; data = msg ; signature } in
|
||||
Lwt_utils_unix.Socket.connect path >>=? fun conn ->
|
||||
Lwt_utils_unix.Socket.send
|
||||
conn Request.encoding (Request.Sign req) >>=? fun () ->
|
||||
Lwt_utils_unix.Socket.recv conn
|
||||
@ -61,7 +66,7 @@ module Make(P : sig
|
||||
|
||||
let description =
|
||||
"Valid locators are of the form\n\
|
||||
\ - unix:///path/to/socket?pkh=tz1..."
|
||||
\ - unix:/path/to/socket?pkh=tz1..."
|
||||
|
||||
let parse uri =
|
||||
assert (Uri.scheme uri = Some scheme) ;
|
||||
@ -109,8 +114,12 @@ module Make(P : sig
|
||||
| _, None ->
|
||||
failwith "Missing host port"
|
||||
| Some path, Some port ->
|
||||
let pkh = Uri.path uri in
|
||||
let pkh =
|
||||
try String.(sub pkh 1 (length pkh - 1))
|
||||
with _ -> "" in
|
||||
Lwt.return
|
||||
(Signature.Public_key_hash.of_b58check (Uri.path uri)) >>=? fun pkh ->
|
||||
(Signature.Public_key_hash.of_b58check pkh) >>=? fun pkh ->
|
||||
return (Lwt_utils_unix.Socket.Tcp (path, port), pkh)
|
||||
|
||||
let public_key uri =
|
||||
|
@ -19,7 +19,7 @@ module Sign = struct
|
||||
|
||||
let to_sign ~pkh ~data =
|
||||
MBytes.concat ""
|
||||
[ MBytes.of_hex (`Hex "04") ;
|
||||
[ MBytes.of_string "\x04" ;
|
||||
Signature.Public_key_hash.to_bytes pkh ;
|
||||
data ]
|
||||
|
||||
|
@ -285,7 +285,7 @@ module Socket = struct
|
||||
| Tcp (host, port) ->
|
||||
get_addrs host >>=? fun addrs ->
|
||||
let rec try_connect = function
|
||||
| [] -> failwith "..."
|
||||
| [] -> failwith "could not resolve host '%s'" host
|
||||
| addr :: addrs ->
|
||||
Lwt.catch
|
||||
(fun () ->
|
||||
@ -307,7 +307,7 @@ module Socket = struct
|
||||
| Tcp (host, port) ->
|
||||
get_addrs host >>=? fun addrs ->
|
||||
let rec try_bind = function
|
||||
| [] -> failwith "..."
|
||||
| [] -> failwith "could not resolve host '%s'" host
|
||||
| addr :: addrs ->
|
||||
Lwt.catch
|
||||
(fun () ->
|
||||
@ -328,18 +328,18 @@ module Socket = struct
|
||||
register_error_kind `Permanent
|
||||
~id: "signer.encoding_error"
|
||||
~title: "Encoding_error"
|
||||
~description: "Error while encoding a request to the remote signer"
|
||||
~description: "Error while encoding a remote signer message"
|
||||
~pp: (fun ppf () ->
|
||||
Format.fprintf ppf "Could not encode a request to the remote signer")
|
||||
Format.fprintf ppf "Could not encode a remote signer message")
|
||||
Data_encoding.empty
|
||||
(function Encoding_error -> Some () | _ -> None)
|
||||
(fun () -> Encoding_error) ;
|
||||
register_error_kind `Permanent
|
||||
~id: "signer.decoding_error"
|
||||
~title: "Decoding_error"
|
||||
~description: "Error while decoding a request to the remote signer"
|
||||
~description: "Error while decoding a remote signer message"
|
||||
~pp: (fun ppf () ->
|
||||
Format.fprintf ppf "Could not decode a request to the remote signer")
|
||||
Format.fprintf ppf "Could not decode a remote signer message")
|
||||
Data_encoding.empty
|
||||
(function Decoding_error -> Some () | _ -> None)
|
||||
(fun () -> Decoding_error)
|
||||
|
Loading…
Reference in New Issue
Block a user