Status pluginDataSet(const Account &account, const std::string &plugin, const std::string &key, const std::string &data) { ABC_CHECK(fileEnsureDir(pluginsDirectory(account))); ABC_CHECK(fileEnsureDir(pluginDirectory(account, plugin))); PluginDataFile json; json.keySet(key); json.dataSet(data); ABC_CHECK(json.save(keyFilename(account, plugin, key), account.login.dataKey())); return Status(); }
Status loginDirCreate(std::string &directory, const std::string &username) { // Make sure the accounts directory is in place: ABC_CHECK(fileEnsureDir(gContext->accountsDir())); // We don't need to do anything if our directory already exists: if (!directory.empty()) return Status(); // Create our own directory: ABC_CHECK(newDirName(directory)); ABC_CHECK(fileEnsureDir(directory)); // Write our user name: UsernameJson json; ABC_CHECK(json.usernameSet(username)); ABC_CHECK(json.save(directory + ACCOUNT_NAME_FILENAME)); return Status(); }
Status Wallet::createNew(const std::string &name, int currency) { // Set up the keys: ABC_CHECK(randomData(bitcoinKey_, BITCOIN_SEED_LENGTH)); bitcoinKeyBackup_ = bitcoinKey_; ABC_CHECK(randomData(dataKey_, DATA_KEY_LENGTH)); DataChunk syncKey; ABC_CHECK(randomData(syncKey, SYNC_KEY_LENGTH)); syncKey_ = base16Encode(syncKey); // Create the sync directory: ABC_CHECK(fileEnsureDir(gContext->paths.walletsDir())); ABC_CHECK(fileEnsureDir(dir())); ABC_CHECK(fileEnsureDir(syncDir())); ABC_CHECK(syncMakeRepo(syncDir())); // Populate the sync directory: ABC_CHECK(currencySet(currency)); ABC_CHECK(nameSet(name)); ABC_CHECK(addresses.load()); // Push the wallet to the server: bool dirty = false; ABC_CHECK(loginServerWalletCreate(account.login, syncKey_)); ABC_CHECK(syncRepo(syncDir(), syncKey_, dirty)); ABC_CHECK(loginServerWalletActivate(account.login, syncKey_)); // If everything worked, add the wallet to the account: WalletJson json; ABC_CHECK(json.bitcoinKeySet(base16Encode(bitcoinKey_))); ABC_CHECK(json.dataKeySet(base16Encode(dataKey_))); ABC_CHECK(json.syncKeySet(syncKey_)); ABC_CHECK(account.wallets.insert(id_, json)); ABC_CHECK(account.sync(dirty)); return Status(); }
Status pluginDataSet(const Account &account, const std::string &plugin, const std::string &key, const std::string &data) { ABC_CHECK(fileEnsureDir(pluginsDirectory(account))); ABC_CHECK(fileEnsureDir(pluginDirectory(account, plugin))); const auto namePath = pluginDirectory(account, plugin) + "Name.json"; if (!fileExists(namePath)) { PluginNameJson json; ABC_CHECK(json.nameSet(plugin)); json.save(namePath, account.login.dataKey()); } PluginDataFile json; json.keySet(key); json.dataSet(data); ABC_CHECK(json.save(keyFilename(account, plugin, key), account.login.dataKey())); return Status(); }
Status Wallet::loadSync() { ABC_CHECK(fileEnsureDir(gContext->paths.walletsDir())); ABC_CHECK(fileEnsureDir(dir())); ABC_CHECK(syncEnsureRepo(syncDir(), dir() + "tmp/", syncKey_)); // Load the currency: CurrencyJson currencyJson; currencyJson.load(syncDir() + WALLET_CURRENCY_FILENAME, dataKey()); ABC_CHECK(currencyJson.currencyOk()); currency_ = currencyJson.currency(); // Load the name (failure is acceptable): NameJson json; json.load(syncDir() + WALLET_NAME_FILENAME, dataKey()); name_ = json.name(); // Load the databases: ABC_CHECK(addresses.load()); ABC_CHECK(txs.load()); return Status(); }
Status AddressDb::stockpile() { ABC_CHECK(fileEnsureDir(dir_)); // Build a list of used indices: std::map<size_t, bool> indices; for (const auto &i: addresses_) indices[i.second.index] = i.second.recyclable; // Check for gaps: size_t lastUsed = 0; for (size_t i = 0; i < addresses_.size() || i < lastUsed + 5; ++i) { auto index = indices.find(i); if (index == indices.end()) { // Create the missing address: auto m00n = mainBranch(wallet_).generate_private_key(i); if (m00n.valid()) { Address address; address.index = i; address.address = m00n.address().encoded(); address.recyclable = true; address.time = time(nullptr); addresses_[address.address] = address; AddressJson json; ABC_CHECK(json.pack(address)); ABC_CHECK(json.save(path(address), wallet_.dataKey())); bridgeWatchAddress(wallet_, address.address).log(); } } else if (!index->second) { lastUsed = i; } } return Status(); }