From 9c5b24f2ce567023bd1e8cb863a6682b13bbad85 Mon Sep 17 00:00:00 2001 From: asdf Date: Sun, 15 Aug 2021 21:30:20 +0900 Subject: [PATCH] Wallet functionalities updated --- monero/backends/jsonrpc/wallet.py | 14 ++++++++++ monero/wallet.py | 46 +++++++++++++++++++++++++++++-- tests/test_jsonrpcwallet.py | 24 +++++++++++++++- 3 files changed, 80 insertions(+), 4 deletions(-) diff --git a/monero/backends/jsonrpc/wallet.py b/monero/backends/jsonrpc/wallet.py index fa5f2b6..fda4e32 100644 --- a/monero/backends/jsonrpc/wallet.py +++ b/monero/backends/jsonrpc/wallet.py @@ -5,6 +5,7 @@ import json import logging import operator import requests +import re from ... import exceptions from ...account import Account @@ -317,6 +318,19 @@ class JSONRPCWallet(object): "Method '{method}' failed with RPC Error of unknown code {code}, " "message: {message}".format(method=method, data=data, result=result, **err)) return result['result'] + + def open_wallet(self, filename, password=''): + params = {'filename': filename, 'password': password} + + return self.raw_request('open_wallet', params=params) + + def create_new_wallet(self, filename, password=''): + params = {'filename': filename, 'password': password, 'language': "English"} + + return self.raw_request('create_wallet', params=params) + + def close_current_wallet(self): + return self.raw_request('close_wallet') _err2exc = { -2: exceptions.WrongAddress, diff --git a/monero/wallet.py b/monero/wallet.py index 25d20d5..a4a1ff5 100644 --- a/monero/wallet.py +++ b/monero/wallet.py @@ -26,20 +26,23 @@ class Wallet(object): The wallet exposes a number of methods that operate on the default account (of index 0). :param backend: a wallet backend + :param wallet_open: if you started the RPC wallet with the argument '--wallet-file', leave it as it is. If the RPC wallet was launched with the argument '--wallet-dir', set its value to False. :param \\**kwargs: arguments to initialize a :class:`JSONRPCWallet ` instance if no backend is given """ accounts = None - def __init__(self, backend=None, **kwargs): + def __init__(self, backend=None, wallet_open=True, **kwargs): if backend and len(kwargs): raise ValueError('backend already given, other arguments are extraneous') self._backend = backend if backend else JSONRPCWallet(**kwargs) self.incoming = PaymentManager(0, self._backend, 'in') self.outgoing = PaymentManager(0, self._backend, 'out') - self.refresh() - + + if wallet_open: + self.refresh() + def refresh(self): """ Reloads the wallet and its accounts. By default, this method is called only once, @@ -66,7 +69,44 @@ class Wallet(object): :rtype: int """ return self._backend.height() + + def open_wallet(self, filename, password=''): + """ + Open a existing wallet within the directory you specified with the argument --wallet-dir, + when launching 'monero-wallet-rpc.' + """ + + if password == '': + self._backend.open_wallet(filename) + else: + self._backend.open_wallet(filename, password) + + self.refresh() + def close_wallet(self): + """ + Saves and closes the wallet and clears the 'accounts' variable of a list type. + If you happened to launch the 'monero-wallet-rpc' with the argument --wallet-file, + you have to restart 'monero-wallet-rpc' without the argument --wallet-file and with the argument --wallet-dir. + """ + + self._backend.close_current_wallet() + + self.accounts.clear() + + def create_wallet(self, filename, password=''): + """ + Creates a new wallet with the 'filename' argument as a name of the wallet file + within the directory you specified with the argument --wallet-dir. + Specify the password if needed. + """ + if password == '': + self._backend.create_new_wallet(filename) + else: + self._backend.create_new_wallet(filename, password) + + self.refresh() + def spend_key(self): """ Returns private spend key. None if wallet is view-only. diff --git a/tests/test_jsonrpcwallet.py b/tests/test_jsonrpcwallet.py index a0ce096..dba8506 100644 --- a/tests/test_jsonrpcwallet.py +++ b/tests/test_jsonrpcwallet.py @@ -36,6 +36,16 @@ class JSONRPCWalletTestCase(JSONTestCase): 'total_balance': 236153709446071, 'total_unlocked_balance': 236153709446071}} + def test_open_wallet(self): + self.wallet = Wallet(JSONRPCWallet(), wallet_open=False) + + self.wallet.open_wallet('ReuseIt', 'hahaha') + + accounts = self.wallet.accounts + + self.assertEqual(type(self.wallet._backend), JSONRPCWallet) + self.assertNotEqual(len(accounts), 0) + @patch('monero.backends.jsonrpc.wallet.requests.post') def test_seed(self, mock_post): mock_post.return_value.status_code = 200 @@ -157,7 +167,7 @@ class JSONRPCWalletTestCase(JSONTestCase): subaddr, index = w.accounts[1].new_address() self.assertIsInstance(subaddr, SubAddress) self.assertIsInstance(index, int) - + @patch('monero.backends.jsonrpc.wallet.requests.post') def test_incoming_confirmed(self, mock_post): mock_post.return_value.status_code = 200 @@ -1271,3 +1281,15 @@ class JSONRPCWalletTestCase(JSONTestCase): with self.assertRaises(ValueError): wallet3 = Wallet(backend=JSONRPCWallet(), port=18089) + + + def test_close_wallet(self): + self.wallet = Wallet(JSONRPCWallet()) + + accounts_result = [] + self.wallet.close_wallet() + + accounts = self.wallet.accounts + self.assertEqual(len(accounts), 0) + +