2016-09-08 21:13:10 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Copyright (c) 2014 - 2016. *)
|
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
2016-11-14 18:55:24 +04:00
|
|
|
module Ed25519 = Environment.Ed25519
|
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
module RawContractAlias = Client_aliases.Alias (struct
|
|
|
|
type t = Contract.t
|
|
|
|
let encoding = Contract.encoding
|
2016-12-03 16:05:02 +04:00
|
|
|
let of_source _ s =
|
2017-02-19 21:22:32 +04:00
|
|
|
match Contract.of_b58check s with
|
2016-09-08 21:13:10 +04:00
|
|
|
| Error _ -> Lwt.fail (Failure "bad contract notation")
|
|
|
|
| Ok s -> Lwt.return s
|
2016-12-03 16:05:02 +04:00
|
|
|
let to_source _ s =
|
2017-02-19 21:22:32 +04:00
|
|
|
Lwt.return (Contract.to_b58check s)
|
2016-09-08 21:13:10 +04:00
|
|
|
let name = "contract"
|
|
|
|
end)
|
|
|
|
|
|
|
|
module ContractAlias = struct
|
2016-12-03 16:05:02 +04:00
|
|
|
let find cctxt s =
|
|
|
|
RawContractAlias.find_opt cctxt s >>= function
|
2016-09-08 21:13:10 +04:00
|
|
|
| Some v -> Lwt.return (s, v)
|
|
|
|
| None ->
|
2016-12-03 16:05:02 +04:00
|
|
|
Client_keys.Public_key_hash.find_opt cctxt s >>= function
|
2016-09-08 21:13:10 +04:00
|
|
|
| Some v ->
|
|
|
|
Lwt.return (s, Contract.default_contract v)
|
|
|
|
| None ->
|
2016-12-03 16:05:02 +04:00
|
|
|
cctxt.error
|
2016-09-08 21:13:10 +04:00
|
|
|
"no contract alias nor key alias names %s" s
|
2016-12-03 16:05:02 +04:00
|
|
|
let find_key cctxt name =
|
|
|
|
Client_keys.Public_key_hash.find cctxt name >>= fun v ->
|
2016-09-08 21:13:10 +04:00
|
|
|
Lwt.return (name, Contract.default_contract v)
|
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let rev_find cctxt c =
|
2016-09-08 21:13:10 +04:00
|
|
|
match Contract.is_default c with
|
|
|
|
| Some hash -> begin
|
2016-12-03 16:05:02 +04:00
|
|
|
Client_keys.Public_key_hash.rev_find cctxt hash >>= function
|
2016-09-08 21:13:10 +04:00
|
|
|
| Some name -> Lwt.return (Some ("key:" ^ name))
|
|
|
|
| None -> Lwt.return_none
|
|
|
|
end
|
2016-12-03 16:05:02 +04:00
|
|
|
| None -> RawContractAlias.rev_find cctxt c
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let get_contract cctxt s =
|
2016-09-08 21:13:10 +04:00
|
|
|
match Utils.split ~limit:1 ':' s with
|
|
|
|
| [ "key" ; key ]->
|
2016-12-03 16:05:02 +04:00
|
|
|
find_key cctxt key
|
|
|
|
| _ -> find cctxt s
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2016-11-22 17:23:40 +04:00
|
|
|
let alias_param ?(name = "name") ?(desc = "existing contract alias") next =
|
|
|
|
let desc =
|
|
|
|
desc ^ "\n"
|
|
|
|
^ "can be an contract alias or a key alias (autodetected in this order)\n\
|
|
|
|
use 'key:name' to force the later" in
|
|
|
|
Cli_entries.param ~name ~desc get_contract next
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2016-11-22 17:23:40 +04:00
|
|
|
let destination_param ?(name = "dst") ?(desc = "destination contract") next =
|
|
|
|
let desc =
|
|
|
|
desc ^ "\n"
|
2016-11-22 20:33:17 +04:00
|
|
|
^ "can be an alias, a key alias, or a literal (autodetected in this order)\n\
|
|
|
|
use 'text:literal', 'alias:name', 'key:name' to force" in
|
2016-11-22 17:23:40 +04:00
|
|
|
Cli_entries.param ~name ~desc
|
2016-12-03 16:05:02 +04:00
|
|
|
(fun cctxt s ->
|
2016-11-22 17:23:40 +04:00
|
|
|
match Utils.split ~limit:1 ':' s with
|
|
|
|
| [ "alias" ; alias ]->
|
2016-12-03 16:05:02 +04:00
|
|
|
find cctxt alias
|
2016-11-22 17:23:40 +04:00
|
|
|
| [ "key" ; text ] ->
|
2016-12-03 16:05:02 +04:00
|
|
|
Client_keys.Public_key_hash.find cctxt text >>= fun v ->
|
2016-11-22 17:23:40 +04:00
|
|
|
Lwt.return (s, Contract.default_contract v)
|
|
|
|
| _ ->
|
|
|
|
Lwt.catch
|
2016-12-03 16:05:02 +04:00
|
|
|
(fun () -> find cctxt s)
|
2016-11-22 17:23:40 +04:00
|
|
|
(fun _ ->
|
2017-02-19 21:22:32 +04:00
|
|
|
match Contract.of_b58check s with
|
2016-11-22 17:23:40 +04:00
|
|
|
| Error _ -> Lwt.fail (Failure "bad contract notation")
|
|
|
|
| Ok v -> Lwt.return (s, v)))
|
|
|
|
next
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let name cctxt contract =
|
|
|
|
rev_find cctxt contract >|= function
|
2017-02-19 21:22:32 +04:00
|
|
|
| None -> Contract.to_b58check contract
|
2016-09-08 21:13:10 +04:00
|
|
|
| Some name -> name
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2017-03-15 04:20:25 +04:00
|
|
|
let list_contracts cctxt =
|
|
|
|
(* List contracts *)
|
|
|
|
RawContractAlias.load cctxt >>= fun raw_contracts ->
|
|
|
|
Lwt_list.map_s (fun (n, v) ->
|
|
|
|
Lwt.return ("", n, v))
|
|
|
|
raw_contracts >>= fun contracts ->
|
|
|
|
Client_keys.Public_key_hash.load cctxt >>= fun keys ->
|
|
|
|
(* List accounts (default contracts of identities) *)
|
|
|
|
Lwt_list.map_s (fun (n, v) ->
|
|
|
|
RawContractAlias.mem cctxt n >>= fun mem ->
|
|
|
|
let p = if mem then "key:" else "" in
|
|
|
|
let v' = Contract.default_contract v in
|
|
|
|
Lwt.return (p, n, v'))
|
|
|
|
keys >>= fun accounts ->
|
|
|
|
Lwt.return (contracts @ accounts)
|
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let get_manager cctxt block source =
|
2016-09-08 21:13:10 +04:00
|
|
|
match Contract.is_default source with
|
|
|
|
| Some hash -> return hash
|
2016-12-03 16:05:02 +04:00
|
|
|
| None -> Client_proto_rpcs.Context.Contract.manager cctxt block source
|
2016-09-08 21:13:10 +04:00
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let get_delegate cctxt block source =
|
2016-09-08 21:13:10 +04:00
|
|
|
let open Client_keys in
|
|
|
|
match Contract.is_default source with
|
|
|
|
| Some hash -> return hash
|
|
|
|
| None ->
|
2016-12-03 16:05:02 +04:00
|
|
|
Client_proto_rpcs.Context.Contract.delegate cctxt block source >>=? function
|
2016-09-08 21:13:10 +04:00
|
|
|
| Some delegate -> return delegate
|
2016-12-03 16:05:02 +04:00
|
|
|
| None -> Client_proto_rpcs.Context.Contract.manager cctxt block source
|
2016-09-08 21:13:10 +04:00
|
|
|
|
|
|
|
let may_check_key sourcePubKey sourcePubKeyHash =
|
|
|
|
match sourcePubKey with
|
|
|
|
| Some sourcePubKey ->
|
2017-02-28 05:56:40 +04:00
|
|
|
if not (Ed25519.Public_key_hash.equal (Ed25519.Public_key.hash sourcePubKey) sourcePubKeyHash)
|
2016-09-08 21:13:10 +04:00
|
|
|
then
|
|
|
|
failwith "Invalid public key in `client_proto_endorsement`"
|
|
|
|
else
|
|
|
|
return ()
|
|
|
|
| None -> return ()
|
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let check_public_key cctxt block ?src_pk src_pk_hash =
|
|
|
|
Client_proto_rpcs.Context.Key.get cctxt block src_pk_hash >>= function
|
2016-09-08 21:13:10 +04:00
|
|
|
| Error errors ->
|
|
|
|
begin
|
|
|
|
match src_pk with
|
|
|
|
| None ->
|
|
|
|
let exn = Client_proto_rpcs.string_of_errors errors in
|
|
|
|
failwith "Unknown public key\n%s" exn
|
|
|
|
| Some key ->
|
|
|
|
may_check_key src_pk src_pk_hash >>=? fun () ->
|
|
|
|
return (Some key)
|
|
|
|
end
|
|
|
|
| Ok _ -> return None
|
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let group =
|
|
|
|
{ Cli_entries.name = "contracts" ;
|
|
|
|
title = "Commands for managing the record of known contracts" }
|
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
let commands () =
|
|
|
|
let open Cli_entries in
|
2017-03-15 04:17:20 +04:00
|
|
|
let open Client_commands in
|
2016-09-08 21:13:10 +04:00
|
|
|
[
|
2016-12-03 16:05:02 +04:00
|
|
|
command ~group ~desc: "add a contract to the wallet"
|
2016-09-08 21:13:10 +04:00
|
|
|
(prefixes [ "remember" ; "contract" ]
|
|
|
|
@@ RawContractAlias.fresh_alias_param
|
|
|
|
@@ RawContractAlias.source_param
|
|
|
|
@@ stop)
|
2016-12-03 16:05:02 +04:00
|
|
|
(fun name hash cctxt -> RawContractAlias.add cctxt name hash) ;
|
|
|
|
command ~group ~desc: "remove a contract from the wallet"
|
2016-09-08 21:13:10 +04:00
|
|
|
(prefixes [ "forget" ; "contract" ]
|
|
|
|
@@ RawContractAlias.alias_param
|
|
|
|
@@ stop)
|
2016-12-03 16:05:02 +04:00
|
|
|
(fun (name, _) cctxt -> RawContractAlias.del cctxt name) ;
|
|
|
|
command ~group ~desc: "lists all known contracts"
|
2016-09-08 21:13:10 +04:00
|
|
|
(fixed [ "list" ; "known" ; "contracts" ])
|
2016-12-03 16:05:02 +04:00
|
|
|
(fun cctxt ->
|
2017-03-15 04:20:25 +04:00
|
|
|
list_contracts cctxt >>= fun contracts ->
|
|
|
|
Lwt_list.iter_s (fun (prefix, alias, contract) ->
|
|
|
|
cctxt.message "%s%s: %s" prefix alias
|
|
|
|
(Contract.to_b58check contract))
|
|
|
|
contracts) ;
|
2016-12-03 16:05:02 +04:00
|
|
|
command ~group ~desc: "forget all known contracts"
|
2016-09-08 21:13:10 +04:00
|
|
|
(fixed [ "forget" ; "all" ; "contracts" ])
|
2016-12-03 16:05:02 +04:00
|
|
|
(fun cctxt ->
|
2017-03-15 04:17:20 +04:00
|
|
|
if not cctxt.config.force then
|
2016-12-03 16:05:02 +04:00
|
|
|
cctxt.Client_commands.error "this can only used with option -force true"
|
2016-09-08 21:13:10 +04:00
|
|
|
else
|
2016-12-03 16:05:02 +04:00
|
|
|
RawContractAlias.save cctxt []) ;
|
|
|
|
command ~group ~desc: "display a contract from the wallet"
|
2016-09-08 21:13:10 +04:00
|
|
|
(prefixes [ "show" ; "known" ; "contract" ]
|
|
|
|
@@ RawContractAlias.alias_param
|
|
|
|
@@ stop)
|
2016-12-03 16:05:02 +04:00
|
|
|
(fun (_, contract) cctxt ->
|
|
|
|
cctxt.message "%a\n%!" Contract.pp contract) ;
|
2016-09-08 21:13:10 +04:00
|
|
|
]
|