Client: refactor data hashing and signing commands

This commit is contained in:
Benjamin Canou 2018-06-15 00:47:02 +02:00
parent 7b5e029537
commit 29a33aedd3
6 changed files with 54 additions and 76 deletions

View File

@ -230,8 +230,8 @@ assert_storage $contract_dir/first.tz '111' '{ 4 }' '4'
# Hash input string # Hash input string
# Test assumed to be correct -- hash is based on encoding of AST # Test assumed to be correct -- hash is based on encoding of AST
assert_storage $contract_dir/hash_string.tz '0x00' '"abcdefg"' '0xc8e816fa5921f08d25ead933aedc189a7b0abfa97c649a8d2eb9e0f323f7909e' assert_storage $contract_dir/hash_string.tz '0x00' '"abcdefg"' '0x46fdbcb4ea4eadad5615cdaa17d67f783e01e21149ce2b27de497600b4cd8f4e'
assert_storage $contract_dir/hash_string.tz '0x00' '"12345"' '0x4fc401c158dad6482d96f45bb2dc10bc445b1ff127a485814d735e5c3dcb36ec' assert_storage $contract_dir/hash_string.tz '0x00' '"12345"' '0xb4c26c20de52a4eaf0d8a340db47ad8cb1e74049570859c9a9a3952b204c772f'
# Test ASSERT # Test ASSERT
assert_storage $contract_dir/assert.tz Unit True Unit assert_storage $contract_dir/assert.tz Unit True Unit
@ -315,11 +315,11 @@ assert_storage $contract_dir/map_caddaadr.tz \
# Did the given key sign the string? (key is bootstrap1) # Did the given key sign the string? (key is bootstrap1)
assert_success $client run script $contract_dir/check_signature.tz \ assert_success $client run script $contract_dir/check_signature.tz \
on storage '(Pair "edsigtursTM9zynxFfyiLyTLuVh369H7Cd1nkH63dyc3jKBoujWeQRE9hZFRhqgTrY3BzdZenDc3D6v7gZEShpdPDMQ7YswgEL3" "hello")' \ on storage '(Pair "edsigthTzJ8X7MPmNeEwybRAvdxS1pupqcM5Mk4uCuyZAe7uEk68YpuGDeViW8wSXMrCi5CwoNgqs8V2w8ayB5dMJzrYCHhD8C7" "hello")' \
and input '"edpkuBknW28nW72KG6RoHtYW7p12T6GKc7nAbwYX5m8Wd9sDVC9yav"' and input '"edpkuBknW28nW72KG6RoHtYW7p12T6GKc7nAbwYX5m8Wd9sDVC9yav"'
assert_fails $client run script $contract_dir/check_signature.tz \ assert_fails $client run script $contract_dir/check_signature.tz \
on storage '(Pair "edsigtursTM9zynxFfyiLyTLuVh369H7Cd1nkH63dyc3jKBoujWeQRE9hZFRhqgTrY3BzdZenDc3D6v7gZEShpdPDMQ7YswgEL3" "abcd")' \ on storage '(Pair "edsigthTzJ8X7MPmNeEwybRAvdxS1pupqcM5Mk4uCuyZAe7uEk68YpuGDeViW8wSXMrCi5CwoNgqs8V2w8ayB5dMJzrYCHhD8C7" "abcd")' \
and input '"edpkuBknW28nW72KG6RoHtYW7p12T6GKc7nAbwYX5m8Wd9sDVC9yav"' and input '"edpkuBknW28nW72KG6RoHtYW7p12T6GKc7nAbwYX5m8Wd9sDVC9yav"'
@ -449,7 +449,7 @@ assert_fails $client typecheck data '{ "A" ; "B" ; "B" }' against type '(set str
# Test hash consistency between Michelson and the CLI # Test hash consistency between Michelson and the CLI
hash_result=`$client hash data '(Pair 22220000000 (Pair "2017-12-13T04:49:00Z" 034))' \ hash_result=`$client hash data '(Pair 22220000000 (Pair "2017-12-13T04:49:00Z" 034))' \
of type '(pair mutez (pair timestamp int))' | grep 0x | sed 's/.*: *//'` of type '(pair mutez (pair timestamp int))' | grep Blake2b | sed 's/.*: *//'`
assert_storage $contract_dir/hash_consistency_checker.tz '0x00' \ assert_storage $contract_dir/hash_consistency_checker.tz '0x00' \
'(Pair 22220000000 (Pair "2017-12-13T04:49:00Z" 034))' "$hash_result" '(Pair 22220000000 (Pair "2017-12-13T04:49:00Z" 034))' "$hash_result"

View File

@ -115,19 +115,6 @@ let trace
(chain, block) (chain, block)
program.expanded (storage.expanded, input.expanded, amount, contract) program.expanded (storage.expanded, input.expanded, amount, contract)
let hash_and_sign
cctxt
?(chain = `Main)
block
?gas
(data : Michelson_v1_parser.parsed)
(typ : Michelson_v1_parser.parsed)
sk =
Alpha_services.Helpers.Scripts.hash_data
cctxt (chain, block) (data.expanded, typ.expanded, gas) >>=? fun (hash, gas) ->
Client_keys.sign cctxt sk (Script_expr_hash.to_bytes hash) >>=? fun signature ->
return (hash, signature, gas)
let typecheck_data let typecheck_data
cctxt cctxt
?(chain = `Main) ?(chain = `Main)

View File

@ -61,16 +61,6 @@ val print_trace_result :
Contract.big_map_diff option) Contract.big_map_diff option)
tzresult -> unit tzresult Lwt.t tzresult -> unit tzresult Lwt.t
val hash_and_sign :
#Proto_alpha.full ->
?chain:Shell_services.chain ->
Shell_services.block ->
?gas:Z.t ->
Michelson_v1_parser.parsed ->
Michelson_v1_parser.parsed ->
Client_keys.sk_uri ->
(Script_expr_hash.t * Signature.t * Gas.t) tzresult Lwt.t
val typecheck_data : val typecheck_data :
#Proto_alpha.rpc_context -> #Proto_alpha.rpc_context ->
?chain:Shell_services.chain -> ?chain:Shell_services.chain ->

View File

@ -189,9 +189,11 @@ let commands () =
cctxt#error "ill-typed data") ; cctxt#error "ill-typed data") ;
command ~group command ~group
~desc: "Ask the node to hash a data expression.\n\ ~desc: "Ask the node to pack a data expression.\n\
The returned hash is the same as what Michelson \ The returned hash is the same as what Michelson \
instruction `H` would have produced." instruction `PACK` would have produced.\n\
Also displays the result of hashing this packed data \
with `BLAKE2B`, `SHA256` or `SHA512` instruction."
(args1 custom_gas_flag) (args1 custom_gas_flag)
(prefixes [ "hash" ; "data" ] (prefixes [ "hash" ; "data" ]
@@ Clic.param ~name:"data" ~desc:"the data to hash" @@ Clic.param ~name:"data" ~desc:"the data to hash"
@ -202,12 +204,22 @@ let commands () =
@@ stop) @@ stop)
(fun custom_gas data typ cctxt -> (fun custom_gas data typ cctxt ->
resolve_max_gas cctxt cctxt#block custom_gas >>=? fun original_gas -> resolve_max_gas cctxt cctxt#block custom_gas >>=? fun original_gas ->
Alpha_services.Helpers.Scripts.hash_data cctxt (`Main, cctxt#block) Alpha_services.Helpers.Scripts.pack_data cctxt (`Main, cctxt#block)
(data.expanded, typ.expanded, Some original_gas) >>= function (data.expanded, typ.expanded, Some original_gas) >>= function
| Ok (hash, remaining_gas) -> | Ok (bytes, remaining_gas) ->
cctxt#message "Hash: %a@,Raw hash bytes: 0x%a@,Gas remaining: %a" let hash = Script_expr_hash.hash_bytes [ bytes ] in
cctxt#message
"Raw packed data: 0x%a@,\
Hash: %a@,\
Raw Blake2b hash: 0x%a@,\
Raw Sha256 hash: 0x%a@,\
Raw Sha512 hash: 0x%a@,\
Gas remaining: %a"
MBytes.pp_hex bytes
Script_expr_hash.pp hash Script_expr_hash.pp hash
MBytes.pp_hex (Script_expr_hash.to_bytes hash) MBytes.pp_hex (Script_expr_hash.to_bytes hash)
MBytes.pp_hex (Alpha_environment.Raw_hashes.sha256 bytes)
MBytes.pp_hex (Alpha_environment.Raw_hashes.sha512 bytes)
Proto_alpha.Alpha_context.Gas.pp remaining_gas >>= fun () -> Proto_alpha.Alpha_context.Gas.pp remaining_gas >>= fun () ->
return () return ()
| Error errs -> | Error errs ->
@ -220,40 +232,29 @@ let commands () =
cctxt#error "ill-formed data") ; cctxt#error "ill-formed data") ;
command ~group command ~group
~desc: "Ask the node to hash a data expression.\n\ ~desc: "Sign a raw sequence of bytes and display it using the \
Uses the same algorithm as Michelson instruction `H` to \ format expected by Michelson instruction \
produce the hash, signs it using a given secret key, and \ `CHECK_SIGNATURE`."
displays it using the format expected by Michelson \ no_options
instruction `CHECK_SIGNATURE`." (prefixes [ "sign" ; "bytes" ]
(args1 custom_gas_flag) @@ Clic.param ~name:"data" ~desc:"the raw data to sign"
(prefixes [ "hash" ; "and" ; "sign" ; "data" ] (parameter (fun (_cctxt : full) s ->
@@ Clic.param ~name:"data" ~desc:"the data to hash" try
data_parameter if String.length s < 2
@@ prefixes [ "of" ; "type" ] || s.[0] <> '0' || s.[1] <> 'x' then
@@ Clic.param ~name:"type" ~desc:"type of the data" raise Exit
data_parameter else
return (MBytes.of_hex (`Hex (String.sub s 2 (String.length s - 2))))
with _ ->
failwith "Invalid bytes, expecting hexadecimal \
notation (e.g. 0x1234abcd)" ))
@@ prefixes [ "for" ] @@ prefixes [ "for" ]
@@ Client_keys.Secret_key.source_param @@ Client_keys.Secret_key.source_param
@@ stop) @@ stop)
(fun gas data typ sk cctxt -> (fun () bytes sk cctxt ->
resolve_max_gas cctxt cctxt#block gas >>=? fun gas -> Client_keys.sign cctxt sk bytes >>=? fun signature ->
Client_proto_programs.hash_and_sign cctxt cctxt#block cctxt#message "Signature: %a" Signature.pp signature >>= fun () ->
~gas data typ sk >>= begin function return ()) ;
| Ok (hash, signature, current_gas) ->
cctxt#message "@[<v 0>Hash: %a@,Raw hash bytes: 0x%a@,Signature: %a@,Remaining gas: %a@]"
Script_expr_hash.pp hash
MBytes.pp_hex (Script_expr_hash.to_bytes hash)
Signature.pp signature
Proto_alpha.Alpha_context.Gas.pp current_gas
| Error errs ->
cctxt#warning "%a"
(Michelson_v1_error_reporter.report_errors
~details:false
~show_source:false
?parsed:None)
errs >>= fun () ->
cctxt#error "ill-formed data"
end >>= return) ;
command ~group command ~group
~desc: "Ask the node to check the signature of a hashed expression." ~desc: "Ask the node to check the signature of a hashed expression."

View File

@ -112,20 +112,20 @@ module Scripts = struct
~output: (obj1 (req "gas" Gas.encoding)) ~output: (obj1 (req "gas" Gas.encoding))
RPC_path.(path / "typecheck_data") RPC_path.(path / "typecheck_data")
let hash_data = let pack_data =
RPC_service.post_service RPC_service.post_service
~description: "Computes the hash of some data expression \ ~description: "Computes the serialized version of some data expression \
using the same algorithm as script instruction H" using the same algorithm as script instruction PACK"
~input: (obj3 ~input: (obj3
(req "data" Script.expr_encoding) (req "data" Script.expr_encoding)
(req "type" Script.expr_encoding) (req "type" Script.expr_encoding)
(opt "gas" z)) (opt "gas" z))
~output: (obj2 ~output: (obj2
(req "hash" Script_expr_hash.encoding) (req "packed" bytes)
(req "gas" Gas.encoding)) (req "gas" Gas.encoding))
~query: RPC_query.empty ~query: RPC_query.empty
RPC_path.(path / "hash_data") RPC_path.(path / "pack_data")
let run_operation = let run_operation =
RPC_service.post_service RPC_service.post_service
@ -184,15 +184,15 @@ module Scripts = struct
Script_ir_translator.typecheck_data ctxt (data, ty) >>=? fun ctxt -> Script_ir_translator.typecheck_data ctxt (data, ty) >>=? fun ctxt ->
return (Gas.level ctxt) return (Gas.level ctxt)
end ; end ;
register0 S.hash_data begin fun ctxt () (expr, typ, maybe_gas) -> register0 S.pack_data begin fun ctxt () (expr, typ, maybe_gas) ->
let open Script_ir_translator in let open Script_ir_translator in
begin match maybe_gas with begin match maybe_gas with
| None -> return (Gas.set_unlimited ctxt) | None -> return (Gas.set_unlimited ctxt)
| Some gas -> Lwt.return (Gas.set_limit ctxt gas) end >>=? fun ctxt -> | Some gas -> Lwt.return (Gas.set_limit ctxt gas) end >>=? fun ctxt ->
Lwt.return (parse_ty ~allow_big_map:false ~allow_operation:false (Micheline.root typ)) >>=? fun (Ex_ty typ) -> Lwt.return (parse_ty ~allow_big_map:false ~allow_operation:false (Micheline.root typ)) >>=? fun (Ex_ty typ) ->
parse_data ctxt typ (Micheline.root expr) >>=? fun (data, ctxt) -> parse_data ctxt typ (Micheline.root expr) >>=? fun (data, ctxt) ->
Script_ir_translator.hash_data ctxt typ data >>=? fun (hash, ctxt) -> Script_ir_translator.pack_data ctxt typ data >>=? fun (bytes, ctxt) ->
return (hash, Gas.level ctxt) return (bytes, Gas.level ctxt)
end ; end ;
register0 S.run_operation begin fun ctxt () register0 S.run_operation begin fun ctxt ()
{ shell ; protocol_data = Operation_data protocol_data } -> { shell ; protocol_data = Operation_data protocol_data } ->
@ -263,8 +263,8 @@ module Scripts = struct
let typecheck_data ctxt block = let typecheck_data ctxt block =
RPC_context.make_call0 S.typecheck_data ctxt block () RPC_context.make_call0 S.typecheck_data ctxt block ()
let hash_data ctxt block = let pack_data ctxt block =
RPC_context.make_call0 S.hash_data ctxt block () RPC_context.make_call0 S.pack_data ctxt block ()
let run_operation ctxt block = let run_operation ctxt block =
RPC_context.make_call0 S.run_operation ctxt block () RPC_context.make_call0 S.run_operation ctxt block ()

View File

@ -48,9 +48,9 @@ module Scripts : sig
'a #RPC_context.simple -> 'a #RPC_context.simple ->
'a -> Script.expr * Script.expr * Z.t option -> Gas.t shell_tzresult Lwt.t 'a -> Script.expr * Script.expr * Z.t option -> Gas.t shell_tzresult Lwt.t
val hash_data: val pack_data:
'a #RPC_context.simple -> 'a #RPC_context.simple ->
'a -> Script.expr * Script.expr * Z.t option -> (Script_expr_hash.t * Gas.t) shell_tzresult Lwt.t 'a -> Script.expr * Script.expr * Z.t option -> (MBytes.t * Gas.t) shell_tzresult Lwt.t
val run_operation: val run_operation:
'a #RPC_context.simple -> 'a #RPC_context.simple ->