Reorganize includes a bit.
{ "newmalleablekey", &newmalleablekey, false, false},
{ "adjustmalleablekey", &adjustmalleablekey, false, false},
{ "adjustmalleablepubkey", &adjustmalleablepubkey, false, false},
+ { "listmalleablepubkeys", &listmalleablepubkeys, false, false},
{ "sendalert", &sendalert, false, false},
};
extern json_spirit::Value newmalleablekey(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value adjustmalleablekey(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value adjustmalleablepubkey(const json_spirit::Array& params, bool fHelp);
+extern json_spirit::Value listmalleablepubkeys(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value getrawtransaction(const json_spirit::Array& params, bool fHelp); // in rcprawtransaction.cpp
extern json_spirit::Value listunspent(const json_spirit::Array& params, bool fHelp);
return true;
}
+// Initialize from octets stream
+bool CPoint::setPubKey(const CPubKey &vchPubKey)
+{
+ return setBytes(vchPubKey.Raw());
+}
+
// Serialize to octets stream
bool CPoint::getBytes(std::vector<unsigned char> &vchBytes)
{
EC_KEY_free(eckey);
CPoint point;
- if (!point.setBytes(pubKeyL.Raw())) {
+ if (!point.setPubKey(pubKeyL)) {
throw key_error("CMalleablePubKey::GetVariant() : Unable to decode L value");
}
bnHash.setuint160(Hash160(vchLr));
CPoint pointH;
- pointH.setBytes(pubKeyH.Raw());
+ pointH.setPubKey(pubKeyH);
CPoint P;
// Calculate P = Hash(L*r)*G + H
vchPubKeyVariant = CPubKey(vchResult);
}
-std::string CMalleablePubKey::ToString()
+std::string CMalleablePubKey::ToString() const
{
CDataStream ssKey(SER_NETWORK, PROTOCOL_VERSION);
ssKey << *this;
Reset();
CKey L, H;
- if (!L.SetSecret(pvchSecretL, true) || !H.SetSecret(pvchSecretH, true))
+ if (pvchSecretL.size() != 32 || !pvchSecretH.size() != 32 || !L.SetSecret(pvchSecretL, true) || !H.SetSecret(pvchSecretH, true))
{
nVersion = 0;
return false;
}
// Check ownership
-bool CMalleableKey::CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubKeyVariant)
+bool CMalleableKey::CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubKeyVariant) const
{
if (IsNull()) {
throw key_error("CMalleableKey::CheckKeyVariant() : Attempting to run on NULL key object.");
}
CPoint point_R;
- if (!point_R.setBytes(R.Raw())) {
+ if (!point_R.setPubKey(R)) {
throw key_error("CMalleableKey::CheckKeyVariant() : Unable to decode R value");
}
std::vector<unsigned char> vchPubKeyH = H.GetPubKey().Raw();
CPoint point_H;
- if (!point_H.setBytes(vchPubKeyH)) {
+ if (!point_H.setPubKey(vchPubKeyH)) {
throw key_error("CMalleableKey::CheckKeyVariant() : Unable to decode H value");
}
CPoint point_P;
- if (!point_P.setBytes(vchPubKeyVariant.Raw())) {
+ if (!point_P.setPubKey(vchPubKeyVariant)) {
throw key_error("CMalleableKey::CheckKeyVariant() : Unable to decode P value");
}
}
// Check ownership and restore private key
-bool CMalleableKey::CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubKeyVariant, CKey &privKeyVariant)
+bool CMalleableKey::CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubKeyVariant, CKey &privKeyVariant) const
{
if (IsNull()) {
throw key_error("CMalleableKey::CheckKeyVariant() : Attempting to run on NULL key object.");
}
CPoint point_R;
- if (!point_R.setBytes(R.Raw())) {
+ if (!point_R.setPubKey(R)) {
throw key_error("CMalleableKey::CheckKeyVariant() : Unable to decode R value");
}
std::vector<unsigned char> vchPubKeyH = H.GetPubKey().Raw();
CPoint point_H;
- if (!point_H.setBytes(vchPubKeyH)) {
+ if (!point_H.setPubKey(vchPubKeyH)) {
throw key_error("CMalleableKey::CheckKeyVariant() : Unable to decode H value");
}
CPoint point_P;
- if (!point_P.setBytes(vchPubKeyVariant.Raw())) {
+ if (!point_P.setPubKey(vchPubKeyVariant)) {
throw key_error("CMalleableKey::CheckKeyVariant() : Unable to decode P value");
}
return true;
}
-std::string CMalleableKey::ToString()
+std::string CMalleableKey::ToString() const
{
CDataStream ssKey(SER_NETWORK, PROTOCOL_VERSION);
ssKey << *this;
CMalleableKeyView::CMalleableKeyView(const CMalleableKey &b)
{
- assert(b.nVersion == CURRENT_VERSION);
+ if (b.vchSecretL.size() != 32)
+ throw key_error("CMalleableKeyView::CMalleableKeyView() : L size must be 32 bytes");
+
+ if (b.vchSecretH.size() != 32)
+ throw key_error("CMalleableKeyView::CMalleableKeyView() : L size must be 32 bytes");
+
vchSecretL = b.vchSecretL;
CKey H;
H.SetSecret(b.vchSecretH, true);
+
vchPubKeyH = H.GetPubKey().Raw();
+ nVersion = b.nVersion;
+}
+
+CMalleableKeyView::CMalleableKeyView(const CMalleableKeyView &b)
+{
+ vchSecretL = b.vchSecretL;
+ vchPubKeyH = b.vchPubKeyH;
+ nVersion = CURRENT_VERSION;
}
CMalleableKeyView::CMalleableKeyView(const CSecret &L, const CPubKey &pvchPubKeyH)
{
vchSecretL = L;
vchPubKeyH = pvchPubKeyH.Raw();
+ nVersion = CURRENT_VERSION;
}
CMalleableKeyView& CMalleableKeyView::operator=(const CMalleableKey &b)
{
- assert(b.nVersion == CURRENT_VERSION);
vchSecretL = b.vchSecretL;
CKey H;
H.SetSecret(b.vchSecretH, true);
vchPubKeyH = H.GetPubKey().Raw();
+ nVersion = b.nVersion;
return (*this);
}
}
// Check ownership
-bool CMalleableKeyView::CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubKeyVariant)
+bool CMalleableKeyView::CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubKeyVariant) const
{
if (!R.IsValid()) {
throw key_error("CMalleableKeyView::CheckKeyVariant() : R is invalid");
}
CPoint point_R;
- if (!point_R.setBytes(R.Raw())) {
+ if (!point_R.setPubKey(R)) {
throw key_error("CMalleableKeyView::CheckKeyVariant() : Unable to decode R value");
}
CPoint point_H;
- if (!point_H.setBytes(vchPubKeyH)) {
+ if (!point_H.setPubKey(vchPubKeyH)) {
throw key_error("CMalleableKeyView::CheckKeyVariant() : Unable to decode H value");
}
CPoint point_P;
- if (!point_P.setBytes(vchPubKeyVariant.Raw())) {
+ if (!point_P.setPubKey(vchPubKeyVariant)) {
throw key_error("CMalleableKeyView::CheckKeyVariant() : Unable to decode P value");
}
return true;
}
+std::string CMalleableKeyView::ToString() const
+{
+ CDataStream ssKey(SER_NETWORK, PROTOCOL_VERSION);
+ ssKey << *this;
+ std::vector<unsigned char> vch(ssKey.begin(), ssKey.end());
+
+ return EncodeBase58Check(vch);
+}
+
+bool CMalleableKeyView::SetString(const std::string& strMutableKey)
+{
+ std::vector<unsigned char> vchTemp;
+ if (!DecodeBase58Check(strMutableKey, vchTemp)) {
+ throw key_error("CMalleableKeyView::SetString() : Provided key data seems corrupted.");
+ }
+
+ CDataStream ssKey(vchTemp, SER_NETWORK, PROTOCOL_VERSION);
+ ssKey >> *this;
+
+ return IsNull();
+}
+
+bool CMalleableKeyView::IsNull() const
+{
+ return nVersion != CURRENT_VERSION;
+}
+
//// Asymmetric encryption
void CPubKey::EncryptData(const std::vector<unsigned char>& data, std::vector<unsigned char>& encrypted)
// Initialize from octets stream
bool setBytes(const std::vector<unsigned char> &vchBytes);
+ // Initialize from pubkey
+ bool setPubKey(const CPubKey &vchPubKey);
+
// Serialize to octets stream
bool getBytes(std::vector<unsigned char> &vchBytes);
bool operator==(const CMalleablePubKey &b);
bool operator!=(const CMalleablePubKey &b) { return !(*this == b); }
- std::string ToString();
+ std::string ToString() const;
bool SetString(const std::string& strMalleablePubKey);
uint256 GetID() const;
READWRITE(vchSecretH);
)
- std::string ToString();
+ std::string ToString() const;
bool SetString(const std::string& strMalleablePubKey);
void Reset();
void GetSecrets(CSecret &pvchSecretL, CSecret &pvchSecretH) const;
CMalleablePubKey GetMalleablePubKey() const;
-
- bool CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubKeyVariant);
- bool CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubKeyVariant, CKey &privKeyVariant);
+ bool CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubKeyVariant) const;
+ bool CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubKeyVariant, CKey &privKeyVariant) const;
};
class CMalleableKeyView
{
private:
+ unsigned char nVersion;
CSecret vchSecretL;
- std::vector<unsigned char> vchPubKeyH;
-
- // disabled constructor
- CMalleableKeyView() { };
+ CPubKey vchPubKeyH;
static const unsigned char CURRENT_VERSION = 1;
public:
+ CMalleableKeyView() { nVersion = 0; };
CMalleableKeyView(const CMalleableKey &b);
CMalleableKeyView(const CSecret &L, const CPubKey &pvchPubKeyH);
CMalleableKeyView& operator=(const CMalleableKey &b);
~CMalleableKeyView();
+
+ IMPLEMENT_SERIALIZE(
+ READWRITE(this->nVersion);
+ nVersion = this->nVersion;
+ READWRITE(vchSecretL);
+ READWRITE(vchPubKeyH);
+ )
+
+ bool IsNull() const;
+ std::string ToString() const;
+ bool SetString(const std::string& strMalleablePubKey);
+
CMalleablePubKey GetMalleablePubKey() const;
+ bool CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubKeyVariant) const;
- bool CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubKeyVariant);
+ bool operator <(const CMalleableKeyView& kv) const { return vchPubKeyH.GetID() < kv.vchPubKeyH.GetID(); }
};
#endif
return (!setWatchOnly.empty());
}
+CCryptoKeyStore::CCryptoKeyStore() : fUseCrypto(false)
+{
+ std::string strMalleableKey = GetArg("-masterkey", "");
+ CMalleableKey malleableKey;
+ if (strMalleableKey != "")
+ malleableKey.SetString(strMalleableKey);
+ else
+ malleableKey.MakeNewKeys();
+
+ const CMalleableKeyView& keyView(malleableKey);
+ mapMalleableKeys[keyView] = malleableKey;
+}
+
bool CCryptoKeyStore::SetCrypted()
{
{
break;
return false;
}
+
vMasterKey = vMasterKeyIn;
}
NotifyStatusChanged(this);
vchSecret = key.GetSecret(fCompressed);
return true;
}
+
+ virtual bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R) const =0;
+ virtual bool CreatePrivKey(const CPubKey &pubKeyVariant, const CPubKey &R, CKey &privKey) const =0;
+ virtual void ListMalleablePubKeys(std::list<CMalleablePubKey> &malleablePubKeyList) const =0;
};
typedef std::map<CKeyID, std::pair<CSecret, bool> > KeyMap;
typedef std::map<CScriptID, CScript > ScriptMap;
typedef std::set<CScript> WatchOnlySet;
+typedef std::map<CMalleableKeyView, CMalleableKey> MalleableKeyMap;
/** Basic key store, that keeps keys in an address->secret map */
class CBasicKeyStore : public CKeyStore
{
protected:
KeyMap mapKeys;
+ MalleableKeyMap mapMalleableKeys;
+
ScriptMap mapScripts;
WatchOnlySet setWatchOnly;
virtual bool RemoveWatchOnly(const CScript &dest);
virtual bool HaveWatchOnly(const CScript &dest) const;
virtual bool HaveWatchOnly() const;
+
+ bool CheckOwnership(const CPubKey &pubKeyVariant, const CPubKey &R) const
+ {
+ {
+ LOCK(cs_KeyStore);
+ for (MalleableKeyMap::const_iterator mi = mapMalleableKeys.begin(); mi != mapMalleableKeys.end(); mi++)
+ {
+ if (mi->first.CheckKeyVariant(R, pubKeyVariant))
+ return true;
+ }
+ }
+ return false;
+ }
+
+ bool CreatePrivKey(const CPubKey &pubKeyVariant, const CPubKey &R, CKey &privKey) const
+ {
+ {
+ LOCK(cs_KeyStore);
+ for (MalleableKeyMap::const_iterator mi = mapMalleableKeys.begin(); mi != mapMalleableKeys.end(); mi++)
+ {
+ if (mi->second.CheckKeyVariant(R, pubKeyVariant, privKey))
+ return true;
+ }
+ }
+ return false;
+ }
+
+ void ListMalleablePubKeys(std::list<CMalleablePubKey> &malleablePubKeyList) const
+ {
+ malleablePubKeyList.clear();
+
+ {
+ LOCK(cs_KeyStore);
+ for (MalleableKeyMap::const_iterator mi = mapMalleableKeys.begin(); mi != mapMalleableKeys.end(); mi++)
+ malleablePubKeyList.push_back(mi->first.GetMalleablePubKey());
+ }
+ }
};
typedef std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char> > > CryptedKeyMap;
bool Unlock(const CKeyingMaterial& vMasterKeyIn);
public:
- CCryptoKeyStore() : fUseCrypto(false)
- {
- }
+ CCryptoKeyStore();
bool IsCrypted() const
{
return error("AddToBlockIndex() : %s already exists", hash.ToString().substr(0,20).c_str());
// Construct new block index object
- CBlockIndex* pindexNew = new CBlockIndex(nFile, nBlockPos, *this);
+ CBlockIndex* pindexNew = new(nothrow) CBlockIndex(nFile, nBlockPos, *this);
if (!pindexNew)
return error("AddToBlockIndex() : new CBlockIndex failed");
pindexNew->phashBlock = &hash;
return result;
}
+
+Value listmalleablepubkeys(const Array& params, bool fHelp)
+{
+ if (fHelp || params.size() != 0)
+ throw runtime_error(
+ "listmalleablepubkeys\n"
+ "Get list of malleable public keys.\n");
+
+ std::list<CMalleablePubKey> keyList;
+ pwalletMain->ListMalleablePubKeys(keyList);
+
+ Array result;
+ BOOST_FOREACH(const CMalleablePubKey &key, keyList)
+ {
+ result.push_back(key.ToString());
+ }
+
+ return result;
+}
return true;
}
+bool SignR(const CPubKey& pubKey, const CPubKey& R, const CKeyStore& keystore, const uint256& hash, int nHashType, CScript& scriptSigRet)
+{
+ CKey key;
+ if (!keystore.CreatePrivKey(pubKey, R, key))
+ return false;
+
+ vector<unsigned char> vchSig;
+ if (!key.Sign(hash, vchSig))
+ return false;
+ vchSig.push_back((unsigned char)nHashType);
+ scriptSigRet << vchSig;
+
+ return true;
+}
+
bool SignN(const vector<valtype>& multisigdata, const CKeyStore& keystore, const uint256& hash, int nHashType, CScript& scriptSigRet)
{
int nSigned = 0;
case TX_NULL_DATA:
return false;
case TX_PUBKEY:
- case TX_PUBKEY_DROP:
keyID = CPubKey(vSolutions[0]).GetID();
return Sign1(keyID, keystore, hash, nHashType, scriptSigRet);
+ case TX_PUBKEY_DROP:
+ {
+ CPubKey key = CPubKey(vSolutions[0]);
+ CPubKey R = CPubKey(vSolutions[1]);
+ return SignR(key, R, keystore, hash, nHashType, scriptSigRet);
+ }
case TX_PUBKEYHASH:
keyID = CKeyID(uint160(vSolutions[0]));
if (!Sign1(keyID, keystore, hash, nHashType, scriptSigRet))
case TX_NULL_DATA:
break;
case TX_PUBKEY:
- case TX_PUBKEY_DROP:
keyID = CPubKey(vSolutions[0]).GetID();
if (keystore.HaveKey(keyID))
return MINE_SPENDABLE;
break;
+ case TX_PUBKEY_DROP:
+ {
+ CPubKey key = CPubKey(vSolutions[0]);
+ if (keystore.HaveKey(key.GetID()))
+ return MINE_SPENDABLE;
+ else
+ {
+ CPubKey R = CPubKey(vSolutions[1]);
+ if (keystore.CheckOwnership(key, R))
+ return MINE_SPENDABLE;
+ }
+ }
+ break;
case TX_PUBKEYHASH:
keyID = CKeyID(uint160(vSolutions[0]));
if (keystore.HaveKey(keyID))
if (!Solver(scriptPubKey, whichType, vSolutions))
return false;
- if (whichType == TX_PUBKEY)
+ if (whichType == TX_PUBKEY || whichType == TX_PUBKEY_DROP)
{
addressRet = CPubKey(vSolutions[0]).GetID();
return true;
return (*mi).second;
// Create new
- CBlockIndex* pindexNew = new CBlockIndex();
+ CBlockIndex* pindexNew = new(nothrow) CBlockIndex();
if (!pindexNew)
throw runtime_error("LoadBlockIndex() : new CBlockIndex failed");
mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
return (*mi).second;
// Create new
- CBlockIndex* pindexNew = new CBlockIndex();
+ CBlockIndex* pindexNew = new(nothrow) CBlockIndex();
if (!pindexNew)
throw runtime_error("LoadBlockIndex() : new CBlockIndex failed");
mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
if (p != buffer)
delete[] p;
limit *= 2;
- p = new char[limit];
+ p = new(nothrow) char[limit];
if (p == NULL)
throw std::bad_alloc();
}