It makes much more sense to keep the from account pointer in the transaction

This commit is contained in:
JoelKatz
2011-11-18 20:07:57 -08:00
parent fa60ccd2df
commit 4d8894f71a
3 changed files with 46 additions and 24 deletions

View File

@@ -1,19 +1,24 @@
#ifndef __ACCOUNT__
#define __ACCOUNT__
#include <boost/shared_ptr.hpp>
#include "key.h"
#include "uint256.h"
class Account
{
public:
typedef boost::shared_ptr<Account> pointer;
private:
uint160 mAddress;
CKey pubKey;
public:
bool CheckSignRaw(const std::vector<unsigned char> &toSign,
bool checkSignRaw(const std::vector<unsigned char> &toSign,
const std::vector<unsigned char> &signature) const;
const uint160& GetAddress(void) const { return mAddress; }
const uint160& getAddress(void) const { return mAddress; }
CKey& peekPubKey() { return pubKey; }
};

View File

@@ -16,41 +16,44 @@ Transaction::Transaction(TransStatus status, LocalAccount& fromLocalAccount, Acc
mAccountTo(toAccount), mAmount(amount), mFromAccountSeq(fromSeq), mSourceLedger(ledger),
mIdent(ident), mInLedger(0), mStatus(NEW)
{
assert(fromAccount.GetAddress()==fromLocalAccount.mAddress);
assert(fromAccount.getAddress()==fromLocalAccount.mAddress);
assert(fromLocalAccount.mAmount>=amount);
assert((fromSeq+1)==fromLocalAccount.mSeqNum);
mAccountFrom=fromAccount.GetAddress();
sign(fromLocalAccount, fromAccount);
mAccountFrom=fromAccount.getAddress();
sign(fromLocalAccount);
}
bool Transaction::sign(LocalAccount& fromLocalAccount, Account& fromAccount)
bool Transaction::sign(LocalAccount& fromLocalAccount)
{
if( (mAmount==0) || (mSourceLedger==0) || (mAccountTo==0) )
return false;
if((mAccountFrom!=fromLocalAccount.mAddress)||(mAccountFrom!=fromAccount.GetAddress()))
if(mpAccountFrom == Account::pointer())
return false;
if((mpAccountFrom->getAddress()!=fromLocalAccount.mAddress) || (mAccountFrom!=mpAccountFrom->getAddress()))
return false;
Serializer::pointer signBuf(getRawUnsigned(fromAccount));
Serializer::pointer signBuf(getRawUnsigned());
if(!signBuf->makeSignature(mSignature, fromLocalAccount.peekPrivKey()))
return false;
signBuf->addRaw(mSignature);
mTransactionID=signBuf->getSHA512Half();
}
bool Transaction::checkSign(Account& fromAccount) const
bool Transaction::checkSign() const
{
if(mAccountFrom!=fromAccount.GetAddress()) return false;
if(mpAccountFrom == Account::pointer()) return false;
if(mpAccountFrom->getAddress()!=mAccountFrom) return false;
Serializer::pointer toSign(getRawUnsigned(fromAccount));
return toSign->checkSignature(mSignature, fromAccount.peekPubKey());
Serializer::pointer toSign(getRawUnsigned());
return toSign->checkSignature(mSignature, mpAccountFrom->peekPubKey());
}
Serializer::pointer Transaction::getRawUnsigned(Account& fromAccount) const
Serializer::pointer Transaction::getRawUnsigned() const
{
Serializer::pointer ret(new Serializer(104));
ret->add32(0x54584e00u);
ret->addRaw(fromAccount.peekPubKey().GetPubKey());
ret->addRaw(mpAccountFrom->peekPubKey().GetPubKey());
ret->add64(mAmount);
ret->add32(mFromAccountSeq);
ret->add32(mInLedger);
@@ -58,7 +61,15 @@ Serializer::pointer Transaction::getRawUnsigned(Account& fromAccount) const
return ret;
}
void Transaction::updateID(Account& fromAccount)
Serializer::pointer Transaction::getRawSigned() const
{
mTransactionID=getRawSigned(fromAccount)->getSHA512Half();
Serializer::pointer ret(getRawUnsigned());
ret->addRaw(mSignature);
return ret;
}
void Transaction::updateID()
{
if( (mpAccountFrom!=Account::pointer()) && (mpAccountFrom->getAddress()==mAccountFrom))
mTransactionID=getRawSigned()->getSHA512Half();
}

View File

@@ -1,13 +1,15 @@
#ifndef __TRANSACTION__
#define __TRANSACTION__
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/cstdint.hpp>
#include "uint256.h"
#include "newcoin.pb.h"
#include "Hanko.h"
#include "Serializer.h"
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/cstdint.hpp>
#include "Account.h"
/*
We could have made something that inherited from the protobuf transaction but this seemed simpler
@@ -41,6 +43,8 @@ private:
uint32 mInLedger;
TransStatus mStatus;
Account::pointer mpAccountFrom;
public:
Transaction();
Transaction(const std::vector<unsigned char> rawTransaction);
@@ -48,12 +52,14 @@ public:
Transaction(TransStatus Status, LocalAccount& fromLocal, Account& from,
uint32 fromSeq, const uint160& to, uint64 amount, uint32 ident, uint32 ledger);
bool sign(LocalAccount& fromLocalAccount, Account& fromAccount);
bool checkSign(Account& fromAccount) const;
void updateID(Account& fromAccount);
void setFromAccountPointer(Account::pointer af) { mpAccountFrom=af; }
Serializer::pointer getRawUnsigned(Account& from) const;
Serializer::pointer getRawSigned(Account& from) const;
bool sign(LocalAccount& fromLocalAccount);
bool checkSign() const;
void updateID();
Serializer::pointer getRawUnsigned() const;
Serializer::pointer getRawSigned() const;
const uint256& getID() const { return mTransactionID; }
const uint160& getFromAccount() const { return mAccountFrom; }