Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/cpp/wallet/py_monero_wallet_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,29 @@ namespace monero {
std::string get_default_ringdb_path(cryptonote::network_type nettype);
}

// like monero-java's MoneroWalletDefault: the transfers of one direction, from a copy of the query with that direction
static std::vector<std::shared_ptr<monero_incoming_transfer>> get_incoming_transfers(PyMoneroWallet& wallet, const monero_transfer_query& query) {
monero_transfer_query incoming_query = query;
if (gen_utils::bool_equals(false, incoming_query.m_is_incoming)) throw monero_error("Transfer query contradicts getting incoming transfers");
incoming_query.m_is_incoming = true;
std::vector<std::shared_ptr<monero_incoming_transfer>> transfers;
for (const std::shared_ptr<monero_transfer>& transfer : wallet.get_transfers(incoming_query)) {
transfers.push_back(std::static_pointer_cast<monero_incoming_transfer>(transfer));
}
return transfers;
}

static std::vector<std::shared_ptr<monero_outgoing_transfer>> get_outgoing_transfers(PyMoneroWallet& wallet, const monero_transfer_query& query) {
monero_transfer_query outgoing_query = query;
if (gen_utils::bool_equals(false, outgoing_query.is_outgoing())) throw monero_error("Transfer query contradicts getting outgoing transfers");
outgoing_query.m_is_incoming = false;
std::vector<std::shared_ptr<monero_outgoing_transfer>> transfers;
for (const std::shared_ptr<monero_transfer>& transfer : wallet.get_transfers(outgoing_query)) {
transfers.push_back(std::static_pointer_cast<monero_outgoing_transfer>(transfer));
}
return transfers;
}

void py_monero_bind_wallet(py::module_& m, PyMoneroTypes& t) {
// monero_wallet_config
t.py_monero_wallet_config
Expand Down Expand Up @@ -832,6 +855,18 @@ void py_monero_bind_wallet(py::module_& m, PyMoneroTypes& t) {
query.m_subaddress_index = subaddress_index;
MONERO_CATCH_AND_RETHROW(self.get_transfers(query));
}, py::arg("account_index"), py::arg("subaddress_index"), py::call_guard<py::gil_scoped_release>())
.def("get_incoming_transfers", [](PyMoneroWallet& self, const monero_transfer_query& query) {
MONERO_CATCH_AND_RETHROW(get_incoming_transfers(self, query));
}, py::arg("query"), py::call_guard<py::gil_scoped_release>())
.def("get_incoming_transfers", [](PyMoneroWallet& self) {
MONERO_CATCH_AND_RETHROW(get_incoming_transfers(self, monero_transfer_query()));
}, py::call_guard<py::gil_scoped_release>())
.def("get_outgoing_transfers", [](PyMoneroWallet& self, const monero_transfer_query& query) {
MONERO_CATCH_AND_RETHROW(get_outgoing_transfers(self, query));
}, py::arg("query"), py::call_guard<py::gil_scoped_release>())
.def("get_outgoing_transfers", [](PyMoneroWallet& self) {
MONERO_CATCH_AND_RETHROW(get_outgoing_transfers(self, monero_transfer_query()));
}, py::call_guard<py::gil_scoped_release>())
.def("get_outputs", [](PyMoneroWallet& self, const monero_output_query& query) {
MONERO_CATCH_AND_RETHROW(self.get_outputs(query));
}, py::arg("query"), py::call_guard<py::gil_scoped_release>())
Expand Down
42 changes: 42 additions & 0 deletions src/monero/monero_wallet.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ from .monero_output_wallet import MoneroOutputWallet
from .monero_output_query import MoneroOutputQuery
from .monero_transfer import MoneroTransfer
from .monero_transfer_query import MoneroTransferQuery
from .monero_incoming_transfer import MoneroIncomingTransfer
from .monero_outgoing_transfer import MoneroOutgoingTransfer
from .monero_message_signature_type import MoneroMessageSignatureType
from .monero_multisig_sign_result import MoneroMultisigSignResult
from .monero_sync_result import MoneroSyncResult
Expand Down Expand Up @@ -424,6 +426,26 @@ class MoneroWallet:
:returns int: the blockchain's approximate height at the given date.
"""
...
@typing.overload
def get_incoming_transfers(self) -> list[MoneroIncomingTransfer]:
"""
Get all of the wallet's incoming transfers.

:returns list[MoneroIncomingTransfer]: the wallet's incoming transfers.
"""
...
@typing.overload
def get_incoming_transfers(self, query: MoneroTransferQuery) -> list[MoneroIncomingTransfer]:
"""
Get incoming transfers that meet the criteria defined in a query.

The query is copied, and its `incoming` is set to `True`.

:param MoneroTransferQuery query: filters the incoming transfers.
:returns list[MoneroIncomingTransfer]: the wallet's incoming transfers that meet the criteria.
:raises MoneroError: if the query is for outgoing transfers (its `incoming` is `False`).
"""
...
def get_integrated_address(self, standard_address: str = '', payment_id: str = '') -> MoneroIntegratedAddress:
"""
Get an integrated address from a standard address and a payment id.
Expand Down Expand Up @@ -462,6 +484,26 @@ class MoneroWallet:
"""
...
@typing.overload
def get_outgoing_transfers(self) -> list[MoneroOutgoingTransfer]:
"""
Get all of the wallet's outgoing transfers.

:returns list[MoneroOutgoingTransfer]: the wallet's outgoing transfers.
"""
...
@typing.overload
def get_outgoing_transfers(self, query: MoneroTransferQuery) -> list[MoneroOutgoingTransfer]:
"""
Get outgoing transfers that meet the criteria defined in a query.

The query is copied, and its `outgoing` is set to `True`.

:param MoneroTransferQuery query: filters the outgoing transfers.
:returns list[MoneroOutgoingTransfer]: the wallet's outgoing transfers that meet the criteria.
:raises MoneroError: if the query is for incoming transfers (its `outgoing` is `False`).
"""
...
@typing.overload
def get_outputs(self) -> list[MoneroOutputWallet]:
"""
Get outputs created from previous transactions that belong to the wallet
Expand Down
55 changes: 55 additions & 0 deletions tests/wallet/test_keys_and_addresses.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,54 @@ def test_get_address_indices(self, wallet: MoneroWallet) -> None:
wallet.get_address_index("this is definitely not an address")
WalletErrorUtils.test_invalid_address_error(exc_info.value)

def _get_subaddress_address(self, wallet: MoneroWallet) -> str:
"""Get the address of the subaddress [0, 1], creating it if the wallet does not have it.

:param MoneroWallet wallet: wallet to get the subaddress from.
:returns str: the address of the subaddress [0, 1].
"""
if len(wallet.get_subaddresses(0)) < 2:
wallet.create_subaddress(0)
subaddress: str | None = wallet.get_subaddress(0, 1).address
assert subaddress is not None
return subaddress

# Can get an integrated address given a payment id
@pytest.mark.skipif(TestUtils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
def test_get_integrated_address(self, wallet: MoneroWallet) -> None:
# save address for later comparison
address: str = wallet.get_primary_address()

# test valid payment id
payment_id: str = "03284e41c342f036"
integrated_address: MoneroIntegratedAddress = wallet.get_integrated_address("", payment_id)
assert integrated_address.standard_address == address
assert integrated_address.payment_id == payment_id

# test empty payment id which generates a new one
integrated_address = wallet.get_integrated_address()
assert integrated_address.standard_address == address
assert integrated_address.payment_id is not None
assert len(integrated_address.payment_id) > 0

# test with primary address
primary_address: str = wallet.get_primary_address()
integrated_address = wallet.get_integrated_address(primary_address, payment_id)
assert integrated_address.standard_address == primary_address
assert integrated_address.payment_id == payment_id

# test with subaddress
subaddress: str = self._get_subaddress_address(wallet)
with pytest.raises(Exception) as exc_info:
wallet.get_integrated_address(subaddress)
assert str(exc_info.value) == "Subaddress shouldn't be used"

# test invalid payment id
invalid_payment_id: str = "invalid_payment_id_123456"
with pytest.raises(Exception) as exc_info:
wallet.get_integrated_address("", invalid_payment_id)
assert str(exc_info.value) == f"Invalid payment ID: {invalid_payment_id}"

# Can decode an integrated address
@pytest.mark.skipif(TestUtils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
def test_decode_integrated_address(self, wallet: MoneroWallet) -> None:
Expand Down Expand Up @@ -284,6 +332,13 @@ class TestMoneroWalletKeysKeysAndAddresses(KeysWalletSupport, BaseTestMoneroWall
def test_get_address_indices(self, wallet: MoneroWallet) -> None:
return super().test_get_address_indices(wallet)

@override
def _get_subaddress_address(self, wallet: MoneroWallet) -> str:
# a keys-only wallet does not enumerate nor create subaddresses, it derives them
subaddress: str | None = wallet.get_subaddress(0, 1).address
assert subaddress is not None
return subaddress

@pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
@override
def test_get_subaddress_address(self, wallet: MoneroWallet) -> None:
Expand Down
Loading
Loading