Electroneum
address_book.cpp
Go to the documentation of this file.
1 // Copyrights(c) 2017-2021, The Electroneum Project
2 // Copyrights(c) 2014-2019, The Monero Project
3 //
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without modification, are
7 // permitted provided that the following conditions are met:
8 //
9 // 1. Redistributions of source code must retain the above copyright notice, this list of
10 // conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 // of conditions and the following disclaimer in the documentation and/or other
14 // materials provided with the distribution.
15 //
16 // 3. Neither the name of the copyright holder nor the names of its contributors may be
17 // used to endorse or promote products derived from this software without specific
18 // prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
31 
32 
33 #include "address_book.h"
34 #include "wallet.h"
35 #include "crypto/hash.h"
36 #include "wallet/wallet2.h"
37 #include "common_defines.h"
38 
39 #include <vector>
40 
41 namespace Electroneum {
42 
44 
46  : m_wallet(wallet), m_errorCode(Status_Ok) {}
47 
48 bool AddressBookImpl::addRow(const std::string &dst_addr , const std::string &payment_id_str, const std::string &description)
49 {
50  clearStatus();
51 
53  if(!cryptonote::get_account_address_from_str(info, m_wallet->m_wallet->nettype(), dst_addr)) {
54  m_errorString = tr("Invalid destination address");
55  m_errorCode = Invalid_Address;
56  return false;
57  }
58 
59  crypto::hash payment_id = crypto::null_hash;
60  bool has_long_pid = (payment_id_str.empty())? false : tools::wallet2::parse_long_payment_id(payment_id_str, payment_id);
61 
62  // Short payment id provided
63  if(payment_id_str.length() == 16) {
64  m_errorString = tr("Invalid payment ID. Short payment ID should only be used in an integrated address");
65  m_errorCode = Invalid_Payment_Id;
66  return false;
67  }
68 
69  // long payment id provided but not valid
70  if(!payment_id_str.empty() && !has_long_pid) {
71  m_errorString = tr("Invalid payment ID");
72  m_errorCode = Invalid_Payment_Id;
73  return false;
74  }
75 
76  // integrated + long payment id provided
77  if(has_long_pid && info.has_payment_id) {
78  m_errorString = tr("Integrated address and long payment ID can't be used at the same time");
79  m_errorCode = Invalid_Payment_Id;
80  return false;
81  }
82 
83  // Pad short pid with zeros
84  if (info.has_payment_id)
85  {
86  memcpy(payment_id.data, info.payment_id.data, 8);
87  }
88 
89  bool r = m_wallet->m_wallet->add_address_book_row(info.address,payment_id,description,info.is_subaddress);
90  if (r)
91  refresh();
92  else
93  m_errorCode = General_Error;
94  return r;
95 }
96 
98 {
99  LOG_PRINT_L2("Refreshing addressbook");
100 
101  clearRows();
102 
103  // Fetch from Wallet2 and create vector of AddressBookRow objects
104  std::vector<tools::wallet2::address_book_row> rows = m_wallet->m_wallet->get_address_book();
105  for (size_t i = 0; i < rows.size(); ++i) {
106  tools::wallet2::address_book_row * row = &rows.at(i);
107 
108  std::string payment_id = (row->m_payment_id == crypto::null_hash)? "" : epee::string_tools::pod_to_hex(row->m_payment_id);
109  std::string address = cryptonote::get_account_address_as_str(m_wallet->m_wallet->nettype(), row->m_is_subaddress, row->m_address);
110  // convert the zero padded short payment id to integrated address
111  if (!row->m_is_subaddress && payment_id.length() > 16 && payment_id.substr(16).find_first_not_of('0') == std::string::npos) {
112  payment_id = payment_id.substr(0,16);
113  crypto::hash8 payment_id_short;
114  if(tools::wallet2::parse_short_payment_id(payment_id, payment_id_short)) {
115  address = cryptonote::get_account_integrated_address_as_str(m_wallet->m_wallet->nettype(), row->m_address, payment_id_short);
116  // Don't show payment id when integrated address is used
117  payment_id = "";
118  }
119  }
120  AddressBookRow * abr = new AddressBookRow(i, address, payment_id, row->m_description);
121  m_rows.push_back(abr);
122  }
123 
124 }
125 
126 bool AddressBookImpl::deleteRow(std::size_t rowId)
127 {
128  LOG_PRINT_L2("Deleting address book row " << rowId);
129  bool r = m_wallet->m_wallet->delete_address_book_row(rowId);
130  if (r)
131  refresh();
132  return r;
133 }
134 
135 int AddressBookImpl::lookupPaymentID(const std::string &payment_id) const
136 {
137  // turn short ones into long ones for comparison
138  const std::string long_payment_id = payment_id + std::string(64 - payment_id.size(), '0');
139 
140  int idx = -1;
141  for (const auto &row: m_rows) {
142  ++idx;
143  // this does short/short and long/long
144  if (payment_id == row->getPaymentId())
145  return idx;
146  // short/long
147  if (long_payment_id == row->getPaymentId())
148  return idx;
149  // one case left: payment_id was long, row's is short
150  const std::string long_row_payment_id = row->getPaymentId() + std::string(64 - row->getPaymentId().size(), '0');
151  if (payment_id == long_row_payment_id)
152  return idx;
153  }
154  return -1;
155 }
156 
157 void AddressBookImpl::clearRows() {
158  for (auto r : m_rows) {
159  delete r;
160  }
161  m_rows.clear();
162 }
163 
164 void AddressBookImpl::clearStatus(){
165  m_errorString = "";
166  m_errorCode = 0;
167 }
168 
169 std::vector<AddressBookRow*> AddressBookImpl::getAll() const
170 {
171  return m_rows;
172 }
173 
174 
176 {
177  clearRows();
178 }
179 
180 } // namespace
181 
182 namespace Bitelectroneum = Electroneum;
static bool parse_long_payment_id(const std::string &payment_id_str, crypto::hash &payment_id)
Definition: wallet2.cpp:5712
#define tr(x)
Definition: common_defines.h:4
#define LOG_PRINT_L2(x)
Definition: misc_log_ex.h:101
std::string get_account_address_as_str(network_type nettype, bool subaddress, account_public_address const &adr)
::std::string string
Definition: gtest-port.h:1097
bool addRow(const std::string &dst_addr, const std::string &payment_id, const std::string &description) override
cryptonote::account_public_address m_address
Definition: wallet2.h:546
std::vector< AddressBookRow * > getAll() const override
std::string pod_to_hex(const t_pod_type &s)
Definition: string_tools.h:317
std::string get_account_integrated_address_as_str(network_type nettype, account_public_address const &adr, crypto::hash8 const &payment_id)
bool deleteRow(std::size_t rowId) override
AddressBookImpl(WalletImpl *wallet)
AddressBookRow - provides functions to manage address book.
Definition: wallet2_api.h:216
static bool parse_short_payment_id(const std::string &payment_id_str, crypto::hash8 &payment_id)
Definition: wallet2.cpp:5725
CXA_THROW_INFO_T * info
Definition: stack_trace.cpp:91
POD_CLASS hash8
Definition: hash.h:53
void * memcpy(void *a, const void *b, size_t c)
bool get_account_address_from_str(address_parse_info &info, network_type nettype, std::string const &str)
POD_CLASS hash
Definition: hash.h:50
const char * address
Definition: multisig.cpp:37
int lookupPaymentID(const std::string &payment_id) const override
int rows
Definition: crypto.h:86