Basic Account class added

pull/3/head
moneroexamples 6 years ago
parent e2c45cfbbf
commit 249f3fe9e3

@ -0,0 +1,50 @@
#include "Account.h"
namespace xmreg
{
Account::Account(
network_type _nettype,
address_parse_info const& _addr_info,
secret_key const& _viewkey,
secret_key const& _spendkey)
: nettype {_nettype},
addr_info {_addr_info},
viewkey {_viewkey},
spendkey {_spendkey}
{}
Account::Account(
network_type _nettype,
address_parse_info const& _addr_info,
secret_key const& _viewkey)
: nettype {_nettype},
addr_info {addr_info},
viewkey {_viewkey}
{}
Account::Account(
network_type _nettype,
address_parse_info const& _addr_info)
: nettype {_nettype},
addr_info {addr_info}
{}
Account::Account(network_type _nettype,
string const& _address,
string const& _viewkey,
string const& _spendkey)
: nettype {_nettype}
{
if (!get_account_address_from_str(addr_info, nettype, _address))
throw std::runtime_error("Cant parse address: " + _address);
if (!_viewkey.empty())
viewkey = parse_secret_key(_viewkey);
if (!_spendkey.empty())
spendkey = parse_secret_key(_spendkey);
}
}

@ -0,0 +1,122 @@
#pragma once
#include "monero_headers.h"
#include <boost/optional.hpp>
namespace xmreg
{
using epee::string_tools::pod_to_hex;
using epee::string_tools::hex_to_pod;
using namespace cryptonote;
using namespace crypto;
using namespace std;
class Account
{
public:
Account(network_type _nettype,
address_parse_info const& _addr_info,
secret_key const& _viewkey,
secret_key const& _spendkey);
Account(network_type _nettype,
address_parse_info const& _addr_info,
secret_key const& _viewkey);
Account(network_type _nettype,
address_parse_info const& _addr_info);
Account(network_type _nettype,
string const& _addr_info,
string const& _viewkey,
string const& _spendkey);
Account(network_type _nettype,
string const& _addr_info,
string const& _viewkey)
: Account(_nettype, _addr_info, _viewkey, ""s)
{}
Account(network_type _nettype,
string const& _addr_info)
: Account(_nettype, _addr_info, ""s, ""s)
{}
inline auto const& ai() const
{return addr_info;}
inline auto ai2str() const
{return ai_to_str(addr_info, nettype);}
inline auto const& vk() const
{return viewkey;}
inline auto vk2str() const
{return viewkey ? pod_to_hex(*viewkey) : ""s;}
inline auto const& sk() const
{return spendkey;}
inline auto sk2str() const
{return spendkey ? pod_to_hex(*spendkey) : ""s;}
inline auto nt() const
{return nettype;}
static inline string
ai_to_str(address_parse_info const& addr_info,
network_type net_type);
static inline secret_key
parse_secret_key(string const& sk);
friend std::ostream&
operator<<(std::ostream& os, Account const& _acc);
private:
network_type nettype {network_type::STAGENET};
address_parse_info addr_info;
boost::optional<secret_key> viewkey;
boost::optional<secret_key> spendkey;
};
inline secret_key
Account::parse_secret_key(string const& sk)
{
secret_key k;
if (!hex_to_pod(sk, k))
throw std::runtime_error("Cant parse secret key: " + sk);
return k;
}
inline string
Account::ai_to_str(address_parse_info const& addr_info,
network_type net_type)
{
return get_account_address_as_str(
net_type,
addr_info.is_subaddress,
addr_info.address);
}
inline std::ostream&
operator<<(std::ostream& os, Account const& _acc)
{
return os << "nt:" << static_cast<size_t>(_acc.nettype)
<< ",a:" << _acc.ai2str()
<< ",v:" << _acc.vk2str()
<< ",s:" << _acc.sk2str();
}
}

@ -8,7 +8,9 @@ set(SOURCE_FILES
tools.h
tools.cpp
UniversalIdentifier.hpp
UniversalIdentifier.cpp)
UniversalIdentifier.cpp
Account.h
Account.cpp)
# make static library called libmyxrm
# that we are going to link to

@ -97,7 +97,8 @@ public:
rct::key rtc_mask;
rct::key rtc_amount;
friend std::ostream& operator<<(std::ostream& os, info const& _info);
friend std::ostream& operator<<(std::ostream& os,
info const& _info);
};
protected:
@ -148,7 +149,8 @@ public:
uint64_t amount;
public_key out_pub_key;
friend std::ostream& operator<<(std::ostream& os, info const& _info);
friend std::ostream& operator<<(std::ostream& os,
info const& _info);
};

@ -19,5 +19,6 @@ endmacro()
resource_dir("res")
#add_om_test(microcore)
add_om_test(universalidentifier)
add_om_test(account)

@ -0,0 +1,107 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "../src/Account.h"
#include "mocks.h"
namespace
{
using namespace xmreg;
TEST(ACCOUNT, FullConstruction)
{
auto jtx = construct_jsontx("ddff95211b53c194a16c2b8f37ae44b643b8bd46b4cb402af961ecabeb8417b2");
ASSERT_TRUE(jtx);
Account acc {jtx->ntype,
jtx->sender.address,
jtx->sender.viewkey,
jtx->sender.spendkey};
auto const& sender = jtx->jtx["sender"];
EXPECT_EQ(acc.nt(), jtx->ntype);
EXPECT_EQ(acc.ai2str(), sender["address"]);
EXPECT_EQ(acc.vk2str(), sender["viewkey"]);
EXPECT_EQ(acc.sk2str(), sender["spendkey"]);
EXPECT_FALSE(acc.ai().is_subaddress);
}
TEST(ACCOUNT, FullConstructionFromStrings)
{
auto jtx = construct_jsontx("ddff95211b53c194a16c2b8f37ae44b643b8bd46b4cb402af961ecabeb8417b2");
ASSERT_TRUE(jtx);
auto const& sender = jtx->jtx["sender"];
Account acc {jtx->ntype,
sender["address"],
sender["viewkey"],
sender["spendkey"]};
EXPECT_EQ(acc.nt(), jtx->ntype);
EXPECT_EQ(acc.ai().address, jtx->sender.address.address);
EXPECT_EQ(acc.vk(), jtx->sender.viewkey);
EXPECT_EQ(acc.sk(), jtx->sender.spendkey);
}
TEST(ACCOUNT, NoSpendandViewKeiesConstruction)
{
auto jtx = construct_jsontx("ddff95211b53c194a16c2b8f37ae44b643b8bd46b4cb402af961ecabeb8417b2");
ASSERT_TRUE(jtx);
auto const& sender = jtx->jtx["sender"];
Account acc {jtx->ntype,
sender["address"],
sender["viewkey"]};
EXPECT_EQ(acc.nt(), jtx->ntype);
EXPECT_EQ(acc.ai().address, jtx->sender.address.address);
EXPECT_EQ(acc.vk(), jtx->sender.viewkey);
EXPECT_FALSE(acc.sk());
Account acc2 {jtx->ntype,
sender["address"]};
EXPECT_EQ(acc2.nt(), jtx->ntype);
EXPECT_EQ(acc2.ai().address, jtx->sender.address.address);
EXPECT_FALSE(acc2.vk());
EXPECT_FALSE(acc2.sk());
}
TEST(ACCOUNT, FullConstructionSubAddress)
{
auto jtx = construct_jsontx("d7dcb2daa64b5718dad71778112d48ad62f4d5f54337037c420cb76efdd8a21c");
ASSERT_TRUE(jtx);
auto const& recipient1 = jtx->recipients[1];
Account acc {jtx->ntype,
recipient1.address,
recipient1.viewkey,
recipient1.spendkey};
auto const& jrecipient = jtx->jtx["recipient"][1];
EXPECT_EQ(acc.nt(), jtx->ntype);
EXPECT_EQ(acc.ai2str(), jrecipient["address"]);
EXPECT_EQ(acc.vk2str(), jrecipient["viewkey"]);
EXPECT_EQ(acc.sk2str(), jrecipient["spendkey"]);
EXPECT_TRUE(acc.ai().is_subaddress);
}
}

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save