mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-20 11:05:54 +00:00
This is a way to do deterministic keys that fits in better.
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
#include "PeerDoor.h"
|
||||
#include "RPCDoor.h"
|
||||
#include "BitcoinUtil.h"
|
||||
#include "DeterministicKeys.h"
|
||||
#include "key.h"
|
||||
#include "database/SqliteDatabase.h"
|
||||
//#include <boost/log/trivial.hpp>
|
||||
#include <iostream>
|
||||
@@ -31,9 +31,9 @@ Application::Application()
|
||||
mPeerDoor=NULL;
|
||||
mRPCDoor=NULL;
|
||||
|
||||
DetKeySet ks("This is a test payphrase.");
|
||||
|
||||
Ledger::pointer firstLedger(new Ledger(ks.getAccountID(0), 1000000));
|
||||
CKey::pointer account_key(new CKey(CKey::GetBaseFromString("This is my payphrase."), 0, false));
|
||||
|
||||
Ledger::pointer firstLedger(new Ledger(account_key->GetAddress().GetHash160(), 1000000));
|
||||
firstLedger->setClosed();
|
||||
firstLedger->setAccepted();
|
||||
mMasterLedger.pushLedger(firstLedger);
|
||||
|
||||
@@ -4,30 +4,20 @@
|
||||
#include <openssl/pem.h>
|
||||
#include <openssl/rand.h>
|
||||
|
||||
#include "DeterministicKeys.h"
|
||||
// Functions to add CKey support for deterministic EC keys
|
||||
|
||||
#include "Serializer.h"
|
||||
|
||||
DetKeySet::DetKeySet(const std::string& phrase)
|
||||
{ // create a deterministic key set based on a phrase
|
||||
getPhrase(phrase, mBase);
|
||||
}
|
||||
|
||||
uint256 DetKeySet::getKeySetName()
|
||||
{ // get the name of a deterministic key set
|
||||
Serializer s;
|
||||
s.add256(mBase);
|
||||
return s.getSHA512Half();
|
||||
}
|
||||
|
||||
void DetKeySet::getPhrase(const std::string& phrase, uint256& base)
|
||||
uint256 CKey::GetBaseFromString(const std::string& phrase)
|
||||
{
|
||||
Serializer s;
|
||||
s.addRaw((const void *) phrase.c_str(), phrase.length());
|
||||
base=s.getSHA512Half();
|
||||
uint256 base(s.getSHA512Half());
|
||||
s.secureErase();
|
||||
return base;
|
||||
}
|
||||
|
||||
static EC_KEY* GenerateDeterministicKey(const uint256& base, int n)
|
||||
EC_KEY* CKey::GenerateDeterministicKey(const uint256& base, uint32 n, bool private_key)
|
||||
{
|
||||
BN_CTX* ctx=BN_CTX_new();
|
||||
if(!ctx) return NULL;
|
||||
@@ -43,6 +33,7 @@ static EC_KEY* GenerateDeterministicKey(const uint256& base, int n)
|
||||
}
|
||||
if(!EC_GROUP_get_order(EC_KEY_get0_group(pkey), order, ctx))
|
||||
{
|
||||
assert(false);
|
||||
BN_free(order);
|
||||
EC_KEY_free(pkey);
|
||||
BN_CTX_free(ctx);
|
||||
@@ -65,8 +56,9 @@ static EC_KEY* GenerateDeterministicKey(const uint256& base, int n)
|
||||
|
||||
BN_free(order);
|
||||
|
||||
if(!EC_KEY_set_private_key(pkey, privKey))
|
||||
if(private_key && !EC_KEY_set_private_key(pkey, privKey))
|
||||
{ // set the random point as the private key
|
||||
assert(false);
|
||||
BN_free(privKey);
|
||||
BN_CTX_free(ctx);
|
||||
return NULL;
|
||||
@@ -75,6 +67,7 @@ static EC_KEY* GenerateDeterministicKey(const uint256& base, int n)
|
||||
EC_POINT *pubKey=EC_POINT_new(EC_KEY_get0_group(pkey));
|
||||
if(!EC_POINT_mul(EC_KEY_get0_group(pkey), pubKey, privKey, NULL, NULL, ctx))
|
||||
{ // compute the corresponding public key point
|
||||
assert(false);
|
||||
BN_free(privKey);
|
||||
EC_POINT_free(pubKey);
|
||||
BN_CTX_free(ctx);
|
||||
@@ -83,6 +76,7 @@ static EC_KEY* GenerateDeterministicKey(const uint256& base, int n)
|
||||
BN_free(privKey);
|
||||
if(!EC_KEY_set_public_key(pkey, pubKey))
|
||||
{
|
||||
assert(false);
|
||||
EC_POINT_free(pubKey);
|
||||
BN_CTX_free(ctx);
|
||||
return NULL;
|
||||
@@ -95,67 +89,8 @@ static EC_KEY* GenerateDeterministicKey(const uint256& base, int n)
|
||||
return pkey;
|
||||
}
|
||||
|
||||
bool DetKeySet::getRandom(uint256& r)
|
||||
uint256 CKey::GetRandomBase(void)
|
||||
{
|
||||
return RAND_bytes((unsigned char *) &r, sizeof(uint256)) == 1;
|
||||
}
|
||||
|
||||
CKey::pointer DetKeySet::getPubKey(uint32 n)
|
||||
{
|
||||
EC_KEY *k=GenerateDeterministicKey(mBase, n);
|
||||
if(k==NULL) return CKey::pointer();
|
||||
|
||||
unsigned int size=i2o_ECPublicKey(k, NULL);
|
||||
std::vector<unsigned char> pubKey(size, 0);
|
||||
unsigned char* begin=&pubKey[0];
|
||||
i2o_ECPublicKey(k, &begin);
|
||||
EC_KEY_free(k);
|
||||
|
||||
CKey::pointer ret(new CKey());
|
||||
ret->SetPubKey(pubKey);
|
||||
return ret;
|
||||
}
|
||||
|
||||
CKey::pointer DetKeySet::getPrivKey(uint32 n)
|
||||
{
|
||||
EC_KEY *k=GenerateDeterministicKey(mBase, n);
|
||||
if(k==NULL) return CKey::pointer();
|
||||
|
||||
unsigned int size=i2d_ECPrivateKey(k, NULL);
|
||||
std::vector<unsigned char> privKey(size, 0);
|
||||
unsigned char* begin=&privKey[0];
|
||||
i2d_ECPrivateKey(k, &begin);
|
||||
EC_KEY_free(k);
|
||||
|
||||
CKey::pointer ret(new CKey());
|
||||
ret->SetPrivKey(privKey);
|
||||
memset(&privKey[0], 0, privKey.size());
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint160 DetKeySet::getAccountID(uint32 n)
|
||||
{
|
||||
return getPubKey(n)->GetAddress().GetHash160();
|
||||
}
|
||||
|
||||
void DetKeySet::getPubKeys(uint32 first, uint32 count, std::list<CKey::pointer>& keys)
|
||||
{
|
||||
while(count-->0)
|
||||
keys.push_back(getPubKey(first++));
|
||||
}
|
||||
|
||||
void DetKeySet::getPrivKeys(uint32 first, uint32 count, std::list<CKey::pointer>& keys)
|
||||
{
|
||||
while(count-->0)
|
||||
keys.push_back(getPrivKey(first++));
|
||||
}
|
||||
|
||||
void DetKeySet::unitTest()
|
||||
{
|
||||
uint256 u;
|
||||
GenerateDeterministicKey(u, 0);
|
||||
GenerateDeterministicKey(u, 1);
|
||||
u++;
|
||||
GenerateDeterministicKey(u, 0);
|
||||
GenerateDeterministicKey(u, 1);
|
||||
uint256 r;
|
||||
return (RAND_bytes((unsigned char *) &r, sizeof(uint256)) == 1) ? r : 0;
|
||||
}
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
#ifndef __DETERMINISTICKEYS__
|
||||
#define __DETERMINISTICKEYS__
|
||||
|
||||
#include <list>
|
||||
|
||||
#include "key.h"
|
||||
|
||||
class DetKeySet
|
||||
{
|
||||
protected:
|
||||
uint256 mBase;
|
||||
|
||||
public:
|
||||
DetKeySet(const uint256& b) : mBase(b) { ; }
|
||||
DetKeySet(const std::string& phrase);
|
||||
|
||||
~DetKeySet()
|
||||
{
|
||||
memset(&mBase, 0, sizeof(mBase));
|
||||
}
|
||||
|
||||
void reBase(const uint256& newBase) { mBase=newBase; }
|
||||
uint256 getKeySetName();
|
||||
|
||||
static bool getRandom(uint256&);
|
||||
static void getPhrase(const std::string& phrase, uint256& base);
|
||||
|
||||
CKey::pointer getPubKey(uint32 n);
|
||||
CKey::pointer getPrivKey(uint32 n);
|
||||
uint160 getAccountID(uint32 n);
|
||||
|
||||
void getPubKeys(uint32 first, uint32 count, std::list<CKey::pointer>& keys);
|
||||
void getPrivKeys(uint32 first, uint32 count, std::list<CKey::pointer>& keys);
|
||||
|
||||
static void unitTest();
|
||||
};
|
||||
|
||||
#endif
|
||||
11
key.h
11
key.h
@@ -86,6 +86,8 @@ protected:
|
||||
EC_KEY* pkey;
|
||||
bool fSet;
|
||||
|
||||
static EC_KEY* GenerateDeterministicKey(const uint256& base, uint32 n, bool private_key);
|
||||
|
||||
public:
|
||||
typedef boost::shared_ptr<CKey> pointer;
|
||||
|
||||
@@ -115,11 +117,20 @@ public:
|
||||
return (*this);
|
||||
}
|
||||
|
||||
|
||||
~CKey()
|
||||
{
|
||||
EC_KEY_free(pkey);
|
||||
}
|
||||
|
||||
CKey(const uint256& base, uint32 seq, bool private_key) : fSet(true)
|
||||
{
|
||||
pkey=GenerateDeterministicKey(base, seq, private_key);
|
||||
}
|
||||
|
||||
static uint256 GetBaseFromString(const std::string& base);
|
||||
static uint256 GetRandomBase(void);
|
||||
|
||||
bool IsNull() const
|
||||
{
|
||||
return !fSet;
|
||||
|
||||
Reference in New Issue
Block a user