rippled
Loading...
Searching...
No Matches
PayChanClaim.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012-2014 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpld/ledger/ReadView.h>
21#include <xrpld/rpc/Context.h>
22#include <xrpld/rpc/detail/RPCHelpers.h>
23
24#include <xrpl/basics/StringUtilities.h>
25#include <xrpl/protocol/ErrorCodes.h>
26#include <xrpl/protocol/PayChan.h>
27#include <xrpl/protocol/RPCErr.h>
28#include <xrpl/protocol/jss.h>
29
30#include <optional>
31
32namespace ripple {
33
34// {
35// secret_key: <signing_secret_key>
36// key_type: optional; either ed25519 or secp256k1 (default to secp256k1)
37// channel_id: 256-bit channel id
38// drops: 64-bit uint (as string)
39// }
42{
43 auto const& params(context.params);
44 for (auto const& p : {jss::channel_id, jss::amount})
45 if (!params.isMember(p))
47
48 // Compatibility if a key type isn't specified. If it is, the
49 // keypairForSignature code will validate parameters and return
50 // the appropriate error.
51 if (!params.isMember(jss::key_type) && !params.isMember(jss::secret))
52 return RPC::missing_field_error(jss::secret);
53
54 Json::Value result;
56 RPC::keypairForSignature(params, result, context.apiVersion);
57
58 XRPL_ASSERT(
59 keyPair || RPC::contains_error(result),
60 "ripple::doChannelAuthorize : valid keyPair or an error");
61 if (!keyPair || RPC::contains_error(result))
62 return result;
63
64 PublicKey const& pk = keyPair->first;
65 SecretKey const& sk = keyPair->second;
66
67 uint256 channelId;
68 if (!channelId.parseHex(params[jss::channel_id].asString()))
70
71 std::optional<std::uint64_t> const optDrops = params[jss::amount].isString()
72 ? to_uint64(params[jss::amount].asString())
73 : std::nullopt;
74
75 if (!optDrops)
77
78 std::uint64_t const drops = *optDrops;
79
80 Serializer msg;
81 serializePayChanAuthorization(msg, channelId, XRPAmount(drops));
82
83 try
84 {
85 auto const buf = sign(pk, sk, msg.slice());
86 result[jss::signature] = strHex(buf);
87 }
88 catch (std::exception const& ex)
89 {
90 result = RPC::make_error(
92 "Exception occurred during signing: " + std::string(ex.what()));
93 }
94 return result;
95}
96
97// {
98// public_key: <public_key>
99// channel_id: 256-bit channel id
100// drops: 64-bit uint (as string)
101// signature: signature to verify
102// }
105{
106 auto const& params(context.params);
107 for (auto const& p :
108 {jss::public_key, jss::channel_id, jss::amount, jss::signature})
109 if (!params.isMember(p))
110 return RPC::missing_field_error(p);
111
113 {
114 std::string const strPk = params[jss::public_key].asString();
115 pk = parseBase58<PublicKey>(TokenType::AccountPublic, strPk);
116
117 if (!pk)
118 {
119 auto pkHex = strUnHex(strPk);
120 if (!pkHex)
122 auto const pkType = publicKeyType(makeSlice(*pkHex));
123 if (!pkType)
125 pk.emplace(makeSlice(*pkHex));
126 }
127 }
128
129 uint256 channelId;
130 if (!channelId.parseHex(params[jss::channel_id].asString()))
132
133 std::optional<std::uint64_t> const optDrops = params[jss::amount].isString()
134 ? to_uint64(params[jss::amount].asString())
135 : std::nullopt;
136
137 if (!optDrops)
139
140 std::uint64_t const drops = *optDrops;
141
142 auto sig = strUnHex(params[jss::signature].asString());
143 if (!sig || !sig->size())
145
146 Serializer msg;
147 serializePayChanAuthorization(msg, channelId, XRPAmount(drops));
148
149 Json::Value result;
150 result[jss::signature_verified] =
151 verify(*pk, msg.slice(), makeSlice(*sig), /*canonical*/ true);
152 return result;
153}
154
155} // namespace ripple
Represents a JSON value.
Definition: json_value.h:148
A public key.
Definition: PublicKey.h:62
A secret key.
Definition: SecretKey.h:38
Slice slice() const noexcept
Definition: Serializer.h:67
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition: base_uint.h:503
T emplace(T... args)
bool contains_error(Json::Value const &json)
Returns true if the json contains an rpc error specification.
Definition: ErrorCodes.cpp:201
Json::Value make_error(error_code_i code)
Returns a new json object that reflects the error code.
Definition: ErrorCodes.cpp:185
Json::Value missing_field_error(std::string const &name)
Definition: ErrorCodes.h:273
std::optional< std::pair< PublicKey, SecretKey > > keypairForSignature(Json::Value const &params, Json::Value &error, unsigned int apiVersion)
Definition: RPCHelpers.cpp:795
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
Json::Value doChannelVerify(RPC::JsonContext &)
std::optional< Blob > strUnHex(std::size_t strSize, Iterator begin, Iterator end)
Json::Value doChannelAuthorize(RPC::JsonContext &)
@ rpcCHANNEL_AMT_MALFORMED
Definition: ErrorCodes.h:101
@ rpcPUBLIC_MALFORMED
Definition: ErrorCodes.h:117
@ rpcINVALID_PARAMS
Definition: ErrorCodes.h:84
@ rpcINTERNAL
Definition: ErrorCodes.h:130
@ rpcCHANNEL_MALFORMED
Definition: ErrorCodes.h:100
bool verify(PublicKey const &publicKey, Slice const &m, Slice const &sig, bool mustBeFullyCanonical=true) noexcept
Verify a signature on a message.
Definition: PublicKey.cpp:288
std::optional< std::uint64_t > to_uint64(std::string const &s)
Json::Value rpcError(int iError)
Definition: RPCErr.cpp:31
void serializePayChanAuthorization(Serializer &msg, uint256 const &key, XRPAmount const &amt)
Buffer sign(PublicKey const &pk, SecretKey const &sk, Slice const &message)
Generate a signature for a message.
Definition: SecretKey.cpp:256
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
Definition: PublicKey.cpp:223
std::string strHex(FwdIt begin, FwdIt end)
Definition: strHex.h:30
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:244
unsigned int apiVersion
Definition: Context.h:49
Json::Value params
Definition: Context.h:63
T what(T... args)