rippled
Loading...
Searching...
No Matches
ChannelVerify.cpp
1#include <xrpld/rpc/Context.h>
2#include <xrpld/rpc/detail/RPCHelpers.h>
3
4#include <xrpl/basics/StringUtilities.h>
5#include <xrpl/protocol/ErrorCodes.h>
6#include <xrpl/protocol/PayChan.h>
7#include <xrpl/protocol/RPCErr.h>
8#include <xrpl/protocol/jss.h>
9
10#include <optional>
11
12namespace xrpl {
13
14// {
15// public_key: <public_key>
16// channel_id: 256-bit channel id
17// drops: 64-bit uint (as string)
18// signature: signature to verify
19// }
22{
23 auto const& params(context.params);
24 for (auto const& p : {jss::public_key, jss::channel_id, jss::amount, jss::signature})
25 {
26 if (!params.isMember(p))
28 }
29
31 {
32 std::string const strPk = params[jss::public_key].asString();
33 pk = parseBase58<PublicKey>(TokenType::AccountPublic, strPk);
34
35 if (!pk)
36 {
37 auto pkHex = strUnHex(strPk);
38 if (!pkHex)
40 auto const pkType = publicKeyType(makeSlice(*pkHex));
41 if (!pkType)
43 pk.emplace(makeSlice(*pkHex));
44 }
45 }
46
47 uint256 channelId;
48 if (!channelId.parseHex(params[jss::channel_id].asString()))
50
51 std::optional<std::uint64_t> const optDrops =
52 params[jss::amount].isString() ? to_uint64(params[jss::amount].asString()) : std::nullopt;
53
54 if (!optDrops)
56
57 std::uint64_t const drops = *optDrops;
58
59 auto sig = strUnHex(params[jss::signature].asString());
60 if (!sig || sig->empty())
62
63 Serializer msg;
64 serializePayChanAuthorization(msg, channelId, XRPAmount(drops));
65
66 Json::Value result;
67 result[jss::signature_verified] = verify(*pk, msg.slice(), makeSlice(*sig));
68 return result;
69}
70
71} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
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 emplace(T... args)
T is_same_v
Json::Value missing_field_error(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
Json::Value doChannelVerify(RPC::JsonContext &context)
std::optional< std::uint64_t > to_uint64(std::string const &s)
bool verify(PublicKey const &publicKey, Slice const &m, Slice const &sig) noexcept
Verify a signature on a message.
void serializePayChanAuthorization(Serializer &msg, uint256 const &key, XRPAmount const &amt)
Definition PayChan.h:11
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
std::optional< Blob > strUnHex(std::size_t strSize, Iterator begin, Iterator end)
Json::Value rpcError(error_code_i iError)
Definition RPCErr.cpp:12
std::enable_if_t< std::is_same< T, char >::value||std::is_same< T, unsigned char >::value, Slice > makeSlice(std::array< T, N > const &a)
Definition Slice.h:215
@ rpcCHANNEL_AMT_MALFORMED
Definition ErrorCodes.h:81
@ rpcPUBLIC_MALFORMED
Definition ErrorCodes.h:97
@ rpcCHANNEL_MALFORMED
Definition ErrorCodes.h:80
@ rpcINVALID_PARAMS
Definition ErrorCodes.h:64
Json::Value params
Definition Context.h:43