// Copyright (c) 2009-2010 Satoshi Nakamoto
+// Copyright (c) 2011 The Bitcoin developers
// Distributed under the MIT/X11 software license, see the accompanying
// file license.txt or http://www.opensource.org/licenses/mit-license.php.
+#ifndef H_BITCOIN_SCRIPT
+#define H_BITCOIN_SCRIPT
+
+#include "base58.h"
+#include "keystore.h"
+
+#include <string>
+#include <vector>
+
+#include <boost/foreach.hpp>
class CTransaction;
-inline string ValueString(const vector<unsigned char>& vch)
+inline std::string ValueString(const std::vector<unsigned char>& vch)
{
if (vch.size() <= 4)
return strprintf("%d", CBigNum(vch).getint());
return HexStr(vch);
}
-inline string StackString(const vector<vector<unsigned char> >& vStack)
+inline std::string StackString(const std::vector<std::vector<unsigned char> >& vStack)
{
- string str;
- foreach(const vector<unsigned char>& vch, vStack)
+ std::string str;
+ BOOST_FOREACH(const std::vector<unsigned char>& vch, vStack)
{
if (!str.empty())
str += " ";
-class CScript : public vector<unsigned char>
+class CScript : public std::vector<unsigned char>
{
protected:
CScript& push_int64(int64 n)
public:
CScript() { }
- CScript(const CScript& b) : vector<unsigned char>(b.begin(), b.end()) { }
- CScript(const_iterator pbegin, const_iterator pend) : vector<unsigned char>(pbegin, pend) { }
+ CScript(const CScript& b) : std::vector<unsigned char>(b.begin(), b.end()) { }
+ CScript(const_iterator pbegin, const_iterator pend) : std::vector<unsigned char>(pbegin, pend) { }
#ifndef _MSC_VER
- CScript(const unsigned char* pbegin, const unsigned char* pend) : vector<unsigned char>(pbegin, pend) { }
+ CScript(const unsigned char* pbegin, const unsigned char* pend) : std::vector<unsigned char>(pbegin, pend) { }
#endif
CScript& operator+=(const CScript& b)
explicit CScript(opcodetype b) { operator<<(b); }
explicit CScript(const uint256& b) { operator<<(b); }
explicit CScript(const CBigNum& b) { operator<<(b); }
- explicit CScript(const vector<unsigned char>& b) { operator<<(b); }
+ explicit CScript(const std::vector<unsigned char>& b) { operator<<(b); }
CScript& operator<<(char b) { return push_int64(b); }
CScript& operator<<(opcodetype opcode)
{
if (opcode < 0 || opcode > 0xff)
- throw runtime_error("CScript::operator<<() : invalid opcode");
+ throw std::runtime_error("CScript::operator<<() : invalid opcode");
insert(end(), (unsigned char)opcode);
return *this;
}
return *this;
}
- CScript& operator<<(const vector<unsigned char>& b)
+ CScript& operator<<(const std::vector<unsigned char>& b)
{
if (b.size() < OP_PUSHDATA1)
{
{
// I'm not sure if this should push the script or concatenate scripts.
// If there's ever a use for pushing a script onto a script, delete this member fn
- assert(("warning: pushing a CScript onto a CScript with << is probably not intended, use + to concatenate", false));
+ assert(!"warning: pushing a CScript onto a CScript with << is probably not intended, use + to concatenate");
return *this;
}
- bool GetOp(iterator& pc, opcodetype& opcodeRet, vector<unsigned char>& vchRet)
+ bool GetOp(iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet)
{
// Wrapper so it can be called with either iterator or const_iterator
const_iterator pc2 = pc;
return fRet;
}
- bool GetOp(const_iterator& pc, opcodetype& opcodeRet, vector<unsigned char>& vchRet) const
+ bool GetOp(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet) const
{
return GetOp2(pc, opcodeRet, &vchRet);
}
return GetOp2(pc, opcodeRet, NULL);
}
- bool GetOp2(const_iterator& pc, opcodetype& opcodeRet, vector<unsigned char>* pvchRet) const
+ bool GetOp2(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet) const
{
opcodeRet = OP_INVALIDOPCODE;
if (pvchRet)
return true;
}
+ static int DecodeOP_N(opcodetype opcode)
+ {
+ if (opcode == OP_0)
+ return 0;
+ assert(opcode >= OP_1 && opcode <= OP_16);
+ return (int)opcode - (int)(OP_1 - 1);
+ }
void FindAndDelete(const CScript& b)
{
}
- uint160 GetBitcoinAddressHash160() const
- {
- opcodetype opcode;
- vector<unsigned char> vch;
- CScript::const_iterator pc = begin();
- if (!GetOp(pc, opcode, vch) || opcode != OP_DUP) return 0;
- if (!GetOp(pc, opcode, vch) || opcode != OP_HASH160) return 0;
- if (!GetOp(pc, opcode, vch) || vch.size() != sizeof(uint160)) return 0;
- uint160 hash160 = uint160(vch);
- if (!GetOp(pc, opcode, vch) || opcode != OP_EQUALVERIFY) return 0;
- if (!GetOp(pc, opcode, vch) || opcode != OP_CHECKSIG) return 0;
- if (pc != end()) return 0;
- return hash160;
- }
-
- string GetBitcoinAddress() const
- {
- uint160 hash160 = GetBitcoinAddressHash160();
- if (hash160 == 0)
- return "";
- return Hash160ToAddress(hash160);
- }
-
- void SetBitcoinAddress(const uint160& hash160)
+ void SetBitcoinAddress(const CBitcoinAddress& address)
{
this->clear();
- *this << OP_DUP << OP_HASH160 << hash160 << OP_EQUALVERIFY << OP_CHECKSIG;
- }
-
- void SetBitcoinAddress(const vector<unsigned char>& vchPubKey)
- {
- SetBitcoinAddress(Hash160(vchPubKey));
+ *this << OP_DUP << OP_HASH160 << address.GetHash160() << OP_EQUALVERIFY << OP_CHECKSIG;
}
- bool SetBitcoinAddress(const string& strAddress)
+ void SetBitcoinAddress(const std::vector<unsigned char>& vchPubKey)
{
- this->clear();
- uint160 hash160;
- if (!AddressToHash160(strAddress, hash160))
- return false;
- SetBitcoinAddress(hash160);
- return true;
+ SetBitcoinAddress(CBitcoinAddress(vchPubKey));
}
+ void SetMultisigAnd(const std::vector<CKey>& keys);
+ void SetMultisigOr(const std::vector<CKey>& keys);
+ void SetMultisigEscrow(const std::vector<CKey>& keys);
void PrintHex() const
printf("CScript(%s)\n", HexStr(begin(), end(), true).c_str());
}
- string ToString() const
+ std::string ToString() const
{
- string str;
+ std::string str;
opcodetype opcode;
- vector<unsigned char> vch;
+ std::vector<unsigned char> vch;
const_iterator pc = begin();
while (pc < end())
{
+bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, int nHashType);
-uint256 SignatureHash(CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType);
bool IsStandard(const CScript& scriptPubKey);
-bool IsMine(const CScript& scriptPubKey);
-bool ExtractPubKey(const CScript& scriptPubKey, bool fMineOnly, vector<unsigned char>& vchPubKeyRet);
-bool ExtractHash160(const CScript& scriptPubKey, uint160& hash160Ret);
-bool SignSignature(const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL, CScript scriptPrereq=CScript());
+bool IsMine(const CKeyStore& keystore, const CScript& scriptPubKey);
+bool ExtractAddress(const CScript& scriptPubKey, const CKeyStore* pkeystore, CBitcoinAddress& addressRet);
+bool SignSignature(const CKeyStore& keystore, const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL, CScript scriptPrereq=CScript());
bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, int nHashType=0);
+
+#endif