From 1a6eb6052d55431c116f2d76fcf775fa9f0faf6b Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Wed, 4 May 2022 16:05:20 +0200 Subject: [PATCH 1/4] Proto/Contract_repr: refactor of_b58check --- src/proto_alpha/lib_protocol/contract_repr.ml | 33 ++++++++++++++----- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/src/proto_alpha/lib_protocol/contract_repr.ml b/src/proto_alpha/lib_protocol/contract_repr.ml index e7befb910d8a..9eebd76f64e3 100644 --- a/src/proto_alpha/lib_protocol/contract_repr.ml +++ b/src/proto_alpha/lib_protocol/contract_repr.ml @@ -59,18 +59,35 @@ let to_b58check = function | Implicit pbk -> Signature.Public_key_hash.to_b58check pbk | Originated h -> Contract_hash.to_b58check h -let of_b58check s = +let implicit_of_b58data : Base58.data -> Signature.public_key_hash option = + function + | Ed25519.Public_key_hash.Data h -> Some (Signature.Ed25519 h) + | Secp256k1.Public_key_hash.Data h -> Some (Signature.Secp256k1 h) + | P256.Public_key_hash.Data h -> Some (Signature.P256 h) + | _ -> None + +let originated_of_b58data = function + | Contract_hash.Data h -> Some h + | _ -> None + +let contract_of_b58data data = + match implicit_of_b58data data with + | Some pkh -> Some (Implicit pkh) + | None -> ( + match originated_of_b58data data with + | Some contract_hash -> Some (Originated contract_hash) + | None -> None) + +let of_b58check_gen ~of_b58data s = match Base58.decode s with | Some data -> ( - match data with - | Ed25519.Public_key_hash.Data h -> ok (Implicit (Signature.Ed25519 h)) - | Secp256k1.Public_key_hash.Data h -> - ok (Implicit (Signature.Secp256k1 h)) - | P256.Public_key_hash.Data h -> ok (Implicit (Signature.P256 h)) - | Contract_hash.Data h -> ok (Originated h) - | _ -> error (Invalid_contract_notation s)) + match of_b58data data with + | Some c -> ok c + | None -> error (Invalid_contract_notation s)) | None -> error (Invalid_contract_notation s) +let of_b58check = of_b58check_gen ~of_b58data:contract_of_b58data + let pp ppf = function | Implicit pbk -> Signature.Public_key_hash.pp ppf pbk | Originated h -> Contract_hash.pp ppf h -- GitLab From 7fc5cdc9aa75e209b53a23761f1030e7549b2ad8 Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Wed, 4 May 2022 16:10:15 +0200 Subject: [PATCH 2/4] Proto/Contract_repr: refactor cases --- src/proto_alpha/lib_protocol/contract_repr.ml | 40 +++++++++++-------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/src/proto_alpha/lib_protocol/contract_repr.ml b/src/proto_alpha/lib_protocol/contract_repr.ml index 9eebd76f64e3..18ba2e2bc722 100644 --- a/src/proto_alpha/lib_protocol/contract_repr.ml +++ b/src/proto_alpha/lib_protocol/contract_repr.ml @@ -96,24 +96,30 @@ let pp_short ppf = function | Implicit pbk -> Signature.Public_key_hash.pp_short ppf pbk | Originated h -> Contract_hash.pp_short ppf h +let implicit_case ~proj ~inj = + let open Data_encoding in + case (Tag 0) ~title:"Implicit" Signature.Public_key_hash.encoding proj inj + +let originated_case ~proj ~inj = + let open Data_encoding in + case + (Tag 1) + (Fixed.add_padding Contract_hash.encoding 1) + ~title:"Originated" + proj + inj + let cases is_contract to_contract = - Data_encoding. - [ - case - (Tag 0) - ~title:"Implicit" - Signature.Public_key_hash.encoding - (fun k -> - match is_contract k with Some (Implicit k) -> Some k | _ -> None) - (fun k -> to_contract (Implicit k)); - case - (Tag 1) - (Fixed.add_padding Contract_hash.encoding 1) - ~title:"Originated" - (fun k -> - match is_contract k with Some (Originated k) -> Some k | _ -> None) - (fun k -> to_contract (Originated k)); - ] + [ + implicit_case + ~proj:(fun k -> + match is_contract k with Some (Implicit k) -> Some k | _ -> None) + ~inj:(fun k -> to_contract (Implicit k)); + originated_case + ~proj:(fun k -> + match is_contract k with Some (Originated k) -> Some k | _ -> None) + ~inj:(fun k -> to_contract (Originated k)); + ] let encoding = let open Data_encoding in -- GitLab From e5383c6c7ef4f0922e18302653a8233a81596bef Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Wed, 4 May 2022 16:16:51 +0200 Subject: [PATCH 3/4] Proto/Contract_repr: refactor encoding --- src/proto_alpha/lib_protocol/contract_repr.ml | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/proto_alpha/lib_protocol/contract_repr.ml b/src/proto_alpha/lib_protocol/contract_repr.ml index 18ba2e2bc722..1c592d3afd0f 100644 --- a/src/proto_alpha/lib_protocol/contract_repr.ml +++ b/src/proto_alpha/lib_protocol/contract_repr.ml @@ -121,25 +121,35 @@ let cases is_contract to_contract = ~inj:(fun k -> to_contract (Originated k)); ] -let encoding = +let encoding_gen ~id_extra ~title_extra ~can_be ~cases ~to_b58check ~of_b58data + = let open Data_encoding in def - "contract_id" - ~title:"A contract handle" + ("contract_id" ^ id_extra) + ~title:("A contract handle" ^ title_extra) ~description: - "A contract notation as given to an RPC or inside scripts. Can be a \ - base58 implicit contract hash or a base58 originated contract hash." + ("A contract notation as given to an RPC or inside scripts. Can be a \ + base58 " ^ can_be) @@ splitted ~binary:(union ~tag_size:`Uint8 @@ cases (fun x -> Some x) (fun x -> x)) ~json: (conv to_b58check (fun s -> - match of_b58check s with + match of_b58check_gen ~of_b58data s with | Ok s -> s | Error _ -> Json.cannot_destruct "Invalid contract notation.") string) +let encoding = + encoding_gen + ~id_extra:"" + ~title_extra:"" + ~can_be:"implicit contract hash or a base58 originated contract hash." + ~cases + ~to_b58check + ~of_b58data:contract_of_b58data + let () = let open Data_encoding in register_error_kind -- GitLab From 2abab39f3b941e3846e2d346b96f0c327ced9aac Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Thu, 5 May 2022 14:59:49 +0200 Subject: [PATCH 4/4] Proto/Contract_repr: contract-compatible implicit/originated encodings --- src/proto_alpha/lib_protocol/contract_repr.ml | 18 ++++++++++++++++++ src/proto_alpha/lib_protocol/contract_repr.mli | 8 ++++++++ 2 files changed, 26 insertions(+) diff --git a/src/proto_alpha/lib_protocol/contract_repr.ml b/src/proto_alpha/lib_protocol/contract_repr.ml index 1c592d3afd0f..3f4dc97444d6 100644 --- a/src/proto_alpha/lib_protocol/contract_repr.ml +++ b/src/proto_alpha/lib_protocol/contract_repr.ml @@ -150,6 +150,24 @@ let encoding = ~to_b58check ~of_b58data:contract_of_b58data +let implicit_encoding = + encoding_gen + ~id_extra:".implicit" + ~title_extra:" -- implicit account" + ~can_be:"implicit contract hash." + ~cases:(fun proj inj -> [implicit_case ~proj ~inj]) + ~to_b58check:Signature.Public_key_hash.to_b58check + ~of_b58data:implicit_of_b58data + +let originated_encoding = + encoding_gen + ~id_extra:".originated" + ~title_extra:" -- originated account" + ~can_be:"originated contract hash." + ~cases:(fun proj inj -> [originated_case ~proj ~inj]) + ~to_b58check:Contract_hash.to_b58check + ~of_b58data:originated_of_b58data + let () = let open Data_encoding in register_error_kind diff --git a/src/proto_alpha/lib_protocol/contract_repr.mli b/src/proto_alpha/lib_protocol/contract_repr.mli index a818e5654e77..ac3695a6f81b 100644 --- a/src/proto_alpha/lib_protocol/contract_repr.mli +++ b/src/proto_alpha/lib_protocol/contract_repr.mli @@ -75,6 +75,14 @@ val pp_short : Format.formatter -> t -> unit val encoding : t Data_encoding.t +(** [implicit_encoding] is an encoding for public key hashes that is + compatible with the [encoding] of contracts for implicit accounts. *) +val implicit_encoding : Signature.Public_key_hash.t Data_encoding.t + +(** [originated_encoding] is an encoding for contract hashes that is + compatible with the [encoding] of contracts for originated accounts. *) +val originated_encoding : Contract_hash.t Data_encoding.t + (** [cases f g] exports the {!Data_encoding.cases} used to define {!encoding}. The only reason why we export that is to let {!Destination_repr.encoding} -- GitLab