Electroneum
Electroneum::AddressBookImpl Class Reference

#include <address_book.h>

Inheritance diagram for Electroneum::AddressBookImpl:
Collaboration diagram for Electroneum::AddressBookImpl:

Public Member Functions

 AddressBookImpl (WalletImpl *wallet)
 
 ~AddressBookImpl ()
 
void refresh () override
 
std::vector< AddressBookRow * > getAll () const override
 
bool addRow (const std::string &dst_addr, const std::string &payment_id, const std::string &description) override
 
bool deleteRow (std::size_t rowId) override
 
std::string errorString () const override
 
int errorCode () const override
 
int lookupPaymentID (const std::string &payment_id) const override
 
- Public Member Functions inherited from Electroneum::AddressBook
virtual ~AddressBook ()=0
 

Additional Inherited Members

- Public Types inherited from Electroneum::AddressBook
enum  ErrorCode { Status_Ok, General_Error, Invalid_Address, Invalid_Payment_Id }
 

Detailed Description

Definition at line 39 of file address_book.h.

Constructor & Destructor Documentation

◆ AddressBookImpl()

Electroneum::AddressBookImpl::AddressBookImpl ( WalletImpl wallet)

Definition at line 45 of file address_book.cpp.

46  : m_wallet(wallet), m_errorCode(Status_Ok) {}

◆ ~AddressBookImpl()

Electroneum::AddressBookImpl::~AddressBookImpl ( )

Definition at line 175 of file address_book.cpp.

176 {
177  clearRows();
178 }

Member Function Documentation

◆ addRow()

bool Electroneum::AddressBookImpl::addRow ( const std::string &  dst_addr,
const std::string &  payment_id,
const std::string &  description 
)
overridevirtual

Implements Electroneum::AddressBook.

Definition at line 48 of file address_book.cpp.

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 }
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
CXA_THROW_INFO_T * info
Definition: stack_trace.cpp:91
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
Here is the call graph for this function:

◆ deleteRow()

bool Electroneum::AddressBookImpl::deleteRow ( std::size_t  rowId)
overridevirtual

Implements Electroneum::AddressBook.

Definition at line 126 of file address_book.cpp.

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 }
#define LOG_PRINT_L2(x)
Definition: misc_log_ex.h:101
Here is the call graph for this function:

◆ errorCode()

int Electroneum::AddressBookImpl::errorCode ( ) const
inlineoverridevirtual

Implements Electroneum::AddressBook.

Definition at line 53 of file address_book.h.

53 {return m_errorCode;}

◆ errorString()

std::string Electroneum::AddressBookImpl::errorString ( ) const
inlineoverridevirtual

Implements Electroneum::AddressBook.

Definition at line 52 of file address_book.h.

52 {return m_errorString;}

◆ getAll()

std::vector< AddressBookRow * > Electroneum::AddressBookImpl::getAll ( ) const
overridevirtual

Implements Electroneum::AddressBook.

Definition at line 169 of file address_book.cpp.

170 {
171  return m_rows;
172 }

◆ lookupPaymentID()

int Electroneum::AddressBookImpl::lookupPaymentID ( const std::string &  payment_id) const
overridevirtual

Implements Electroneum::AddressBook.

Definition at line 135 of file address_book.cpp.

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 }
::std::string string
Definition: gtest-port.h:1097

◆ refresh()

void Electroneum::AddressBookImpl::refresh ( )
overridevirtual

Implements Electroneum::AddressBook.

Definition at line 97 of file address_book.cpp.

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 }
#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
cryptonote::account_public_address m_address
Definition: wallet2.h:546
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)
static bool parse_short_payment_id(const std::string &payment_id_str, crypto::hash8 &payment_id)
Definition: wallet2.cpp:5725
POD_CLASS hash8
Definition: hash.h:53
const char * address
Definition: multisig.cpp:37
int rows
Definition: crypto.h:86
Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following files: