1
0
Fork 0
forked from lthn/blockchain

Implementd rpc for crypto api

This commit is contained in:
cryptozoidberg 2023-04-05 19:05:45 +02:00
parent dcf1b0adae
commit fed1df53f0
No known key found for this signature in database
GPG key ID: 22DEB97A54C6FDEC
3 changed files with 142 additions and 4 deletions

View file

@ -1318,6 +1318,95 @@ namespace wallet_public
};
};
struct COMMAND_SIGN_MESSAGE
{
struct request
{
std::string buff; //base64 encoded data
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(buff)
END_KV_SERIALIZE_MAP()
};
struct response
{
crypto::signature sig;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE_POD_AS_HEX_STRING(sig)
END_KV_SERIALIZE_MAP()
};
};
struct COMMAND_VALIDATE_SIGNATURE
{
struct request
{
std::string buff; //base64 encoded data
crypto::signature sig;
crypto::public_key pkey;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(buff)
KV_SERIALIZE_POD_AS_HEX_STRING(sig)
KV_SERIALIZE_POD_AS_HEX_STRING(pkey)
END_KV_SERIALIZE_MAP()
};
struct response
{
BEGIN_KV_SERIALIZE_MAP()
END_KV_SERIALIZE_MAP()
};
};
struct COMMAND_ENCRYPT_DATA
{
struct request
{
std::string buff; //base64 encoded data
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(buff)
END_KV_SERIALIZE_MAP()
};
struct response
{
std::string res_buff; //base64 encoded encrypted data
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(res_buff)
END_KV_SERIALIZE_MAP()
};
};
struct COMMAND_DECRYPT_DATA
{
struct request
{
std::string buff; //base64 encoded encrypted data
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(buff)
END_KV_SERIALIZE_MAP()
};
struct response
{
std::string res_buff; //base64 encoded data
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(res_buff)
END_KV_SERIALIZE_MAP()
};
};
struct assets_whitelist
{

View file

@ -1030,9 +1030,45 @@ namespace tools
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
bool wallet_rpc_server::reset_active_wallet(wallet2& w)
bool on_sign_message(const wallet_public::COMMAND_SIGN_MESSAGE& req, wallet_public::COMMAND_SIGN_MESSAGE::response& res, epee::json_rpc::error& er, connection_context& cntx)
{
m_pwallet = &w;
std::string buff = epee::string_encoding::base64_decode(req.buff);
get_wallet()->sign_buffer(buff, res.sig);
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
bool on_validate_signature(const wallet_public::COMMAND_VALIDATE_SIGNATURE& req, wallet_public::COMMAND_VALIDATE_SIGNATURE::response& res, epee::json_rpc::error& er, connection_context& cntx)
{
std::string buff = epee::string_encoding::base64_decode(req.buff);
bool r = get_wallet()->validate_sign(buff, req.sig, req.pkey);
if (!r)
{
er.code = WALLET_RPC_ERROR_CODE_WRONG_ARGUMENT;
er.message = "WALLET_RPC_ERROR_CODE_WRONG_ARGUMENT";
return false;
}
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
bool on_encrypt_data(const wallet_public::COMMAND_ENCRYPT_DATA& req, wallet_public::COMMAND_ENCRYPT_DATA::response& res, epee::json_rpc::error& er, connection_context& cntx)
{
std::string buff = epee::string_encoding::base64_decode(req.buff);
bool r = get_wallet()->encrypt_buffer(buff, res.res_buff);
res.res_buff = epee::string_encoding::base64_encode(res.res_buff);
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
bool on_decrypt_data(const wallet_public::COMMAND_DECRYPT_DATA& req, wallet_public::COMMAND_DECRYPT_DATA::response& res, epee::json_rpc::error& er, connection_context& cntx)
{
std::string buff = epee::string_encoding::base64_decode(req.buff);
bool r = get_wallet()->encrypt_buffer(buff, res.res_buff);
res.res_buff = epee::string_encoding::base64_encode(res.res_buff);
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
bool wallet_rpc_server::reset_active_wallet(std::shared_ptr<wallet2*> w)
{
m_pwallet = w;
return true;
}
//------------------------------------------------------------------------------------------------------------------------------

View file

@ -82,6 +82,11 @@ namespace tools
MAP_JON_RPC_WE("mw_get_wallets", on_mw_get_wallets, wallet_public::COMMAND_MW_GET_WALLETS)
MAP_JON_RPC_WE("mw_select_wallet", on_mw_select_wallet, wallet_public::COMMAND_MW_SELECT_WALLET)
//basic crypto operations
MAP_JON_RPC_WE("sign_message", on_sign_message, wallet_public::COMMAND_SIGN_MESSAGE)
MAP_JON_RPC_WE("validate_signature", on_validate_signature, wallet_public::COMMAND_VALIDATE_SIGNATURE)
MAP_JON_RPC_WE("encrypt_data", on_encrypt_data, wallet_public::COMMAND_ENCRYPT_DATA)
MAP_JON_RPC_WE("decrypt_data", on_decrypt_data, wallet_public::COMMAND_DECRYPT_DATA)
END_JSON_RPC_MAP()
END_URI_MAP2()
@ -129,9 +134,17 @@ namespace tools
bool on_mw_get_wallets(const wallet_public::COMMAND_MW_GET_WALLETS& req, wallet_public::COMMAND_MW_GET_WALLETS::response& res, epee::json_rpc::error& er, connection_context& cntx);
bool on_mw_select_wallet(const wallet_public::COMMAND_MW_SELECT_WALLET& req, wallet_public::COMMAND_MW_SELECT_WALLET::response& res, epee::json_rpc::error& er, connection_context& cntx);
bool on_sign_message(const wallet_public::COMMAND_SIGN_MESSAGE& req, wallet_public::COMMAND_SIGN_MESSAGE::response& res, epee::json_rpc::error& er, connection_context& cntx);
bool on_validate_signature(const wallet_public::COMMAND_VALIDATE_SIGNATURE& req, wallet_public::COMMAND_VALIDATE_SIGNATURE::response& res, epee::json_rpc::error& er, connection_context& cntx);
bool on_encrypt_data(const wallet_public::COMMAND_ENCRYPT_DATA& req, wallet_public::COMMAND_ENCRYPT_DATA::response& res, epee::json_rpc::error& er, connection_context& cntx);
bool on_decrypt_data(const wallet_public::COMMAND_DECRYPT_DATA& req, wallet_public::COMMAND_DECRYPT_DATA::response& res, epee::json_rpc::error& er, connection_context& cntx);
std::shared_ptr<wallet2*> get_wallet();
bool reset_active_wallet(wallet2& w);
bool reset_active_wallet(std::shared_ptr<wallet2*> w);
bool handle_command_line(const boost::program_options::variables_map& vm);