|
| 1 | +# The MIT License (MIT) |
| 2 | + |
| 3 | +# Copyright © 2021 Yuma Rao |
| 4 | +# Copyright © 2022 Opentensor Foundation |
| 5 | +# Copyright © 2023 Opentensor Technologies |
| 6 | + |
| 7 | +# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 8 | +# documentation files (the “Software”), to deal in the Software without restriction, including without limitation |
| 9 | +# the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, |
| 10 | +# and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 11 | + |
| 12 | +# The above copyright notice and this permission notice shall be included in all copies or substantial portions of |
| 13 | +# the Software. |
| 14 | + |
| 15 | +# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO |
| 16 | +# THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 | +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 18 | +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 19 | +# DEALINGS IN THE SOFTWARE. |
| 20 | + |
| 21 | +from bittensor_wallet import serialized_keypair_to_keyfile_data, Keyfile |
| 22 | +from bittensor_wallet import Keypair |
| 23 | + |
| 24 | +class MockKeyfile( Keyfile ): |
| 25 | + """ Defines an interface to a mocked keyfile object (nothing is created on device) keypair is treated as non encrypted and the data is just the string version. |
| 26 | + """ |
| 27 | + def __init__( self, path: str ): |
| 28 | + super().__init__( path ) |
| 29 | + |
| 30 | + self._mock_keypair = Keypair.create_from_mnemonic( mnemonic = 'arrive produce someone view end scout bargain coil slight festival excess struggle' ) |
| 31 | + self._mock_data = serialized_keypair_to_keyfile_data( self._mock_keypair ) |
| 32 | + |
| 33 | + def __str__(self): |
| 34 | + if not self.exists_on_device(): |
| 35 | + return "Keyfile (empty, {})>".format( self.path ) |
| 36 | + if self.is_encrypted(): |
| 37 | + return "Keyfile (encrypted, {})>".format( self.path ) |
| 38 | + else: |
| 39 | + return "Keyfile (decrypted, {})>".format( self.path ) |
| 40 | + |
| 41 | + def __repr__(self): |
| 42 | + return self.__str__() |
| 43 | + |
| 44 | + @property |
| 45 | + def keypair( self ) -> 'Keypair': |
| 46 | + return self._mock_keypair |
| 47 | + |
| 48 | + @property |
| 49 | + def data( self ) -> bytes: |
| 50 | + return bytes(self._mock_data) |
| 51 | + |
| 52 | + @property |
| 53 | + def keyfile_data( self ) -> bytes: |
| 54 | + return bytes( self._mock_data) |
| 55 | + |
| 56 | + def set_keypair ( self, keypair: 'Keypair', encrypt: bool = True, overwrite: bool = False, password:str = None): |
| 57 | + self._mock_keypair = keypair |
| 58 | + self._mock_data = serialized_keypair_to_keyfile_data( self._mock_keypair ) |
| 59 | + |
| 60 | + def get_keypair(self, password: str = None) -> 'Keypair': |
| 61 | + return self._mock_keypair |
| 62 | + |
| 63 | + def make_dirs( self ): |
| 64 | + return |
| 65 | + |
| 66 | + def exists_on_device( self ) -> bool: |
| 67 | + return True |
| 68 | + |
| 69 | + def is_readable( self ) -> bool: |
| 70 | + return True |
| 71 | + |
| 72 | + def is_writable( self ) -> bool: |
| 73 | + return True |
| 74 | + |
| 75 | + def is_encrypted ( self ) -> bool: |
| 76 | + return False |
| 77 | + |
| 78 | + def encrypt( self, password: str = None): |
| 79 | + raise ValueError('Cannot encrypt a mock keyfile') |
| 80 | + |
| 81 | + def decrypt( self, password: str = None): |
| 82 | + return |
0 commit comments