123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2021 Nomadic Labs, <contact@nomadic-labs.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. *)(* *)(*****************************************************************************)typeerror+=|(* `Branch *)Unrevealed_manager_keyofContract_repr.t|(* `Permanent *)Inconsistent_hashof{public_key:Signature.Public_key.t;expected_hash:Signature.Public_key_hash.t;provided_hash:Signature.Public_key_hash.t;}|(* `Branch *)Previously_revealed_keyofContract_repr.tlet()=register_error_kind`Branch~id:"contract.unrevealed_key"~title:"Manager operation precedes key revelation"~description:"One tried to apply a manager operation without revealing the manager \
public key"~pp:(funppfs->Format.fprintfppf"Unrevealed manager key for contract %a."Contract_repr.pps)Data_encoding.(obj1(req"contract"Contract_repr.encoding))(functionUnrevealed_manager_keys->Somes|_->None)(funs->Unrevealed_manager_keys);register_error_kind`Permanent~id:"contract.manager.inconsistent_hash"~title:"Inconsistent public key hash"~description:"A revealed manager public key is inconsistent with the announced hash"~pp:(funppf(k,eh,ph)->Format.fprintfppf"The hash of the manager public key %s is not %a as announced but %a"(Signature.Public_key.to_b58checkk)Signature.Public_key_hash.ppphSignature.Public_key_hash.ppeh)Data_encoding.(obj3(req"public_key"Signature.Public_key.encoding)(req"expected_hash"Signature.Public_key_hash.encoding)(req"provided_hash"Signature.Public_key_hash.encoding))(function|Inconsistent_hash{public_key;expected_hash;provided_hash}->Some(public_key,expected_hash,provided_hash)|_->None)(fun(public_key,expected_hash,provided_hash)->Inconsistent_hash{public_key;expected_hash;provided_hash});register_error_kind`Branch~id:"contract.previously_revealed_key"~title:"Manager operation already revealed"~description:"One tried to reveal twice a manager public key"~pp:(funppfs->Format.fprintfppf"Previously revealed manager key for contract %a."Contract_repr.pps)Data_encoding.(obj1(req"contract"Contract_repr.encoding))(functionPreviously_revealed_keys->Somes|_->None)(funs->Previously_revealed_keys)letinit=Storage.Contract.Manager.initletis_manager_key_revealedcmanager=letcontract=Contract_repr.implicit_contractmanagerinStorage.Contract.Manager.findccontract>>=?function|None->return_false|Some(Manager_repr.Hash_)->return_false|Some(Manager_repr.Public_key_)->return_trueletreveal_manager_keycmanagerpublic_key=letcontract=Contract_repr.implicit_contractmanagerinStorage.Contract.Manager.getccontract>>=?function|Public_key_->fail(Previously_revealed_keycontract)|Hashv->letactual_hash=Signature.Public_key.hashpublic_keyinifSignature.Public_key_hash.equalactual_hashvthenletv=Manager_repr.Public_keypublic_keyinStorage.Contract.Manager.updateccontractvelsefail(Inconsistent_hash{public_key;expected_hash=v;provided_hash=actual_hash})letget_manager_key?errorctxtpkh=letcontract=Contract_repr.implicit_contractpkhinStorage.Contract.Manager.findctxtcontract>>=?function|None->(matcherrorwith|None->failwith"get_manager_key"|Someerror->failerror)|Some(Manager_repr.Hash_)->(matcherrorwith|None->fail(Unrevealed_manager_keycontract)|Someerror->failerror)|Some(Manager_repr.Public_keypk)->returnpkletremove_existing=Storage.Contract.Manager.remove_existing