Debian Codegraph @

src/wallet/api/address_book.cpp

raw symbols SCIP
1 // Copyright (c) 2014-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
30
31
32 #include "address_book.h"
33 #include "wallet.h"
34 #include "crypto/hash.h"
35 #include "wallet/wallet2.h"
36 #include "common_defines.h"
37
38 #include <vector>
39
40 namespace Monero {
41
42 AddressBook::~AddressBook() {}
43
44 AddressBookImpl::AddressBookImpl(WalletImpl *wallet)
45 : m_wallet(wallet), m_errorCode(Status_Ok) {}
46
47 bool AddressBookImpl::addRow(const std::string &dst_addr , const std::string &payment_id_str, const std::string &description)
48 {
49 clearStatus();
50
51 cryptonote::address_parse_info info;
52 if(!cryptonote::get_account_address_from_str(info, m_wallet->m_wallet->nettype(), dst_addr)) {
53 m_errorString = tr("Invalid destination address");
54 m_errorCode = Invalid_Address;
55 return false;
56 }
57
58 if (!payment_id_str.empty())
59 {
60 m_errorString = tr("Payment ID supplied: this is obsolete");
61 m_errorCode = Invalid_Payment_Id;
62 return false;
63 }
64
65 bool r = m_wallet->m_wallet->add_address_book_row(info.address, info.has_payment_id ? &info.payment_id : NULL,description,info.is_subaddress);
66 if (r)
67 refresh();
68 else
69 m_errorCode = General_Error;
70 return r;
71 }
72
73 bool AddressBookImpl::setDescription(std::size_t index, const std::string &description)
74 {
75 clearStatus();
76
77 const auto ab = m_wallet->m_wallet->get_address_book();
78 if (index >= ab.size()){
79 return false;
80 }
81
82 tools::wallet2::address_book_row entry = ab[index];
83 entry.m_description = description;
84 bool r = m_wallet->m_wallet->set_address_book_row(index, entry.m_address, entry.m_has_payment_id ? &entry.m_payment_id : nullptr, entry.m_description, entry.m_is_subaddress);
85 if (r)
86 refresh();
87 else
88 m_errorCode = General_Error;
89 return r;
90 }
91
92 void AddressBookImpl::refresh()
93 {
94 LOG_PRINT_L2("Refreshing addressbook");
95
96 clearRows();
97
98 // Fetch from Wallet2 and create vector of AddressBookRow objects
99 std::vector<tools::wallet2::address_book_row> rows = m_wallet->m_wallet->get_address_book();
100 for (size_t i = 0; i < rows.size(); ++i) {
101 tools::wallet2::address_book_row * row = &rows.at(i);
102
103 std::string address;
104 if (row->m_has_payment_id)
105 address = cryptonote::get_account_integrated_address_as_str(m_wallet->m_wallet->nettype(), row->m_address, row->m_payment_id);
106 else
107 address = get_account_address_as_str(m_wallet->m_wallet->nettype(), row->m_is_subaddress, row->m_address);
108 AddressBookRow * abr = new AddressBookRow(i, address, "", row->m_description);
109 m_rows.push_back(abr);
110 }
111
112 }
113
114 bool AddressBookImpl::deleteRow(std::size_t rowId)
115 {
116 LOG_PRINT_L2("Deleting address book row " << rowId);
117 bool r = m_wallet->m_wallet->delete_address_book_row(rowId);
118 if (r)
119 refresh();
120 return r;
121 }
122
123 int AddressBookImpl::lookupPaymentID(const std::string &payment_id) const
124 {
125 // turn short ones into long ones for comparison
126 const std::string long_payment_id = payment_id + std::string(64 - payment_id.size(), '0');
127
128 int idx = -1;
129 for (const auto &row: m_rows) {
130 ++idx;
131 // this does short/short and long/long
132 if (payment_id == row->getPaymentId())
133 return idx;
134 // short/long
135 if (long_payment_id == row->getPaymentId())
136 return idx;
137 // one case left: payment_id was long, row's is short
138 const std::string long_row_payment_id = row->getPaymentId() + std::string(64 - row->getPaymentId().size(), '0');
139 if (payment_id == long_row_payment_id)
140 return idx;
141 }
142 return -1;
143 }
144
145 void AddressBookImpl::clearRows() {
146 for (auto r : m_rows) {
147 delete r;
148 }
149 m_rows.clear();
150 }
151
152 void AddressBookImpl::clearStatus(){
153 m_errorString = "";
154 m_errorCode = 0;
155 }
156
157 std::vector<AddressBookRow*> AddressBookImpl::getAll() const
158 {
159 return m_rows;
160 }
161
162
163 AddressBookImpl::~AddressBookImpl()
164 {
165 clearRows();
166 }
167
168 } // namespace