Electroneum
Toggle main menu visibility
Loading...
Searching...
No Matches
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
43
AddressBook::~AddressBook
() {}
44
45
AddressBookImpl::AddressBookImpl
(
WalletImpl
*wallet)
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
52
cryptonote::address_parse_info
info
;
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
97
void
AddressBookImpl::refresh
()
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
175
AddressBookImpl::~AddressBookImpl
()
176
{
177
clearRows();
178
}
179
180
}
// namespace
181
182
namespace
Bitelectroneum
=
Electroneum
;
address_book.h
Electroneum::AddressBookImpl::~AddressBookImpl
~AddressBookImpl()
Definition
address_book.cpp:175
Electroneum::AddressBookImpl::deleteRow
bool deleteRow(std::size_t rowId) override
Definition
address_book.cpp:126
Electroneum::AddressBookImpl::getAll
std::vector< AddressBookRow * > getAll() const override
Definition
address_book.cpp:169
Electroneum::AddressBookImpl::lookupPaymentID
int lookupPaymentID(const std::string &payment_id) const override
Definition
address_book.cpp:135
Electroneum::AddressBookImpl::addRow
bool addRow(const std::string &dst_addr, const std::string &payment_id, const std::string &description) override
Definition
address_book.cpp:48
Electroneum::AddressBookImpl::AddressBookImpl
AddressBookImpl(WalletImpl *wallet)
Definition
address_book.cpp:45
Electroneum::AddressBookImpl::refresh
void refresh() override
Definition
address_book.cpp:97
Electroneum::WalletImpl
Definition
wallet.h:54
tools::wallet2::parse_long_payment_id
static bool parse_long_payment_id(const std::string &payment_id_str, crypto::hash &payment_id)
Definition
wallet2.cpp:5712
tools::wallet2::parse_short_payment_id
static bool parse_short_payment_id(const std::string &payment_id_str, crypto::hash8 &payment_id)
Definition
wallet2.cpp:5725
common_defines.h
tr
#define tr(x)
Definition
common_defines.h:4
memcpy
void * memcpy(void *a, const void *b, size_t c)
Definition
glibc_compat.cpp:16
LOG_PRINT_L2
#define LOG_PRINT_L2(x)
Definition
misc_log_ex.h:101
Electroneum
Definition
address_book.cpp:41
crypto::hash8
POD_CLASS hash8
Definition
hash.h:53
crypto::hash
POD_CLASS hash
Definition
hash.h:50
cryptonote::get_account_address_from_str
bool get_account_address_from_str(address_parse_info &info, network_type nettype, std::string const &str)
Definition
cryptonote_basic_impl.cpp:243
cryptonote::get_account_address_as_str
std::string get_account_address_as_str(network_type nettype, bool subaddress, account_public_address const &adr)
Definition
cryptonote_basic_impl.cpp:207
cryptonote::get_account_integrated_address_as_str
std::string get_account_integrated_address_as_str(network_type nettype, account_public_address const &adr, crypto::hash8 const &payment_id)
Definition
cryptonote_basic_impl.cpp:218
epee::string_tools::pod_to_hex
std::string pod_to_hex(const t_pod_type &s)
Definition
string_tools.h:317
hash.h
false
#define false
info
CXA_THROW_INFO_T * info
Definition
stack_trace.cpp:91
Electroneum::AddressBook::General_Error
@ General_Error
Definition
wallet2_api.h:245
Electroneum::AddressBook::Invalid_Payment_Id
@ Invalid_Payment_Id
Definition
wallet2_api.h:247
Electroneum::AddressBook::Invalid_Address
@ Invalid_Address
Definition
wallet2_api.h:246
Electroneum::AddressBook::Status_Ok
@ Status_Ok
Definition
wallet2_api.h:244
Electroneum::AddressBook::~AddressBook
virtual ~AddressBook()=0
Definition
address_book.cpp:43
Electroneum::AddressBookRow
AddressBookRow - provides functions to manage address book.
Definition
wallet2_api.h:216
cryptonote::address_parse_info
Definition
cryptonote_basic_impl.h:80
tools::wallet2::address_book_row
Definition
wallet2.h:545
tools::wallet2::address_book_row::m_is_subaddress
bool m_is_subaddress
Definition
wallet2.h:549
tools::wallet2::address_book_row::m_description
std::string m_description
Definition
wallet2.h:548
tools::wallet2::address_book_row::m_payment_id
crypto::hash m_payment_id
Definition
wallet2.h:547
tools::wallet2::address_book_row::m_address
cryptonote::account_public_address m_address
Definition
wallet2.h:546
address
const char * address
Definition
multisig.cpp:37
wallet2.h
wallet.h
src
wallet
api
address_book.cpp
Generated on
for Electroneum by
1.17.0