Unverified Commit 47a2b90b authored by Oleh Nikolaiev's avatar Oleh Nikolaiev Committed by GitHub

Merge pull request #119 from skalenetwork/bug/SKALE-2844-backup-key

Bug/skale 2844 backup key
parents 8f8749a3 a852dfff
...@@ -53,14 +53,15 @@ std::shared_ptr<string> LevelDB::readString(const string &_key) { ...@@ -53,14 +53,15 @@ std::shared_ptr<string> LevelDB::readString(const string &_key) {
throw SGXException(NULL_DATABASE, "Null db"); throw SGXException(NULL_DATABASE, "Null db");
} }
auto status = db->Get(readOptions, _key, &*result); spdlog::info("key to read from db: {}",_key );
spdlog::debug("key to read from db: {}",_key ); auto status = db->Get(readOptions, _key, &*result);
throwExceptionOnError(status); throwExceptionOnError(status);
if (status.IsNotFound()) if (status.IsNotFound()) {
return nullptr; return nullptr;
}
return result; return result;
} }
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
#include "BLSCrypto.h" #include "BLSCrypto.h"
#include "LevelDB.h" #include "LevelDB.h"
#include <fstream>
#include <iostream> #include <iostream>
#include <algorithm> #include <algorithm>
...@@ -130,8 +131,13 @@ void gen_SEK(){ ...@@ -130,8 +131,13 @@ void gen_SEK(){
carray2Hex(encr_SEK.data(), enc_len, hexEncrKey.data()); carray2Hex(encr_SEK.data(), enc_len, hexEncrKey.data());
cout << "ATTENTION! THIS IS YOUR KEY FOR BACK UP. PLEASE COPY IT TO THE SAFE PLACE" << endl; std::ofstream sek_file("backup_key.txt");
cout << "YOUR KEY IS " << SEK << endl; sek_file.clear();
cout << "ATTENTION! YOUR BACKUP KEY WILL BE WRITTEN INTO backup_key.txt.\n" <<
"PLEASE COPY IT TO THE SAFE PLACE AND THEN DELETE THE FILE MANUALLY BY RUNNING THE FOLLOWING COMMAND:\n" <<
"`sudo apt-get install secure-delete && srm -vz backup_key.txt`" << endl;
sek_file << SEK;
if (!autoconfirm) { if (!autoconfirm) {
std::string confirm_str = "I confirm"; std::string confirm_str = "I confirm";
......
...@@ -667,14 +667,18 @@ Json::Value SGXWalletServer::deleteBlsKeyImpl(const std::string& name) { ...@@ -667,14 +667,18 @@ Json::Value SGXWalletServer::deleteBlsKeyImpl(const std::string& name) {
result["deleted"] = false; result["deleted"] = false;
try { try {
if (!checkName(name, "BLS_KEY")) { if (!checkName(name, "BLS_KEY")) {
throw SGXException(INVALID_BLS_NAME, "Invalid BLSKey name"); throw SGXException(INVALID_BLS_NAME, "Invalid BLSKey name format");
} }
std::shared_ptr <std::string> bls_ptr = LevelDB::getLevelDb()->readString(name); std::string key = "BLSKEYSHARE:" + name;
std::shared_ptr <std::string> bls_ptr = LevelDB::getLevelDb()->readString(key);
if (bls_ptr != nullptr) { if (bls_ptr != nullptr) {
result["deleted"] = true; result["deleted"] = true;
return result; return result;
} } else {
std::string error_msg = "BLS key with such name not found: " + name;
throw SGXException(INVALID_BLS_NAME, error_msg.c_str());
}
LevelDB::getLevelDb()->deleteKey(name); LevelDB::getLevelDb()->deleteKey(name);
} HANDLE_SGX_EXCEPTION(result) } HANDLE_SGX_EXCEPTION(result)
return result; return result;
......
...@@ -424,4 +424,4 @@ void TestUtils::doDKG(StubClient &c, int n, int t, ...@@ -424,4 +424,4 @@ void TestUtils::doDKG(StubClient &c, int n, int t,
cerr << i << endl; cerr << i << endl;
} }
\ No newline at end of file
1.52.0 1.53.0
\ No newline at end of file
...@@ -4,6 +4,11 @@ ...@@ -4,6 +4,11 @@
When SGXWallet is initialized, the server will print the backup key. When SGXWallet is initialized, the server will print the backup key.
**This key must be securely recorded and stored.** **This key must be securely recorded and stored.**
Be sure to store this key in a safe place, then securely remove it with the following command:
```bash
sudo apt-get install secure-delete && srm -vz backup_key.txt
```
Master-Slave replication is recommended to support the SGXWallet backup strategy. Below are general instructions for a basic backup and recovery process. Master-Slave replication is recommended to support the SGXWallet backup strategy. Below are general instructions for a basic backup and recovery process.
......
...@@ -57,8 +57,7 @@ string stringFromFr(libff::alt_bn128_Fr& _el) { ...@@ -57,8 +57,7 @@ string stringFromFr(libff::alt_bn128_Fr& _el) {
return string(tmp); return string(tmp);
} }
template<class T> template<class T> string ConvertToString(T field_elem, int base = 10) {
string ConvertToString(T field_elem, int base = 10) {
mpz_t t; mpz_t t;
mpz_init(t); mpz_init(t);
......
...@@ -573,11 +573,29 @@ TEST_CASE_METHOD(TestFixture, "DKG_BLS test", "[dkg-bls]") { ...@@ -573,11 +573,29 @@ TEST_CASE_METHOD(TestFixture, "DKG_BLS test", "[dkg-bls]") {
dkgID = TestUtils::randGen(); dkgID = TestUtils::randGen();
TestUtils::doDKG(c, 16, 5, ecdsaKeyNames, blsKeyNames, schainID, dkgID); TestUtils::doDKG(c, 16, 5, ecdsaKeyNames, blsKeyNames, schainID, dkgID);
}
for (const auto& name : blsKeyNames) { TEST_CASE_METHOD(TestFixture, "Delete Bls Key", "[delete-bls-key]") {
REQUIRE(c.deleteBlsKey(name)["deleted"] == true); HttpClient client(RPC_ENDPOINT);
} StubClient c(client, JSONRPC_CLIENT_V2);
std::string name = "BLS_KEY:SCHAIN_ID:123456789:NODE_ID:0:DKG_ID:0";
libff::alt_bn128_Fr key = libff::alt_bn128_Fr("6507625568967977077291849236396320012317305261598035438182864059942098934847");
std::string key_str = TestUtils::stringFromFr(key);
c.importBLSKeyShare(key_str, name, 1, 2, 1);
REQUIRE(c.deleteBlsKey(name)["deleted"] == true);
}
TEST_CASE_METHOD(TestFixture, "Backup Key", "[backup-key]") {
HttpClient client(RPC_ENDPOINT);
StubClient c(client, JSONRPC_CLIENT_V2);
std::ifstream sek_file("backup_key.txt");
REQUIRE(sek_file.good());
std::string sek;
sek_file >> sek;
REQUIRE(sek.size() == 32);
} }
TEST_CASE_METHOD(TestFixture, "Get ServerStatus", "[get-server-status]") { TEST_CASE_METHOD(TestFixture, "Get ServerStatus", "[get-server-status]") {
......
...@@ -32,6 +32,8 @@ print("Top directory is:" + topDir) ...@@ -32,6 +32,8 @@ print("Top directory is:" + topDir)
testList = [ "[cert-sign]", testList = [ "[cert-sign]",
"[get-server-status]", "[get-server-status]",
"[get-server-version]", "[get-server-version]",
"[backup-key]",
"[delete-bls-key]",
"[ecdsa-key-gen]", "[ecdsa-key-gen]",
"[ecdsa-aes-key-gen]", "[ecdsa-aes-key-gen]",
"[ecdsa-key-sig-gen]", "[ecdsa-key-sig-gen]",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment