rippled
Loading...
Searching...
No Matches
ChannelAuthorize.cpp
1#include <xrpld/app/main/Application.h>
2#include <xrpld/rpc/Context.h>
3#include <xrpld/rpc/detail/RPCHelpers.h>
4
5#include <xrpl/basics/StringUtilities.h>
6#include <xrpl/protocol/ErrorCodes.h>
7#include <xrpl/protocol/PayChan.h>
8#include <xrpl/protocol/RPCErr.h>
9#include <xrpl/protocol/jss.h>
10
11#include <optional>
12
13namespace xrpl {
14
15// {
16// secret_key: <signing_secret_key>
17// key_type: optional; either ed25519 or secp256k1 (default to secp256k1)
18// channel_id: 256-bit channel id
19// drops: 64-bit uint (as string)
20// }
23{
24 if (context.role != Role::ADMIN && !context.app.config().canSign())
25 {
26 return RPC::make_error(rpcNOT_SUPPORTED, "Signing is not supported by this server.");
27 }
28
29 auto const& params(context.params);
30 for (auto const& p : {jss::channel_id, jss::amount})
31 {
32 if (!params.isMember(p))
34 }
35
36 // Compatibility if a key type isn't specified. If it is, the
37 // keypairForSignature code will validate parameters and return
38 // the appropriate error.
39 if (!params.isMember(jss::key_type) && !params.isMember(jss::secret))
40 return RPC::missing_field_error(jss::secret);
41
42 Json::Value result;
44 RPC::keypairForSignature(params, result, context.apiVersion);
45
46 XRPL_ASSERT(
47 keyPair || RPC::contains_error(result),
48 "xrpl::doChannelAuthorize : valid keyPair or an error");
49 if (!keyPair || RPC::contains_error(result))
50 return result;
51
52 PublicKey const& pk = keyPair->first;
53 SecretKey const& sk = keyPair->second;
54
55 uint256 channelId;
56 if (!channelId.parseHex(params[jss::channel_id].asString()))
58
59 std::optional<std::uint64_t> const optDrops =
60 params[jss::amount].isString() ? to_uint64(params[jss::amount].asString()) : std::nullopt;
61
62 if (!optDrops)
64
65 std::uint64_t const drops = *optDrops;
66
67 Serializer msg;
68 serializePayChanAuthorization(msg, channelId, XRPAmount(drops));
69
70 try
71 {
72 auto const buf = sign(pk, sk, msg.slice());
73 result[jss::signature] = strHex(buf);
74 }
75 catch (std::exception const& ex)
76 {
77 // LCOV_EXCL_START
78 result = RPC::make_error(
79 rpcINTERNAL, "Exception occurred during signing: " + std::string(ex.what()));
80 // LCOV_EXCL_STOP
81 }
82 return result;
83}
84
85} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
virtual Config & config()=0
bool canSign() const
Definition Config.h:328
A public key.
Definition PublicKey.h:42
A secret key.
Definition SecretKey.h:18
Slice slice() const noexcept
Definition Serializer.h:44
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition base_uint.h:476
T is_same_v
Json::Value missing_field_error(std::string const &name)
Definition ErrorCodes.h:231
std::optional< std::pair< PublicKey, SecretKey > > keypairForSignature(Json::Value const &params, Json::Value &error, unsigned int apiVersion)
Generates a keypair for signature from RPC parameters.
Json::Value make_error(error_code_i code)
Returns a new json object that reflects the error code.
bool contains_error(Json::Value const &json)
Returns true if the json contains an rpc error specification.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
Json::Value doChannelAuthorize(RPC::JsonContext &context)
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:10
std::optional< std::uint64_t > to_uint64(std::string const &s)
void serializePayChanAuthorization(Serializer &msg, uint256 const &key, XRPAmount const &amt)
Definition PayChan.h:11
Json::Value rpcError(error_code_i iError)
Definition RPCErr.cpp:12
Buffer sign(PublicKey const &pk, SecretKey const &sk, Slice const &message)
Generate a signature for a message.
@ rpcCHANNEL_AMT_MALFORMED
Definition ErrorCodes.h:81
@ rpcINTERNAL
Definition ErrorCodes.h:110
@ rpcCHANNEL_MALFORMED
Definition ErrorCodes.h:80
@ rpcNOT_SUPPORTED
Definition ErrorCodes.h:112
Application & app
Definition Context.h:21
unsigned int apiVersion
Definition Context.h:29
Json::Value params
Definition Context.h:43
T what(T... args)