Skip to content
Snippets Groups Projects

Proto: contract-compatible encodings for implicit and originated accounts

Merged Mehdi Bouaziz requested to merge mehdi@compatible-encodings into master
Files
2
@@ -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)
+3
| 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
@@ -79,44 +96,78 @@ let pp_short ppf = function
| Implicit pbk -> Signature.Public_key_hash.pp_short ppf pbk
| Originated h -> Contract_hash.pp_short ppf h
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));
]
let implicit_case ~proj ~inj =
let open Data_encoding in
case (Tag 0) ~title:"Implicit" Signature.Public_key_hash.encoding proj inj
let encoding =
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 =
[
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_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 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
Loading