123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213(*****************************************************************************)(* *)(* 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=match(l1,l2)with|(Implicitpkh1,Implicitpkh2)->Signature.Public_key_hash.comparepkh1pkh2|(Originatedh1,Originatedh2)->Contract_hash.compareh1h2|(Implicit_,Originated_)->-1|(Originated_,Implicit_)->1end)typecontract=tletblake2b_hash_size=letopenCache_memory_helpersinheader_size+!word_size+!string_size_gen20letpublic_key_hash_in_memory_size=letopenCache_memory_helpersinheader_size+!word_size+!blake2b_hash_sizeletin_memory_size=letopenCache_memory_helpersinfunction|Implicit_->header_size+!word_size+!public_key_hash_in_memory_size|Originated_->header_size+!word_size+!blake2b_hash_sizetypeerror+=Invalid_contract_notationofstring(* `Permanent *)letto_b58check=function|Implicitpbk->Signature.Public_key_hash.to_b58checkpbk|Originatedh->Contract_hash.to_b58checkhletof_b58checks=matchBase58.decodeswith|Somedata->(matchdatawith|Ed25519.Public_key_hash.Datah->ok(Implicit(Signature.Ed25519h))|Secp256k1.Public_key_hash.Datah->ok(Implicit(Signature.Secp256k1h))|P256.Public_key_hash.Datah->ok(Implicit(Signature.P256h))|Contract_hash.Datah->ok(Originatedh)|_->error(Invalid_contract_notations))|None->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=Implicitidletis_implicit=functionImplicitm->Somem|Originated_->Noneletis_originated=functionImplicit_->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);let[@coq_struct"origination_index"]reccontractsaccorigination_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=Result.map_error(fun_->"Cannot parse contract id")(of_b58checkhash)inRPC_arg.make~descr:"A contract identifier encoded in b58check."~name:"contract_id"~construct~destruct()moduleIndex=structtypet=contractletpath_length=1letto_pathcl=letraw_key=Data_encoding.Binary.to_bytes_exnencodingcinlet(`Hexkey)=Hex.of_bytesraw_keyinkey::lletof_path=function|[key]->letraw_key=Hex.to_bytes(`Hexkey)inData_encoding.Binary.of_bytes_optencodingraw_key|_->Noneletrpc_arg=rpc_argletencoding=encodingletcompare=compareend