123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2022 Nomadic Labs <contact@nomadic-labs.com> *)(* Copyright (c) 2022 Trili Tech, <contact@trili.tech> *)(* Copyright (c) 2023 Marigold <contact@marigold.dev> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)(* Reserve the first byte in the encoding to support multi-versioning
in the future. *)moduleBlake2B=structincludeBlake2B.Make(Base58)(structletname="Smart_rollup_reveal_data_blake2b_hash"lettitle="A smart rollup reveal hash"letb58check_prefix="\230\206\128\200\196"(* "scrrh1(56)" decoded from Base58. *)letsize=Some32end)let()=Base58.check_encoded_prefixb58check_encoding"scrrh1"56endtypesupported_hashes=Blake2Btypet=Blake2BofBlake2B.tletzero~(scheme:supported_hashes)=matchschemewithBlake2B->Blake2BBlake2B.zeroletppppfhash=matchhashwithBlake2Bhash->Blake2B.ppppfhashletequalh1h2=match(h1,h2)withBlake2Bh1,Blake2Bh2->Blake2B.equalh1h2letcompareh1h2=match(h1,h2)withBlake2Bh1,Blake2Bh2->Blake2B.compareh1h2moduleMap=Map.Make(structtypetmp=ttypet=tmpletcompare=compareend)(* Size of the hash is the size of the inner hash plus one byte for the
tag used to identify the hashing scheme. *)letsize~(scheme:supported_hashes)=lettag_size=1inletsize_without_tag=matchschemewithBlake2B->Blake2B.sizeintag_size+size_without_tagletencoding=letopenData_encodinginunion~tag_size:`Uint8[case~title:"Reveal_data_hash_v0"(Tag0)Blake2B.encoding(fun(Blake2Bs)->Somes)(funs->Blake2Bs);]lethash_string~(scheme:supported_hashes)?keystrings=matchschemewithBlake2B->Blake2B(Blake2B.hash_string?keystrings)lethash_bytes~(scheme:supported_hashes)?keybytes=matchschemewithBlake2B->Blake2B(Blake2B.hash_bytes?keybytes)letscheme_of_hashhash=matchhashwithBlake2B_hash->(Blake2B:supported_hashes)letto_hexhash=let(`Hexhash)=(* The [encoding] of a hash here never, so [to_string_exn] is safe. *)Hex.of_string@@Data_encoding.Binary.to_string_exnencodinghashinhashletof_hexhex=letopenOption_syntaxinlet*hash=Hex.to_bytes(`Hexhex)inData_encoding.Binary.of_bytes_optencodinghashletrpc_arg=letconstruct=to_hexinletdestructhash=matchof_hexhashwith|None->Error"Cannot parse reveal hash"|Somereveal_hash->Okreveal_hashinRPC_arg.make~descr:"A reveal hash"~name:"reveal_hash"~destruct~construct()(** The preimage of {!well_known_reveal_hash}. *)letwell_known_reveal_preimage=""(** The hash requested by the WASM PVM if it cannot decode the input
provided by the WASM kernel, that is, if the bytes value cannot
be decoded with {!Sc_rollup_reveal_hash.encoding}. *)letwell_known_reveal_hash=hash_string~scheme:Blake2B[well_known_reveal_preimage]