123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214(*****************************************************************************)(* *)(* 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. *)(* *)(*****************************************************************************)typet=|ImplicitofSignature.Public_key_hash.t|OriginatedofContract_hash.tincludeCompare.Make(structtypenonrect=tletcomparel1l2=matchl1,l2with|Implicitpkh1,Implicitpkh2->Signature.Public_key_hash.comparepkh1pkh2|Originatedh1,Originatedh2->Contract_hash.compareh1h2|Implicit_,Originated_->-1|Originated_,Implicit_->1end)typecontract=ttypeerror+=Invalid_contract_notationofstring(* `Permanent *)letto_b58check=function|Implicitpbk->Signature.Public_key_hash.to_b58checkpbk|Originatedh->Contract_hash.to_b58checkhletof_b58checks=matchBase58.decodeswith|Some(Ed25519.Public_key_hash.Datah)->ok(Implicit(Signature.Ed25519h))|Some(Secp256k1.Public_key_hash.Datah)->ok(Implicit(Signature.Secp256k1h))|Some(P256.Public_key_hash.Datah)->ok(Implicit(Signature.P256h))|Some(Contract_hash.Datah)->ok(Originatedh)|_->error(Invalid_contract_notations)letppppf=function|Implicitpbk->Signature.Public_key_hash.ppppfpbk|Originatedh->Contract_hash.ppppfhletpp_shortppf=function|Implicitpbk->Signature.Public_key_hash.pp_shortppfpbk|Originatedh->Contract_hash.pp_shortppfhletencoding=letopenData_encodingindef"contract_id"~title:"A contract handle"~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."@@splitted~binary:(union~tag_size:`Uint8[case(Tag0)~title:"Implicit"Signature.Public_key_hash.encoding(functionImplicitk->Somek|_->None)(funk->Implicitk);case(Tag1)(Fixed.add_paddingContract_hash.encoding1)~title:"Originated"(functionOriginatedk->Somek|_->None)(funk->Originatedk);])~json:(convto_b58check(funs->matchof_b58checkswith|Oks->s|Error_->Json.cannot_destruct"Invalid contract notation.")string)let()=letopenData_encodinginregister_error_kind`Permanent~id:"contract.invalid_contract_notation"~title:"Invalid contract notation"~pp:(funppfx->Format.fprintfppf"Invalid contract notation %S"x)~description:"A malformed contract notation was given to an RPC or in a script."(obj1(req"notation"string))(functionInvalid_contract_notationloc->Someloc|_->None)(funloc->Invalid_contract_notationloc)letimplicit_contractid=Implicitidletoriginated_contract_004id=Originatedidletis_implicit=function|Implicitm->Somem|Originated_->Noneletis_originated=function|Implicit_->None|Originatedh->Somehtypeorigination_nonce={operation_hash:Operation_hash.t;origination_index:int32}letorigination_nonce_encoding=letopenData_encodinginconv(fun{operation_hash;origination_index}->(operation_hash,origination_index))(fun(operation_hash,origination_index)->{operation_hash;origination_index})@@obj2(req"operation"Operation_hash.encoding)(dft"index"int320l)letoriginated_contractnonce=letdata=Data_encoding.Binary.to_bytes_exnorigination_nonce_encodingnonceinOriginated(Contract_hash.hash_bytes[data])letoriginated_contracts~since:{origination_index=first;operation_hash=first_hash}~until:({origination_index=last;operation_hash=last_hash}asorigination_nonce)=assert(Operation_hash.equalfirst_hashlast_hash);letreccontractsaccorigination_index=ifCompare.Int32.(origination_index<first)thenaccelseletorigination_nonce={origination_noncewithorigination_index}inletacc=originated_contractorigination_nonce::accincontractsacc(Int32.predorigination_index)incontracts[](Int32.predlast)letinitial_origination_nonceoperation_hash={operation_hash;origination_index=0l}letincr_origination_noncenonce=letorigination_index=Int32.succnonce.origination_indexin{noncewithorigination_index}letrpc_arg=letconstruct=to_b58checkinletdestructhash=matchof_b58checkhashwith|Error_->Error"Cannot parse contract id"|Okcontract->OkcontractinRPC_arg.make~descr:"A contract identifier encoded in b58check."~name:"contract_id"~construct~destruct()moduleIndex=structtypet=contractletpath_length=7letto_pathcl=letraw_key=Data_encoding.Binary.to_bytes_exnencodingcinlet`Hexkey=MBytes.to_hexraw_keyinlet`Hexindex_key=MBytes.to_hex(Raw_hashes.blake2braw_key)inString.subindex_key02::String.subindex_key22::String.subindex_key42::String.subindex_key62::String.subindex_key82::String.subindex_key102::key::lletof_path=function|[]|[_]|[_;_]|[_;_;_]|[_;_;_;_]|[_;_;_;_;_]|[_;_;_;_;_;_]|_::_::_::_::_::_::_::_::_->None|[index1;index2;index3;index4;index5;index6;key]->letraw_key=MBytes.of_hex(`Hexkey)inlet`Hexindex_key=MBytes.to_hex(Raw_hashes.blake2braw_key)inassertCompare.String.(String.subindex_key02=index1);assertCompare.String.(String.subindex_key22=index2);assertCompare.String.(String.subindex_key42=index3);assertCompare.String.(String.subindex_key62=index4);assertCompare.String.(String.subindex_key82=index5);assertCompare.String.(String.subindex_key102=index6);Data_encoding.Binary.of_bytesencodingraw_keyletrpc_arg=rpc_argletencoding=encodingletcompare=compareend