Merge branch 'master' of github.com:jedmccaleb/NewCoin

This commit is contained in:
JoelKatz
2012-05-10 16:05:28 -07:00
9 changed files with 160 additions and 31 deletions

View File

@@ -20,9 +20,11 @@ public:
typedef boost::shared_ptr<AccountState> pointer;
private:
NewcoinAddress mAccountID;
SerializedLedgerEntry::pointer mLedgerEntry;
bool mValid;
NewcoinAddress mAccountID;
NewcoinAddress mAuthorizedKey;
SerializedLedgerEntry::pointer mLedgerEntry;
bool mValid;
public:
AccountState(const NewcoinAddress& AccountID); // For new accounts

View File

@@ -106,16 +106,64 @@ void Application::run()
//
mConnectionPool.start();
// New stuff.
NewcoinAddress rootSeedMaster;
NewcoinAddress rootSeedRegular;
NewcoinAddress rootGeneratorMaster;
NewcoinAddress rootGeneratorRegular;
NewcoinAddress reservedPublicRegular;
NewcoinAddress reservedPrivateRegular;
NewcoinAddress rootAddress;
rootSeedMaster.setFamilySeed(CKey::PassPhraseToKey("Master passphrase."));
rootSeedRegular.setFamilySeed(CKey::PassPhraseToKey("Regular passphrase."));
std::cerr << "Master seed: " << rootSeedMaster.humanFamilySeed() << std::endl;
std::cerr << "Regular seed: " << rootSeedRegular.humanFamilySeed() << std::endl;
rootGeneratorMaster.setFamilyGenerator(rootSeedMaster);
rootGeneratorRegular.setFamilyGenerator(rootSeedRegular);
std::cerr << "Master generator: " << rootGeneratorMaster.humanFamilyGenerator() << std::endl;
std::cerr << "Regular generator: " << rootGeneratorRegular.humanFamilyGenerator() << std::endl;
rootAddress.setAccountPublic(rootGeneratorMaster, 0);
std::cerr << "Regular address: " << rootAddress.humanAccountPublic() << std::endl;
reservedPublicRegular.setAccountPublic(rootGeneratorRegular, -1);
reservedPrivateRegular.setAccountPrivate(rootGeneratorRegular, rootSeedRegular, -1);
std::cerr << "Reserved public regular: " << reservedPublicRegular.humanAccountPublic() << std::endl;
std::cerr << "Reserved private regular: " << reservedPrivateRegular.humanAccountPrivate() << std::endl;
// hash of regular account #reserved public key.
uint160 uiGeneratorID = reservedPublicRegular.getAccountID();
// std::cerr << "uiGeneratorID: " << uiGeneratorID << std::endl;
// Encrypt with regular account #reserved private key.
std::vector<unsigned char> vucGeneratorCipher = reservedPrivateRegular.accountPrivateEncrypt(reservedPublicRegular, rootGeneratorMaster.getFamilyGenerator());
std::cerr << "Plain: " << strHex(rootGeneratorMaster.getFamilyGenerator()) << std::endl;
std::cerr << "Cipher: " << strHex(vucGeneratorCipher) << std::endl;
std::vector<unsigned char> vucGeneratorText = reservedPrivateRegular.accountPrivateDecrypt(reservedPublicRegular, vucGeneratorCipher);
std::cerr << "Plain: " << strHex(vucGeneratorText) << std::endl;
// Temporary root account will be ["This is my payphrase."]:0
NewcoinAddress rootFamilySeed; // Hold the 128 password.
NewcoinAddress rootFamilyGenerator; // Hold the generator.
NewcoinAddress rootAddress;
// NewcoinAddress rootAddress;
rootFamilySeed.setFamilySeed(CKey::PassPhraseToKey("This is my payphrase."));
rootFamilyGenerator.setFamilyGenerator(rootFamilySeed);
rootAddress.setAccountPublic(rootFamilyGenerator, 0);
std::cerr << "Root account: " << rootAddress.humanAccountID() << std::endl;
Ledger::pointer firstLedger = boost::make_shared<Ledger>(rootAddress, 100000000);
assert(!!firstLedger->getAccountState(rootAddress));
firstLedger->updateHash();

View File

@@ -16,7 +16,6 @@
#include "BitcoinUtil.h"
#include "SHAMap.h"
enum LedgerStateParms
{
lepNONE = 0, // no special flags
@@ -38,20 +37,19 @@ class Ledger : public boost::enable_shared_from_this<Ledger>
public:
typedef boost::shared_ptr<Ledger> pointer;
enum TransResult
{
TR_ERROR =-1,
TR_SUCCESS =0,
TR_NOTFOUND =1,
TR_ALREADY =2,
TR_BADTRANS =3, // the transaction itself is corrupt
TR_BADACCT =4, // one of the accounts is invalid
TR_INSUFF =5, // the sending(apply)/receiving(remove) account is broke
TR_PASTASEQ =6, // account is past this transaction
TR_PREASEQ =7, // account is missing transactions before this
TR_BADLSEQ =8, // ledger too early
TR_TOOSMALL =9, // amount is less than Tx fee
TR_ERROR = -1,
TR_SUCCESS = 0,
TR_NOTFOUND = 1,
TR_ALREADY = 2,
TR_BADTRANS = 3, // the transaction itself is corrupt
TR_BADACCT = 4, // one of the accounts is invalid
TR_INSUFF = 5, // the sending(apply)/receiving(remove) account is broke
TR_PASTASEQ = 6, // account is past this transaction
TR_PREASEQ = 7, // account is missing transactions before this
TR_BADLSEQ = 8, // ledger too early
TR_TOOSMALL = 9, // amount is less than Tx fee
};

View File

@@ -8,6 +8,7 @@ LedgerEntryFormat LedgerFormats[]=
{ "AccountRoot", ltACCOUNT_ROOT, {
{ S_FIELD(Flags), STI_UINT32, SOE_FLAGS, 0 },
{ S_FIELD(Account), STI_ACCOUNT, SOE_REQUIRED, 0 },
{ S_FIELD(AuthorizedKey),STI_VL, SOE_REQUIRED, 0 },
{ S_FIELD(Sequence), STI_UINT32, SOE_REQUIRED, 0 },
{ S_FIELD(Balance), STI_UINT64, SOE_REQUIRED, 0 },
{ S_FIELD(LastReceive), STI_UINT32, SOE_REQUIRED, 0 },

View File

@@ -3,14 +3,14 @@
#include "Config.h"
#include "BitcoinUtil.h"
#include "rfc1751.h"
#include "utils.h"
#include "openssl/rand.h"
#include <cassert>
#include <algorithm>
#include <iostream>
#include <boost/format.hpp>
#include <boost/functional/hash.hpp>
#include <cassert>
#include <iostream>
#include <openssl/rand.h>
NewcoinAddress::NewcoinAddress()
{
@@ -272,7 +272,7 @@ void NewcoinAddress::setAccountPublic(const std::vector<unsigned char>& vPublic)
void NewcoinAddress::setAccountPublic(const NewcoinAddress& generator, int seq)
{
CKey pubkey = CKey(generator, seq);
CKey pubkey = CKey(generator, seq+1);
setAccountPublic(pubkey.GetPubKey());
}
@@ -281,14 +281,14 @@ void NewcoinAddress::setAccountPublic(const NewcoinAddress& generator, int seq)
// AccountPrivate
//
uint256 NewcoinAddress::getAccountPrivate() const
const std::vector<unsigned char>& NewcoinAddress::getAccountPrivate() const
{
switch (nVersion) {
case VER_NONE:
throw std::runtime_error("unset source");
case VER_ACCOUNT_PRIVATE:
return uint256(vchData);
return vchData;
default:
throw std::runtime_error(str(boost::format("bad source: %d") % int(nVersion)));
@@ -324,6 +324,73 @@ void NewcoinAddress::setAccountPrivate(uint256 hash256)
SetData(VER_ACCOUNT_PRIVATE, hash256.begin(), 32);
}
void NewcoinAddress::setAccountPrivate(const NewcoinAddress& generator, const NewcoinAddress& seed, int seq)
{
CKey privkey = CKey(generator, seed.getFamilyPrivateKey(), seq+1);
setAccountPrivate(privkey.GetPrivKey());
}
std::vector<unsigned char> NewcoinAddress::accountPrivateEncrypt(const NewcoinAddress& naPublicTo, const std::vector<unsigned char>& vucPlainText)
{
CKey ckPrivate;
CKey ckPublic;
std::vector<unsigned char> vucCipherText;
if (!ckPublic.SetPubKey(naPublicTo.getAccountPublic()))
{
// Bad public key.
std::cerr << "accountPrivateEncrypt: Bad public key." << std::endl;
}
else if (!ckPrivate.SetPrivKey(getAccountPrivate()))
{
// Bad private key.
std::cerr << "accountPrivateEncrypt: Bad private key." << std::endl;
}
else
{
try {
vucCipherText = ckPrivate.encryptECIES(ckPublic, vucPlainText);
}
catch (...)
{
nothing();
}
}
return vucCipherText;
}
std::vector<unsigned char> NewcoinAddress::accountPrivateDecrypt(const NewcoinAddress& naPublicFrom, const std::vector<unsigned char>& vucCipherText)
{
CKey ckPrivate;
CKey ckPublic;
std::vector<unsigned char> vucPlainText;
if (!ckPublic.SetPubKey(naPublicFrom.getAccountPublic()))
{
// Bad public key.
std::cerr << "accountPrivateDecrypt: Bad public key." << std::endl;
}
else if (!ckPrivate.SetPrivKey(getAccountPrivate()))
{
// Bad private key.
std::cerr << "accountPrivateDecrypt: Bad private key." << std::endl;
}
else
{
try {
vucPlainText = ckPrivate.decryptECIES(ckPublic, vucCipherText);
}
catch (...)
{
nothing();
}
}
return vucPlainText;
}
//
// Family Generators
//

View File

@@ -78,16 +78,24 @@ public:
//
// Accounts Private
//
uint256 getAccountPrivate() const;
const std::vector<unsigned char>& getAccountPrivate() const;
std::string humanAccountPrivate() const;
bool setAccountPrivate(const std::string& strPrivate);
void setAccountPrivate(const std::vector<unsigned char>& vPrivate);
void setAccountPrivate(uint256 hash256);
void setAccountPrivate(const NewcoinAddress& generator, const NewcoinAddress& seed, int seq);
// Encrypt a message.
std::vector<unsigned char> accountPrivateEncrypt(const NewcoinAddress& naPublicTo, const std::vector<unsigned char>& vucPlainText);
// Decrypt a message.
std::vector<unsigned char> accountPrivateDecrypt(const NewcoinAddress& naPublicFrom, const std::vector<unsigned char>& vucCipherText);
//
// Family Generators
// Given a seed, hold a generator.
//
BIGNUM* getFamilyGeneratorBN() const;
const std::vector<unsigned char>& getFamilyGenerator() const;

View File

@@ -32,6 +32,7 @@ enum SOE_Field
sfBorrower, sfLender, sfLimit, sfOfferCurrency, sfLedgerHash,
sfLastReceive, sfLastTxn, sfNextRate, sfNextRateLgr, sfNextRateExp,
sfNickname, sfMinimumOffer,
sfAuthorizedKey,
// test fields
sfTest1, sfTest2, sfTest3, sfTest4

View File

@@ -69,7 +69,7 @@ void DH_der_gen_hex(std::string& strDer, int iKeyLength)
DH_der_gen(strBuf, iKeyLength);
strHex(strDer, strBuf);
strDer = strHex(strBuf);
}
DH* DH_der_load(const std::string& strDer)

View File

@@ -36,8 +36,10 @@ std::string strJoin(Iterator first, Iterator last, std::string strSeperator)
char charHex(int iDigit);
template<class Iterator>
void strHex(std::string& strDst, Iterator first, int iSize)
std::string strHex(Iterator first, int iSize)
{
std::string strDst;
strDst.resize(iSize*2);
for (int i = 0; i < iSize; i++) {
@@ -46,14 +48,16 @@ void strHex(std::string& strDst, Iterator first, int iSize)
strDst[i*2] = charHex(c >> 4);
strDst[i*2+1] = charHex(c & 15);
}
return strDst;
}
inline void strHex(std::string& strDst, const std::string& strSrc) {
strHex(strDst, strSrc.begin(), strSrc.size());
inline const std::string strHex(const std::string& strSrc) {
return strHex(strSrc.begin(), strSrc.size());
}
inline void strHex(std::string& strDst, const std::vector<unsigned char> vchData) {
strHex(strDst, vchData.begin(), vchData.size());
inline std::string strHex(const std::vector<unsigned char> vchData) {
return strHex(vchData.begin(), vchData.size());
}
int charUnHex(char cDigit);