123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2023 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. *)(* *)(*****************************************************************************)typehash=bytestyperaw_hash=byteslethash_to_bytes=Fun.idletraw_hash_to_bytes=Fun.idlethash_to_hexhash=Hex.of_byteshashtypesupported_hashes=Blake2Bletraw_compare=Bytes.compareletraw_hash_encoding=letopenData_encodinginunion[case(Tag0)Data_encoding.bytes~title:"raw_hash"(funraw_hash->Someraw_hash)(funraw_hash->raw_hash);]lethash_to_raw=Fun.idletraw_hash_to_hexraw_hash=Hex.of_bytesraw_hashletraw_hash_of_hexhex=Hex.to_byteshexletraw_hash_of_bytes=Fun.idletraw_hash_rpc_arg=letconstructraw_hash=Hex.show@@raw_hash_to_hexraw_hashinletdestructhash=matchraw_hash_of_hex(`Hexhash)with|None->Error"Impossible to parse raw_hash"|Somereveal_hash->Okreveal_hashinTezos_rpc.Arg.make~descr:"A reveal hash"~name:"reveal_hash"~destruct~construct()typecannot_convert_raw_hash_to_hash={raw_hash:raw_hash;proto:Protocol_hash.t;}typeerror+=Cannot_convert_raw_hash_to_hashofcannot_convert_raw_hash_to_hashletpp_raw_hashfmtraw_hash=Hex.ppfmt(raw_hash_to_hexraw_hash)let()=register_error_kind`Permanent~id:"cannot_convert_raw_hash_to_hash"~title:"Impossible to retrieve hash from raw_hash"~description:"Impossible to validate the provided raw_hash against the protocol"~pp:(funppf(raw_hash,proto)->Format.fprintfppf"Impossible to validate the provided raw_hash %a against the actual \
protocol %s and transform it to a valid hash"pp_raw_hashraw_hash(Protocol_hash.to_stringproto))Data_encoding.(obj2(req"raw_hash"raw_hash_encoding)(req"proto"Protocol_hash.encoding))(function|Cannot_convert_raw_hash_to_hash{raw_hash;proto}->Some(raw_hash,proto)|_->None)(fun(raw_hash,proto)->Cannot_convert_raw_hash_to_hash{raw_hash;proto})moduletypeT=sigvalencoding:hashData_encoding.tvalequal:hash->hash->boolvalhash_string:scheme:supported_hashes->?key:string->stringlist->hashvalhash_bytes:scheme:supported_hashes->?key:bytes->byteslist->hashvalscheme_of_hash:hash->supported_hashesvalof_hex:string->hashoptionvalto_hex:hash->stringvalsize:scheme:supported_hashes->intmoduleProto:Registered_protocol.Tendtypet=(moduleT)letraw_to_hash((modulePlugin):t)raw_hash=letof_bytes_opt=Data_encoding.Binary.of_bytes_optPlugin.encodingraw_hashinmatchof_bytes_optwith|Somehash->Okhash|None->Result_syntax.tzfail@@Cannot_convert_raw_hash_to_hash{raw_hash;proto=Plugin.Proto.hash}lettable:tProtocol_hash.Table.t=Protocol_hash.Table.create5letregister(make_plugin:(bytes->hash)->t):unit=letdac_plugin=make_pluginFun.idinletmodulePlugin=(valdac_plugin)inassert(not(Protocol_hash.Table.memtablePlugin.Proto.hash));Protocol_hash.Table.addtablePlugin.Proto.hash(modulePlugin)letgethash=Protocol_hash.Table.findtablehash