-
Notifications
You must be signed in to change notification settings - Fork 972
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1156 from stellar/next-db-and-proto-3
Next db and proto 3 Reviewed-by: vogel
- Loading branch information
Showing
109 changed files
with
4,510 additions
and
1,674 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2016 Stellar Development Foundation and contributors. Licensed | ||
// under the Apache License, Version 2.0. See the COPYING file at the root | ||
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
#include "KeyUtils.h" | ||
|
||
#include "crypto/StrKey.h" | ||
|
||
namespace stellar | ||
{ | ||
|
||
size_t | ||
KeyUtils::getKeyVersionSize(strKey::StrKeyVersionByte keyVersion) | ||
{ | ||
switch (keyVersion) | ||
{ | ||
case strKey::STRKEY_PUBKEY_ED25519: | ||
case strKey::STRKEY_SEED_ED25519: | ||
return crypto_sign_PUBLICKEYBYTES; | ||
case strKey::STRKEY_PRE_AUTH_TX: | ||
case strKey::STRKEY_HASH_X: | ||
return 32U; | ||
default: | ||
throw std::invalid_argument("invalid key version: " + std::to_string(keyVersion)); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
#pragma once | ||
|
||
// Copyright 2016 Stellar Development Foundation and contributors. Licensed | ||
// under the Apache License, Version 2.0. See the COPYING file at the root | ||
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
#include "crypto/StrKey.h" | ||
#include "xdr/Stellar-types.h" | ||
|
||
#include <sodium.h> | ||
|
||
#include <string> | ||
|
||
namespace stellar | ||
{ | ||
|
||
template<typename T> | ||
struct KeyFunctions | ||
{ | ||
struct getKeyTypeEnum | ||
{ | ||
}; | ||
|
||
static std::string getKeyTypeName(); | ||
static bool getKeyVersionIsSupported(strKey::StrKeyVersionByte keyVersion); | ||
static typename getKeyTypeEnum::type toKeyType(strKey::StrKeyVersionByte keyVersion); | ||
static strKey::StrKeyVersionByte toKeyVersion(typename getKeyTypeEnum::type keyType); | ||
static uint256 & getKeyValue(T &key); | ||
static uint256 const& getKeyValue(T const &key); | ||
}; | ||
|
||
// signer key utility functions | ||
namespace KeyUtils | ||
{ | ||
|
||
template<typename T> | ||
std::string | ||
toStrKey(T const& key) | ||
{ | ||
return strKey::toStrKey(KeyFunctions<T>::toKeyVersion(key.type()), KeyFunctions<T>::getKeyValue(key)); | ||
} | ||
|
||
template<typename T> | ||
std::string | ||
toShortString(T const& key) | ||
{ | ||
return toStrKey(key).substr(0, 5); | ||
} | ||
|
||
std::size_t | ||
getKeyVersionSize(strKey::StrKeyVersionByte keyVersion); | ||
|
||
template<typename T> | ||
T | ||
fromStrKey(std::string const& s) | ||
{ | ||
T key; | ||
uint8_t verByte; | ||
std::vector<uint8_t> k; | ||
if (!strKey::fromStrKey(s, verByte, k)) | ||
{ | ||
throw std::invalid_argument("bad " + KeyFunctions<T>::getKeyTypeName()); | ||
} | ||
|
||
strKey::StrKeyVersionByte ver = static_cast<strKey::StrKeyVersionByte>(verByte); | ||
if (!KeyFunctions<T>::getKeyVersionIsSupported(ver) || | ||
(k.size() != getKeyVersionSize(ver)) || | ||
(s.size() != strKey::getStrKeySize(getKeyVersionSize(ver)))) | ||
{ | ||
throw std::invalid_argument("bad " + KeyFunctions<T>::getKeyTypeName()); | ||
} | ||
|
||
key.type(KeyFunctions<T>::toKeyType(ver)); | ||
std::copy(k.begin(), k.end(), KeyFunctions<T>::getKeyValue(key).begin()); | ||
return key; | ||
} | ||
|
||
template<typename T, typename F> | ||
bool | ||
canConvert(F const &fromKey) | ||
{ | ||
return KeyFunctions<T>::getKeyVersionIsSupported(KeyFunctions<F>::toKeyVersion(fromKey.type())); | ||
} | ||
|
||
template<typename T, typename F> | ||
T | ||
convertKey(F const &fromKey) | ||
{ | ||
T toKey; | ||
toKey.type(KeyFunctions<T>::toKeyType(KeyFunctions<F>::toKeyVersion(fromKey.type()))); | ||
KeyFunctions<T>::getKeyValue(toKey) = KeyFunctions<F>::getKeyValue(fromKey); | ||
return toKey; | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.