This commit is contained in:
parent
8456a873bb
commit
0258cfb694
3 changed files with 61 additions and 65 deletions
|
|
@ -1,5 +1,5 @@
|
|||
(executable
|
||||
(public_name offline)
|
||||
(name offline)
|
||||
(modules offline offline_bin)
|
||||
(modules offline offline_impl offline_bin)
|
||||
(libraries cmdliner bos fmt mirage-crypto ptime mte vif))
|
||||
|
|
|
|||
|
|
@ -1,67 +1,6 @@
|
|||
(* TODO
|
||||
- clean up cmdliner
|
||||
- setup: need public key in config file
|
||||
- check sigs *)
|
||||
(* doc: https://docs.taler.net/manpages/taler-exchange-offline.1.html *)
|
||||
|
||||
open Syntax
|
||||
|
||||
let read_file fname = Bos.OS.File.read (Fpath.v fname) |> unwrap_err_msg
|
||||
|
||||
let write_file fname content =
|
||||
Bos.OS.File.write (Fpath.v fname) content |> unwrap_err_msg
|
||||
|
||||
let base_url = Uri.of_string "http://localhost:3434/"
|
||||
|
||||
let management_keys_url =
|
||||
Uri.with_path base_url "/management/keys/" |> Uri.to_string
|
||||
|
||||
let download ~output =
|
||||
let open Bos in
|
||||
let uri = management_keys_url in
|
||||
OS.Cmd.run Cmd.(v "curl" % "-s" % "-o" % output % "-X" % "GET" % uri)
|
||||
|
||||
let upload ~input =
|
||||
let open Bos in
|
||||
let uri = management_keys_url in
|
||||
OS.Cmd.run
|
||||
Cmd.(
|
||||
v "curl"
|
||||
% "-i"
|
||||
% "-X"
|
||||
% "POST"
|
||||
% "-H"
|
||||
% "Content-Type: application/json"
|
||||
% "--data"
|
||||
% ("@" ^ input)
|
||||
% uri)
|
||||
|
||||
let setup ~output =
|
||||
let () = Mirage_crypto_rng_unix.use_default () in
|
||||
let priv, pub = Mirage_crypto_ec.Ed25519.generate () in
|
||||
Fmt.pr "generated master public key:@\n%s@."
|
||||
(Mirage_crypto_ec.Ed25519.pub_to_octets pub |> B32.encode);
|
||||
let priv_data = Mirage_crypto_ec.Ed25519.priv_to_octets priv in
|
||||
write_file output priv_data
|
||||
|
||||
let sign ~master_key ~input ~output =
|
||||
let open Crypto in
|
||||
let* input = read_file input in
|
||||
let* master_key = read_file master_key in
|
||||
let master_key = EddsaPrivateKey.of_octets master_key in
|
||||
let* future_key_response = Api.decode Api.FutureKeysResponse.jsont input in
|
||||
let* () = Offline_bin.verify_future_key_response future_key_response in
|
||||
let master_signatures =
|
||||
Offline_bin.make_master_signatures ~master_key future_key_response
|
||||
in
|
||||
let* s = Api.encode Api.MasterSignatures.jsont master_signatures in
|
||||
let* () = write_file output s in
|
||||
Ok ()
|
||||
|
||||
(* CLI *)
|
||||
|
||||
open Cmdliner
|
||||
open Cmdliner.Term.Syntax
|
||||
open Offline_impl
|
||||
|
||||
let default_master_offline_key_file = "data/master_offline_private_key"
|
||||
let default_response_file = "response.json"
|
||||
|
|
@ -95,7 +34,7 @@ let download_cmd =
|
|||
@@ Term.ret
|
||||
@@
|
||||
let+ output = output in
|
||||
download ~output |> unwrap_err_msg |> to_term_ret
|
||||
download ~output |> to_term_ret
|
||||
|
||||
let upload_cmd =
|
||||
let doc = "POST /management/keys/" in
|
||||
|
|
@ -109,7 +48,7 @@ let upload_cmd =
|
|||
@@ Term.ret
|
||||
@@
|
||||
let+ input = input in
|
||||
upload ~input |> unwrap_err_msg |> to_term_ret
|
||||
upload ~input |> to_term_ret
|
||||
|
||||
let sign_cmd =
|
||||
let doc = "Sign FutureKeysResponse." in
|
||||
|
|
|
|||
57
tools/offline_impl.ml
Normal file
57
tools/offline_impl.ml
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
(* TODO
|
||||
- check sigs *)
|
||||
open Syntax
|
||||
|
||||
let read_file fname = Bos.OS.File.read (Fpath.v fname) |> unwrap_err_msg
|
||||
|
||||
let write_file fname content =
|
||||
Bos.OS.File.write (Fpath.v fname) content |> unwrap_err_msg
|
||||
|
||||
let base_url = Uri.of_string "http://localhost:3434/"
|
||||
|
||||
let management_keys_url =
|
||||
Uri.with_path base_url "/management/keys/" |> Uri.to_string
|
||||
|
||||
let download ~output =
|
||||
let open Bos in
|
||||
let uri = management_keys_url in
|
||||
OS.Cmd.run Cmd.(v "curl" % "-s" % "-o" % output % "-X" % "GET" % uri)
|
||||
|> unwrap_err_msg
|
||||
|
||||
let upload ~input =
|
||||
let open Bos in
|
||||
let uri = management_keys_url in
|
||||
OS.Cmd.run
|
||||
Cmd.(
|
||||
v "curl"
|
||||
% "-i"
|
||||
% "-X"
|
||||
% "POST"
|
||||
% "-H"
|
||||
% "Content-Type: application/json"
|
||||
% "--data"
|
||||
% ("@" ^ input)
|
||||
% uri)
|
||||
|> unwrap_err_msg
|
||||
|
||||
let setup ~output =
|
||||
let () = Mirage_crypto_rng_unix.use_default () in
|
||||
let priv, pub = Mirage_crypto_ec.Ed25519.generate () in
|
||||
Fmt.pr "generated master public key:@\n%s@."
|
||||
(Mirage_crypto_ec.Ed25519.pub_to_octets pub |> B32.encode);
|
||||
let priv_data = Mirage_crypto_ec.Ed25519.priv_to_octets priv in
|
||||
write_file output priv_data
|
||||
|
||||
let sign ~master_key ~input ~output =
|
||||
let open Crypto in
|
||||
let* input = read_file input in
|
||||
let* master_key = read_file master_key in
|
||||
let master_key = EddsaPrivateKey.of_octets master_key in
|
||||
let* future_key_response = Api.decode Api.FutureKeysResponse.jsont input in
|
||||
let* () = Offline_bin.verify_future_key_response future_key_response in
|
||||
let master_signatures =
|
||||
Offline_bin.make_master_signatures ~master_key future_key_response
|
||||
in
|
||||
let* s = Api.encode Api.MasterSignatures.jsont master_signatures in
|
||||
let* () = write_file output s in
|
||||
Ok ()
|
||||
Loading…
Add table
Add a link
Reference in a new issue