|
4 | 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
5 | 5 |
|
6 | 6 | #include "sapling/util.h" |
| 7 | +#include "sync.h" |
7 | 8 | #include <algorithm> |
| 9 | +#include <librustzcash.h> |
8 | 10 | #include <stdexcept> |
| 11 | +#include <iostream> |
| 12 | + |
| 13 | +static boost::filesystem::path zc_paramsPathCached; |
| 14 | +static RecursiveMutex csPathCached; |
| 15 | + |
| 16 | +static boost::filesystem::path ZC_GetBaseParamsDir() |
| 17 | +{ |
| 18 | + // Copied from GetDefaultDataDir and adapter for zcash params. |
| 19 | + |
| 20 | + namespace fs = boost::filesystem; |
| 21 | + // Windows < Vista: C:\Documents and Settings\Username\Application Data\ZcashParams |
| 22 | + // Windows >= Vista: C:\Users\Username\AppData\Roaming\ZcashParams |
| 23 | + // Mac: ~/Library/Application Support/ZcashParams |
| 24 | + // Unix: ~/.zcash-params |
| 25 | +#ifdef WIN32 |
| 26 | + // Windows |
| 27 | + return GetSpecialFolderPath(CSIDL_APPDATA) / "ZcashParams"; |
| 28 | +#else |
| 29 | + fs::path pathRet; |
| 30 | + char* pszHome = getenv("HOME"); |
| 31 | + if (pszHome == NULL || strlen(pszHome) == 0) |
| 32 | + pathRet = fs::path("/"); |
| 33 | + else |
| 34 | + pathRet = fs::path(pszHome); |
| 35 | +#ifdef MAC_OSX |
| 36 | + // Mac |
| 37 | + pathRet /= "Library/Application Support"; |
| 38 | + TryCreateDirectory(pathRet); |
| 39 | + return pathRet / "ZcashParams"; |
| 40 | +#else |
| 41 | + // Unix |
| 42 | + return pathRet / ".zcash-params"; |
| 43 | +#endif |
| 44 | +#endif |
| 45 | +} |
| 46 | + |
| 47 | +const boost::filesystem::path &ZC_GetParamsDir() |
| 48 | +{ |
| 49 | + namespace fs = boost::filesystem; |
| 50 | + |
| 51 | + LOCK(csPathCached); // Reuse the same lock as upstream. |
| 52 | + |
| 53 | + fs::path &path = zc_paramsPathCached; |
| 54 | + |
| 55 | + // This can be called during exceptions by LogPrintf(), so we cache the |
| 56 | + // value so we don't have to do memory allocations after that. |
| 57 | + if (!path.empty()) |
| 58 | + return path; |
| 59 | + |
| 60 | + path = ZC_GetBaseParamsDir(); |
| 61 | + |
| 62 | + return path; |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Ignores exceptions thrown by Boost's create_directory if the requested directory exists. |
| 67 | + * Specifically handles case where path p exists, but it wasn't possible for the user to |
| 68 | + * write to the parent directory. |
| 69 | + */ |
| 70 | +bool TryCreateDirectory(const boost::filesystem::path& p) |
| 71 | +{ |
| 72 | + try { |
| 73 | + return boost::filesystem::create_directory(p); |
| 74 | + } catch (const boost::filesystem::filesystem_error&) { |
| 75 | + if (!boost::filesystem::exists(p) || !boost::filesystem::is_directory(p)) |
| 76 | + throw; |
| 77 | + } |
| 78 | + |
| 79 | + // create_directory didn't create the directory, it had to have existed already |
| 80 | + return false; |
| 81 | +} |
| 82 | + |
| 83 | +void initZKSNARKS() |
| 84 | +{ |
| 85 | + boost::filesystem::path sapling_spend = ZC_GetParamsDir() / "sapling-spend.params"; |
| 86 | + boost::filesystem::path sapling_output = ZC_GetParamsDir() / "sapling-output.params"; |
| 87 | + boost::filesystem::path sprout_groth16 = ZC_GetParamsDir() / "sprout-groth16.params"; |
| 88 | + |
| 89 | + if (!(boost::filesystem::exists(sapling_spend) && |
| 90 | + boost::filesystem::exists(sapling_output) && |
| 91 | + boost::filesystem::exists(sprout_groth16) |
| 92 | + )) { |
| 93 | + throw std::runtime_error("Sapling params don't exist"); |
| 94 | + } |
| 95 | + |
| 96 | + std::string sapling_spend_str = sapling_spend.string(); |
| 97 | + std::string sapling_output_str = sapling_output.string(); |
| 98 | + std::string sprout_groth16_str = sprout_groth16.string(); |
| 99 | + |
| 100 | + librustzcash_init_zksnark_params( |
| 101 | + sapling_spend_str.c_str(), |
| 102 | + "8270785a1a0d0bc77196f000ee6d221c9c9894f55307bd9357c3f0105d31ca63991ab91324160d8f53e2bbd3c2633a6eb8bdf5205d822e7f3f73edac51b2b70c", |
| 103 | + sapling_output_str.c_str(), |
| 104 | + "657e3d38dbb5cb5e7dd2970e8b03d69b4787dd907285b5a7f0790dcc8072f60bf593b32cc2d1c030e00ff5ae64bf84c5c3beb84ddc841d48264b4a171744d028", |
| 105 | + sprout_groth16_str.c_str(), |
| 106 | + "e9b238411bd6c0ec4791e9d04245ec350c9c5744f5610dfcce4365d5ca49dfefd5054e371842b3f88fa1b9d7e8e075249b3ebabd167fa8b0f3161292d36c180a" |
| 107 | + ); |
| 108 | + |
| 109 | + std::cout << "### Sapling params initialized ###" << std::endl; |
| 110 | +} |
9 | 111 |
|
10 | 112 | std::vector<unsigned char> convertIntToVectorLE(const uint64_t val_int) { |
11 | 113 | std::vector<unsigned char> bytes; |
|
0 commit comments