LNS: Add value decryption for wallet_rpc

This commit is contained in:
Doyle 2020-03-23 15:03:44 +11:00
parent b88c262f20
commit cd1e155f66
4 changed files with 107 additions and 0 deletions

View File

@ -4431,6 +4431,81 @@ namespace tools
res.name = lns::name_to_base64_hash(req.name);
return true;
}
bool wallet_rpc_server::on_lns_decrypt_value(const wallet_rpc::COMMAND_RPC_LNS_DECRYPT_VALUE::request& req, wallet_rpc::COMMAND_RPC_LNS_DECRYPT_VALUE::response& res, epee::json_rpc::error& er, const connection_context *ctx)
{
if (!m_wallet) return not_open(er);
// ---------------------------------------------------------------------------------------------
//
// Validate encrypted value
//
// ---------------------------------------------------------------------------------------------
if (req.encrypted_value.size() % 2 != 0)
{
er.code = WALLET_RPC_ERROR_CODE_LNS_VALUE_LENGTH_NOT_EVEN;
er.message = "Value length not divisible by 2, length=" + std::to_string(req.encrypted_value.size());
return false;
}
if (req.encrypted_value.size() >= (lns::mapping_value::BUFFER_SIZE * 2))
{
er.code = WALLET_RPC_ERROR_CODE_LNS_VALUE_TOO_LONG;
er.message = "Value too long to decrypt=" + req.encrypted_value;
return false;
}
if (!lokimq::is_hex(req.encrypted_value))
{
er.code = WALLET_RPC_ERROR_CODE_LNS_VALUE_NOT_HEX;
er.message = "Value is not hex=" + req.encrypted_value;
return false;
}
// ---------------------------------------------------------------------------------------------
//
// Validate type and name
//
// ---------------------------------------------------------------------------------------------
std::string reason;
lns::mapping_type type = {};
{
if (!lns::validate_mapping_type(req.type, &type, &reason))
{
er.code = WALLET_RPC_ERROR_CODE_WRONG_LNS_TYPE;
er.message = "Wrong lns type given=" + reason;
return false;
}
if (!lns::validate_lns_name(type, req.name, &reason))
{
er.code = WALLET_RPC_ERROR_CODE_LNS_VALUE_NOT_HEX;
er.message = "Value is not hex=" + req.encrypted_value;
return false;
}
}
// ---------------------------------------------------------------------------------------------
//
// Decrypt value
//
// ---------------------------------------------------------------------------------------------
lns::mapping_value encrypted_value = {};
encrypted_value.len = req.encrypted_value.size() / 2;
lokimq::from_hex(req.encrypted_value.begin(), req.encrypted_value.end(), encrypted_value.buffer.begin());
lns::mapping_value value = {};
if (!lns::decrypt_mapping_value(req.name, encrypted_value, value))
{
er.code = WALLET_RPC_ERROR_CODE_LNS_VALUE_NOT_HEX;
er.message = "Value decryption failure";
return false;
}
// TODO(loki): If lokinet, base32z, if wallet wallet address else hex public key
res.value = epee::to_hex::string(encrypted_value.to_span());
return true;
}
}
class t_daemon

View File

@ -168,6 +168,7 @@ namespace tools
MAP_JON_RPC_WE("lns_update_mapping", on_lns_update_mapping, wallet_rpc::COMMAND_RPC_LNS_UPDATE_MAPPING)
MAP_JON_RPC_WE("lns_make_update_mapping_signature", on_lns_make_update_mapping_signature, wallet_rpc::COMMAND_RPC_LNS_MAKE_UPDATE_SIGNATURE)
MAP_JON_RPC_WE("lns_hash_name", on_lns_hash_name, wallet_rpc::COMMAND_RPC_LNS_HASH_NAME)
MAP_JON_RPC_WE("lns_decrypt_value", on_lns_decrypt_value, wallet_rpc::COMMAND_RPC_LNS_DECRYPT_VALUE)
END_JSON_RPC_MAP()
END_URI_MAP2()
@ -262,6 +263,7 @@ namespace tools
bool on_lns_update_mapping(const wallet_rpc::COMMAND_RPC_LNS_UPDATE_MAPPING::request& req, wallet_rpc::COMMAND_RPC_LNS_UPDATE_MAPPING::response& res, epee::json_rpc::error& er, const connection_context *ctx = NULL);
bool on_lns_make_update_mapping_signature(const wallet_rpc::COMMAND_RPC_LNS_MAKE_UPDATE_SIGNATURE::request& req, wallet_rpc::COMMAND_RPC_LNS_MAKE_UPDATE_SIGNATURE::response& res, epee::json_rpc::error& er, const connection_context *ctx = NULL);
bool on_lns_hash_name(const wallet_rpc::COMMAND_RPC_LNS_HASH_NAME::request& req, wallet_rpc::COMMAND_RPC_LNS_HASH_NAME::response& res, epee::json_rpc::error& er, const connection_context *ctx = NULL);
bool on_lns_decrypt_value(const wallet_rpc::COMMAND_RPC_LNS_DECRYPT_VALUE::request& req, wallet_rpc::COMMAND_RPC_LNS_DECRYPT_VALUE::response& res, epee::json_rpc::error& er, const connection_context *ctx = NULL);
//json rpc v2
bool on_query_key(const wallet_rpc::COMMAND_RPC_QUERY_KEY::request& req, wallet_rpc::COMMAND_RPC_QUERY_KEY::response& res, epee::json_rpc::error& er, const connection_context *ctx = NULL);

View File

@ -3057,5 +3057,31 @@ This command is only required if the open wallet is one of the owners of a LNS r
};
typedef epee::misc_utils::struct_init<response_t> response;
};
LOKI_RPC_DOC_INTROSPECT
// Takes a LNS encrypted value and decrypts the mapping value.
struct COMMAND_RPC_LNS_DECRYPT_VALUE
{
struct request_t
{
std::string name; // The desired name to hash
std::string type; // The mapping type, currently we only support "session". In future "lokinet" and "blockchain" mappings will be available.
std::string encrypted_value; // The encrypted value represented in hex
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(name);
KV_SERIALIZE(encrypted_value);
END_KV_SERIALIZE_MAP()
};
typedef epee::misc_utils::struct_init<request_t> request;
struct response_t
{
std::string value; // The value decrypted
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(value)
END_KV_SERIALIZE_MAP()
};
typedef epee::misc_utils::struct_init<response_t> response;
};
}
}

View File

@ -81,3 +81,7 @@
#define WALLET_RPC_ERROR_CODE_HF_QUERY_FAILED -1001
#define WALLET_RPC_ERROR_CODE_WRONG_LNS_TYPE -1002
#define WALLET_RPC_ERROR_CODE_LNS_BAD_NAME -1003
#define WALLET_RPC_ERROR_CODE_LNS_VALUE_TOO_LONG -1004
#define WALLET_RPC_ERROR_CODE_LNS_VALUE_NOT_HEX -1005
#define WALLET_RPC_ERROR_CODE_LNS_VALUE_LENGTH_NOT_EVEN -1006
#define WALLET_RPC_ERROR_CODE_LNS_VALUE_DECRYPT_FAILED -1007