From 5e876ceb84f82f71cc39b09b0191d50ed5d0801c Mon Sep 17 00:00:00 2001 From: dsc Date: Sat, 21 May 2022 04:16:30 +0200 Subject: [PATCH] Lookup table for restore heights and a blockheight calculation function Software wallets that use this library for 14 word mnemonic seed(s) may now use `seed->blockheight()` to obtain the Wownero seed blockheight (restore height) - previously, software wallets had to implement this functionality themselves. --- CMakeLists.txt | 1 + include/wownero_seed/heights.hpp | 2 + include/wownero_seed/wownero_seed.hpp | 1 + src/heights.cpp | 258 ++++++++++++++++++++++++++ src/main.cpp | 1 + src/wownero_seed.cpp | 5 + 6 files changed, 268 insertions(+) create mode 100644 include/wownero_seed/heights.hpp create mode 100644 src/heights.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e7462d2..341ea58 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,6 +21,7 @@ src/argon2/ref.c src/galois_field.cpp src/gf_elem.cpp src/gf_poly.cpp +src/heights.cpp src/wownero_seed.cpp src/pbkdf2.c src/reed_solomon_code.cpp diff --git a/include/wownero_seed/heights.hpp b/include/wownero_seed/heights.hpp new file mode 100644 index 0000000..ad97a52 --- /dev/null +++ b/include/wownero_seed/heights.hpp @@ -0,0 +1,2 @@ +#pragma once +unsigned int dateToRestoreHeight(time_t _t); \ No newline at end of file diff --git a/include/wownero_seed/wownero_seed.hpp b/include/wownero_seed/wownero_seed.hpp index 42a06f0..f2b71fb 100644 --- a/include/wownero_seed/wownero_seed.hpp +++ b/include/wownero_seed/wownero_seed.hpp @@ -23,6 +23,7 @@ public: std::time_t date() const { return date_; } + unsigned blockheight() const; const std::string& correction() const { return correction_; } diff --git a/src/heights.cpp b/src/heights.cpp new file mode 100644 index 0000000..6a8e1f9 --- /dev/null +++ b/src/heights.cpp @@ -0,0 +1,258 @@ +#include +#include +#include +#include + +#include + +std::map heightLookup = { + {1522624244, 1}, + {1523019716, 2000}, + {1523798485, 4000}, + {1524369547, 6000}, + {1524928599, 8000}, + {1525536409, 10000}, + {1526135584, 12000}, + {1526746540, 14000}, + {1527366570, 16000}, + {1527977555, 18000}, + {1528596889, 20000}, + {1529196634, 22000}, + {1529812899, 24000}, + {1530426833, 26000}, + {1531043072, 28000}, + {1531660804, 30000}, + {1532267242, 32000}, + {1532867728, 34000}, + {1533470854, 36000}, + {1534072871, 38000}, + {1534666585, 40000}, + {1535290349, 42000}, + {1535889551, 44000}, + {1536503490, 46000}, + {1537109771, 48000}, + {1537716080, 50000}, + {1538321475, 52000}, + {1538927269, 54000}, + {1539529646, 56000}, + {1540147604, 58000}, + {1540788117, 60000}, + {1541462710, 62000}, + {1542141772, 64000}, + {1543204218, 66000}, + {1544189412, 68000}, + {1545142715, 70000}, + {1546002226, 72000}, + {1546850714, 74000}, + {1547960022, 76000}, + {1548921502, 78000}, + {1549693643, 80000}, + {1550599183, 82000}, + {1551184199, 84000}, + {1551779594, 86000}, + {1552398262, 88000}, + {1552994646, 90000}, + {1553601832, 92000}, + {1554201779, 94000}, + {1554801655, 96000}, + {1555400363, 98000}, + {1556001445, 100000}, + {1556602014, 102000}, + {1557214500, 104000}, + {1557811286, 106000}, + {1558413175, 108000}, + {1559009373, 110000}, + {1559610734, 112000}, + {1560207825, 114000}, + {1560778626, 116000}, + {1561370593, 118000}, + {1561983210, 120000}, + {1562593362, 122000}, + {1563205114, 124000}, + {1563794760, 126000}, + {1564403277, 128000}, + {1564995864, 130000}, + {1565606876, 132000}, + {1566200716, 134000}, + {1566794840, 136000}, + {1567391114, 138000}, + {1567989857, 140000}, + {1568594389, 142000}, + {1569196321, 144000}, + {1569793216, 146000}, + {1570390761, 148000}, + {1570991907, 150000}, + {1571598059, 152000}, + {1572192822, 154000}, + {1572791579, 156000}, + {1573392270, 158000}, + {1573987210, 160000}, + {1574600312, 162000}, + {1575203907, 164000}, + {1575802276, 166000}, + {1576401859, 168000}, + {1577002032, 170000}, + {1577598018, 172000}, + {1578199780, 174000}, + {1578802610, 176000}, + {1579403148, 178000}, + {1580004525, 180000}, + {1580600038, 182000}, + {1581202399, 184000}, + {1581795609, 186000}, + {1582398217, 188000}, + {1583000366, 190000}, + {1583601824, 192000}, + {1584200913, 194000}, + {1584796804, 196000}, + {1585404000, 198000}, + {1585987106, 200000}, + {1586591709, 202000}, + {1587201986, 204000}, + {1587813470, 206000}, + {1588431854, 208000}, + {1589057972, 210000}, + {1589669885, 212000}, + {1590276257, 214000}, + {1590876865, 216000}, + {1591475760, 218000}, + {1592067104, 220000}, + {1592681388, 222000}, + {1593290342, 224000}, + {1593896282, 226000}, + {1594489197, 228000}, + {1595105073, 230000}, + {1595706859, 232000}, + {1596303095, 234000}, + {1596887281, 236000}, + {1597498842, 238000}, + {1598099448, 240000}, + {1598705242, 242000}, + {1599308983, 244000}, + {1599914532, 246000}, + {1600507836, 248000}, + {1601109430, 250000}, + {1601710572, 252000}, + {1602305645, 254000}, + {1602898089, 256000}, + {1603501419, 258000}, + {1604102323, 260000}, + {1604694785, 262000}, + {1605289509, 264000}, + {1605889778, 266000}, + {1606493643, 268000}, + {1607083718, 270000}, + {1607693879, 272000}, + {1608288061, 274000}, + {1608890342, 276000}, + {1609479282, 278000}, + {1610078383, 280000}, + {1610678028, 282000}, + {1611276041, 284000}, + {1611874904, 286000}, + {1612479368, 288000}, + {1613076479, 290000}, + {1613679332, 292000}, + {1614269634, 294000}, + {1614866506, 296000}, + {1615467310, 298000}, + {1616071879, 300000}, + {1616668758, 302000}, + {1617268180, 304000}, + {1617878649, 306000}, + {1618490259, 308000}, + {1619069699, 310000}, + {1619658329, 312000}, + {1620256511, 314000}, + {1620854930, 316000}, + {1621453789, 318000}, + {1622047235, 320000}, + {1622644711, 322000}, + {1623238613, 324000}, + {1623834283, 326000}, + {1624440270, 328000}, + {1625034084, 330000}, + {1625411381, 332000}, + {1626061496, 334000}, + {1626634887, 336000}, + {1627198199, 338000}, + {1627767827, 340000}, + {1628363606, 342000}, + {1628975947, 344000}, + {1629547598, 346000}, + {1630145454, 348000}, + {1630719697, 350000}, + {1631356160, 352000}, + {1631986172, 354000}, + {1632569490, 356000}, + {1633170324, 358000}, + {1633775768, 360000}, + {1634383491, 362000}, + {1635002324, 364000}, + {1635617950, 366000}, + {1636210188, 368000}, + {1636817670, 370000}, + {1637418065, 372000}, + {1638024048, 374000}, + {1638643331, 376000}, + {1639235224, 378000}, + {1639896738, 380000}, + {1640494376, 382000}, + {1641106951, 384000}, + {1641676722, 386000}, + {1642298995, 388000}, + {1643009648, 390000}, + {1643571105, 392000}, + {1644187183, 394000}, + {1644747412, 396000}, + {1645350272, 398000}, + {1645949729, 400000}, + {1646543179, 402000}, + {1647168997, 404000}, + {1647725920, 406000}, + {1648354637, 408000}, + {1648978293, 410000}, + {1649555694, 412000}, + {1650212215, 414000}, + {1650774962, 416000}, + {1651412717, 418000}, + {1651999579, 420000} +}; + + +unsigned int dateToRestoreHeight(const time_t _t) { + auto t = static_cast(_t); + unsigned int prev = 0; + + for (auto const&item: heightLookup) { + auto epoch = item.first; + auto height = item.second; + + if(t <= epoch) { + if(prev == 0) return 1; + return heightLookup[prev]; + } + + prev = item.first; + } + + // height not found in the lookup table, calculate + auto begin = heightLookup.begin(); + unsigned first_epoch = begin->first; + unsigned first_height = begin->second; + + unsigned last_epoch = prev; + unsigned last_height = heightLookup[prev]; + + const unsigned secs_per_day = 86400; + unsigned days = (last_epoch - first_epoch) / secs_per_day; + unsigned blocks_per_day = last_height / days; + blocks_per_day = blocks_per_day - 10; // fault tolerance + + unsigned days_delta = (t - last_epoch) / secs_per_day; + if(days_delta == 0) return last_height; + + unsigned blocks_delta = days_delta * blocks_per_day; + unsigned calculated_height = last_height + blocks_delta; + return calculated_height; +} diff --git a/src/main.cpp b/src/main.cpp index 33590f9..d813fc1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -63,6 +63,7 @@ void print_seed(const wownero_seed& seed, const char* coin, bool phrase) { auto created_on = seed.date(); std::tm tm = *std::localtime(&created_on); std::cout << "- created on or after: " << std::put_time(&tm, "%d/%b/%Y") << std::endl; + std::cout << "- blockheight: " << seed.blockheight() << std::endl; } int main(int argc, const char* argv[]) { diff --git a/src/wownero_seed.cpp b/src/wownero_seed.cpp index 523bce4..283f7f2 100644 --- a/src/wownero_seed.cpp +++ b/src/wownero_seed.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include "argon2/argon2.h" #include "argon2/blake2/blake2-impl.h" #include "pbkdf2.h" @@ -222,6 +223,10 @@ wownero_seed::wownero_seed(const std::string& phrase, const std::string& coin) { pbkdf2_hmac_sha256(seed_.data(), seed_.size(), salt, sizeof(salt), pbkdf2_iterations, key_.data(), key_.size()); } +unsigned wownero_seed::blockheight() const { + return dateToRestoreHeight(this->date()); +} + std::ostream& operator<<(std::ostream& os, const wownero_seed& seed) { for (int i = 0; i <= seed.message_.degree(); ++i) { if (i > 0) {