From 15c940e0367682b27fc32369f4dc0008b7b46a8f Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Thu, 10 May 2012 15:48:55 -0700 Subject: [PATCH 1/4] Simplify strHex --- src/utils.cpp | 2 +- src/utils.h | 14 +++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/utils.cpp b/src/utils.cpp index f67a87724..52bbd43cc 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -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) diff --git a/src/utils.h b/src/utils.h index 986b059f6..1d7ace235 100644 --- a/src/utils.h +++ b/src/utils.h @@ -36,8 +36,10 @@ std::string strJoin(Iterator first, Iterator last, std::string strSeperator) char charHex(int iDigit); template -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 vchData) { - strHex(strDst, vchData.begin(), vchData.size()); +inline std::string strHex(const std::vector vchData) { + return strHex(vchData.begin(), vchData.size()); } int charUnHex(char cDigit); From 5097eab66629c6f55c95971f1716eb83e701a564 Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Thu, 10 May 2012 15:49:46 -0700 Subject: [PATCH 2/4] Add encryption support to Newcoin address. --- src/NewcoinAddress.cpp | 81 ++++++++++++++++++++++++++++++++++++++---- src/NewcoinAddress.h | 10 +++++- 2 files changed, 83 insertions(+), 8 deletions(-) diff --git a/src/NewcoinAddress.cpp b/src/NewcoinAddress.cpp index e07a7d334..a9f229919 100644 --- a/src/NewcoinAddress.cpp +++ b/src/NewcoinAddress.cpp @@ -3,14 +3,14 @@ #include "Config.h" #include "BitcoinUtil.h" #include "rfc1751.h" +#include "utils.h" -#include "openssl/rand.h" - -#include #include -#include #include #include +#include +#include +#include NewcoinAddress::NewcoinAddress() { @@ -272,7 +272,7 @@ void NewcoinAddress::setAccountPublic(const std::vector& 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& 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 NewcoinAddress::accountPrivateEncrypt(const NewcoinAddress& naPublicTo, const std::vector& vucPlainText) +{ + CKey ckPrivate; + CKey ckPublic; + std::vector 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 NewcoinAddress::accountPrivateDecrypt(const NewcoinAddress& naPublicFrom, const std::vector& vucCipherText) +{ + CKey ckPrivate; + CKey ckPublic; + std::vector 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 // diff --git a/src/NewcoinAddress.h b/src/NewcoinAddress.h index 3621c19ef..6ab13e665 100644 --- a/src/NewcoinAddress.h +++ b/src/NewcoinAddress.h @@ -78,16 +78,24 @@ public: // // Accounts Private // - uint256 getAccountPrivate() const; + const std::vector& getAccountPrivate() const; std::string humanAccountPrivate() const; bool setAccountPrivate(const std::string& strPrivate); void setAccountPrivate(const std::vector& vPrivate); void setAccountPrivate(uint256 hash256); + void setAccountPrivate(const NewcoinAddress& generator, const NewcoinAddress& seed, int seq); + + // Encrypt a message. + std::vector accountPrivateEncrypt(const NewcoinAddress& naPublicTo, const std::vector& vucPlainText); + + // Decrypt a message. + std::vector accountPrivateDecrypt(const NewcoinAddress& naPublicFrom, const std::vector& vucCipherText); // // Family Generators + // Given a seed, hold a generator. // BIGNUM* getFamilyGeneratorBN() const; const std::vector& getFamilyGenerator() const; From 9a7e7b961c10153165ed7b14678a8e5b8ac45690 Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Thu, 10 May 2012 15:50:58 -0700 Subject: [PATCH 3/4] Generate information for root regular/master. --- src/Application.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/src/Application.cpp b/src/Application.cpp index dfbf216b5..918022bae 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -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 vucGeneratorCipher = reservedPrivateRegular.accountPrivateEncrypt(reservedPublicRegular, rootGeneratorMaster.getFamilyGenerator()); + + std::cerr << "Plain: " << strHex(rootGeneratorMaster.getFamilyGenerator()) << std::endl; + + std::cerr << "Cipher: " << strHex(vucGeneratorCipher) << std::endl; + + std::vector 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(rootAddress, 100000000); assert(!!firstLedger->getAccountState(rootAddress)); firstLedger->updateHash(); From 26fd9f3e43791607d6d73dae6e2bede53de0cb7b Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Thu, 10 May 2012 15:51:19 -0700 Subject: [PATCH 4/4] Work toward authorized keys. --- src/AccountState.h | 8 +++++--- src/Ledger.h | 24 +++++++++++------------- src/LedgerFormats.cpp | 1 + src/SerializedObject.h | 1 + 4 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/AccountState.h b/src/AccountState.h index be8677b5d..c6aaf1b3a 100644 --- a/src/AccountState.h +++ b/src/AccountState.h @@ -20,9 +20,11 @@ public: typedef boost::shared_ptr 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 diff --git a/src/Ledger.h b/src/Ledger.h index 90b1d7a07..d64577a64 100644 --- a/src/Ledger.h +++ b/src/Ledger.h @@ -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 public: typedef boost::shared_ptr 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 }; diff --git a/src/LedgerFormats.cpp b/src/LedgerFormats.cpp index ca833557a..7a21c7271 100644 --- a/src/LedgerFormats.cpp +++ b/src/LedgerFormats.cpp @@ -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 }, diff --git a/src/SerializedObject.h b/src/SerializedObject.h index 4e41fc056..94177d2bf 100644 --- a/src/SerializedObject.h +++ b/src/SerializedObject.h @@ -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