123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233(*****************************************************************************)(* *)(* 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)letin_memory_size=letopenCache_memory_helpersinfunction|Implicit_->h1w+!public_key_hash_in_memory_size|Originated_->h1w+!blake2b_hash_sizetypeerror+=Invalid_contract_notationofstring(* `Permanent *)letto_b58check=function|Implicitpbk->Signature.Public_key_hash.to_b58checkpbk|Originatedh->Contract_hash.to_b58checkhletimplicit_of_b58data:Base58.data->Signature.public_key_hashoption=function|Ed25519.Public_key_hash.Datah->Some(Signature.Ed25519h)|Secp256k1.Public_key_hash.Datah->Some(Signature.Secp256k1h)|P256.Public_key_hash.Datah->Some(Signature.P256h)|Bls.Public_key_hash.Datah->Some(Signature.Blsh)|_->Noneletoriginated_of_b58data=function|Contract_hash.Datah->Someh|_->Noneletcontract_of_b58datadata=matchimplicit_of_b58datadatawith|Somepkh->Some(Implicitpkh)|None->(matchoriginated_of_b58datadatawith|Somecontract_hash->Some(Originatedcontract_hash)|None->None)letof_b58check_gen~of_b58datas=matchBase58.decodeswith|Somedata->(matchof_b58datadatawith|Somec->okc|None->error(Invalid_contract_notations))|None->error(Invalid_contract_notations)letof_b58check=of_b58check_gen~of_b58data:contract_of_b58dataletppppf=function|Implicitpbk->Signature.Public_key_hash.ppppfpbk|Originatedh->Contract_hash.ppppfhletpp_shortppf=function|Implicitpbk->Signature.Public_key_hash.pp_shortppfpbk|Originatedh->Contract_hash.pp_shortppfhletimplicit_case~proj~inj=letopenData_encodingincase(Tag0)~title:"Implicit"Signature.Public_key_hash.encodingprojinjletoriginated_case~proj~inj=letopenData_encodingincase(Tag1)(Fixed.add_paddingContract_hash.encoding1)~title:"Originated"projinjletcasesis_contractto_contract=[implicit_case~proj:(funk->matchis_contractkwithSome(Implicitk)->Somek|_->None)~inj:(funk->to_contract(Implicitk));originated_case~proj:(funk->matchis_contractkwithSome(Originatedk)->Somek|_->None)~inj:(funk->to_contract(Originatedk));]letencoding_gen~id_extra~title_extra~can_be~cases~to_b58check~of_b58data=letopenData_encodingindef("contract_id"^id_extra)~title:("A contract handle"^title_extra)~description:("A contract notation as given to an RPC or inside scripts. Can be a \
base58 "^can_be)@@splitted~binary:(union~tag_size:`Uint8@@cases(funx->Somex)(funx->x))~json:(convto_b58check(funs->matchof_b58check_gen~of_b58dataswith|Oks->s|Error_->Json.cannot_destruct"Invalid contract notation.")(stringPlain))letencoding=encoding_gen~id_extra:""~title_extra:""~can_be:"implicit contract hash or a base58 originated contract hash."~cases~to_b58check~of_b58data:contract_of_b58dataletimplicit_encoding=encoding_gen~id_extra:".implicit"~title_extra:" -- implicit account"~can_be:"implicit contract hash."~cases:(funprojinj->[implicit_case~proj~inj])~to_b58check:Signature.Public_key_hash.to_b58check~of_b58data:implicit_of_b58dataletoriginated_encoding=encoding_gen~id_extra:".originated"~title_extra:" -- originated account"~can_be:"originated contract hash."~cases:(funprojinj->[originated_case~proj~inj])~to_b58check:Contract_hash.to_b58check~of_b58data:originated_of_b58datalet()=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"(stringPlain)))(functionInvalid_contract_notationloc->Someloc|_->None)(funloc->Invalid_contract_notationloc)letoriginated_contractnonce=Originated(Contract_hash.of_noncenonce)letoriginated_contracts~since:Origination_nonce.{origination_index=first;operation_hash=first_hash}~until:(Origination_nonce.{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=Contract_hash.of_nonceorigination_nonce::accincontractsacc(Int32.predorigination_index)incontracts[](Int32.predlast)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=structtypenonrect=tletpath_length=1letto_pathcl=letraw_key=Data_encoding.Binary.to_bytes_exnencodingcinlet(`Hexkey)=Hex.of_bytesraw_keyinkey::lletof_path=function|[key]->Option.bind(Hex.to_bytes(`Hexkey))(Data_encoding.Binary.of_bytes_optencoding)|_->Noneletrpc_arg=rpc_argletencoding=encodingletcompare=compareend(* Renamed exports. *)letof_b58data=contract_of_b58data