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