mirror of
https://github.com/XRPLF/rippled.git
synced 2025-12-06 17:27:55 +00:00
New serialized object, public key, and private key interfaces
This introduces functions get and set, and a family of specialized structs called STExchange. These interfaces allow efficient and seamless interchange between serialized object fields and user defined types, especially variable length objects. A new base class template TypedField is mixed into existing SField declarations to encode information on the field, allowing template metaprograms to both customize interchange based on the type and detect misuse at compile-time. New types AnyPublicKey and AnySecretKey are introduced. These are intended to replace the corresponding functionality in the deprecated class RippleAddress. Specializations of STExchange for these types are provided to allow interchange. New free functions verify and sign allow signature verification and signature generation for serialized objects. * Add Buffer and Slice primitives * Add TypedField and modify some SField * Add STExchange and specializations for STBlob and STInteger * Improve STBlob and STInteger to support STExchange * Expose raw data in RippleAddress and Serializer
This commit is contained in:
committed by
Tom Ritchford
parent
79ce4ed226
commit
a2acffdfa3
@@ -1780,6 +1780,8 @@
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\basics\Blob.h">
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\basics\Buffer.h">
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\basics\ByteOrder.h">
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\basics\CheckLibraryVersions.h">
|
||||
@@ -1852,6 +1854,8 @@
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\basics\seconds_clock.h">
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\basics\Slice.h">
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\basics\strHex.h">
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\basics\StringUtilities.h">
|
||||
@@ -2302,6 +2306,10 @@
|
||||
<ClCompile Include="..\..\src\ripple\peerfinder\tests\PeerFinder_test.cpp">
|
||||
<ExcludedFromBuild>True</ExcludedFromBuild>
|
||||
</ClCompile>
|
||||
<ClInclude Include="..\..\src\ripple\protocol\AnyPublicKey.h">
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\protocol\AnySecretKey.h">
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\protocol\Book.h">
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\protocol\BuildInfo.h">
|
||||
@@ -2310,6 +2318,12 @@
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\protocol\HashPrefix.h">
|
||||
</ClInclude>
|
||||
<ClCompile Include="..\..\src\ripple\protocol\impl\AnyPublicKey.cpp">
|
||||
<ExcludedFromBuild>True</ExcludedFromBuild>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\src\ripple\protocol\impl\AnySecretKey.cpp">
|
||||
<ExcludedFromBuild>True</ExcludedFromBuild>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\src\ripple\protocol\impl\BuildInfo.cpp">
|
||||
<ExcludedFromBuild>True</ExcludedFromBuild>
|
||||
</ClCompile>
|
||||
@@ -2426,6 +2440,8 @@
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\protocol\STBlob.h">
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\protocol\STExchange.h">
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\protocol\STInteger.h">
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\protocol\STLedgerEntry.h">
|
||||
|
||||
@@ -2667,6 +2667,9 @@
|
||||
<ClInclude Include="..\..\src\ripple\basics\Blob.h">
|
||||
<Filter>ripple\basics</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\basics\Buffer.h">
|
||||
<Filter>ripple\basics</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\basics\ByteOrder.h">
|
||||
<Filter>ripple\basics</Filter>
|
||||
</ClInclude>
|
||||
@@ -2754,6 +2757,9 @@
|
||||
<ClInclude Include="..\..\src\ripple\basics\seconds_clock.h">
|
||||
<Filter>ripple\basics</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\basics\Slice.h">
|
||||
<Filter>ripple\basics</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\basics\strHex.h">
|
||||
<Filter>ripple\basics</Filter>
|
||||
</ClInclude>
|
||||
@@ -3315,6 +3321,12 @@
|
||||
<ClCompile Include="..\..\src\ripple\peerfinder\tests\PeerFinder_test.cpp">
|
||||
<Filter>ripple\peerfinder\tests</Filter>
|
||||
</ClCompile>
|
||||
<ClInclude Include="..\..\src\ripple\protocol\AnyPublicKey.h">
|
||||
<Filter>ripple\protocol</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\protocol\AnySecretKey.h">
|
||||
<Filter>ripple\protocol</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\protocol\Book.h">
|
||||
<Filter>ripple\protocol</Filter>
|
||||
</ClInclude>
|
||||
@@ -3327,6 +3339,12 @@
|
||||
<ClInclude Include="..\..\src\ripple\protocol\HashPrefix.h">
|
||||
<Filter>ripple\protocol</Filter>
|
||||
</ClInclude>
|
||||
<ClCompile Include="..\..\src\ripple\protocol\impl\AnyPublicKey.cpp">
|
||||
<Filter>ripple\protocol\impl</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\src\ripple\protocol\impl\AnySecretKey.cpp">
|
||||
<Filter>ripple\protocol\impl</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\src\ripple\protocol\impl\BuildInfo.cpp">
|
||||
<Filter>ripple\protocol\impl</Filter>
|
||||
</ClCompile>
|
||||
@@ -3462,6 +3480,9 @@
|
||||
<ClInclude Include="..\..\src\ripple\protocol\STBlob.h">
|
||||
<Filter>ripple\protocol</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\protocol\STExchange.h">
|
||||
<Filter>ripple\protocol</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\protocol\STInteger.h">
|
||||
<Filter>ripple\protocol</Filter>
|
||||
</ClInclude>
|
||||
|
||||
149
src/ripple/basics/Buffer.h
Normal file
149
src/ripple/basics/Buffer.h
Normal file
@@ -0,0 +1,149 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of rippled: https://github.com/ripple/rippled
|
||||
Copyright (c) 2012, 2013 Ripple Labs Inc.
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#ifndef RIPPLE_BASICS_BUFFER_H_INCLUDED
|
||||
#define RIPPLE_BASICS_BUFFER_H_INCLUDED
|
||||
|
||||
#include <beast/utility/noexcept.h>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
namespace ripple {
|
||||
|
||||
/** Like std::vector<char> but better.
|
||||
Meets the requirements of BufferFactory.
|
||||
*/
|
||||
class Buffer
|
||||
{
|
||||
private:
|
||||
std::unique_ptr<
|
||||
std::uint8_t[]> p_;
|
||||
std::size_t size_ = 0;
|
||||
|
||||
public:
|
||||
Buffer() = default;
|
||||
Buffer (Buffer const&) = delete;
|
||||
Buffer& operator= (Buffer const&) = delete;
|
||||
|
||||
/** Move-construct.
|
||||
The other buffer is reset.
|
||||
*/
|
||||
Buffer (Buffer&& other)
|
||||
: p_ (std::move(other.p_))
|
||||
, size_ (other.size_)
|
||||
{
|
||||
other.size_ = 0;
|
||||
}
|
||||
|
||||
/** Move-assign.
|
||||
The other buffer is reset.
|
||||
*/
|
||||
Buffer& operator= (Buffer&& other)
|
||||
{
|
||||
p_ = std::move(other.p_);
|
||||
size_ = other.size_;
|
||||
other.size_ = 0;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** Create an uninitialized buffer with the given size. */
|
||||
explicit
|
||||
Buffer (std::size_t size)
|
||||
: p_ (size ?
|
||||
new std::uint8_t[size] : nullptr)
|
||||
, size_ (size)
|
||||
{
|
||||
}
|
||||
|
||||
/** Create a buffer as a copy of existing memory. */
|
||||
Buffer (void const* data, std::size_t size)
|
||||
: p_ (size ?
|
||||
new std::uint8_t[size] : nullptr)
|
||||
, size_ (size)
|
||||
{
|
||||
std::memcpy(p_.get(), data, size);
|
||||
}
|
||||
|
||||
/** Returns the number of bytes in the buffer. */
|
||||
std::size_t
|
||||
size() const noexcept
|
||||
{
|
||||
return size_;
|
||||
}
|
||||
|
||||
/** Return a pointer to beginning of the storage.
|
||||
@note The return type is guaranteed to be a pointer
|
||||
to a single byte, to facilitate pointer arithmetic.
|
||||
*/
|
||||
/** @{ */
|
||||
std::uint8_t const*
|
||||
data() const noexcept
|
||||
{
|
||||
return p_.get();
|
||||
}
|
||||
|
||||
std::uint8_t*
|
||||
data() noexcept
|
||||
{
|
||||
return p_.get();
|
||||
}
|
||||
/** @} */
|
||||
|
||||
/** Reset the buffer.
|
||||
All memory is deallocated. The resulting size is 0.
|
||||
*/
|
||||
void
|
||||
clear() noexcept
|
||||
{
|
||||
p_.reset();
|
||||
size_ = 0;
|
||||
}
|
||||
|
||||
/** Reallocate the storage.
|
||||
Existing data, if any, is discarded.
|
||||
*/
|
||||
std::uint8_t*
|
||||
alloc (std::size_t n)
|
||||
{
|
||||
if (n == 0)
|
||||
{
|
||||
clear();
|
||||
return nullptr;
|
||||
}
|
||||
if (n != size_)
|
||||
{
|
||||
p_.reset(new std::uint8_t[n]);
|
||||
size_ = n;
|
||||
}
|
||||
return p_.get();
|
||||
}
|
||||
|
||||
// Meet the requirements of BufferFactory
|
||||
void*
|
||||
operator()(std::size_t n)
|
||||
{
|
||||
return alloc(n);
|
||||
}
|
||||
};
|
||||
|
||||
} // ripple
|
||||
|
||||
#endif
|
||||
110
src/ripple/basics/Slice.h
Normal file
110
src/ripple/basics/Slice.h
Normal file
@@ -0,0 +1,110 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of rippled: https://github.com/ripple/rippled
|
||||
Copyright (c) 2012, 2013 Ripple Labs Inc.
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#ifndef RIPPLE_BASICS_SLICE_H_INCLUDED
|
||||
#define RIPPLE_BASICS_SLICE_H_INCLUDED
|
||||
|
||||
#include <beast/utility/noexcept.h>
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
|
||||
namespace ripple {
|
||||
|
||||
/** An immutable linear range of bytes.
|
||||
|
||||
A fully constructed Slice is guaranteed to be in a valid state.
|
||||
Default construction, construction from nullptr, and zero-byte
|
||||
ranges are disallowed. A Slice is lightweight and copyable, it
|
||||
retains no ownership of the underlying memory.
|
||||
*/
|
||||
class Slice
|
||||
{
|
||||
private:
|
||||
std::uint8_t const* data_;
|
||||
std::size_t size_;
|
||||
|
||||
public:
|
||||
// Disallowed
|
||||
Slice() = delete;
|
||||
|
||||
Slice (Slice const&) = default;
|
||||
|
||||
Slice& operator= (Slice const&) = default;
|
||||
|
||||
/** Create a slice pointing to existing memory. */
|
||||
Slice (void const* data, std::size_t size)
|
||||
: data_ (reinterpret_cast<
|
||||
std::uint8_t const*>(data))
|
||||
, size_ (size)
|
||||
{
|
||||
assert(data_ != nullptr);
|
||||
assert(size_ > 0);
|
||||
}
|
||||
|
||||
/** Returns the number of bytes in the storage.
|
||||
|
||||
This will never be zero.
|
||||
*/
|
||||
std::size_t
|
||||
size() const noexcept
|
||||
{
|
||||
return size_;
|
||||
}
|
||||
|
||||
/** Return a pointer to beginning of the storage.
|
||||
@note The return type is guaranteed to be a pointer
|
||||
to a single byte, to facilitate pointer arithmetic.
|
||||
*/
|
||||
std::uint8_t const*
|
||||
data() const noexcept
|
||||
{
|
||||
return data_;
|
||||
}
|
||||
};
|
||||
|
||||
template <class Hasher>
|
||||
inline
|
||||
void
|
||||
hash_append (Hasher& h, Slice const& v)
|
||||
{
|
||||
h.append(v.data(), v.size());
|
||||
}
|
||||
|
||||
inline
|
||||
bool
|
||||
operator== (Slice const& lhs, Slice const& rhs) noexcept
|
||||
{
|
||||
return lhs.size() == rhs.size() &&
|
||||
std::memcmp(
|
||||
lhs.data(), rhs.data(), lhs.size()) == 0;
|
||||
}
|
||||
|
||||
inline
|
||||
bool
|
||||
operator< (Slice const& lhs, Slice const& rhs) noexcept
|
||||
{
|
||||
return std::lexicographical_compare(
|
||||
lhs.data(), lhs.data() + lhs.size(),
|
||||
rhs.data(), rhs.data() + rhs.size());
|
||||
}
|
||||
|
||||
} // ripple
|
||||
|
||||
#endif
|
||||
185
src/ripple/protocol/AnyPublicKey.h
Normal file
185
src/ripple/protocol/AnyPublicKey.h
Normal file
@@ -0,0 +1,185 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of rippled: https://github.com/ripple/rippled
|
||||
Copyright (c) 2012, 2013 Ripple Labs Inc.
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#ifndef RIPPLE_PROTOCOL_ANYPUBLICKEY_H_INCLUDED
|
||||
#define RIPPLE_PROTOCOL_ANYPUBLICKEY_H_INCLUDED
|
||||
|
||||
#include <ripple/basics/Buffer.h>
|
||||
#include <ripple/protocol/HashPrefix.h>
|
||||
#include <ripple/protocol/STExchange.h>
|
||||
#include <ripple/protocol/STObject.h>
|
||||
#include <beast/hash/hash_append.h>
|
||||
#include <beast/utility/noexcept.h>
|
||||
#include <boost/utility/base_from_member.hpp>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
namespace ripple {
|
||||
|
||||
enum class KeyType
|
||||
{
|
||||
unknown,
|
||||
secp256k1,
|
||||
ed25519
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
/** Variant container for all public keys. */
|
||||
class AnyPublicKeySlice
|
||||
: public Slice
|
||||
{
|
||||
public:
|
||||
#ifdef _MSC_VER
|
||||
AnyPublicKeySlice (
|
||||
void const* data, std::size_t size)
|
||||
: Slice (data, size)
|
||||
{
|
||||
}
|
||||
#else
|
||||
using Slice::Slice;
|
||||
#endif
|
||||
|
||||
AnyPublicKeySlice() = delete;
|
||||
|
||||
AnyPublicKeySlice (
|
||||
AnyPublicKeySlice const&) = default;
|
||||
|
||||
AnyPublicKeySlice& operator= (
|
||||
AnyPublicKeySlice const&) = default;
|
||||
|
||||
/** Returns the type of key stored. */
|
||||
KeyType
|
||||
type() const noexcept;
|
||||
|
||||
/** Verify a signature using this public key. */
|
||||
bool
|
||||
verify (void const* msg, std::size_t msg_size,
|
||||
void const* sig, std::size_t sig_size) const;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct STExchange<STBlob, AnyPublicKeySlice>
|
||||
{
|
||||
using value_type = AnyPublicKeySlice;
|
||||
|
||||
static
|
||||
void
|
||||
get (boost::optional<value_type>& t,
|
||||
STBlob const& u)
|
||||
{
|
||||
t = boost::in_place(u.data(), u.size());
|
||||
}
|
||||
|
||||
static
|
||||
std::unique_ptr<STBlob>
|
||||
set (SField const& f, AnyPublicKeySlice const& t)
|
||||
{
|
||||
return std::make_unique<STBlob>(
|
||||
f, t.data(), t.size());
|
||||
}
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
/** Variant container for all public keys, with ownership. */
|
||||
class AnyPublicKey
|
||||
: private boost::base_from_member<Buffer>
|
||||
, public AnyPublicKeySlice
|
||||
{
|
||||
private:
|
||||
using buffer_type = boost::base_from_member<Buffer>;
|
||||
|
||||
public:
|
||||
AnyPublicKey() = delete;
|
||||
AnyPublicKey (AnyPublicKey const&) = delete;
|
||||
AnyPublicKey& operator= (AnyPublicKey const&) = delete;
|
||||
|
||||
#ifdef _MSC_VER
|
||||
AnyPublicKey (AnyPublicKey&& other)
|
||||
: buffer_type(std::move(other.buffer_type::member))
|
||||
, AnyPublicKeySlice (buffer_type::member.data(),
|
||||
buffer_type::member.size())
|
||||
{
|
||||
}
|
||||
|
||||
AnyPublicKey& operator= (AnyPublicKey&& other)
|
||||
{
|
||||
buffer_type::member =
|
||||
std::move(other.buffer_type::member);
|
||||
return *this;
|
||||
}
|
||||
#else
|
||||
AnyPublicKey (AnyPublicKey&&) = default;
|
||||
AnyPublicKey& operator= (AnyPublicKey&&) = default;
|
||||
#endif
|
||||
|
||||
AnyPublicKey (void const* data_, std::size_t size_)
|
||||
: buffer_type (data_, size_)
|
||||
, AnyPublicKeySlice (
|
||||
member.data(), member.size())
|
||||
{
|
||||
}
|
||||
|
||||
/** Returns ownership of the underlying Buffer.
|
||||
After calling this function, only the destructor
|
||||
or the move assignment operator may be called.
|
||||
*/
|
||||
Buffer
|
||||
releaseBuffer() noexcept
|
||||
{
|
||||
return std::move(buffer_type::member);
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct STExchange<STBlob, AnyPublicKey>
|
||||
{
|
||||
using value_type = AnyPublicKey;
|
||||
|
||||
static
|
||||
void
|
||||
get (boost::optional<value_type>& t,
|
||||
STBlob const& u)
|
||||
{
|
||||
t = boost::in_place(u.data(), u.size());
|
||||
}
|
||||
|
||||
static
|
||||
std::unique_ptr<STBlob>
|
||||
set (SField const& f, AnyPublicKey const& t)
|
||||
{
|
||||
return std::make_unique<STBlob>(
|
||||
f, t.data(), t.size());
|
||||
}
|
||||
|
||||
static
|
||||
std::unique_ptr<STBlob>
|
||||
set (SField const& f, AnyPublicKey&& t)
|
||||
{
|
||||
return std::make_unique<STBlob>(
|
||||
f, t.releaseBuffer());
|
||||
}
|
||||
};
|
||||
|
||||
} // ripple
|
||||
|
||||
#endif
|
||||
85
src/ripple/protocol/AnySecretKey.h
Normal file
85
src/ripple/protocol/AnySecretKey.h
Normal file
@@ -0,0 +1,85 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of rippled: https://github.com/ripple/rippled
|
||||
Copyright (c) 2012, 2013 Ripple Labs Inc.
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#ifndef RIPPLE_PROTOCOL_ANYSECRETKEY_H_INCLUDED
|
||||
#define RIPPLE_PROTOCOL_ANYSECRETKEY_H_INCLUDED
|
||||
|
||||
#include <ripple/basics/Buffer.h>
|
||||
#include <ripple/protocol/AnyPublicKey.h>
|
||||
#include <ripple/protocol/HashPrefix.h>
|
||||
#include <boost/utility/base_from_member.hpp>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
namespace ripple {
|
||||
|
||||
/** Variant container for secret key, with ownership. */
|
||||
class AnySecretKey
|
||||
{
|
||||
private:
|
||||
Buffer p_;
|
||||
KeyType type_;
|
||||
|
||||
public:
|
||||
AnySecretKey() = delete;
|
||||
AnySecretKey (AnySecretKey const&) = delete;
|
||||
AnySecretKey& operator= (AnySecretKey const&) = delete;
|
||||
|
||||
/** Destroy the key.
|
||||
The memory area is secure erased.
|
||||
*/
|
||||
~AnySecretKey();
|
||||
|
||||
AnySecretKey (AnySecretKey&& other);
|
||||
|
||||
AnySecretKey& operator= (AnySecretKey&& other);
|
||||
|
||||
AnySecretKey (KeyType type,
|
||||
void const* data, std::size_t size);
|
||||
|
||||
/** Returns the type of secret key. */
|
||||
KeyType
|
||||
type() const noexcept
|
||||
{
|
||||
return type_;
|
||||
}
|
||||
|
||||
/** Returns the corresponding public key. */
|
||||
AnyPublicKey
|
||||
publicKey() const;
|
||||
|
||||
/** Create a signature for the given message. */
|
||||
Buffer
|
||||
sign (void const* msg, std::size_t msg_len) const;
|
||||
|
||||
/** Securely generate a new ed25519 secret key. */
|
||||
static
|
||||
AnySecretKey
|
||||
make_ed25519();
|
||||
|
||||
/** Securely generate a new secp256k1 key pair. */
|
||||
static
|
||||
std::pair<AnySecretKey, AnyPublicKey>
|
||||
make_secp256k1_pair();
|
||||
};
|
||||
|
||||
} // ripple
|
||||
|
||||
#endif
|
||||
@@ -25,6 +25,7 @@
|
||||
#include <ripple/protocol/RipplePublicKey.h>
|
||||
#include <ripple/protocol/UInt160.h>
|
||||
#include <ripple/protocol/UintTypes.h>
|
||||
#include <beast/utility/noexcept.h>
|
||||
|
||||
namespace ripple {
|
||||
|
||||
@@ -53,6 +54,18 @@ private:
|
||||
public:
|
||||
RippleAddress ();
|
||||
|
||||
void const*
|
||||
data() const noexcept
|
||||
{
|
||||
return vchData.data();
|
||||
}
|
||||
|
||||
std::size_t
|
||||
size() const noexcept
|
||||
{
|
||||
return vchData.size();
|
||||
}
|
||||
|
||||
// For public and private key, checks if they are legal.
|
||||
bool isValid () const
|
||||
{
|
||||
|
||||
@@ -22,9 +22,16 @@
|
||||
|
||||
#include <ripple/basics/BasicTypes.h>
|
||||
#include <ripple/json/json_value.h>
|
||||
#include <cstdint>
|
||||
#include <utility>
|
||||
|
||||
namespace ripple {
|
||||
|
||||
// Forwards
|
||||
class STBlob;
|
||||
template <class>
|
||||
class STInteger;
|
||||
|
||||
enum SerializedTypeID
|
||||
{
|
||||
// special types
|
||||
@@ -139,7 +146,7 @@ public:
|
||||
{}
|
||||
#endif
|
||||
|
||||
private:
|
||||
protected:
|
||||
// These constructors can only be called from FieldNames.cpp
|
||||
SField (SerializedTypeID tid, int fv, const char* fn,
|
||||
int meta = sMD_Default, bool signing = true);
|
||||
@@ -253,6 +260,25 @@ private:
|
||||
static int num;
|
||||
};
|
||||
|
||||
/** A field with a type known at compile time. */
|
||||
template <class T>
|
||||
struct TypedField : SField
|
||||
{
|
||||
using type = T;
|
||||
|
||||
template <class... Args>
|
||||
explicit
|
||||
TypedField (Args&&... args)
|
||||
: SField(std::forward<Args>(args)...)
|
||||
{
|
||||
}
|
||||
|
||||
TypedField (TypedField&& u)
|
||||
: SField(std::move(u))
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
extern SField const sfInvalid;
|
||||
extern SField const sfGeneric;
|
||||
extern SField const sfLedgerEntry;
|
||||
@@ -272,7 +298,7 @@ extern SField const sfTransactionType;
|
||||
// 32-bit integers (common)
|
||||
extern SField const sfFlags;
|
||||
extern SField const sfSourceTag;
|
||||
extern SField const sfSequence;
|
||||
extern TypedField<STInteger<std::uint32_t>> const sfSequence;
|
||||
extern SField const sfPreviousTxnLgrSeq;
|
||||
extern SField const sfLedgerSequence;
|
||||
extern SField const sfCloseTime;
|
||||
@@ -359,12 +385,12 @@ extern SField const sfRippleEscrow;
|
||||
extern SField const sfDeliveredAmount;
|
||||
|
||||
// variable length
|
||||
extern SField const sfPublicKey;
|
||||
extern TypedField<STBlob> const sfPublicKey;
|
||||
extern SField const sfMessageKey;
|
||||
extern SField const sfSigningPubKey;
|
||||
extern TypedField<STBlob> const sfSigningPubKey;
|
||||
extern SField const sfTxnSignature;
|
||||
extern SField const sfGenerator;
|
||||
extern SField const sfSignature;
|
||||
extern TypedField<STBlob> const sfSignature;
|
||||
extern SField const sfDomain;
|
||||
extern SField const sfFundCode;
|
||||
extern SField const sfRemoveCode;
|
||||
@@ -413,6 +439,8 @@ extern SField const sfSufficient;
|
||||
extern SField const sfAffectedNodes;
|
||||
extern SField const sfMemos;
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
} // ripple
|
||||
|
||||
#endif
|
||||
|
||||
@@ -20,7 +20,10 @@
|
||||
#ifndef RIPPLE_PROTOCOL_STBLOB_H_INCLUDED
|
||||
#define RIPPLE_PROTOCOL_STBLOB_H_INCLUDED
|
||||
|
||||
#include <ripple/basics/Buffer.h>
|
||||
#include <ripple/basics/Slice.h>
|
||||
#include <ripple/protocol/STBase.h>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
|
||||
namespace ripple {
|
||||
@@ -30,19 +33,58 @@ class STBlob
|
||||
: public STBase
|
||||
{
|
||||
public:
|
||||
using value_type = Slice;
|
||||
|
||||
STBlob () = default;
|
||||
|
||||
/** Construct with size and initializer.
|
||||
Init will be called as:
|
||||
void(void* data, std::size_t size)
|
||||
*/
|
||||
template <class Init>
|
||||
STBlob (SField::ref f, std::size_t size,
|
||||
Init&& init)
|
||||
: STBase(f)
|
||||
{
|
||||
value.resize(size);
|
||||
init(value.data(), value.size());
|
||||
}
|
||||
|
||||
STBlob (SField::ref f,
|
||||
void const* data, std::size_t size)
|
||||
: STBase(f)
|
||||
{
|
||||
value.resize(size);
|
||||
std::memcpy(value.data(), data, size);
|
||||
}
|
||||
|
||||
STBlob (SField const& f, Buffer&& b)
|
||||
: STBase(f)
|
||||
{
|
||||
// VFALCO TODO Really move the buffer
|
||||
value.resize(b.size());
|
||||
std::memcpy(value.data(),
|
||||
b.data(), b.size());
|
||||
auto tmp = std::move(b);
|
||||
}
|
||||
|
||||
// VFALCO DEPRECATED
|
||||
STBlob (Blob const& v)
|
||||
: value (v)
|
||||
{ }
|
||||
{
|
||||
}
|
||||
|
||||
// VFALCO DEPRECATED
|
||||
STBlob (SField::ref n, Blob const& v)
|
||||
: STBase (n), value (v)
|
||||
{ }
|
||||
: STBase (n)
|
||||
, value (v)
|
||||
{
|
||||
}
|
||||
|
||||
STBlob (SField::ref n)
|
||||
: STBase (n)
|
||||
{ }
|
||||
{
|
||||
}
|
||||
|
||||
STBlob (SerialIter&, SField::ref name = sfGeneric);
|
||||
|
||||
@@ -53,6 +95,19 @@ public:
|
||||
return std::make_unique<STBlob> (name, sit.getVL ());
|
||||
}
|
||||
|
||||
std::size_t
|
||||
size() const
|
||||
{
|
||||
return value.size();
|
||||
}
|
||||
|
||||
std::uint8_t const*
|
||||
data() const
|
||||
{
|
||||
return reinterpret_cast<
|
||||
std::uint8_t const*>(value.data());
|
||||
}
|
||||
|
||||
SerializedTypeID
|
||||
getSType () const override
|
||||
{
|
||||
@@ -95,6 +150,13 @@ public:
|
||||
value = v;
|
||||
}
|
||||
|
||||
void
|
||||
setValue (void const* data, std::size_t size)
|
||||
{
|
||||
value.resize(size);
|
||||
std::memcpy(value.data(), data, size);
|
||||
}
|
||||
|
||||
explicit
|
||||
operator Blob () const
|
||||
{
|
||||
|
||||
202
src/ripple/protocol/STExchange.h
Normal file
202
src/ripple/protocol/STExchange.h
Normal file
@@ -0,0 +1,202 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of rippled: https://github.com/ripple/rippled
|
||||
Copyright (c) 2012, 2013 Ripple Labs Inc.
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#ifndef RIPPLE_PROTOCOL_STEXCHANGE_H_INCLUDED
|
||||
#define RIPPLE_PROTOCOL_STEXCHANGE_H_INCLUDED
|
||||
|
||||
#include <ripple/basics/Buffer.h>
|
||||
#include <ripple/basics/Slice.h>
|
||||
#include <ripple/protocol/SField.h>
|
||||
#include <ripple/protocol/STBlob.h>
|
||||
#include <ripple/protocol/STInteger.h>
|
||||
#include <ripple/protocol/STObject.h>
|
||||
#include <ripple/basics/Blob.h>
|
||||
#include <boost/optional.hpp>
|
||||
#include <beast/cxx14/memory.h> // <memory>
|
||||
#include <stdexcept>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
namespace ripple {
|
||||
|
||||
/** Convert between serialized type U and C++ type T. */
|
||||
template <class U, class T>
|
||||
struct STExchange;
|
||||
|
||||
|
||||
template <class U, class T>
|
||||
struct STExchange<STInteger<U>, T>
|
||||
{
|
||||
using value_type = U;
|
||||
|
||||
static
|
||||
void
|
||||
get (boost::optional<T>& t,
|
||||
STInteger<U> const& u)
|
||||
{
|
||||
t = u.getValue();
|
||||
}
|
||||
|
||||
static
|
||||
std::unique_ptr<STInteger<U>>
|
||||
set (SField const& f, T const& t)
|
||||
{
|
||||
return std::make_unique<
|
||||
STInteger<U>>(f, t);
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct STExchange<STBlob, Slice>
|
||||
{
|
||||
using value_type = Slice;
|
||||
|
||||
static
|
||||
void
|
||||
get (boost::optional<value_type>& t,
|
||||
STBlob const& u)
|
||||
{
|
||||
t = boost::in_place(u.data(), u.size());
|
||||
}
|
||||
|
||||
static
|
||||
std::unique_ptr<STBlob>
|
||||
set (TypedField<STBlob> const& f,
|
||||
Slice const& t)
|
||||
{
|
||||
return std::make_unique<STBlob>(
|
||||
f, t.data(), t.size());
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct STExchange<STBlob, Buffer>
|
||||
{
|
||||
using value_type = Buffer;
|
||||
|
||||
static
|
||||
void
|
||||
get (boost::optional<Buffer>& t,
|
||||
STBlob const& u)
|
||||
{
|
||||
t = boost::in_place(
|
||||
u.data(), u.size());
|
||||
}
|
||||
|
||||
static
|
||||
std::unique_ptr<STBlob>
|
||||
set (TypedField<STBlob> const& f,
|
||||
Buffer const& t)
|
||||
{
|
||||
return std::make_unique<STBlob>(
|
||||
f, t.data(), t.size());
|
||||
}
|
||||
|
||||
static
|
||||
std::unique_ptr<STBlob>
|
||||
set (TypedField<STBlob> const& f,
|
||||
Buffer&& t)
|
||||
{
|
||||
return std::make_unique<STBlob>(
|
||||
f, std::move(t));
|
||||
}
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
/** Return the value of a field in an STObject as a given type. */
|
||||
/** @{ */
|
||||
template <class T, class U>
|
||||
boost::optional<T>
|
||||
get (STObject const& st,
|
||||
TypedField<U> const& f)
|
||||
{
|
||||
boost::optional<T> t;
|
||||
STBase const* const b =
|
||||
st.peekAtPField(f);
|
||||
if (! b)
|
||||
return t;
|
||||
auto const id = b->getSType();
|
||||
if (id == STI_NOTPRESENT)
|
||||
return t;
|
||||
auto const u =
|
||||
dynamic_cast<U const*>(b);
|
||||
// This should never happen
|
||||
if (! u)
|
||||
throw std::runtime_error (
|
||||
"Wrong field type");
|
||||
STExchange<U, T>::get(t, *u);
|
||||
return t;
|
||||
}
|
||||
|
||||
template <class U>
|
||||
boost::optional<typename STExchange<
|
||||
U, typename U::value_type>::value_type>
|
||||
get (STObject const& st,
|
||||
TypedField<U> const& f)
|
||||
{
|
||||
return get<typename U::value_type>(st, f);
|
||||
}
|
||||
/** @} */
|
||||
|
||||
/** Set a field value in an STObject. */
|
||||
template <class U, class T>
|
||||
void
|
||||
set (STObject& st,
|
||||
TypedField<U> const& f, T&& t)
|
||||
{
|
||||
st.set(STExchange<U,
|
||||
typename std::decay<T>::type>::set(
|
||||
f, std::forward<T>(t)));
|
||||
}
|
||||
|
||||
/** Set a blob field using an init function. */
|
||||
template <class Init>
|
||||
void
|
||||
set (STObject& st,
|
||||
TypedField<STBlob> const& f,
|
||||
std::size_t size, Init&& init)
|
||||
{
|
||||
st.set(std::make_unique<STBlob>(
|
||||
f, size, init));
|
||||
}
|
||||
|
||||
/** Set a blob field from data. */
|
||||
template <class = void>
|
||||
void
|
||||
set (STObject& st,
|
||||
TypedField<STBlob> const& f,
|
||||
void const* data, std::size_t size)
|
||||
{
|
||||
st.set(std::make_unique<STBlob>(
|
||||
f, data, size));
|
||||
}
|
||||
|
||||
/** Remove a field in an STObject. */
|
||||
template <class U>
|
||||
void
|
||||
erase (STObject& st,
|
||||
TypedField<U> const& f)
|
||||
{
|
||||
st.makeFieldAbsent(f);
|
||||
}
|
||||
|
||||
} // ripple
|
||||
|
||||
#endif
|
||||
@@ -29,6 +29,8 @@ class STInteger
|
||||
: public STBase
|
||||
{
|
||||
public:
|
||||
using value_type = Integer;
|
||||
|
||||
explicit
|
||||
STInteger (Integer v)
|
||||
: value_ (v)
|
||||
|
||||
@@ -221,6 +221,12 @@ public:
|
||||
const STVector256& getFieldV256 (SField::ref field) const;
|
||||
const STArray& getFieldArray (SField::ref field) const;
|
||||
|
||||
/** Set a field.
|
||||
if the field already exists, it is replaced.
|
||||
*/
|
||||
void
|
||||
set (std::unique_ptr<STBase> v);
|
||||
|
||||
void setFieldU8 (SField::ref field, unsigned char);
|
||||
void setFieldU16 (SField::ref field, std::uint16_t);
|
||||
void setFieldU32 (SField::ref field, std::uint32_t);
|
||||
|
||||
@@ -63,6 +63,18 @@ public:
|
||||
;
|
||||
}
|
||||
|
||||
std::size_t
|
||||
size() const noexcept
|
||||
{
|
||||
return mData.size();
|
||||
}
|
||||
|
||||
void const*
|
||||
data() const noexcept
|
||||
{
|
||||
return mData.data();
|
||||
}
|
||||
|
||||
// assemble functions
|
||||
int add8 (unsigned char byte);
|
||||
int add16 (std::uint16_t);
|
||||
@@ -239,10 +251,6 @@ public:
|
||||
{
|
||||
return mData.end ();
|
||||
}
|
||||
Blob ::size_type size () const
|
||||
{
|
||||
return mData.size ();
|
||||
}
|
||||
void reserve (size_t n)
|
||||
{
|
||||
mData.reserve (n);
|
||||
|
||||
50
src/ripple/protocol/Sign.h
Normal file
50
src/ripple/protocol/Sign.h
Normal file
@@ -0,0 +1,50 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of rippled: https://github.com/ripple/rippled
|
||||
Copyright (c) 2012, 2013 Ripple Labs Inc.
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#ifndef RIPPLE_PROTOCOL_SIGN_H_INCLUDED
|
||||
#define RIPPLE_PROTOCOL_SIGN_H_INCLUDED
|
||||
|
||||
#include <ripple/protocol/AnyPublicKey.h>
|
||||
#include <ripple/protocol/AnySecretKey.h>
|
||||
#include <ripple/protocol/HashPrefix.h>
|
||||
#include <ripple/protocol/STObject.h>
|
||||
#include <utility>
|
||||
|
||||
namespace ripple {
|
||||
|
||||
/** Sign a STObject using any secret key.
|
||||
The signature is placed in sfSignature. If
|
||||
a signature already exists, it is overwritten.
|
||||
*/
|
||||
void
|
||||
sign (STObject& st,
|
||||
HashPrefix const& prefix,
|
||||
AnySecretKey const& sk);
|
||||
|
||||
/** Verify the signature on a STObject.
|
||||
The signature must be contained in sfSignature.
|
||||
*/
|
||||
bool
|
||||
verify (STObject const& st,
|
||||
HashPrefix const& prefix,
|
||||
AnyPublicKeySlice const& pk);
|
||||
|
||||
} // ripple
|
||||
|
||||
#endif
|
||||
93
src/ripple/protocol/impl/AnyPublicKey.cpp
Normal file
93
src/ripple/protocol/impl/AnyPublicKey.cpp
Normal file
@@ -0,0 +1,93 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of rippled: https://github.com/ripple/rippled
|
||||
Copyright (c) 2012, 2013 Ripple Labs Inc.
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include <ripple/protocol/AnyPublicKey.h>
|
||||
#include <ripple/protocol/Serializer.h>
|
||||
#include <ripple/protocol/STExchange.h>
|
||||
#include <ed25519-donna/ed25519.h>
|
||||
#include <cassert>
|
||||
|
||||
namespace ripple {
|
||||
|
||||
/** Verify a secp256k1 signature. */
|
||||
bool
|
||||
verify_secp256k1 (void const* pk,
|
||||
void const* msg, std::size_t msg_size,
|
||||
void const* sig, std::size_t sig_size)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool
|
||||
verify_ed25519 (void const* pk,
|
||||
void const* msg, std::size_t msg_size,
|
||||
void const* sig, std::size_t sig_size)
|
||||
{
|
||||
if (sig_size != 64)
|
||||
return false;
|
||||
ed25519_public_key epk;
|
||||
ed25519_signature es;
|
||||
std::memcpy(epk, pk, 32);
|
||||
std::memcpy(es, sig, sig_size);
|
||||
return ed25519_sign_open(
|
||||
reinterpret_cast<unsigned char const*>(msg),
|
||||
msg_size, epk, es) == 0;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
KeyType
|
||||
AnyPublicKeySlice::type() const noexcept
|
||||
{
|
||||
auto const pk = data();
|
||||
auto const pk_size = size();
|
||||
|
||||
if (pk_size < 1)
|
||||
return KeyType::unknown;
|
||||
auto const len = pk_size - 1;
|
||||
if (len == 32 &&
|
||||
pk[0] == 0xED)
|
||||
return KeyType::ed25519;
|
||||
if (len == 33 &&
|
||||
(pk[0] == 0x02 || pk[0] == 0x03))
|
||||
return KeyType::secp256k1;
|
||||
return KeyType::unknown;
|
||||
}
|
||||
|
||||
bool
|
||||
AnyPublicKeySlice::verify (
|
||||
void const* msg, std::size_t msg_size,
|
||||
void const* sig, std::size_t sig_size) const
|
||||
{
|
||||
switch(type())
|
||||
{
|
||||
case KeyType::ed25519:
|
||||
return verify_ed25519(data() + 1,
|
||||
msg, msg_size, sig, sig_size);
|
||||
case KeyType::secp256k1:
|
||||
return verify_secp256k1(data() + 1,
|
||||
msg, msg_size, sig, sig_size);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
// throw?
|
||||
return false;
|
||||
}
|
||||
|
||||
} // ripple
|
||||
143
src/ripple/protocol/impl/AnySecretKey.cpp
Normal file
143
src/ripple/protocol/impl/AnySecretKey.cpp
Normal file
@@ -0,0 +1,143 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of rippled: https://github.com/ripple/rippled
|
||||
Copyright (c) 2012, 2013 Ripple Labs Inc.
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include <ripple/protocol/AnySecretKey.h>
|
||||
#include <ripple/protocol/RippleAddress.h>
|
||||
#include <ripple/protocol/Serializer.h>
|
||||
#include <ripple/crypto/RandomNumbers.h>
|
||||
#include <ed25519-donna/ed25519.h>
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
|
||||
namespace ripple {
|
||||
|
||||
AnySecretKey::~AnySecretKey()
|
||||
{
|
||||
// secure erase
|
||||
std::fill(p_.data(), p_.data() + p_.size(), 0);
|
||||
}
|
||||
|
||||
AnySecretKey::AnySecretKey (AnySecretKey&& other)
|
||||
: p_ (std::move(other.p_))
|
||||
, type_ (other.type_)
|
||||
{
|
||||
other.type_ = KeyType::unknown;
|
||||
}
|
||||
|
||||
AnySecretKey&
|
||||
AnySecretKey::operator= (AnySecretKey&& other)
|
||||
{
|
||||
p_ = std::move(other.p_);
|
||||
type_ = other.type_;
|
||||
other.type_ = KeyType::unknown;
|
||||
return *this;
|
||||
}
|
||||
|
||||
AnySecretKey::AnySecretKey (KeyType type,
|
||||
void const* data, std::size_t size)
|
||||
: p_ (data, size)
|
||||
, type_ (type)
|
||||
{
|
||||
if (type_ == KeyType::unknown)
|
||||
throw std::runtime_error(
|
||||
"AnySecretKey: unknown type");
|
||||
if (type_ == KeyType::ed25519 &&
|
||||
size != 32)
|
||||
throw std::runtime_error(
|
||||
"AnySecretKey: wrong ed25519 size");
|
||||
if (type_ == KeyType::secp256k1 &&
|
||||
size != 32)
|
||||
throw std::runtime_error(
|
||||
"AnySecretKey: wrong secp256k1 size");
|
||||
}
|
||||
|
||||
AnyPublicKey
|
||||
AnySecretKey::publicKey() const
|
||||
{
|
||||
switch (type())
|
||||
{
|
||||
case KeyType::ed25519:
|
||||
{
|
||||
unsigned char buf[33];
|
||||
buf[0] = 0xED;
|
||||
ed25519_publickey(p_.data() + 1, &buf[1]);
|
||||
return AnyPublicKey(buf, sizeof(buf));
|
||||
}
|
||||
default:
|
||||
throw std::runtime_error(
|
||||
"AnySecretKey: unknown type");
|
||||
};
|
||||
}
|
||||
|
||||
Buffer
|
||||
AnySecretKey::sign (
|
||||
void const* msg, std::size_t msg_len) const
|
||||
{
|
||||
switch(type_)
|
||||
{
|
||||
case KeyType::ed25519:
|
||||
{
|
||||
auto const sk = p_.data() + 1;
|
||||
ed25519_public_key pk;
|
||||
ed25519_publickey(sk, pk);
|
||||
Buffer b(64);
|
||||
ed25519_sign(reinterpret_cast<
|
||||
unsigned char const*>(msg), msg_len,
|
||||
sk, pk, b.data());
|
||||
return b;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
throw std::runtime_error(
|
||||
"AnySecretKey: unknown type");
|
||||
}
|
||||
|
||||
AnySecretKey
|
||||
AnySecretKey::make_ed25519()
|
||||
{
|
||||
std::uint8_t buf[32];
|
||||
random_fill(&buf[0], sizeof(buf));
|
||||
AnySecretKey ask(KeyType::ed25519,
|
||||
buf, sizeof(buf));
|
||||
// secure erase
|
||||
std::fill(buf, buf + sizeof(buf), 0);
|
||||
return ask;
|
||||
}
|
||||
|
||||
std::pair<AnySecretKey, AnyPublicKey>
|
||||
AnySecretKey::make_secp256k1_pair()
|
||||
{
|
||||
// VFALCO What a pile
|
||||
RippleAddress s;
|
||||
s.setSeedRandom();
|
||||
RippleAddress const g =
|
||||
RippleAddress::createGeneratorPublic(s);
|
||||
RippleAddress sk;
|
||||
sk.setAccountPrivate (g, s, 0);
|
||||
RippleAddress pk;
|
||||
pk.setAccountPublic (g, 0);
|
||||
return std::pair<AnySecretKey, AnyPublicKey>(
|
||||
std::piecewise_construct, std::make_tuple(
|
||||
KeyType::secp256k1, sk.data(), sk.size()),
|
||||
std::make_tuple(pk.data(), pk.size()));
|
||||
}
|
||||
|
||||
} // ripple
|
||||
@@ -41,7 +41,6 @@ typedef std::lock_guard <std::mutex> StaticScopedLockType;
|
||||
// Give this translation unit only, permission to construct SFields
|
||||
struct SField::make
|
||||
{
|
||||
#ifndef _MSC_VER
|
||||
template <class ...Args>
|
||||
static SField one(SField const* p, Args&& ...args)
|
||||
{
|
||||
@@ -49,45 +48,14 @@ struct SField::make
|
||||
knownCodeToField[result.fieldCode] = p;
|
||||
return result;
|
||||
}
|
||||
#else // remove this when VS gets variadic templates
|
||||
template <class A0>
|
||||
static SField one(SField const* p, A0&& arg0)
|
||||
{
|
||||
SField result(std::forward<A0>(arg0));
|
||||
knownCodeToField[result.fieldCode] = p;
|
||||
return result;
|
||||
}
|
||||
|
||||
template <class A0, class A1, class A2>
|
||||
static SField one(SField const* p, A0&& arg0, A1&& arg1, A2&& arg2)
|
||||
template <class T, class ...Args>
|
||||
static TypedField<T> one(SField const* p, Args&& ...args)
|
||||
{
|
||||
SField result(std::forward<A0>(arg0), std::forward<A1>(arg1),
|
||||
std::forward<A2>(arg2));
|
||||
TypedField<T> result(std::forward<Args>(args)...);
|
||||
knownCodeToField[result.fieldCode] = p;
|
||||
return result;
|
||||
}
|
||||
|
||||
template <class A0, class A1, class A2, class A3>
|
||||
static SField one(SField const* p, A0&& arg0, A1&& arg1, A2&& arg2,
|
||||
A3&& arg3)
|
||||
{
|
||||
SField result(std::forward<A0>(arg0), std::forward<A1>(arg1),
|
||||
std::forward<A2>(arg2), std::forward<A3>(arg3));
|
||||
knownCodeToField[result.fieldCode] = p;
|
||||
return result;
|
||||
}
|
||||
|
||||
template <class A0, class A1, class A2, class A3, class A4>
|
||||
static SField one(SField const* p, A0&& arg0, A1&& arg1, A2&& arg2,
|
||||
A3&& arg3, A4&& arg4)
|
||||
{
|
||||
SField result(std::forward<A0>(arg0), std::forward<A1>(arg1),
|
||||
std::forward<A2>(arg2), std::forward<A3>(arg3),
|
||||
std::forward<A4>(arg4));
|
||||
knownCodeToField[result.fieldCode] = p;
|
||||
return result;
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
||||
using make = SField::make;
|
||||
@@ -116,7 +84,7 @@ SField const sfTransactionType = make::one(&sfTransactionType, STI_UINT16, 2, "T
|
||||
// 32-bit integers (common)
|
||||
SField const sfFlags = make::one(&sfFlags, STI_UINT32, 2, "Flags");
|
||||
SField const sfSourceTag = make::one(&sfSourceTag, STI_UINT32, 3, "SourceTag");
|
||||
SField const sfSequence = make::one(&sfSequence, STI_UINT32, 4, "Sequence");
|
||||
TypedField<STInteger<std::uint32_t>> const sfSequence = make::one<STInteger<std::uint32_t>>(&sfSequence, STI_UINT32, 4, "Sequence");
|
||||
SField const sfPreviousTxnLgrSeq = make::one(&sfPreviousTxnLgrSeq, STI_UINT32, 5, "PreviousTxnLgrSeq", SField::sMD_DeleteFinal);
|
||||
SField const sfLedgerSequence = make::one(&sfLedgerSequence, STI_UINT32, 6, "LedgerSequence");
|
||||
SField const sfCloseTime = make::one(&sfCloseTime, STI_UINT32, 7, "CloseTime");
|
||||
@@ -203,12 +171,12 @@ SField const sfRippleEscrow = make::one(&sfRippleEscrow, STI_AMOUNT, 17, "
|
||||
SField const sfDeliveredAmount = make::one(&sfDeliveredAmount, STI_AMOUNT, 18, "DeliveredAmount");
|
||||
|
||||
// variable length
|
||||
SField const sfPublicKey = make::one(&sfPublicKey, STI_VL, 1, "PublicKey");
|
||||
TypedField<STBlob> const sfPublicKey = make::one<STBlob>(&sfPublicKey, STI_VL, 1, "PublicKey");
|
||||
TypedField<STBlob> const sfSigningPubKey = make::one<STBlob>(&sfSigningPubKey, STI_VL, 3, "SigningPubKey");
|
||||
TypedField<STBlob> const sfSignature = make::one<STBlob>(&sfSignature, STI_VL, 6, "Signature", SField::sMD_Default, false);
|
||||
SField const sfMessageKey = make::one(&sfMessageKey, STI_VL, 2, "MessageKey");
|
||||
SField const sfSigningPubKey = make::one(&sfSigningPubKey, STI_VL, 3, "SigningPubKey");
|
||||
SField const sfTxnSignature = make::one(&sfTxnSignature, STI_VL, 4, "TxnSignature", SField::sMD_Default, false);
|
||||
SField const sfGenerator = make::one(&sfGenerator, STI_VL, 5, "Generator");
|
||||
SField const sfSignature = make::one(&sfSignature, STI_VL, 6, "Signature", SField::sMD_Default, false);
|
||||
SField const sfDomain = make::one(&sfDomain, STI_VL, 7, "Domain");
|
||||
SField const sfFundCode = make::one(&sfFundCode, STI_VL, 8, "FundCode");
|
||||
SField const sfRemoveCode = make::one(&sfRemoveCode, STI_VL, 9, "RemoveCode");
|
||||
|
||||
@@ -762,6 +762,24 @@ const STVector256& STObject::getFieldV256 (SField::ref field) const
|
||||
return getFieldByConstRef <STVector256> (field, empty);
|
||||
}
|
||||
|
||||
void
|
||||
STObject::set (std::unique_ptr<STBase> v)
|
||||
{
|
||||
auto const i =
|
||||
getFieldIndex(v->getFName());
|
||||
if (i != -1)
|
||||
{
|
||||
mData.replace(i, v.release());
|
||||
}
|
||||
else
|
||||
{
|
||||
if (! isFree())
|
||||
throw std::runtime_error(
|
||||
"missing field in templated STObject");
|
||||
mData.push_back(v.release());
|
||||
}
|
||||
}
|
||||
|
||||
void STObject::setFieldU8 (SField::ref field, unsigned char v)
|
||||
{
|
||||
setFieldUsingSetValue <STUInt8> (field, v);
|
||||
|
||||
51
src/ripple/protocol/impl/Sign.cpp
Normal file
51
src/ripple/protocol/impl/Sign.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of rippled: https://github.com/ripple/rippled
|
||||
Copyright (c) 2012, 2013 Ripple Labs Inc.
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include <ripple/protocol/Sign.h>
|
||||
|
||||
namespace ripple {
|
||||
|
||||
void
|
||||
sign (STObject& st, HashPrefix const& prefix,
|
||||
AnySecretKey const& sk)
|
||||
{
|
||||
Serializer ss;
|
||||
ss.add32(prefix);
|
||||
st.add(ss, false);
|
||||
set(st, sfSignature,
|
||||
sk.sign(ss.data(), ss.size()));
|
||||
}
|
||||
|
||||
bool
|
||||
verify (STObject const& st,
|
||||
HashPrefix const& prefix,
|
||||
AnyPublicKeySlice const& pk)
|
||||
{
|
||||
auto const sig = get(st, sfSignature);
|
||||
if (! sig)
|
||||
return false;
|
||||
Serializer ss;
|
||||
ss.add32(prefix);
|
||||
st.add(ss, false);
|
||||
return pk.verify(
|
||||
ss.data(), ss.size(),
|
||||
sig->data(), sig->size());
|
||||
}
|
||||
|
||||
} // ripple
|
||||
@@ -19,6 +19,8 @@
|
||||
|
||||
#include <BeastConfig.h>
|
||||
|
||||
#include <ripple/protocol/impl/AnyPublicKey.cpp>
|
||||
#include <ripple/protocol/impl/AnySecretKey.cpp>
|
||||
#include <ripple/protocol/impl/BuildInfo.cpp>
|
||||
#include <ripple/protocol/impl/ByteOrder.cpp>
|
||||
#include <ripple/protocol/impl/ErrorCodes.cpp>
|
||||
|
||||
Reference in New Issue
Block a user