123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765(*****************************************************************************)(* *)(* 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. *)(* *)(*****************************************************************************)(* Tezos Protocol Implementation - Low level Repr. of Operations *)moduleKind=structtypeseed_nonce_revelation=Seed_nonce_revelation_kindtypedouble_endorsement_evidence=Double_endorsement_evidence_kindtypedouble_baking_evidence=Double_baking_evidence_kindtypeactivate_account=Activate_account_kindtypeendorsement=Endorsement_kindtypeproposals=Proposals_kindtypeballot=Ballot_kindtypereveal=Reveal_kindtypetransaction=Transaction_kindtypeorigination=Origination_kindtypedelegation=Delegation_kindtype'amanager=|Reveal_manager_kind:revealmanager|Transaction_manager_kind:transactionmanager|Origination_manager_kind:originationmanager|Delegation_manager_kind:delegationmanagerendtyperaw=Operation.t={shell:Operation.shell_header;proto:MBytes.t;}letraw_encoding=Operation.encodingtype'kindoperation={shell:Operation.shell_header;protocol_data:'kindprotocol_data;}and'kindprotocol_data={contents:'kindcontents_list;signature:Signature.toption;}and_contents_list=|Single:'kindcontents->'kindcontents_list|Cons:'kindKind.managercontents*'restKind.managercontents_list->(('kind*'rest)Kind.manager)contents_listand_contents=|Endorsement:{level:Raw_level_repr.t;}->Kind.endorsementcontents|Seed_nonce_revelation:{level:Raw_level_repr.t;nonce:Seed_repr.nonce;}->Kind.seed_nonce_revelationcontents|Double_endorsement_evidence:{op1:Kind.endorsementoperation;op2:Kind.endorsementoperation;}->Kind.double_endorsement_evidencecontents|Double_baking_evidence:{bh1:Block_header_repr.t;bh2:Block_header_repr.t;}->Kind.double_baking_evidencecontents|Activate_account:{id:Ed25519.Public_key_hash.t;activation_code:Blinded_public_key_hash.activation_code;}->Kind.activate_accountcontents|Proposals:{source:Signature.Public_key_hash.t;period:Voting_period_repr.t;proposals:Protocol_hash.tlist;}->Kind.proposalscontents|Ballot:{source:Signature.Public_key_hash.t;period:Voting_period_repr.t;proposal:Protocol_hash.t;ballot:Vote_repr.ballot;}->Kind.ballotcontents|Manager_operation:{source:Contract_repr.contract;fee:Tez_repr.tez;counter:counter;operation:'kindmanager_operation;gas_limit:Z.t;storage_limit:Z.t;}->'kindKind.managercontentsand_manager_operation=|Reveal:Signature.Public_key.t->Kind.revealmanager_operation|Transaction:{amount:Tez_repr.tez;parameters:Script_repr.lazy_exproption;destination:Contract_repr.contract;}->Kind.transactionmanager_operation|Origination:{manager:Signature.Public_key_hash.t;delegate:Signature.Public_key_hash.toption;script:Script_repr.toption;spendable:bool;delegatable:bool;credit:Tez_repr.tez;preorigination:Contract_repr.toption;}->Kind.originationmanager_operation|Delegation:Signature.Public_key_hash.toption->Kind.delegationmanager_operationandcounter=Z.tletmanager_kind:typekind.kindmanager_operation->kindKind.manager=function|Reveal_->Kind.Reveal_manager_kind|Transaction_->Kind.Transaction_manager_kind|Origination_->Kind.Origination_manager_kind|Delegation_->Kind.Delegation_manager_kindtype'kindinternal_operation={source:Contract_repr.contract;operation:'kindmanager_operation;nonce:int;}typepacked_manager_operation=|Manager:'kindmanager_operation->packed_manager_operationtypepacked_contents=|Contents:'kindcontents->packed_contentstypepacked_contents_list=|Contents_list:'kindcontents_list->packed_contents_listtypepacked_protocol_data=|Operation_data:'kindprotocol_data->packed_protocol_datatypepacked_operation={shell:Operation.shell_header;protocol_data:packed_protocol_data;}letpack({shell;protocol_data}:_operation):packed_operation={shell;protocol_data=Operation_dataprotocol_data;}typepacked_internal_operation=|Internal_operation:'kindinternal_operation->packed_internal_operationletrecto_list=function|Contents_list(Singleo)->[Contentso]|Contents_list(Cons(o,os))->Contentso::to_list(Contents_listos)letrecof_list=function|[]->assertfalse|[Contentso]->Contents_list(Singleo)|(Contentso)::os->letContents_listos=of_listosinmatcho,oswith|Manager_operation_,Single(Manager_operation_)->Contents_list(Cons(o,os))|Manager_operation_,Cons_->Contents_list(Cons(o,os))|_->Pervasives.failwith"Operation list of length > 1 \
should only contains manager operations."moduleEncoding=structopenData_encodingletcasetagnameargsprojinj=letopenData_encodingincasetag~title:(String.capitalize_asciiname)(merge_objs(obj1(req"kind"(constantname)))args)(funx->matchprojxwithNone->None|Somex->Some((),x))(fun((),x)->injx)moduleManager_operations=structtype'kindcase=MCase:{tag:int;name:string;encoding:'aData_encoding.t;select:packed_manager_operation->'kindmanager_operationoption;proj:'kindmanager_operation->'a;inj:'a->'kindmanager_operation}->'kindcaseletreveal_case=MCase{tag=0;name="reveal";encoding=(obj1(req"public_key"Signature.Public_key.encoding));select=(function|Manager(Reveal_asop)->Someop|_->None);proj=(functionRevealpkh->pkh);inj=(funpkh->Revealpkh)}lettransaction_case=MCase{tag=1;name="transaction";encoding=(obj3(req"amount"Tez_repr.encoding)(req"destination"Contract_repr.encoding)(opt"parameters"Script_repr.lazy_expr_encoding));select=(function|Manager(Transaction_asop)->Someop|_->None);proj=(function|Transaction{amount;destination;parameters}->(amount,destination,parameters));inj=(fun(amount,destination,parameters)->Transaction{amount;destination;parameters})}letorigination_case=MCase{tag=2;name="origination";encoding=(obj6(req"managerPubkey"Signature.Public_key_hash.encoding)(req"balance"Tez_repr.encoding)(dft"spendable"booltrue)(dft"delegatable"booltrue)(opt"delegate"Signature.Public_key_hash.encoding)(opt"script"Script_repr.encoding));select=(function|Manager(Origination_asop)->Someop|_->None);proj=(function|Origination{manager;credit;spendable;delegatable;delegate;script;preorigination=_(* the hash is only used internally
when originating from smart
contracts, don't serialize it *)}->(manager,credit,spendable,delegatable,delegate,script));inj=(fun(manager,credit,spendable,delegatable,delegate,script)->Origination{manager;credit;spendable;delegatable;delegate;script;preorigination=None})}letdelegation_case=MCase{tag=3;name="delegation";encoding=(obj1(opt"delegate"Signature.Public_key_hash.encoding));select=(function|Manager(Delegation_asop)->Someop|_->None);proj=(functionDelegationkey->key);inj=(funkey->Delegationkey)}letencoding=letmake(MCase{tag;name;encoding;select;proj;inj})=case(Tagtag)nameencoding(funo->matchselectowithNone->None|Someo->Some(projo))(funx->Manager(injx))inunion~tag_size:`Uint8[makereveal_case;maketransaction_case;makeorigination_case;makedelegation_case;]endtype'bcase=Case:{tag:int;name:string;encoding:'aData_encoding.t;select:packed_contents->'bcontentsoption;proj:'bcontents->'a;inj:'a->'bcontents}->'bcaseletendorsement_encoding=obj1(req"level"Raw_level_repr.encoding)letendorsement_case=Case{tag=0;name="endorsement";encoding=endorsement_encoding;select=(function|Contents(Endorsement_asop)->Someop|_->None);proj=(fun(Endorsement{level})->level);inj=(funlevel->Endorsement{level})}letendorsement_encoding=letmake(Case{tag;name;encoding;select=_;proj;inj})=case(Tagtag)nameencoding(funo->Some(projo))(funx->injx)inletto_list:Kind.endorsementcontents_list->_=function|Singleo->oinletof_list:Kind.endorsementcontents->_=function|o->Singleoindef"inlined.endorsement"@@conv(fun({shell;protocol_data={contents;signature}}:_operation)->(shell,(contents,signature)))(fun(shell,(contents,signature))->({shell;protocol_data={contents;signature}}:_operation))(merge_objsOperation.shell_header_encoding(obj2(req"operations"(convto_listof_list@@def"inlined.endorsement.contents"@@union[makeendorsement_case;]))(varopt"signature"Signature.encoding)))letseed_nonce_revelation_case=Case{tag=1;name="seed_nonce_revelation";encoding=(obj2(req"level"Raw_level_repr.encoding)(req"nonce"Seed_repr.nonce_encoding));select=(function|Contents(Seed_nonce_revelation_asop)->Someop|_->None);proj=(fun(Seed_nonce_revelation{level;nonce})->(level,nonce));inj=(fun(level,nonce)->Seed_nonce_revelation{level;nonce})}letdouble_endorsement_evidence_case:Kind.double_endorsement_evidencecase=Case{tag=2;name="double_endorsement_evidence";encoding=(obj2(req"op1"(dynamic_sizeendorsement_encoding))(req"op2"(dynamic_sizeendorsement_encoding)));select=(function|Contents(Double_endorsement_evidence_asop)->Someop|_->None);proj=(fun(Double_endorsement_evidence{op1;op2})->(op1,op2));inj=(fun(op1,op2)->(Double_endorsement_evidence{op1;op2}))}letdouble_baking_evidence_case=Case{tag=3;name="double_baking_evidence";encoding=(obj2(req"bh1"(dynamic_sizeBlock_header_repr.encoding))(req"bh2"(dynamic_sizeBlock_header_repr.encoding)));select=(function|Contents(Double_baking_evidence_asop)->Someop|_->None);proj=(fun(Double_baking_evidence{bh1;bh2})->(bh1,bh2));inj=(fun(bh1,bh2)->Double_baking_evidence{bh1;bh2});}letactivate_account_case=Case{tag=4;name="activate_account";encoding=(obj2(req"pkh"Ed25519.Public_key_hash.encoding)(req"secret"Blinded_public_key_hash.activation_code_encoding));select=(function|Contents(Activate_account_asop)->Someop|_->None);proj=(fun(Activate_account{id;activation_code})->(id,activation_code));inj=(fun(id,activation_code)->Activate_account{id;activation_code})}letproposals_case=Case{tag=5;name="proposals";encoding=(obj3(req"source"Signature.Public_key_hash.encoding)(req"period"Voting_period_repr.encoding)(req"proposals"(listProtocol_hash.encoding)));select=(function|Contents(Proposals_asop)->Someop|_->None);proj=(fun(Proposals{source;period;proposals})->(source,period,proposals));inj=(fun(source,period,proposals)->Proposals{source;period;proposals});}letballot_case=Case{tag=6;name="ballot";encoding=(obj4(req"source"Signature.Public_key_hash.encoding)(req"period"Voting_period_repr.encoding)(req"proposal"Protocol_hash.encoding)(req"ballot"Vote_repr.ballot_encoding));select=(function|Contents(Ballot_asop)->Someop|_->None);proj=(function(Ballot{source;period;proposal;ballot})->(source,period,proposal,ballot));inj=(fun(source,period,proposal,ballot)->Ballot{source;period;proposal;ballot});}letmanager_encoding=(obj5(req"source"Contract_repr.encoding)(req"fee"Tez_repr.encoding)(req"counter"(check_size10n))(req"gas_limit"(check_size10n))(req"storage_limit"(check_size10n)))letextract(typekind)(Manager_operation{source;fee;counter;gas_limit;storage_limit;operation=_}:kindKind.managercontents)=(source,fee,counter,gas_limit,storage_limit)letrebuild(source,fee,counter,gas_limit,storage_limit)operation=Manager_operation{source;fee;counter;gas_limit;storage_limit;operation}letmake_manager_casetag(typekind)(Manager_operations.MCasemcase:kindManager_operations.case)=Case{tag;name=mcase.name;encoding=merge_objsmanager_encodingmcase.encoding;select=(function|Contents(Manager_operation({operation;_}asop))->beginmatchmcase.select(Manageroperation)with|None->None|Someoperation->Some(Manager_operation{opwithoperation})end|_->None);proj=(function|Manager_operation{operation;_}asop->(extractop,mcase.projoperation));inj=(fun(op,contents)->(rebuildop(mcase.injcontents)))}letreveal_case=make_manager_case7Manager_operations.reveal_caselettransaction_case=make_manager_case8Manager_operations.transaction_caseletorigination_case=make_manager_case9Manager_operations.origination_caseletdelegation_case=make_manager_case10Manager_operations.delegation_caseletcontents_encoding=letmake(Case{tag;name;encoding;select;proj;inj})=case(Tagtag)nameencoding(funo->matchselectowithNone->None|Someo->Some(projo))(funx->Contents(injx))indef"operation.alpha.contents"@@union[makeendorsement_case;makeseed_nonce_revelation_case;makedouble_endorsement_evidence_case;makedouble_baking_evidence_case;makeactivate_account_case;makeproposals_case;makeballot_case;makereveal_case;maketransaction_case;makeorigination_case;makedelegation_case;]letcontents_list_encoding=convto_listof_list(Variable.listcontents_encoding)letoptional_signature_encoding=conv(functionSomes->s|None->Signature.zero)(funs->ifSignature.equalsSignature.zerothenNoneelseSomes)Signature.encodingletprotocol_data_encoding=def"operation.alpha.contents_and_signature"@@conv(fun(Operation_data{contents;signature})->(Contents_listcontents,signature))(fun(Contents_listcontents,signature)->Operation_data{contents;signature})(obj2(req"contents"contents_list_encoding)(req"signature"optional_signature_encoding))letoperation_encoding=conv(fun({shell;protocol_data})->(shell,protocol_data))(fun(shell,protocol_data)->{shell;protocol_data})(merge_objsOperation.shell_header_encodingprotocol_data_encoding)letunsigned_operation_encoding=def"operation.alpha.unsigned_operation"@@merge_objsOperation.shell_header_encoding(obj1(req"contents"contents_list_encoding))letinternal_operation_encoding=def"operation.alpha.internal_operation"@@conv(fun(Internal_operation{source;operation;nonce})->((source,nonce),Manageroperation))(fun((source,nonce),Manageroperation)->Internal_operation{source;operation;nonce})(merge_objs(obj2(req"source"Contract_repr.encoding)(req"nonce"uint16))Manager_operations.encoding)endletencoding=Encoding.operation_encodingletcontents_encoding=Encoding.contents_encodingletcontents_list_encoding=Encoding.contents_list_encodingletprotocol_data_encoding=Encoding.protocol_data_encodingletunsigned_operation_encoding=Encoding.unsigned_operation_encodingletinternal_operation_encoding=Encoding.internal_operation_encodingletraw({shell;protocol_data}:_operation)=letproto=Data_encoding.Binary.to_bytes_exnprotocol_data_encoding(Operation_dataprotocol_data)in{Operation.shell;proto}letacceptable_passes(op:packed_operation)=letOperation_dataprotocol_data=op.protocol_datainmatchprotocol_data.contentswith|Single(Endorsement_)->[0]|Single(Proposals_)->[1]|Single(Ballot_)->[1]|Single(Seed_nonce_revelation_)->[2]|Single(Double_endorsement_evidence_)->[2]|Single(Double_baking_evidence_)->[2]|Single(Activate_account_)->[2]|Single(Manager_operation_)->[3]|Cons_->[3]typeerror+=Invalid_signature(* `Permanent *)typeerror+=Missing_signature(* `Permanent *)let()=register_error_kind`Permanent~id:"operation.invalid_signature"~title:"Invalid operation signature"~description:"The operation signature is ill-formed \
or has been made with the wrong public key"~pp:(funppf()->Format.fprintfppf"The operation signature is invalid")Data_encoding.unit(functionInvalid_signature->Some()|_->None)(fun()->Invalid_signature);register_error_kind`Permanent~id:"operation.missing_signature"~title:"Missing operation signature"~description:"The operation is of a kind that must be signed, \
but the signature is missing"~pp:(funppf()->Format.fprintfppf"The operation requires a signature")Data_encoding.unit(functionMissing_signature->Some()|_->None)(fun()->Missing_signature)letcheck_signature_sync(typekind)keychain_id({shell;protocol_data}:kindoperation)=letcheck~watermarkcontentssignature=letunsigned_operation=Data_encoding.Binary.to_bytes_exnunsigned_operation_encoding(shell,contents)inifSignature.check~watermarkkeysignatureunsigned_operationthenOk()elseError[Invalid_signature]inmatchprotocol_data.contents,protocol_data.signaturewith|Single_,None->Error[Missing_signature]|Cons_,None->Error[Missing_signature]|Single(Endorsement_)ascontents,Somesignature->check~watermark:(Endorsementchain_id)(Contents_listcontents)signature|Single_ascontents,Somesignature->check~watermark:Generic_operation(Contents_listcontents)signature|Cons_ascontents,Somesignature->check~watermark:Generic_operation(Contents_listcontents)signatureletcheck_signaturepkchain_idop=Lwt.return(check_signature_syncpkchain_idop)lethash_raw=Operation.hashlethash(o:_operation)=letproto=Data_encoding.Binary.to_bytes_exnprotocol_data_encoding(Operation_datao.protocol_data)inOperation.hash{shell=o.shell;proto}lethash_packed(o:packed_operation)=letproto=Data_encoding.Binary.to_bytes_exnprotocol_data_encodingo.protocol_datainOperation.hash{shell=o.shell;proto}type('a,'b)eq=Eq:('a,'a)eqletequal_manager_operation_kind:typeab.amanager_operation->bmanager_operation->(a,b)eqoption=funop1op2->matchop1,op2with|Reveal_,Reveal_->SomeEq|Reveal_,_->None|Transaction_,Transaction_->SomeEq|Transaction_,_->None|Origination_,Origination_->SomeEq|Origination_,_->None|Delegation_,Delegation_->SomeEq|Delegation_,_->Noneletequal_contents_kind:typeab.acontents->bcontents->(a,b)eqoption=funop1op2->matchop1,op2with|Endorsement_,Endorsement_->SomeEq|Endorsement_,_->None|Seed_nonce_revelation_,Seed_nonce_revelation_->SomeEq|Seed_nonce_revelation_,_->None|Double_endorsement_evidence_,Double_endorsement_evidence_->SomeEq|Double_endorsement_evidence_,_->None|Double_baking_evidence_,Double_baking_evidence_->SomeEq|Double_baking_evidence_,_->None|Activate_account_,Activate_account_->SomeEq|Activate_account_,_->None|Proposals_,Proposals_->SomeEq|Proposals_,_->None|Ballot_,Ballot_->SomeEq|Ballot_,_->None|Manager_operationop1,Manager_operationop2->beginmatchequal_manager_operation_kindop1.operationop2.operationwith|None->None|SomeEq->SomeEqend|Manager_operation_,_->Noneletrecequal_contents_kind_list:typeab.acontents_list->bcontents_list->(a,b)eqoption=funop1op2->matchop1,op2with|Singleop1,Singleop2->equal_contents_kindop1op2|Single_,Cons_->None|Cons_,Single_->None|Cons(op1,ops1),Cons(op2,ops2)->beginmatchequal_contents_kindop1op2with|None->None|SomeEq->matchequal_contents_kind_listops1ops2with|None->None|SomeEq->SomeEqendletequal:typeab.aoperation->boperation->(a,b)eqoption=funop1op2->ifnot(Operation_hash.equal(hashop1)(hashop2))thenNoneelseequal_contents_kind_listop1.protocol_data.contentsop2.protocol_data.contents