mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-06 17:27:52 +00:00
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
291 lines
8.1 KiB
C++
291 lines
8.1 KiB
C++
//------------------------------------------------------------------------------
|
|
/*
|
|
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_RIPPLEADDRESS_H_INCLUDED
|
|
#define RIPPLE_PROTOCOL_RIPPLEADDRESS_H_INCLUDED
|
|
|
|
#include <ripple/crypto/Base58Data.h>
|
|
#include <ripple/crypto/ECDSACanonical.h>
|
|
#include <ripple/protocol/RipplePublicKey.h>
|
|
#include <ripple/protocol/UInt160.h>
|
|
#include <ripple/protocol/UintTypes.h>
|
|
#include <beast/utility/noexcept.h>
|
|
|
|
namespace ripple {
|
|
|
|
//
|
|
// Used to hold addresses and parse and produce human formats.
|
|
//
|
|
// XXX This needs to be reworked to store data in uint160 and uint256.
|
|
|
|
class RippleAddress : private CBase58Data
|
|
{
|
|
private:
|
|
typedef enum
|
|
{
|
|
VER_NONE = 1,
|
|
VER_NODE_PUBLIC = 28,
|
|
VER_NODE_PRIVATE = 32,
|
|
VER_ACCOUNT_ID = 0,
|
|
VER_ACCOUNT_PUBLIC = 35,
|
|
VER_ACCOUNT_PRIVATE = 34,
|
|
VER_FAMILY_GENERATOR = 41,
|
|
VER_FAMILY_SEED = 33,
|
|
} VersionEncoding;
|
|
|
|
bool mIsValid;
|
|
|
|
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
|
|
{
|
|
return mIsValid;
|
|
}
|
|
|
|
void clear ();
|
|
bool isSet () const;
|
|
|
|
static void clearCache ();
|
|
|
|
/** Returns the public key.
|
|
Precondition: version == VER_NODE_PUBLIC
|
|
*/
|
|
RipplePublicKey
|
|
toPublicKey() const;
|
|
|
|
//
|
|
// Node Public - Also used for Validators
|
|
//
|
|
NodeID getNodeID () const;
|
|
Blob const& getNodePublic () const;
|
|
|
|
std::string humanNodePublic () const;
|
|
|
|
bool setNodePublic (std::string const& strPublic);
|
|
void setNodePublic (Blob const& vPublic);
|
|
bool verifyNodePublic (uint256 const& hash, Blob const& vchSig,
|
|
ECDSA mustBeFullyCanonical) const;
|
|
bool verifyNodePublic (uint256 const& hash, std::string const& strSig,
|
|
ECDSA mustBeFullyCanonical) const;
|
|
|
|
static RippleAddress createNodePublic (RippleAddress const& naSeed);
|
|
static RippleAddress createNodePublic (Blob const& vPublic);
|
|
static RippleAddress createNodePublic (std::string const& strPublic);
|
|
|
|
//
|
|
// Node Private
|
|
//
|
|
Blob const& getNodePrivateData () const;
|
|
uint256 getNodePrivate () const;
|
|
|
|
std::string humanNodePrivate () const;
|
|
|
|
bool setNodePrivate (std::string const& strPrivate);
|
|
void setNodePrivate (Blob const& vPrivate);
|
|
void setNodePrivate (uint256 hash256);
|
|
void signNodePrivate (uint256 const& hash, Blob& vchSig) const;
|
|
|
|
static RippleAddress createNodePrivate (RippleAddress const& naSeed);
|
|
|
|
//
|
|
// Accounts IDs
|
|
//
|
|
Account getAccountID () const;
|
|
|
|
std::string humanAccountID () const;
|
|
|
|
bool setAccountID (
|
|
std::string const& strAccountID,
|
|
Base58::Alphabet const& alphabet = Base58::getRippleAlphabet());
|
|
void setAccountID (Account const& hash160In);
|
|
|
|
static RippleAddress createAccountID (Account const& uiAccountID);
|
|
|
|
//
|
|
// Accounts Public
|
|
//
|
|
Blob const& getAccountPublic () const;
|
|
|
|
std::string humanAccountPublic () const;
|
|
|
|
bool setAccountPublic (std::string const& strPublic);
|
|
void setAccountPublic (Blob const& vPublic);
|
|
void setAccountPublic (RippleAddress const& generator, int seq);
|
|
|
|
bool accountPublicVerify (uint256 const& uHash, Blob const& vucSig,
|
|
ECDSA mustBeFullyCanonical) const;
|
|
|
|
static RippleAddress createAccountPublic (Blob const& vPublic)
|
|
{
|
|
RippleAddress naNew;
|
|
naNew.setAccountPublic (vPublic);
|
|
return naNew;
|
|
}
|
|
|
|
static std::string createHumanAccountPublic (Blob const& vPublic)
|
|
{
|
|
return createAccountPublic (vPublic).humanAccountPublic ();
|
|
}
|
|
|
|
// Create a deterministic public key from a public generator.
|
|
static RippleAddress createAccountPublic (
|
|
RippleAddress const& naGenerator, int iSeq);
|
|
|
|
//
|
|
// Accounts Private
|
|
//
|
|
uint256 getAccountPrivate () const;
|
|
|
|
bool setAccountPrivate (std::string const& strPrivate);
|
|
void setAccountPrivate (Blob const& vPrivate);
|
|
void setAccountPrivate (uint256 hash256);
|
|
void setAccountPrivate (RippleAddress const& naGenerator,
|
|
RippleAddress const& naSeed, int seq);
|
|
|
|
bool accountPrivateSign (uint256 const& uHash, Blob& vucSig) const;
|
|
|
|
// Encrypt a message.
|
|
Blob accountPrivateEncrypt (
|
|
RippleAddress const& naPublicTo, Blob const& vucPlainText) const;
|
|
|
|
// Decrypt a message.
|
|
Blob accountPrivateDecrypt (
|
|
RippleAddress const& naPublicFrom, Blob const& vucCipherText) const;
|
|
|
|
static RippleAddress createAccountPrivate (
|
|
RippleAddress const& generator, RippleAddress const& seed, int iSeq);
|
|
|
|
static RippleAddress createAccountPrivate (Blob const& vPrivate)
|
|
{
|
|
RippleAddress naNew;
|
|
|
|
naNew.setAccountPrivate (vPrivate);
|
|
|
|
return naNew;
|
|
}
|
|
|
|
//
|
|
// Generators
|
|
// Use to generate a master or regular family.
|
|
//
|
|
Blob const& getGenerator () const;
|
|
|
|
std::string humanGenerator () const;
|
|
|
|
bool setGenerator (std::string const& strGenerator);
|
|
void setGenerator (Blob const& vPublic);
|
|
// void setGenerator(RippleAddress const& seed);
|
|
|
|
// Create generator for making public deterministic keys.
|
|
static RippleAddress createGeneratorPublic (RippleAddress const& naSeed);
|
|
|
|
//
|
|
// Seeds
|
|
// Clients must disallow reconizable entries from being seeds.
|
|
uint128 getSeed () const;
|
|
|
|
std::string humanSeed () const;
|
|
std::string humanSeed1751 () const;
|
|
|
|
bool setSeed (std::string const& strSeed);
|
|
int setSeed1751 (std::string const& strHuman1751);
|
|
bool setSeedGeneric (std::string const& strText);
|
|
void setSeed (uint128 hash128);
|
|
void setSeedRandom ();
|
|
|
|
static RippleAddress createSeedRandom ();
|
|
static RippleAddress createSeedGeneric (std::string const& strText);
|
|
|
|
std::string ToString () const
|
|
{return static_cast<CBase58Data const&>(*this).ToString();}
|
|
|
|
template <class Hasher>
|
|
friend
|
|
void
|
|
hash_append(Hasher& hasher, RippleAddress const& value)
|
|
{
|
|
using beast::hash_append;
|
|
hash_append(hasher, static_cast<CBase58Data const&>(value));
|
|
}
|
|
|
|
friend
|
|
bool
|
|
operator==(RippleAddress const& lhs, RippleAddress const& rhs)
|
|
{
|
|
return static_cast<CBase58Data const&>(lhs) ==
|
|
static_cast<CBase58Data const&>(rhs);
|
|
}
|
|
|
|
friend
|
|
bool
|
|
operator <(RippleAddress const& lhs, RippleAddress const& rhs)
|
|
{
|
|
return static_cast<CBase58Data const&>(lhs) <
|
|
static_cast<CBase58Data const&>(rhs);
|
|
}
|
|
};
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
inline
|
|
bool
|
|
operator!=(RippleAddress const& lhs, RippleAddress const& rhs)
|
|
{
|
|
return !(lhs == rhs);
|
|
}
|
|
|
|
inline
|
|
bool
|
|
operator >(RippleAddress const& lhs, RippleAddress const& rhs)
|
|
{
|
|
return rhs < lhs;
|
|
}
|
|
|
|
inline
|
|
bool
|
|
operator<=(RippleAddress const& lhs, RippleAddress const& rhs)
|
|
{
|
|
return !(rhs < lhs);
|
|
}
|
|
|
|
inline
|
|
bool
|
|
operator>=(RippleAddress const& lhs, RippleAddress const& rhs)
|
|
{
|
|
return !(lhs < rhs);
|
|
}
|
|
|
|
} // ripple
|
|
|
|
#endif
|