Beispiel #1
0
 def test_can_set_options_set_in_user_config(self):
     another_path = tempfile.mkdtemp()
     fake_read_user = lambda _: {"electrum_path": self.electrum_dir}
     read_user_dir = lambda: self.user_dir
     config = SimpleConfig(options={},
                           read_user_config_function=fake_read_user,
                           read_user_dir_function=read_user_dir)
     config.set_key("electrum_path", another_path)
     self.assertEqual(another_path, config.get("electrum_path"))
Beispiel #2
0
 def test_cannot_set_options_passed_by_command_line(self):
     fake_read_user = lambda _: {"electrum_path": "b"}
     read_user_dir = lambda: self.user_dir
     config = SimpleConfig(options=self.options,
                           read_user_config_function=fake_read_user,
                           read_user_dir_function=read_user_dir)
     config.set_key("electrum_path", "c")
     self.assertEqual(self.options.get("electrum_path"),
                      config.get("electrum_path"))
 def test_can_set_options_set_in_user_config(self):
     another_path = tempfile.mkdtemp()
     fake_read_user = lambda _: {"electrum_path": self.electrum_dir}
     read_user_dir = lambda : self.user_dir
     config = SimpleConfig(options={},
                           read_user_config_function=fake_read_user,
                           read_user_dir_function=read_user_dir)
     config.set_key("electrum_path", another_path)
     self.assertEqual(another_path, config.get("electrum_path"))
 def test_cannot_set_options_passed_by_command_line(self):
     fake_read_user = lambda _: {"electrum_path": "b"}
     read_user_dir = lambda : self.user_dir
     config = SimpleConfig(options=self.options,
                           read_user_config_function=fake_read_user,
                           read_user_dir_function=read_user_dir)
     config.set_key("electrum_path", "c")
     self.assertEqual(self.options.get("electrum_path"),
                      config.get("electrum_path"))
class ElectrumWallet:
    """
    An Electrum wallet wrapper
    """
    def __init__(self, name, config):
        """
        Initializes new electrum wallet instance

        @param name: Name of the wallet
        @param config: Configuration dictionary e.g {
            'server': 'localhost:7777:s',
            'rpc_user': '******',
            'rpc_pass_': 'pass',
            'electrum_path': '/opt/var/data/electrum',
            'seed': '....',
            'fee': 10000,
            'testnet': 1
        }
        """
        self._name = name
        self._config = config
        self._config['testnet'] = bool(self._config['testnet'])
        if self._config['testnet'] is True:
            constants.set_testnet()

        self._config['verbos'] = False
        self._electrum_config = SimpleConfig(self._config)
        self._wallet_path = os.path.join(self._electrum_config.path, 'wallets',
                                         self._name)
        self._storage = WalletStorage(path=self._wallet_path)
        if not self._storage.file_exists():
            self._electrum_config.set_key('default_wallet_path',
                                          self._wallet_path)
            k = keystore.from_seed(self._config['seed'],
                                   self._config['passphrase'], False)
            k.update_password(None, self._config['password'])
            self._storage.put('keystore', k.dump())
            self._storage.put('wallet_type', 'standard')
            self._storage.put('use_encryption', bool(self._config['password']))
            self._storage.write()
            self._wallet = Wallet(self._storage)
            # self._server = daemon.get_server(self._electrum_config)
            self._network = Network(self._electrum_config)
            self._network.start()
            self._wallet.start_threads(self._network)
            self._wallet.synchronize()
            self._wallet.wait_until_synchronized()
            self._wallet.stop_threads()
            self._wallet.storage.write()
        else:
            self._network = None
            self._wallet = self._wallet = Wallet(self._storage)

        self._commands = Commands(config=self._electrum_config,
                                  wallet=self._wallet,
                                  network=self._network)

        self._init_commands()

    def _init_commands(self):
        """
        Scans the electrum commands class and binds all its methods to this class
        """
        execlude_cmd = lambda item: (not item[0].startswith('_')) and item[
            0] not in EXECLUDED_COMMANDS
        for name, func in filter(
                execlude_cmd,
                inspect.getmembers(self._commands, inspect.ismethod)):
            setattr(self, name, func)
from electrum.commands import Commands


config = SimpleConfig({"testnet": True})  # to use ~/.electrum/testnet as datadir
constants.set_testnet()  # to set testnet magic bytes
daemon = Daemon(config, listen_jsonrpc=False)
network = daemon.network
assert network.asyncio_loop.is_running()

command_runner = Commands(config, wallet=None, network=network)

# get wallet on disk
wallet_dir = os.path.dirname(config.get_wallet_path())
wallet_path = os.path.join(wallet_dir, "test_wallet")
if not os.path.exists(wallet_path):
    config.set_key('wallet_path', wallet_path)
    command_runner.create(segwit=True)

# open wallet
storage = WalletStorage(wallet_path)
wallet = Wallet(storage)
wallet.start_network(network)

# you can use ~CLI commands by accessing command_runner
command_runner.wallet = wallet
print("balance", command_runner.getbalance())
print("addr",    command_runner.getunusedaddress())
print("gettx",   command_runner.gettransaction("bd3a700b2822e10a034d110c11a596ee7481732533eb6aca7f9ca02911c70a4f"))

# but you might as well interact with the underlying methods directly
print("balance", wallet.get_balance())