iota.lib.cpp
IOTA C++ Library
trinary.hpp
1 //
2 // MIT License
3 //
4 // Copyright (c) 2017-2018 Thibault Martinez and Simon Ninon
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in all
14 // copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 // SOFTWARE.
23 //
24 //
25 
26 #pragma once
27 
28 #include <iota/types/trits.hpp>
29 #include <iota/types/trytes.hpp>
30 
31 namespace IOTA {
32 
33 namespace Types {
34 
39 int8_t tryteIndex(const char& tryte);
40 
41 bool isValidTryte(const char& tryte);
42 
46 bool isValidTrytes(const Trytes& trytes);
47 
51 bool isValidTrit(const int8_t& trit);
52 
56 bool isValidHash(const Trytes& s);
57 
65 bool isArrayOfHashes(const std::vector<Trytes>& hashes);
66 
103 Trytes charToTrytes(const char c);
104 
114 Trytes stringToTrytes(const std::string& str);
115 
125 std::string trytesToString(const Trytes& trytes);
126 
127 std::vector<uint8_t> tritsToBytes(const Trits& trits, std::size_t offset = 0);
128 Trits bytesToTrits(const std::vector<uint8_t>& bytes, std::size_t offset = 0);
129 
130 std::vector<uint8_t> trytesToBytes(const Trytes& trytes);
131 Trytes bytesToTrytes(const std::vector<uint8_t>& bytes);
132 
133 Trits trytesToTrits(const Trytes& trytes);
134 Trytes tritsToTrytes(const Trits& trits);
135 Trytes tritsToTrytes(const Trits& trits, std::size_t length);
136 
137 Types::Trits intToTrits(const int64_t& value);
138 Types::Trits intToTrits(const int64_t& value, std::size_t length);
139 
140 template <typename T>
141 T
142 tritsToInt(const Trits& trits) {
143  T res = 0;
144 
145  for (std::size_t i = trits.size(); i > 0; --i) {
146  res = res * 3 + trits[i - 1];
147  }
148 
149  return res;
150 }
151 
157 void incrementTrits(Trits& trits);
158 
159 } // namespace Types
160 
161 } // namespace IOTA
Definition: core.hpp:33