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/app/main/Application.h>
21#include <xrpld/ledger/ReadView.h>
22#include <xrpld/rpc/Context.h>
23#include <xrpld/rpc/detail/RPCHelpers.h>
24#include <xrpld/rpc/detail/Tuning.h>
25#include <xrpl/basics/StringUtilities.h>
26#include <xrpl/protocol/ErrorCodes.h>
27#include <xrpl/protocol/PayChan.h>
28#include <xrpl/protocol/RPCErr.h>
29#include <xrpl/protocol/STAccount.h>
30#include <xrpl/protocol/jss.h>
31#include <xrpl/resource/Fees.h>
32
33#include <optional>
34
35namespace ripple {
36
37// {
38// secret_key: <signing_secret_key>
39// key_type: optional; either ed25519 or secp256k1 (default to secp256k1)
40// channel_id: 256-bit channel id
41// drops: 64-bit uint (as string)
42// }
45{
46 auto const& params(context.params);
47 for (auto const& p : {jss::channel_id, jss::amount})
48 if (!params.isMember(p))
50
51 // Compatibility if a key type isn't specified. If it is, the
52 // keypairForSignature code will validate parameters and return
53 // the appropriate error.
54 if (!params.isMember(jss::key_type) && !params.isMember(jss::secret))
55 return RPC::missing_field_error(jss::secret);
56
57 Json::Value result;
59 RPC::keypairForSignature(params, result, context.apiVersion);
60
61 XRPL_ASSERT(
62 keyPair || RPC::contains_error(result),
63 "ripple::doChannelAuthorize : valid keyPair or an error");
64 if (!keyPair || RPC::contains_error(result))
65 return result;
66
67 PublicKey const& pk = keyPair->first;
68 SecretKey const& sk = keyPair->second;
69
70 uint256 channelId;
71 if (!channelId.parseHex(params[jss::channel_id].asString()))
73
74 std::optional<std::uint64_t> const optDrops = params[jss::amount].isString()
75 ? to_uint64(params[jss::amount].asString())
76 : std::nullopt;
77
78 if (!optDrops)
80
81 std::uint64_t const drops = *optDrops;
82
83 Serializer msg;
84 serializePayChanAuthorization(msg, channelId, XRPAmount(drops));
85
86 try
87 {
88 auto const buf = sign(pk, sk, msg.slice());
89 result[jss::signature] = strHex(buf);
90 }
91 catch (std::exception const& ex)
92 {
93 result = RPC::make_error(
95 "Exception occurred during signing: " + std::string(ex.what()));
96 }
97 return result;
98}
99
100// {
101// public_key: <public_key>
102// channel_id: 256-bit channel id
103// drops: 64-bit uint (as string)
104// signature: signature to verify
105// }
108{
109 auto const& params(context.params);
110 for (auto const& p :
111 {jss::public_key, jss::channel_id, jss::amount, jss::signature})
112 if (!params.isMember(p))
113 return RPC::missing_field_error(p);
114
116 {
117 std::string const strPk = params[jss::public_key].asString();
118 pk = parseBase58<PublicKey>(TokenType::AccountPublic, strPk);
119
120 if (!pk)
121 {
122 auto pkHex = strUnHex(strPk);
123 if (!pkHex)
125 auto const pkType = publicKeyType(makeSlice(*pkHex));
126 if (!pkType)
128 pk.emplace(makeSlice(*pkHex));
129 }
130 }
131
132 uint256 channelId;
133 if (!channelId.parseHex(params[jss::channel_id].asString()))
135
136 std::optional<std::uint64_t> const optDrops = params[jss::amount].isString()
137 ? to_uint64(params[jss::amount].asString())
138 : std::nullopt;
139
140 if (!optDrops)
142
143 std::uint64_t const drops = *optDrops;
144
145 auto sig = strUnHex(params[jss::signature].asString());
146 if (!sig || !sig->size())
148
149 Serializer msg;
150 serializePayChanAuthorization(msg, channelId, XRPAmount(drops));
151
152 Json::Value result;
153 result[jss::signature_verified] =
154 verify(*pk, msg.slice(), makeSlice(*sig), /*canonical*/ true);
155 return result;
156}
157
158} // namespace ripple
Represents a JSON value.
Definition: json_value.h:147
A public key.
Definition: PublicKey.h:62
A secret key.
Definition: SecretKey.h:37
Slice slice() const noexcept
Definition: Serializer.h:66
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition: base_uint.h:502
T emplace(T... args)
bool contains_error(Json::Value const &json)
Returns true if the json contains an rpc error specification.
Definition: ErrorCodes.cpp:197
Json::Value make_error(error_code_i code)
Returns a new json object that reflects the error code.
Definition: ErrorCodes.cpp:181
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:272
std::optional< std::uint64_t > to_uint64(std::string const &s)
Json::Value rpcError(int iError)
Definition: RPCErr.cpp:29
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:238
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
Definition: PublicKey.cpp:207
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:243
unsigned int apiVersion
Definition: Context.h:50
Json::Value params
Definition: Context.h:64
T what(T... args)