Merge branch 'master' into misc

This commit is contained in:
Arthur Britto
2012-06-09 15:06:15 -07:00
12 changed files with 121 additions and 121 deletions

View File

@@ -3,6 +3,7 @@
#include <openssl/bn.h>
#include <openssl/ecdsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>
// Functions to add CKey support for deterministic EC keys
@@ -115,26 +116,32 @@ EC_KEY* CKey::GenerateRootDeterministicKey(const uint128& seed)
// <-- root public generator in EC format
EC_KEY* CKey::GenerateRootPubKey(BIGNUM* pubGenerator)
{
if(pubGenerator==NULL) return NULL;
if (pubGenerator == NULL)
{
assert(false);
return NULL;
}
EC_KEY* pkey=EC_KEY_new_by_curve_name(NID_secp256k1);
if(!pkey)
EC_KEY* pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
if (!pkey)
{
BN_free(pubGenerator);
return NULL;
}
EC_KEY_set_conv_form(pkey, POINT_CONVERSION_COMPRESSED);
EC_POINT* pubPoint=EC_POINT_bn2point(EC_KEY_get0_group(pkey), pubGenerator, NULL, NULL);
EC_POINT* pubPoint = EC_POINT_bn2point(EC_KEY_get0_group(pkey), pubGenerator, NULL, NULL);
BN_free(pubGenerator);
if(!pubPoint)
{
assert(false);
EC_KEY_free(pkey);
return NULL;
}
if(!EC_KEY_set_public_key(pkey, pubPoint))
{
assert(false);
EC_POINT_free(pubPoint);
EC_KEY_free(pkey);
return NULL;

View File

@@ -1,12 +0,0 @@
#include "Hanko.h"
#include <boost/foreach.hpp>
using namespace boost;
using namespace std;
Hanko::Hanko()
{
}
// vim:ts=4

View File

@@ -1,65 +0,0 @@
#ifndef __HANKO__
#define __HANKO__
// We use SECP256K1 http://www.secg.org/collateral/sec2_final.pdf
#include "key.h"
enum HankoFormat
{
TEXT, // Hanko in text form
RAW, // Hanko in raw binary form
CONTACT, // Hanko contact block
};
class Hanko
{
public:
static const int smPubKeySize= 65;
static const int smPrivKeySize = 279;
static const int smSigSize = 57;
private:
std::string mHanko;
std::vector<unsigned char> mContactBlock;
CKey mPubKey;
public:
Hanko();
Hanko(const std::string& TextHanko);
Hanko(const std::vector<unsigned char>& Data, HankoFormat format);
Hanko(const CKey &pubKey);
Hanko(const Hanko &);
std::string GetHankoString(HankoFormat format) const;
std::vector<unsigned char> GetHankoBinary(HankoFormat format) const;
const std::vector<unsigned char>& GetContactBlock() const { return mContactBlock; }
const CKey& GetPublicKey() const { return mPubKey; }
int UpdateContact(std::vector<unsigned char>& Contact);
bool CheckHashSign(const uint256& hash, const std::vector<unsigned char>& Signature);
bool CheckPrefixSign(const std::vector<unsigned char>& data, uint64 type,
const std::vector<unsigned char> &signature);
};
class LocalHanko : public Hanko
{
private:
CKey mPrivKey;
public:
LocalHanko(std::vector<unsigned char> &PrivKey);
LocalHanko(const CKey &Privkey);
LocalHanko(const LocalHanko &);
~LocalHanko();
bool HashSign(const uint256& hash, std::vector<unsigned char>& Signature);
bool PrefixSign(std::vector<unsigned char> data, uint64 type, std::vector<unsigned char> &Signature);
};
#endif
// vim:ts=4

View File

@@ -336,20 +336,37 @@ void Ledger::addJson(Json::Value& ret, int options)
else ledger["Closed"] = false;
if (mCloseTime != 0)
ledger["CloseTime"] = boost::posix_time::to_simple_string(ptFromSeconds(mCloseTime));
if ((options & LEDGER_JSON_DUMP_TXNS) != 0)
bool full = (options & LEDGER_JSON_FULL) != 0;
if (full || ((options & LEDGER_JSON_DUMP_TXNS) != 0))
{
Json::Value txns(Json::arrayValue);
for (SHAMapItem::pointer item = mTransactionMap->peekFirstItem(); !!item;
item = mTransactionMap->peekNextItem(item->getTag()))
txns.append(item->getTag().GetHex());
{
if (full)
{
SerializerIterator sit(item->peekSerializer());
SerializedTransaction txn(sit);
txns.append(txn.getJson(0));
}
else txns.append(item->getTag().GetHex());
}
ledger["Transactions"] = txns;
}
if ((options & LEDGER_JSON_DUMP_STATE) != 0)
if (full || ((options & LEDGER_JSON_DUMP_STATE) != 0))
{
Json::Value state(Json::arrayValue);
for (SHAMapItem::pointer item = mAccountStateMap->peekFirstItem(); !!item;
item = mAccountStateMap->peekNextItem(item->getTag()))
state.append(item->getTag().GetHex());
{
if (full)
{
SerializerIterator sit(item->peekSerializer());
SerializedLedgerEntry sle(sit, item->getTag());
state.append(sle.getJson(0));
}
else state.append(item->getTag().GetHex());
}
ledger["AccountState"] = state;
}
ret[boost::lexical_cast<std::string>(mLedgerSeq)] = ledger;

View File

@@ -35,6 +35,7 @@ enum LedgerStateParms
#define LEDGER_JSON_DUMP_TXNS 0x10000000
#define LEDGER_JSON_DUMP_STATE 0x20000000
#define LEDGER_JSON_FULL 0x40000000
class Ledger : public boost::enable_shared_from_this<Ledger>
{ // The basic Ledger structure, can be opened, closed, or synching

View File

@@ -654,6 +654,7 @@ void LedgerConsensus::accept(SHAMap::pointer set)
Log(lsDEBUG) << "Previous LCL " << mPreviousLedger->getHash().GetHex();
Ledger::pointer newLCL = boost::make_shared<Ledger>(false, boost::ref(*mPreviousLedger));
uint32 newLedgerSeq = newLCL->getLedgerSeq();
#ifdef DEBUG
Json::StyledStreamWriter ssw;
@@ -707,6 +708,7 @@ void LedgerConsensus::accept(SHAMap::pointer set)
applyTransactions(theApp->getMasterLedger().getCurrentLedger()->peekTransactionMap(), newOL, failedTransactions);
theApp->getMasterLedger().pushLedger(newLCL, newOL);
mState = lcsACCEPTED;
sl.unlock();
#ifdef DEBUG
if (1)
@@ -718,8 +720,6 @@ void LedgerConsensus::accept(SHAMap::pointer set)
}
#endif
sl.unlock();
SerializedValidation v(newLCLHash, mOurPosition->peekKey(), true);
std::vector<unsigned char> validation = v.getSigned();
newcoin::TMValidation val;
@@ -727,6 +727,34 @@ void LedgerConsensus::accept(SHAMap::pointer set)
theApp->getConnectionPool().relayMessage(NULL, boost::make_shared<PackedMessage>(val, newcoin::mtVALIDATION));
Log(lsINFO) << "Validation sent " << newLCL->getHash().GetHex();
statusChange(newcoin::neACCEPTED_LEDGER, newOL);
// Insert the transactions in set into the AcctTxn database
Database *db = theApp->getAcctTxnDB()->getDB();
ScopedLock dbLock = theApp->getAcctTxnDB()->getDBLock();
db->executeSQL("BEGIN TRANSACTION");
for (SHAMapItem::pointer item = set->peekFirstItem(); !!item; item = set->peekNextItem(item->getTag()))
{
SerializerIterator sit(item->peekSerializer());
SerializedTransaction txn(sit);
std::vector<NewcoinAddress> accts = txn.getAffectedAccounts();
std::string sql = "INSERT INTO AccountTransactions (TransID,Account,LedgerSeq) VALUES ";
bool first = true;
for (std::vector<NewcoinAddress>::iterator it = accts.begin(), end = accts.end(); it != end; ++it)
{
if (!first) sql += ", (";
else sql += "(";
sql += txn.getTransactionID().GetHex();
sql += ",";
sql += it->humanAccountID();
sql += ",";
sql += boost::lexical_cast<std::string>(newLedgerSeq);
sql += ")";
}
sql += ";";
db->executeSQL(sql);
}
db->executeSQL("COMMIT TRANSACTION");
}
void LedgerConsensus::endConsensus()

View File

@@ -490,7 +490,7 @@ std::vector<unsigned char> NewcoinAddress::accountPrivateDecrypt(const NewcoinAd
//
BIGNUM* NewcoinAddress::getFamilyGeneratorBN() const
{
{ // returns the public generator
switch (nVersion) {
case VER_NONE:
throw std::runtime_error("unset source");
@@ -503,35 +503,13 @@ BIGNUM* NewcoinAddress::getFamilyGeneratorBN() const
throw std::runtime_error(str(boost::format("bad source: %d") % int(nVersion)));
}
assert(vchData.size() <= (256 / 8));
BIGNUM* ret = BN_bin2bn(&vchData[0], vchData.size(), NULL);
assert(ret);
return ret;
}
uint256 NewcoinAddress::getFamilyGeneratorU() const
{
switch (nVersion) {
case VER_NONE:
throw std::runtime_error("unset source");
case VER_FAMILY_GENERATOR:
// Do nothing.
break;
default:
throw std::runtime_error(str(boost::format("bad source: %d") % int(nVersion)));
}
assert(vchData.size() <= (256 / 8));
uint256 ret;
memcpy(ret.begin() + (ret.size() - vchData.size()), &vchData[0], vchData.size());
return ret;
}
const std::vector<unsigned char>& NewcoinAddress::getFamilyGenerator() const
{
{ // returns the public generator
switch (nVersion) {
case VER_NONE:
throw std::runtime_error("unset source");

View File

@@ -136,7 +136,6 @@ public:
// Use to generate a master or regular family.
//
BIGNUM* getFamilyGeneratorBN() const; // DEPRECATED
uint256 getFamilyGeneratorU() const;
const std::vector<unsigned char>& getFamilyGenerator() const;
std::string humanFamilyGenerator() const;

View File

@@ -1445,10 +1445,20 @@ Json::Value RPCServer::doTx(Json::Value& params)
return JSONRPCError(501, "not implemented");
}
// ledger
// ledger [id|current|lastclosed] [full]
Json::Value RPCServer::doLedger(Json::Value& params)
{
if (getParamCount(params)== 0)
if (params.size() > 2)
{
return "invalid params";
}
else if (!mNetOps->available())
{
return JSONRPCError(503, "network not available");
}
if (getParamCount(params) == 0)
{
Json::Value ret(Json::objectValue), current(Json::objectValue), closed(Json::objectValue);
theApp->getMasterLedger().getCurrentLedger()->addJson(current, 0);
@@ -1458,7 +1468,35 @@ Json::Value RPCServer::doLedger(Json::Value& params)
return ret;
}
return JSONRPCError(501, "not implemented");
std::string param;
if (!extractString(param, params, 0))
{
return "bad params";
}
Ledger::pointer ledger;
if (param == "current")
ledger = theApp->getMasterLedger().getCurrentLedger();
else if (param == "lastclosed")
ledger = theApp->getMasterLedger().getClosedLedger();
else if (param.size() > 12)
ledger = theApp->getMasterLedger().getLedgerByHash(uint256(param));
else
ledger = theApp->getMasterLedger().getLedgerBySeq(boost::lexical_cast<uint32>(param));
if (!ledger)
return JSONRPCError(503, "Unable to locate ledger");
bool full = false;
if (extractString(param, params, 1))
{
if (param == "full")
full = true;
}
Json::Value ret(Json::objectValue);
ledger->addJson(ret, full ? LEDGER_JSON_FULL : 0);
return ret;
}
// unl_add <domain>|<node_public> [<comment>]
@@ -2140,12 +2178,13 @@ Json::Value RPCServer::doCommand(const std::string& command, Json::Value& params
if (command == "wallet_propose") return doWalletPropose(params);
if (command == "wallet_seed") return doWalletSeed(params);
if (command=="ledger") return doLedger(params);
//
// Obsolete or need rewrite:
//
if (command=="tx") return doTx(params);
if (command=="ledger") return doLedger(params);
return "unknown command";
}

View File

@@ -57,6 +57,7 @@ std::string SerializedLedgerEntry::getText() const
Json::Value SerializedLedgerEntry::getJson(int options) const
{
Json::Value ret(mObject.getJson(options));
ret["Type"] = mFormat->t_name;
ret["Index"] = mIndex.GetHex();
ret["Version"] = mVersion.getText();
return ret;

View File

@@ -272,11 +272,14 @@ void SerializedTransaction::makeITFieldAbsent(SOE_Field field)
Json::Value SerializedTransaction::getJson(int options) const
{
Json::Value ret(Json::objectValue);
ret["Type"] = mFormat->t_name;
Json::Value ret = Json::objectValue;
ret["ID"] = getTransactionID().GetHex();
ret["Signature"] = mSignature.getText();
ret["Middle"] = mMiddleTxn.getJson(options);
Json::Value middle = mMiddleTxn.getJson(options);
middle["Type"] = mFormat->t_name;
ret["Middle"] = middle;
ret["Inner"] = mInnerTxn.getJson(options);
return ret;
}

View File

@@ -156,14 +156,16 @@ public:
SetPrivateKeyU(privateKey);
}
#if 0
CKey(const NewcoinAddress& masterKey, int keyNum, bool isPublic) : pkey(NULL), fSet(false)
{
if (isPublic)
SetPubSeq(masterKey, keyNum);
else
SetPrivSeq(masterKey, keyNum);
SetPrivSeq(masterKey, keyNum); // broken, need seed
fSet = true;
}
#endif
bool IsNull() const
{
@@ -257,8 +259,9 @@ public:
fSet = true;
}
#if 0
void SetPrivSeq(const NewcoinAddress& masterKey, int keyNum)
{
{ // broken: Need the seed
uint256 privKey;
EC_KEY* key = GeneratePrivateDeterministicKey(masterKey, masterKey.getFamilyGeneratorU(), keyNum);
privKey.zero();
@@ -267,6 +270,7 @@ public:
pkey = key;
fSet = true;
}
#endif
CPrivKey GetPrivKey()
{