|
| 1 | +# The MIT License (MIT) |
| 2 | +# Copyright © 2021 Yuma Rao |
| 3 | +# Copyright © 2022 Opentensor Foundation |
| 4 | +# Copyright © 2023 Opentensor Technologies Inc |
| 5 | + |
| 6 | +# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 7 | +# documentation files (the “Software”), to deal in the Software without restriction, including without limitation |
| 8 | +# the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, |
| 9 | +# and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 10 | + |
| 11 | +# The above copyright notice and this permission notice shall be included in all copies or substantial portions of |
| 12 | +# the Software. |
| 13 | + |
| 14 | +# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO |
| 15 | +# THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 16 | +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 17 | +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 18 | +# DEALINGS IN THE SOFTWARE. |
| 19 | + |
| 20 | +__version__ = "0.0.0" |
| 21 | +__ss58_format__ = 42 # Bittensor ss58 format |
| 22 | + |
| 23 | +import argparse |
| 24 | +import copy |
| 25 | +import os |
| 26 | +from typing import Optional |
| 27 | + |
| 28 | +import bittensor_config |
| 29 | + |
| 30 | +from .wallet_impl import Wallet as Wallet, WalletConfig as WalletConfig |
| 31 | +from ._keyfile import Keyfile as Keyfile, KeyFileError as KeyFileError, keyfile as keyfile, serialized_keypair_to_keyfile_data as serialized_keypair_to_keyfile_data |
| 32 | +from .keypair_impl import Keypair as Keypair |
| 33 | +from . import utils as utils |
| 34 | + |
| 35 | + |
| 36 | +class wallet: |
| 37 | + """ Create and init wallet that stores hot and coldkey |
| 38 | + """ |
| 39 | + defaults: WalletConfig = WalletConfig.default() |
| 40 | + |
| 41 | + def __new__( |
| 42 | + cls, |
| 43 | + config: Optional[WalletConfig] = None, |
| 44 | + name: str = None, |
| 45 | + hotkey: str = None, |
| 46 | + path: str = None, |
| 47 | + ) -> 'wallet_impl.Wallet': |
| 48 | + r""" Init bittensor wallet object containing a hot and coldkey. |
| 49 | +
|
| 50 | + Args: |
| 51 | + config (:obj:`bittensor_config.Config`, `optional`): |
| 52 | + bittensor_wallet.wallet.config() |
| 53 | + name (required=False, default='default'): |
| 54 | + The name of the wallet to unlock for running bittensor |
| 55 | + hotkey (required=False, default='default'): |
| 56 | + The name of hotkey used to running the miner. |
| 57 | + path (required=False, default='~/.bittensor/wallets/'): |
| 58 | + The path to your bittensor wallets |
| 59 | + """ |
| 60 | + if config == None: |
| 61 | + config = wallet.config() |
| 62 | + config = copy.deepcopy( config ) |
| 63 | + config.wallet.name = name if name != None else config.wallet.name |
| 64 | + config.wallet.hotkey = hotkey if hotkey != None else config.wallet.hotkey |
| 65 | + config.wallet.path = path if path != None else config.wallet.path |
| 66 | + wallet.check_config( config ) |
| 67 | + |
| 68 | + return wallet_impl.Wallet( |
| 69 | + name = config.wallet.get('name', cls.defaults.name), |
| 70 | + hotkey = config.wallet.get('hotkey', cls.defaults.hotkey), |
| 71 | + path = config.wallet.path, |
| 72 | + config = config |
| 73 | + ) |
| 74 | + |
| 75 | + @classmethod |
| 76 | + def config(cls) -> 'bittensor_config.Config': |
| 77 | + """ Get config from the argument parser |
| 78 | + Return: bittensor_config.config object |
| 79 | + """ |
| 80 | + parser = argparse.ArgumentParser() |
| 81 | + wallet.add_args( parser ) |
| 82 | + return bittensor_config.config( parser ) |
| 83 | + |
| 84 | + @classmethod |
| 85 | + def help(cls): |
| 86 | + """ Print help to stdout |
| 87 | + """ |
| 88 | + parser = argparse.ArgumentParser() |
| 89 | + cls.add_args( parser ) |
| 90 | + print (cls.__new__.__doc__) |
| 91 | + parser.print_help() |
| 92 | + |
| 93 | + @classmethod |
| 94 | + def add_args(cls, parser: argparse.ArgumentParser, prefix: str = None ): |
| 95 | + """ Accept specific arguments from parser |
| 96 | + """ |
| 97 | + prefix_str = '' if prefix == None else prefix + '.' |
| 98 | + try: |
| 99 | + parser.add_argument('--' + prefix_str + 'wallet.name', required=False, default=cls.defaults.name, help='''The name of the wallet to unlock for running bittensor (name mock is reserved for mocking this wallet)''') |
| 100 | + parser.add_argument('--' + prefix_str + 'wallet.hotkey', required=False, default=cls.defaults.hotkey, help='''The name of wallet's hotkey.''') |
| 101 | + parser.add_argument('--' + prefix_str + 'wallet.path', required=False, default=cls.defaults.path, help='''The path to your bittensor wallets''') |
| 102 | + |
| 103 | + except argparse.ArgumentError as e: |
| 104 | + pass |
| 105 | + |
| 106 | + |
| 107 | + @classmethod |
| 108 | + def add_defaults(cls, defaults: bittensor_config.Config, prefix: str = 'wallet' ) -> None: |
| 109 | + """ Adds parser defaults to object, optionally using enviroment variables. |
| 110 | + """ |
| 111 | + default_config = WalletConfig() |
| 112 | + default_config.name = os.getenv('BT_WALLET_NAME') if os.getenv('BT_WALLET_NAME') != None else cls.defaults.name |
| 113 | + default_config.hotkey = os.getenv('BT_WALLET_HOTKEY') if os.getenv('BT_WALLET_HOTKEY') != None else cls.defaults.hotkey |
| 114 | + default_config.path = os.getenv('BT_WALLET_PATH') if os.getenv('BT_WALLET_PATH') != None else cls.defaults.path |
| 115 | + |
| 116 | + setattr( defaults, prefix, default_config ) |
| 117 | + |
| 118 | + @classmethod |
| 119 | + def check_config(cls, config: 'bittensor_config.Config' ): |
| 120 | + """ Check config for wallet name/hotkey/path/hotkeys/sort_by |
| 121 | + """ |
| 122 | + assert 'wallet' in config |
| 123 | + assert isinstance(config.wallet.name, str) |
| 124 | + assert isinstance(config.wallet.hotkey, str ) or config.wallet.hotkey == None # Optional |
| 125 | + assert isinstance(config.wallet.path, str) |
0 commit comments