Client: select commands depending on the network

This commit is contained in:
Benjamin Canou 2018-06-30 14:52:20 +02:00
parent 99e5cd3186
commit 6cf03d0f6b
11 changed files with 102 additions and 94 deletions

View File

@ -75,7 +75,7 @@ let sandbox () =
\ Do @{<warning>NOT@} use your fundraiser keys on this network.@,\
You should not see this message if you are not a developer.@]@\n@."
let check_version ctxt =
let check_network ctxt =
Shell_services.P2p.versions ctxt >>= function
| Error _ ->
default () ;
@ -98,12 +98,12 @@ let check_version ctxt =
default () ;
Lwt.return_none
let get_commands_for_version ctxt block protocol =
let get_commands_for_version ctxt network block protocol =
Shell_services.Blocks.protocols ctxt ~block () >>= function
| Ok { next_protocol = version } -> begin
match protocol with
| None ->
return (Some version, Client_commands.commands_for_version version)
return (Some version, Client_commands.commands_for_version version network)
| Some given_version -> begin
if not (Protocol_hash.equal version given_version) then
Format.eprintf
@ -112,7 +112,7 @@ let get_commands_for_version ctxt block protocol =
is not the one retrieved from the node (%a).@]@\n@."
Protocol_hash.pp_short given_version
Protocol_hash.pp_short version ;
return (Some version, Client_commands.commands_for_version given_version)
return (Some version, Client_commands.commands_for_version given_version network)
end
end
| Error errs -> begin
@ -125,18 +125,18 @@ let get_commands_for_version ctxt block protocol =
return (None, [])
end
| Some version ->
return (Some version, Client_commands.commands_for_version version)
return (Some version, Client_commands.commands_for_version version network)
end
let select_commands ctxt { block ; protocol } =
check_version ctxt >>= fun version ->
get_commands_for_version ctxt block protocol >>|? fun (_, commands_for_version) ->
check_network ctxt >>= fun network ->
get_commands_for_version ctxt network block protocol >>|? fun (_, commands_for_version) ->
Client_rpc_commands.commands @
List.map
(Clic.map_command
(fun (o : Client_context.full) -> (o :> Client_context.io_wallet)))
(Tezos_signer_backends.Ledger.commands () @
Client_keys_commands.commands version) @
Client_keys_commands.commands network) @
Client_helpers_commands.commands () @
commands_for_version

View File

@ -10,6 +10,7 @@
open Client_context
type command = full Clic.command
type network = [ `Betanet | `Alphanet | `Zeronet | `Sandbox ]
exception Version_not_found
@ -24,8 +25,9 @@ let get_versions () =
let register name commands =
let previous =
try Protocol_hash.Table.find versions name
with Not_found -> [] in
Protocol_hash.Table.replace versions name (commands @ previous)
with Not_found -> (fun (_network : network option) -> ([] : command list)) in
Protocol_hash.Table.replace versions name
(fun (network : network option) -> (commands network @ previous network))
let commands_for_version version =
try Protocol_hash.Table.find versions version

View File

@ -10,9 +10,10 @@
open Client_context
type command = full Clic.command
type network = [ `Betanet | `Alphanet | `Zeronet | `Sandbox ]
exception Version_not_found
val register: Protocol_hash.t -> command list -> unit
val commands_for_version: Protocol_hash.t -> command list
val get_versions: unit -> (Protocol_hash.t * (command list)) list
val register: Protocol_hash.t -> (network option -> command list) -> unit
val commands_for_version: Protocol_hash.t -> network option -> command list
val get_versions: unit -> (Protocol_hash.t * (network option -> command list)) list

View File

@ -279,30 +279,32 @@ let commands version : Client_context.io_wallet Clic.command list =
"Tezos address added: %a"
Signature.Public_key_hash.pp pkh >>= fun () ->
register_key cctxt ~force (pkh, pk_uri, sk_uri) ?public_key name) ;
command ~group ~desc: "Add a fundraiser secret key to the wallet."
(args1 (Secret_key.force_switch ()))
(prefix "import"
@@ prefixes [ "fundraiser" ; "secret" ; "key" ]
@@ Secret_key.fresh_alias_param
@@ stop)
(fun force name (cctxt : Client_context.io_wallet) ->
Secret_key.of_fresh cctxt force name >>=? fun name ->
input_fundraiser_params cctxt >>=? fun sk ->
Tezos_signer_backends.Encrypted.encrypt cctxt sk >>=? fun sk_uri ->
Client_keys.neuterize sk_uri >>=? fun pk_uri ->
begin
Public_key.find_opt cctxt name >>=? function
| None -> return_unit
| Some (pk_uri_found, _) ->
fail_unless (pk_uri = pk_uri_found || force)
(failure
"public and secret keys '%s' don't correspond, \
please don't use --force" name)
end >>=? fun () ->
Client_keys.public_key_hash pk_uri >>=? fun (pkh, _public_key) ->
register_key cctxt ~force (pkh, pk_uri, sk_uri) name) ;
] @
(if version <> (Some `Betanet) then [] else [
command ~group ~desc: "Add a fundraiser secret key to the wallet."
(args1 (Secret_key.force_switch ()))
(prefix "import"
@@ prefixes [ "fundraiser" ; "secret" ; "key" ]
@@ Secret_key.fresh_alias_param
@@ stop)
(fun force name (cctxt : Client_context.io_wallet) ->
Secret_key.of_fresh cctxt force name >>=? fun name ->
input_fundraiser_params cctxt >>=? fun sk ->
Tezos_signer_backends.Encrypted.encrypt cctxt sk >>=? fun sk_uri ->
Client_keys.neuterize sk_uri >>=? fun pk_uri ->
begin
Public_key.find_opt cctxt name >>=? function
| None -> return_unit
| Some (pk_uri_found, _) ->
fail_unless (pk_uri = pk_uri_found || force)
(failure
"public and secret keys '%s' don't correspond, \
please don't use --force" name)
end >>=? fun () ->
Client_keys.public_key_hash pk_uri >>=? fun (pkh, _public_key) ->
register_key cctxt ~force (pkh, pk_uri, sk_uri) name) ;
]) @
[
command ~group ~desc: "Add a public key to the wallet."
(args1 (Public_key.force_switch ()))
(prefix "import"

View File

@ -8,7 +8,7 @@
(**************************************************************************)
let () =
Client_commands.register Proto_alpha.hash @@
Client_commands.register Proto_alpha.hash @@ fun _network ->
List.map (Clic.map_command (new Proto_alpha.wrap_full)) @@
Delegate_commands.accuser_commands ()

View File

@ -8,7 +8,7 @@
(**************************************************************************)
let () =
Client_commands.register Proto_alpha.hash @@
Client_commands.register Proto_alpha.hash @@ fun _network ->
List.map (Clic.map_command (new Proto_alpha.wrap_full)) @@
Delegate_commands.delegate_commands ()

View File

@ -8,7 +8,7 @@
(**************************************************************************)
let () =
Client_commands.register Proto_alpha.hash @@
Client_commands.register Proto_alpha.hash @@ fun _network ->
List.map (Clic.map_command (new Proto_alpha.wrap_full)) @@
Delegate_commands.delegate_commands ()

View File

@ -8,8 +8,8 @@
(**************************************************************************)
let () =
Client_commands.register Proto_alpha.hash @@
Client_commands.register Proto_alpha.hash @@ fun network ->
List.map (Clic.map_command (new Proto_alpha.wrap_full)) @@
Client_proto_programs_commands.commands () @
Client_proto_contracts_commands.commands () @
Client_proto_context_commands.commands ()
Client_proto_context_commands.commands network ()

View File

@ -58,7 +58,7 @@ let binary_description =
{ Clic.name = "description" ;
title = "Binary Description" }
let commands () =
let commands version () =
let open Clic in
[
command ~group ~desc: "Access the timestamp of the block."
@ -342,55 +342,58 @@ let commands () =
~fee ~manager_sk:src_sk src_pk >>=? fun _res ->
return_unit
end;
command ~group ~desc:"Register and activate an Alphanet/Zeronet faucet account."
(args2
(Secret_key.force_switch ())
encrypted_switch)
(prefixes [ "activate" ; "account" ]
@@ Secret_key.fresh_alias_param
@@ prefixes [ "with" ]
@@ param ~name:"activation_key"
~desc:"Activate an Alphanet/Zeronet faucet account from the doanloaded JSON file."
file_parameter
@@ stop)
(fun (force, encrypted) name activation_key_file cctxt ->
Secret_key.of_fresh cctxt force name >>=? fun name ->
Lwt_utils_unix.Json.read_file activation_key_file >>=? fun json ->
match Data_encoding.Json.destruct
Client_proto_context.activation_key_encoding
json with
| exception (Data_encoding.Json.Cannot_destruct _ as exn) ->
Format.kasprintf (fun s -> failwith "%s" s)
"Invalid activation file: %a %a"
(fun ppf -> Data_encoding.Json.print_error ppf) exn
Data_encoding.Json.pp json
| key ->
activate_account cctxt
~chain:`Main ~block:cctxt#block ?confirmations:cctxt#confirmations
~encrypted ~force key name >>=? fun _res ->
return_unit
);
command ~group ~desc:"Activate a fundraiser account."
(args1 dry_run_switch)
(prefixes [ "activate" ; "fundraiser" ; "account" ]
@@ Public_key_hash.alias_param
@@ prefixes [ "with" ]
@@ param ~name:"code"
(Clic.parameter (fun _ctx code ->
protect (fun () ->
return (Blinded_public_key_hash.activation_code_of_hex code))))
~desc:"Activation code obtained from the Tezos foundation."
@@ stop)
(fun dry_run (name, _pkh) code cctxt ->
activate_existing_account cctxt ~chain:`Main
~block:cctxt#block ?confirmations:cctxt#confirmations
~dry_run
name code >>=? fun _res ->
return_unit
);
] @
(if version = (Some `Betanet) then [] else [
command ~group ~desc:"Register and activate an Alphanet/Zeronet faucet account."
(args2
(Secret_key.force_switch ())
encrypted_switch)
(prefixes [ "activate" ; "account" ]
@@ Secret_key.fresh_alias_param
@@ prefixes [ "with" ]
@@ param ~name:"activation_key"
~desc:"Activate an Alphanet/Zeronet faucet account from the downloaded JSON file."
file_parameter
@@ stop)
(fun (force, encrypted) name activation_key_file cctxt ->
Secret_key.of_fresh cctxt force name >>=? fun name ->
Lwt_utils_unix.Json.read_file activation_key_file >>=? fun json ->
match Data_encoding.Json.destruct
Client_proto_context.activation_key_encoding
json with
| exception (Data_encoding.Json.Cannot_destruct _ as exn) ->
Format.kasprintf (fun s -> failwith "%s" s)
"Invalid activation file: %a %a"
(fun ppf -> Data_encoding.Json.print_error ppf) exn
Data_encoding.Json.pp json
| key ->
activate_account cctxt
~chain:`Main ~block:cctxt#block ?confirmations:cctxt#confirmations
~encrypted ~force key name >>=? fun _res ->
return_unit
);
]) @
(if version <> Some `Betanet then [] else [
command ~group ~desc:"Activate a fundraiser account."
(args1 dry_run_switch)
(prefixes [ "activate" ; "fundraiser" ; "account" ]
@@ Public_key_hash.alias_param
@@ prefixes [ "with" ]
@@ param ~name:"code"
(Clic.parameter (fun _ctx code ->
protect (fun () ->
return (Blinded_public_key_hash.activation_code_of_hex code))))
~desc:"Activation code obtained from the Tezos foundation."
@@ stop)
(fun dry_run (name, _pkh) code cctxt ->
activate_existing_account cctxt ~chain:`Main
~block:cctxt#block ?confirmations:cctxt#confirmations
~dry_run
name code >>=? fun _res ->
return_unit
);
]) @
[
command ~desc:"Wait until an operation is included in a block"
(let int_param =
parameter

View File

@ -8,6 +8,6 @@
(**************************************************************************)
let () =
Client_commands.register Proto_alpha.hash @@
Client_commands.register Proto_alpha.hash @@ fun _network ->
List.map (Clic.map_command (new Proto_alpha.wrap_full)) @@
Delegate_commands.delegate_commands ()

View File

@ -101,5 +101,5 @@ let commands () =
]
let () =
Client_commands.register protocol @@
Client_commands.register protocol @@ fun _network ->
commands ()