Offline Transaction Signing (OTS) 0.1.0
Loading...
Searching...
No Matches
ots-internal.hpp
Go to the documentation of this file.
1#pragma once
2
13#include "ots.hpp"
14#include "data.hpp"
15#include <memwipe.h> // for memwipe
16// TODO: remove #include "crypto/crypto.h" // for crypto::secret_key
17#include <int-util.h> // for SWAP32LE
18// TODO: remove #include "mnemonics/electrum-words.h"
19#include <cryptonote_basic/cryptonote_basic.h> // for cryptonote::network_type
20// TODO: remove #include "cryptonote_core/cryptonote_core.h" // for cryptonote::address_parse_info
21
22namespace ots {
29 template <typename T>
31 public:
32 explicit ZeroizeReferenceCopy(const T& value): ptr_(new T(value)) {}
34 if(ptr_) {
35 // TODO: investigate if memwipe realy does more than std::memset
36 // std::memset(ptr_.get(), 0, sizeof(T));
37 memwipe(ptr_.get(), sizeof(T));
38 delete ptr_.release();
39 }
40 }
41 operator T&() { return *ptr_; }
42 std::unique_ptr<T> ptr_;
43 };
44
53 cryptonote::network_type cryptonoteNetwork(Network network) noexcept;
54
62 std::vector<std::string> splitString(const std::string &input, const std::string &separator = "", size_t fixedWidth = 4);
63
75 SeedIndices seedIndices(const unsigned char* bytes, size_t byte_length, size_t word_list_length = 1626, size_t bytes_per_chunk = 4, size_t words_per_chunk = 3);
76
78 template<size_t byte_count>
79 std::array<unsigned char, byte_count> seedBytes(
80 const SeedIndices& indices,
81 const size_t word_list_length = 1626,
82 const size_t bytes_per_chunk = 4,
83 const size_t words_per_chunk = 3
84 );
85
87 template<size_t byte_count>
88 std::array<unsigned char, byte_count> seedBytes(
89 const SeedIndices& indices,
90 const size_t word_list_length,
91 const size_t bytes_per_chunk,
92 const size_t words_per_chunk
93 ) {
94 size_t words = indices.size();
95 if (byte_count * words_per_chunk / bytes_per_chunk != words)
96 throw ots::exception::seed::SeedDecodingFailed("Invalid number of indices for byte count.");
97
98 std::array<unsigned char, byte_count> out;
99 for (size_t pos = 0; pos < words; pos += words_per_chunk) {
100 uint32_t chunk = 0;
101 uint32_t previous = 0;
102 uint32_t multiplier = 1;
103 for (size_t word = 0; word < words_per_chunk; word++) {
104 uint32_t current = indices[pos + word];
105 chunk += (multiplier * (((word_list_length - previous) + current) % word_list_length));
106 previous = current;
107 multiplier *= word_list_length;
108 }
109
110 chunk = SWAP32LE(chunk);
111 memcpy(out.data() + (pos / words_per_chunk) * bytes_per_chunk, &chunk, bytes_per_chunk);
112 }
113 return out;
114 }
115
116 extern const std::map<Network, ots::data::NetworkData> NETWORK_DATA_MAP;
118}
Secure container for seed word indices.
Definition ots.hpp:1613
size_t size() const noexcept
Get vector size.
Definition seedindices.cpp:30
A class to handle a reference copy of a value and zeroize it on destruction.
Definition ots-internal.hpp:30
std::unique_ptr< T > ptr_
Definition ots-internal.hpp:42
ZeroizeReferenceCopy(const T &value)
Definition ots-internal.hpp:32
~ZeroizeReferenceCopy()
Definition ots-internal.hpp:33
a seed could not be encoded
Definition ots-exceptions.hpp:349
Constants for the Monero network to calculate the height from a timestamp and reverse.
The library exists complete only in this namespace.
Definition account.hpp:39
const ots::data::NetworkData * getNetworkData(Network network)
Definition ots.cpp:151
Network
Represents the monero network.
Definition ots.hpp:63
cryptonote::network_type cryptonoteNetwork(Network network) noexcept
Converts the network type from the library network type to cryptonote network type.
Definition ots.cpp:99
SeedIndices seedIndices(const unsigned char *bytes, size_t byte_length, size_t word_list_length=1626, size_t bytes_per_chunk=4, size_t words_per_chunk=3)
Returns the indices of a seed phrase.
Definition ots.cpp:105
const std::map< Network, ots::data::NetworkData > NETWORK_DATA_MAP
Definition ots.cpp:145
std::vector< std::string > splitString(const std::string &input, const std::string &separator="", size_t fixedWidth=4)
Converts a string by a separator or fixed 4 char width.
Definition ots.cpp:126
std::array< unsigned char, byte_count > seedBytes(const SeedIndices &indices, const size_t word_list_length=1626, const size_t bytes_per_chunk=4, const size_t words_per_chunk=3)
Definition ots-internal.hpp:88
Header for the C++ library.
Definition data.hpp:33