1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or https://www.opensource.org/licenses/mit-license.php .
6 #include "script/sign.h"
8 #include "primitives/transaction.h"
11 #include "script/standard.h"
13 #include "cc/CCinclude.h"
16 #include "pbaas/identity.h"
18 #include <boost/foreach.hpp>
22 typedef std::vector<unsigned char> valtype;
24 TransactionSignatureCreator::TransactionSignatureCreator(const CKeyStore* keystoreIn, const CTransaction* txToIn, unsigned int nInIn, const CAmount& amountIn, const CScript &scriptPubKey, uint32_t spendHeight, int nHashTypeIn)
25 : BaseSignatureCreator(keystoreIn), txTo(txToIn), nIn(nInIn), nHashType(nHashTypeIn), amount(amountIn), checker(txTo, nIn, amountIn, &scriptPubKey, keystoreIn, spendHeight) {}
27 TransactionSignatureCreator::TransactionSignatureCreator(const CKeyStore* keystoreIn, const CTransaction* txToIn, unsigned int nInIn, const CAmount& amountIn, int nHashTypeIn)
28 : BaseSignatureCreator(keystoreIn), txTo(txToIn), nIn(nInIn), nHashType(nHashTypeIn), amount(amountIn), checker(txTo, nIn, amountIn) {}
30 bool TransactionSignatureCreator::CreateSig(std::vector<unsigned char> &vchSig, const CKeyID& address, const CScript& scriptCode, uint32_t consensusBranchId, CKey *pprivKey, void *extraData) const
35 else if (!keystore || !keystore->GetKey(address, key))
40 hash = SignatureHash(scriptCode, *txTo, nIn, nHashType, amount, consensusBranchId);
41 } catch (logic_error ex) {
46 if (scriptCode.IsPayToCryptoCondition(p))
48 if (p.IsValid() && p.version >= p.VERSION_V3)
50 CSmartTransactionSignatures signatures(nHashType, std::map<uint160, CSmartTransactionSignature>());
53 signatures = CSmartTransactionSignatures(vchSig);
54 if (!signatures.IsValid())
59 unsigned char *onesig;
60 if (!cc_MakeSecp256k1Signature(hash.begin(), key.begin(), &onesig))
64 CSmartTransactionSignature signature(CSmartTransactionSignature::SIGTYPE_SECP256K1, key.GetPubKey(), std::vector<unsigned char>(onesig, onesig + CSmartTransactionSignature::SIGTYPE_SECP256K1_LEN));
66 signatures.AddSignature(signature);
67 vchSig = signatures.AsVector();
71 printf("signatures: %s\n", signatures.ToUniValue().write().c_str());
72 CC *cc = (CC *)extraData;
73 if (!cc || cc_signTreeSecp256k1Msg32(cc, key.begin(), hash.begin()) == 0)
75 char *jsonCondStr = cc_conditionToJSONString(cc);
78 printf("Signed condition: %s\n", jsonCondStr);
79 cJSON_free(jsonCondStr);
85 CC *cc = (CC *)extraData;
87 if (!cc || cc_signTreeSecp256k1Msg32(cc, key.begin(), hash.begin()) == 0)
89 vchSig = CCSigVec(cc);
95 if (!key.Sign(hash, vchSig))
98 vchSig.push_back((unsigned char)nHashType);
102 static bool Sign1(const CKeyID& address, const BaseSignatureCreator& creator, const CScript& scriptCode, std::vector<valtype>& ret, uint32_t consensusBranchId)
104 vector<unsigned char> vchSig;
105 if (!creator.CreateSig(vchSig, address, scriptCode, consensusBranchId))
107 ret.push_back(vchSig);
111 static bool SignN(const vector<valtype>& multisigdata, const BaseSignatureCreator& creator, const CScript& scriptCode, std::vector<valtype>& ret, uint32_t consensusBranchId)
114 int nRequired = multisigdata.front()[0];
115 for (unsigned int i = 1; i < multisigdata.size()-1 && nSigned < nRequired; i++)
117 const valtype& pubkey = multisigdata[i];
118 CKeyID keyID = CPubKey(pubkey).GetID();
119 if (Sign1(keyID, creator, scriptCode, ret, consensusBranchId))
122 return nSigned==nRequired;
125 CC *CCcond1of2(uint8_t evalcode,CPubKey pk1,CPubKey pk2)
127 std::vector<CC*> pks;
128 pks.push_back(CCNewSecp256k1(pk1));
129 pks.push_back(CCNewSecp256k1(pk2));
130 CC *condCC = CCNewEval(E_MARSHAL(ss << evalcode));
131 CC *Sig = CCNewThreshold(1, pks);
132 return CCNewThreshold(2, {condCC, Sig});
135 CC *CCcond1(uint8_t evalcode,CPubKey pk)
137 std::vector<CC*> pks;
138 pks.push_back(CCNewSecp256k1(pk));
139 CC *condCC = CCNewEval(E_MARSHAL(ss << evalcode));
140 CC *Sig = CCNewThreshold(1, pks);
141 return CCNewThreshold(2, {condCC, Sig});
144 CC *CCcond1(uint8_t evalcode, CTxDestination dest)
146 CPubKey pk = boost::apply_visitor<GetPubKeyForPubKey>(GetPubKeyForPubKey(), dest);
147 std::vector<CC*> pks;
150 pks.push_back(CCNewSecp256k1(pk));
154 pks.push_back(CCNewHashedSecp256k1(CKeyID(GetDestinationID(dest))));
156 CC *condCC = CCNewEval(E_MARSHAL(ss << evalcode));
157 CC *Sig = CCNewThreshold(1, pks);
158 return CCNewThreshold(2, {condCC, Sig});
161 CC *CCcondAny(uint8_t evalcode, std::vector<CTxDestination> dests)
163 std::vector<CC*> pks;
164 for (auto dest : dests)
166 CPubKey pk = boost::apply_visitor<GetPubKeyForPubKey>(GetPubKeyForPubKey(), dest);
169 pks.push_back(CCNewSecp256k1(pk));
173 pks.push_back(CCNewHashedSecp256k1(CKeyID(GetDestinationID(dest))));
177 CC *condCC = CCNewEval(E_MARSHAL(ss << evalcode));
178 CC *Sig = CCNewThreshold(1, pks);
179 return CCNewThreshold(2, {condCC, Sig});
182 CC *MakeCCcondMofN(uint8_t evalcode, const std::vector<CTxDestination> &dests, int M)
184 std::vector<CC*> pks;
185 for (auto dest : dests)
187 CPubKey pk = boost::apply_visitor<GetPubKeyForPubKey>(GetPubKeyForPubKey(), dest);
190 pks.push_back(CCNewSecp256k1(pk));
194 pks.push_back(CCNewHashedSecp256k1(CKeyID(GetDestinationID(dest))));
203 CC *condCC = CCNewEval(E_MARSHAL(ss << evalcode));
204 CC *Sig = CCNewThreshold(M, pks);
205 return CCNewThreshold(2, {condCC, Sig});
208 CC *MakeCCcondOneSig(const CTxDestination &dest)
210 //printf("making condition with ID: %s\n", GetDestinationID(dest).GetHex().c_str());
212 CPubKey pk = boost::apply_visitor<GetPubKeyForPubKey>(GetPubKeyForPubKey(), dest);
215 return CCNewSecp256k1(pk);
219 return CCNewHashedSecp256k1(CKeyID(GetDestinationID(dest)));
223 CC *MakeCCcondMofN(const std::vector<CTxDestination> &dests, int M)
225 std::vector<CC*> pks;
226 for (auto dest : dests)
228 pks.push_back(MakeCCcondOneSig(dest));
239 return CCNewThreshold(M, pks);
242 CC *MakeCCcondMofN(const std::vector<CC*> &conditions, int M)
244 if (M > conditions.size())
246 M = conditions.size();
249 return CCNewThreshold(M, conditions);
252 CC *MakeCCcondMofN(uint8_t evalcode, const std::vector<CC*> &conditions, int M)
254 if (evalcode == EVAL_NONE)
256 return MakeCCcondMofN(conditions, M);
259 if (M > conditions.size())
261 M = conditions.size();
264 CC *condCC = CCNewEval(E_MARSHAL(ss << evalcode));
265 CC *Sig = CCNewThreshold(M, conditions);
266 return CCNewThreshold(2, {condCC, Sig});
269 // TOBJ is CConditionObj of a CC output type
270 template <typename TOBJ>
271 CC *MakeMofNCC(TOBJ &conditionObj)
273 return MakeCCcondMofN(conditionObj.evalCode, conditionObj.dests, conditionObj.m);
276 template <typename TOBJ1, typename TOBJ2>
277 CC *MakeMofNCC(int M, TOBJ1 &condition1, TOBJ2 &condition2)
280 std::vector<CC*> conditions({MakeCCcondMofN(condition1.evalCode, condition1.dests, condition1.m), MakeCCcondMofN(condition2.evalCode, condition2.dests, condition2.m)});
281 return CCNewThreshold(M, conditions);
284 template <typename TOBJ1, typename TOBJ2, typename TOBJ3>
285 CC *MakeMofNCC(int M, TOBJ1 &condition1, TOBJ2 &condition2, TOBJ3 &condition3)
288 std::vector<CC*> conditions({MakeCCcondMofN(condition1.evalCode, condition1.dests, condition1.m), MakeCCcondMofN(condition2.evalCode, condition2.dests, condition2.m), MakeCCcondMofN(condition3.evalCode, condition3.dests, condition3.m)});
289 return CCNewThreshold(M, conditions);
292 template <typename TOBJ1, typename TOBJ2, typename TOBJ3, typename TOBJ4>
293 CC *MakeMofNCC(int M, TOBJ1 &condition1, TOBJ2 &condition2, TOBJ3 &condition3, TOBJ4 &condition4)
296 std::vector<CC*> conditions({MakeCCcondMofN(condition1.evalCode, condition1.dests, condition1.m),
297 MakeCCcondMofN(condition2.evalCode, condition2.dests, condition2.m),
298 MakeCCcondMofN(condition3.evalCode, condition3.dests, condition3.m),
299 MakeCCcondMofN(condition4.evalCode, condition4.dests, condition4.m)});
300 return CCNewThreshold(M, conditions);
303 CScript _CCPubKey(const CC *cond)
305 unsigned char buf[2000];
306 size_t len = cc_conditionBinary(cond, buf, 2000);
307 return CScript() << std::vector<unsigned char>(buf, buf+len) << OP_CHECKCRYPTOCONDITION;
310 CIdentity LookupIdentity(const BaseSignatureCreator& creator, const CIdentityID &idID)
312 std::pair<CIdentityMapKey, CIdentityMapValue> identity;
314 //printf("Looking up %s identity\n", EncodeDestination(CTxDestination(idID)).c_str());
315 if (creator.IsKeystoreValid() &&
316 creator.KeyStore().GetIdentity(idID, identity) &&
317 identity.second.IsValidUnrevoked())
319 return identity.second;
324 static bool SignStepCC(const BaseSignatureCreator& creator, const CScript& scriptPubKey, vector<valtype> &vSolutions,
325 vector<valtype>& ret, uint32_t consensusBranchId)
328 std::vector<CTxDestination> vPK;
329 std::vector<valtype> vParams = std::vector<valtype>();
332 // get information to sign with
335 if (scriptPubKey.IsPayToCryptoCondition(p) && p.IsValid() && p.n >= 1 && p.vKeys.size() >= p.n)
337 if (p.version >= p.VERSION_V3 && p.vData.size())
339 // decode the entire crypto-condition
340 COptCCParams master = COptCCParams(p.vData.back());
341 bool ccValid = master.IsValid();
342 std::vector<CConditionObj<COptCCParams>> conditions;
343 std::map<uint160, CTxDestination> destMap; // all destinations
344 std::map<uint160, CKey> privKeyMap; // private keys located for each id
345 std::vector<CC*> ccs;
348 bool identitySpend = false;
350 if (p.evalCode == EVAL_IDENTITY_PRIMARY)
352 identity = CIdentity(p.vData[0]);
353 identitySpend = identity.IsValid();
354 idID = identity.GetID();
357 // if we are sign-only, "p" will have no data object of its own, so we do not have to subtract 1
358 int loopMax = p.evalCode ? p.vData.size() - 1 : p.vData.size();
360 for (int i = 0; ccValid && i < loopMax; i++)
362 // "p", our first COptCCParams object will be considered first, then nested objects after
363 COptCCParams oneP = i ? COptCCParams(p.vData[i]) : p;
364 ccValid = oneP.IsValid();
365 std::vector<CC*> vCC;
368 conditions.push_back(CConditionObj<COptCCParams>(oneP.evalCode, oneP.vKeys, oneP.m));
371 if (p.evalCode && CCinit(&C, p.evalCode))
373 CPubKey evalPK(ParseHex(C.CChexstr));
375 std::vector<unsigned char> vch(&(C.CCpriv[0]), C.CCpriv + sizeof(C.CCpriv));
376 priv.Set(vch.begin(), vch.end(), true);
377 privKeyMap[evalPK.GetID()] = priv;
387 for (auto &dest : oneP.vKeys)
389 uint160 destId = GetDestinationID(dest);
390 if (dest.which() == COptCCParams::ADDRTYPE_ID)
392 // lookup identity, we must have all registered target identity scripts in our keystore, or we try as if they are a keyID, which will be the same
393 // if revoked or undefined
396 // if this is an identity self spend, we always use the absolute latest version to control it ,mcf
402 if ((!id.IsValid() && !(id = LookupIdentity(creator, CIdentityID(destId))).IsValid()) || id.IsRevoked())
404 destMap[destId] = dest;
405 vCC.push_back(MakeCCcondOneSig(CKeyID(GetDestinationID(dest))));
409 for (auto oneKey : id.primaryAddresses)
411 destMap[GetDestinationID(oneKey)] = oneKey;
413 if (id.primaryAddresses.size() == 1)
415 vCC.push_back(MakeCCcondOneSig(CKeyID(GetDestinationID(id.primaryAddresses[0]))));
419 vCC.push_back(MakeCCcondMofN(id.primaryAddresses, id.minSigs));
425 destMap[destId] = dest;
426 vCC.push_back(MakeCCcondOneSig(dest));
430 if (oneP.evalCode != EVAL_NONE)
432 ccs.push_back(MakeCCcondMofN(oneP.evalCode, vCC, oneP.m ? oneP.m : 1));
438 ccs.push_back(vCC[0]);
442 ccs.push_back(MakeCCcondMofN(vCC, oneP.m));
448 CC *outputCC = nullptr;
452 assert(ccs.size() == conditions.size());
457 outputCC = MakeCCcondMofN(master.evalCode, ccs, master.m);
468 outputCC = MakeCCcondMofN(master.evalCode, ccs, master.m);
472 outputCC = MakeCCcondMofN(ccs, master.m);
477 // loop through all private keys we have and sign with each of them
480 // loop through keys and sign with all that we have
481 std::map<CKeyID, CKey> privKeys;
482 for (auto destPair : destMap)
484 //printf("Checking for private key of destination: %s ", EncodeDestination(destPair.second).c_str());
487 auto dit = privKeyMap.find(destPair.first);
488 if (dit != privKeyMap.end())
490 privKeys[dit->first] = dit->second;
491 //printf("...using key for crypto condition\n");
493 else if (creator.IsKeystoreValid() && creator.KeyStore().GetKey(destPair.first, privKey))
495 privKeys[destPair.first] = privKey;
496 //printf("...using key from wallet\n");
500 vector<unsigned char> vch = ret.size() ? ret[0] : vector<unsigned char>();
502 CScript signScript = _CCPubKey(outputCC);
504 for (auto privKey : privKeys)
506 // if we have an error or can't sign and it isn't fulfilled, fail, if we have no error, can't sign again, and we have a fulfilled sig, it must be optimized and OK
507 if (error || (!creator.CreateSig(vch, privKey.first, scriptPubKey, consensusBranchId, &privKey.second, (void *)outputCC)))
510 //CPubKey errKey = privKey.second.GetPubKey();
511 //fprintf(stderr,"vin has MofN CC signing error with pubkey: %s, ID: %s\n", HexBytes(&(std::vector<unsigned char>(errKey.begin(), errKey.end())[0]), errKey.size()).c_str(), errKey.GetID().GetHex().c_str());
515 if (!error && vch.size())
521 return !error && ret.size() != 0;
526 bool is0ofAny = (p.m == 0 && p.n >= 1);
527 bool is1ofn = (p.m == 1 && p.n >= 2);
530 // must be a valid cc eval code
531 if (CCinit(&C, p.evalCode))
533 // pay to cc address is a valid tx
536 CPubKey pubk = CPubKey(ParseHex(C.CChexstr));
537 CKeyID keyID = pubk.GetID();
538 bool havePriv = false;
540 // loop through and sign with the first of either the private key or a match on the CCs private key, otherwise, fail
541 for (auto dest : p.vKeys)
543 uint160 keyID = GetDestinationID(dest);
544 if (creator.IsKeystoreValid() && creator.KeyStore().GetKey(keyID, privKey))
549 CPubKey tempPub = boost::apply_visitor<GetPubKeyForPubKey>(GetPubKeyForPubKey(), dest);
550 if ((tempPub.IsValid() && tempPub == pubk) || (keyID == tempPub.GetID()))
552 // found the pub key for this crypto condition, so use the private key
553 std::vector<unsigned char> vch(&(C.CCpriv[0]), C.CCpriv + sizeof(C.CCpriv));
554 privKey.Set(vch.begin(), vch.end(), true);
562 fprintf(stderr,"Do not have or cannot locate private key for %s\n", EncodeDestination(p.vKeys[0]).c_str());
566 CC *cc = CCcondAny(p.evalCode, p.vKeys);
570 vector<unsigned char> vch;
571 if (creator.CreateSig(vch, GetDestinationID(p.vKeys[0]), _CCPubKey(cc), consensusBranchId, &privKey, (void *)cc))
577 fprintf(stderr,"vin has 1ofAny CC signing error with address.(%s)\n", EncodeDestination(p.vKeys[0]).c_str());
581 return ret.size() != 0;
586 uint160 keyID = GetDestinationID(p.vKeys[0]);
587 bool havePriv = creator.IsKeystoreValid() && creator.KeyStore().GetKey(keyID, privKey);
590 // if we don't have the private key, it must be the unspendable address
593 std::vector<unsigned char> vkch = GetDestinationBytes(p.vKeys[0]);
594 if (vkch.size() == 33)
596 pubk = CPubKey(vkch);
600 creator.KeyStore().GetPubKey(keyID, pubk);
606 std::vector<unsigned char> vch(&(C.CCpriv[0]), C.CCpriv + sizeof(C.CCpriv));
608 privKey.Set(vch.begin(), vch.end(), true);
609 pubk = CPubKey(ParseHex(C.CChexstr));
612 CC *cc = CCcond1(p.evalCode, pubk);
616 vector<unsigned char> vch;
617 if (creator.CreateSig(vch, GetDestinationID(p.vKeys[0]), _CCPubKey(cc), consensusBranchId, &privKey, (void *)cc))
623 fprintf(stderr,"vin has 1of1 CC signing error with address.(%s)\n", keyID.ToString().c_str());
627 return ret.size() != 0;
632 // first of priv key in our key store or contract address is what we sign with if we have it
633 std::vector<CPubKey> keys;
634 for (auto pk : p.vKeys)
636 uint160 keyID = GetDestinationID(pk);
638 if (!(creator.IsKeystoreValid() && creator.KeyStore().GetPubKey(keyID, foundKey)))
640 std::vector<unsigned char> vkch = GetDestinationBytes(pk);
641 if (vkch.size() == 33)
643 foundKey = CPubKey(vkch);
647 if (foundKey.IsFullyValid())
649 keys.push_back(foundKey);
653 // if we only have one key, and this is version 2, add the cc pub key
654 if (keys.size() <= 1 && p.version == p.VERSION_V2)
656 keys.push_back(CPubKey(ParseHex(C.CChexstr)));
659 // we need something to sign with
667 if (creator.IsKeystoreValid() && creator.KeyStore().GetKey(pk.GetID(), privKey) && privKey.IsValid())
672 if (pk == CPubKey(ParseHex(C.CChexstr)))
675 std::vector<unsigned char> vch(&(C.CCpriv[0]), C.CCpriv + sizeof(C.CCpriv));
676 privKey.Set(vch.begin(), vch.end(), true);
681 if (!privKey.IsValid())
687 cc = CCcond1of2(p.evalCode, keys[0], keys[1]);
691 cc = CCcond1(p.evalCode, keys[0]);
696 vector<unsigned char> vch;
697 if (creator.CreateSig(vch, keys[0].GetID(), _CCPubKey(cc), consensusBranchId, &privKey, (void *)cc))
703 fprintf(stderr,"vin has 1ofn CC signing error with addresses.(%s)\n(%s)\n", keys[0].GetID().ToString().c_str(), keys[1].GetID().ToString().c_str());
707 return ret.size() != 0;
717 * Sign scriptPubKey using signature made with creator.
718 * Signatures are returned in scriptSigRet (or returns false if scriptPubKey can't be signed),
719 * unless whichTypeRet is TX_SCRIPTHASH, in which case scriptSigRet is the redemption script.
720 * Returns false if scriptPubKey could not be completely satisfied.
722 static bool SignStep(const BaseSignatureCreator& creator, const CScript& scriptPubKey,
723 std::vector<valtype>& ret, txnouttype& whichTypeRet, uint32_t consensusBranchId)
729 vector<valtype> vSolutions;
731 if (!Solver(scriptPubKey, whichTypeRet, vSolutions))
733 // if this is a CLTV script, solve for the destination after CLTV
734 if (scriptPubKey.IsCheckLockTimeVerify())
736 uint8_t pushOp = scriptPubKey[0];
737 uint32_t scriptStart = pushOp + 3;
739 // check post CLTV script
740 CScript postfix = CScript(scriptPubKey.size() > scriptStart ? scriptPubKey.begin() + scriptStart : scriptPubKey.end(), scriptPubKey.end());
742 // check again with only postfix subscript
743 if (!Solver(postfix, whichTypeRet, vSolutions))
752 switch (whichTypeRet)
758 keyID = CPubKey(vSolutions[0]).GetID();
759 return Sign1(keyID, creator, scriptPubKey, ret, consensusBranchId);
761 keyID = CKeyID(uint160(vSolutions[0]));
762 if (!Sign1(keyID, creator, scriptPubKey, ret, consensusBranchId))
767 creator.KeyStore().GetPubKey(keyID, vch);
768 ret.push_back(ToByteVector(vch));
772 if (creator.KeyStore().GetCScript(uint160(vSolutions[0]), scriptRet)) {
773 ret.push_back(std::vector<unsigned char>(scriptRet.begin(), scriptRet.end()));
778 case TX_CRYPTOCONDITION:
779 return SignStepCC(creator, scriptPubKey, vSolutions, ret, consensusBranchId);
782 ret.push_back(valtype()); // workaround CHECKMULTISIG bug
783 return (SignN(vSolutions, creator, scriptPubKey, ret, consensusBranchId));
790 static CScript PushAll(const vector<valtype>& values)
793 BOOST_FOREACH(const valtype& v, values) {
796 } else if (v.size() == 1 && v[0] >= 1 && v[0] <= 16) {
797 result << CScript::EncodeOP_N(v[0]);
805 bool ProduceSignature(const BaseSignatureCreator& creator, const CScript& fromPubKey, SignatureData& sigdata, uint32_t consensusBranchId)
807 CScript script = fromPubKey;
809 std::vector<valtype> result;
810 txnouttype whichType;
811 solved = SignStep(creator, script, result, whichType, consensusBranchId);
814 if (solved && whichType == TX_SCRIPTHASH)
816 // Solver returns the subscript that needs to be evaluated;
817 // the final scriptSig is the signatures from that
818 // and then the serialized subscript:
819 script = subscript = CScript(result[0].begin(), result[0].end());
820 solved = solved && SignStep(creator, script, result, whichType, consensusBranchId) && whichType != TX_SCRIPTHASH;
821 result.push_back(std::vector<unsigned char>(subscript.begin(), subscript.end()));
824 sigdata.scriptSig = PushAll(result);
827 return solved && VerifyScript(sigdata.scriptSig, fromPubKey, STANDARD_SCRIPT_VERIFY_FLAGS, creator.Checker(), consensusBranchId);
830 SignatureData DataFromTransaction(const CMutableTransaction& tx, unsigned int nIn)
833 assert(tx.vin.size() > nIn);
834 data.scriptSig = tx.vin[nIn].scriptSig;
838 void UpdateTransaction(CMutableTransaction& tx, unsigned int nIn, const SignatureData& data)
840 assert(tx.vin.size() > nIn);
841 tx.vin[nIn].scriptSig = data.scriptSig;
845 const CKeyStore &keystore,
846 const CScript& fromPubKey,
847 CMutableTransaction& txTo,
849 const CAmount& amount,
851 uint32_t consensusBranchId)
853 assert(nIn < txTo.vin.size());
855 CTransaction txToConst(txTo);
856 TransactionSignatureCreator creator(&keystore, &txToConst, nIn, amount, nHashType);
858 SignatureData sigdata;
859 bool ret = ProduceSignature(creator, fromPubKey, sigdata, consensusBranchId);
860 UpdateTransaction(txTo, nIn, sigdata);
865 const CKeyStore &keystore,
866 const CTransaction& txFrom,
867 CMutableTransaction& txTo,
870 uint32_t consensusBranchId)
872 assert(nIn < txTo.vin.size());
873 CTxIn& txin = txTo.vin[nIn];
874 assert(txin.prevout.n < txFrom.vout.size());
875 const CTxOut& txout = txFrom.vout[txin.prevout.n];
877 return SignSignature(keystore, txout.scriptPubKey, txTo, nIn, txout.nValue, nHashType, consensusBranchId);
880 static vector<valtype> CombineMultisig(const CScript& scriptPubKey, const BaseSignatureChecker& checker,
881 const vector<valtype>& vSolutions,
882 const vector<valtype>& sigs1, const vector<valtype>& sigs2, uint32_t consensusBranchId)
884 // Combine all the signatures we've got:
885 set<valtype> allsigs;
886 BOOST_FOREACH(const valtype& v, sigs1)
891 BOOST_FOREACH(const valtype& v, sigs2)
897 // Build a map of pubkey -> signature by matching sigs to pubkeys:
898 assert(vSolutions.size() > 1);
899 unsigned int nSigsRequired = vSolutions.front()[0];
900 unsigned int nPubKeys = vSolutions.size()-2;
901 map<valtype, valtype> sigs;
902 BOOST_FOREACH(const valtype& sig, allsigs)
904 for (unsigned int i = 0; i < nPubKeys; i++)
906 const valtype& pubkey = vSolutions[i+1];
907 if (sigs.count(pubkey))
908 continue; // Already got a sig for this pubkey
910 if (checker.CheckSig(sig, pubkey, scriptPubKey, consensusBranchId))
917 // Now build a merged CScript:
918 unsigned int nSigsHave = 0;
919 std::vector<valtype> result; result.push_back(valtype()); // pop-one-too-many workaround
920 for (unsigned int i = 0; i < nPubKeys && nSigsHave < nSigsRequired; i++)
922 if (sigs.count(vSolutions[i+1]))
924 result.push_back(sigs[vSolutions[i+1]]);
928 // Fill any missing with OP_0:
929 for (unsigned int i = nSigsHave; i < nSigsRequired; i++)
930 result.push_back(valtype());
939 std::vector<valtype> script;
942 explicit Stacks(const std::vector<valtype>& scriptSigStack_) : script(scriptSigStack_) {}
943 explicit Stacks(const SignatureData& data, uint32_t consensusBranchId) {
944 EvalScript(script, data.scriptSig, SCRIPT_VERIFY_STRICTENC, BaseSignatureChecker(), consensusBranchId);
947 SignatureData Output() const {
948 SignatureData result;
949 result.scriptSig = PushAll(script);
955 static Stacks CombineSignatures(const CScript& scriptPubKey, const BaseSignatureChecker& checker,
956 const txnouttype txType, const vector<valtype>& vSolutions,
957 Stacks sigs1, Stacks sigs2, uint32_t consensusBranchId)
963 // Don't know anything about this, assume bigger one is correct:
964 if (sigs1.script.size() >= sigs2.script.size())
969 case TX_CRYPTOCONDITION:
970 // Signatures are bigger than placeholders or empty scripts:
971 if (sigs1.script.empty() || sigs1.script[0].empty())
975 if (sigs1.script.empty() || sigs1.script.back().empty())
977 else if (sigs2.script.empty() || sigs2.script.back().empty())
982 valtype spk = sigs1.script.back();
983 CScript pubKey2(spk.begin(), spk.end());
986 vector<vector<unsigned char> > vSolutions2;
987 Solver(pubKey2, txType2, vSolutions2);
988 sigs1.script.pop_back();
989 sigs2.script.pop_back();
990 Stacks result = CombineSignatures(pubKey2, checker, txType2, vSolutions2, sigs1, sigs2, consensusBranchId);
991 result.script.push_back(spk);
995 return Stacks(CombineMultisig(scriptPubKey, checker, vSolutions, sigs1.script, sigs2.script, consensusBranchId));
1001 SignatureData CombineSignatures(const CScript& scriptPubKey, const BaseSignatureChecker& checker,
1002 const SignatureData& scriptSig1, const SignatureData& scriptSig2,
1003 uint32_t consensusBranchId)
1006 vector<vector<unsigned char> > vSolutions;
1007 Solver(scriptPubKey, txType, vSolutions);
1009 return CombineSignatures(
1010 scriptPubKey, checker, txType, vSolutions,
1011 Stacks(scriptSig1, consensusBranchId),
1012 Stacks(scriptSig2, consensusBranchId),
1013 consensusBranchId).Output();
1017 /** Dummy signature checker which accepts all signatures. */
1018 class DummySignatureChecker : public BaseSignatureChecker
1021 DummySignatureChecker() {}
1024 const std::vector<unsigned char>& scriptSig,
1025 const std::vector<unsigned char>& vchPubKey,
1026 const CScript& scriptCode,
1027 uint32_t consensusBranchId) const
1032 int CheckCryptoCondition(
1033 const std::vector<unsigned char>& condBin,
1034 const std::vector<unsigned char>& ffillBin,
1035 const CScript& scriptCode,
1036 uint32_t consensusBranchId) const
1041 const DummySignatureChecker dummyChecker;
1044 const BaseSignatureChecker& DummySignatureCreator::Checker() const
1046 return dummyChecker;
1049 bool DummySignatureCreator::CreateSig(
1050 std::vector<unsigned char>& vchSig,
1051 const CKeyID& keyid,
1052 const CScript& scriptCode,
1053 uint32_t consensusBranchId,
1055 void *extraData) const
1057 if (scriptCode.IsPayToCryptoCondition())
1059 vchSig = CCPartialSigVec(MakeCCcondOneSig(keyid));
1063 // Create a dummy signature that is a valid DER-encoding
1064 vchSig.assign(72, '\000');
1070 vchSig[4 + 33] = 0x02;
1071 vchSig[5 + 33] = 32;
1072 vchSig[6 + 33] = 0x01;
1073 vchSig[6 + 33 + 32] = SIGHASH_ALL;