Status syncEnsureRepo(const std::string &syncDir, const std::string &tempDir, const std::string &syncKey) { AutoSyncLock lock(gSyncMutex); if (!fileExists(syncDir)) { if (fileExists(tempDir)) ABC_CHECK(fileDelete(tempDir)); ABC_CHECK(syncMakeRepo(tempDir)); bool dirty = false; ABC_CHECK(syncRepo(tempDir, syncKey, dirty)); if (rename(tempDir.c_str(), syncDir.c_str())) return ABC_ERROR(ABC_CC_SysError, "rename failed"); } 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(); }