123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)(* *)(* 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. *)(* *)(*****************************************************************************)openError_monadtypet=stringletname="Chain_id"lettitle="Network identifier"letextractbh=Bytes.sub_string(Block_hash.to_bytesbh)04lethash_bytes?keyl=extract(Block_hash.hash_bytes?keyl)lethash_string?keyl=extract(Block_hash.hash_string?keyl)letsize=4letof_string_opts=ifString.lengths<>sizethenNoneelseSomesletof_strings=matchof_string_optswith|None->error_with"%s.of_string: wrong string size (%d instead of %d)"name(String.lengths)size|Someh->Okhletof_string_exns=matchof_string_optswith|None->Format.kasprintfinvalid_arg"%s.of_string_exn: wrong string size (%d instead of %d)"name(String.lengths)size|Someh->hletto_strings=sletof_hexs=matchHex.to_stringswith|None->error_with"%s.of_hex: invalid hex string (%a)"nameHex.pps|Somes->of_stringsletof_hex_opts=Option.bind(Hex.to_strings)of_string_optletof_hex_exns=matchHex.to_stringswith|None->Format.kasprintfinvalid_arg"%s.of_hex_exn: invalid hex string (%a)"nameHex.pps|Somes->of_string_exnsletto_hexs=Hex.of_string(to_strings)letof_bytes_optb=ifBytes.lengthb<>sizethenNoneelseSome(Bytes.to_stringb)letof_bytes_exnb=matchof_bytes_optbwith|None->letmsg=Printf.sprintf"%s.of_bytes_exn: wrong string size (%d instead of %d)"name(Bytes.lengthb)sizeinraise(Invalid_argumentmsg)|Someh->hletof_bytess=matchof_bytes_optswith|Somex->Okx|None->error_with"Failed to deserialize a hash (%s)"nameletto_bytes=Bytes.of_stringletpath_length=1letto_pathkeyl=let(`Hexh)=to_hexkeyinh::lletof_pathpath=letpath=String.concat""pathinof_hex_opt(`Hexpath)letof_path_exnpath=letpath=String.concat""pathinof_hex_exn(`Hexpath)letprefix_pathp=let(`Hexp)=Hex.of_stringpin[p]letzero=of_hex_exn(`Hex(String.make(size*2)'0'))typeBase58.data+=Dataoftletb58check_encoding=Base58.register_encoding~prefix:Base58.Prefix.chain_id~length:size~wrap:(funs->Datas)~of_raw:of_string_opt~to_raw:to_stringletraw_encoding=letopenData_encodinginconvto_bytesof_bytes_exn(Fixed.bytessize)letof_block_hashbh=hash_bytes[Block_hash.to_bytesbh]include(Compare.String:Compare.Swithtypet:=t)lethash=Stdlib.Hashtbl.hashletseeded_hash=Stdlib.Hashtbl.seeded_hashincludeHelpers.Make(structtypenonrect=tlettitle=titleletname=nameletb58check_encoding=b58check_encodingletraw_encoding=raw_encodingletcompare=compareletequal=equallethash=hashletseeded_hash=seeded_hashend)