PRCYCoin  2.0.0.7rc1
P2P Digital Currency
blocksignature.cpp
Go to the documentation of this file.
1 // Copyright (c) 2015-2018 The PIVX developers
2 // Copyright (c) 2018-2020 The DAPS Project developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "blocksignature.h"
7 #include "main.h"
8 
9 bool SignBlockWithKey(CBlock& block, const CKey& key)
10 {
11  if (!key.Sign(block.GetHash(), block.vchBlockSig))
12  return error("%s: failed to sign block hash with key", __func__);
13 
14  return true;
15 }
16 
17 bool GetKeyIDFromUTXO(const CTxOut& txout, CKeyID& keyID)
18 {
19  std::vector<valtype> vSolutions;
20  txnouttype whichType;
21  if (!Solver(txout.scriptPubKey, whichType, vSolutions))
22  return false;
23  if (whichType == TX_PUBKEY) {
24  keyID = CPubKey(vSolutions[0]).GetID();
25  } else if (whichType == TX_PUBKEYHASH) {
26  keyID = CKeyID(uint160(vSolutions[0]));
27  }
28 
29  return true;
30 }
31 
32 bool SignBlock(CBlock& block, const CKeyStore& keystore)
33 {
34  CKeyID keyID;
35  if (block.IsProofOfWork()) {
36  bool fFoundID = false;
37  for (const CTxOut& txout :block.vtx[0].vout) {
38  if (!GetKeyIDFromUTXO(txout, keyID))
39  continue;
40  fFoundID = true;
41  break;
42  }
43  if (!fFoundID)
44  return error("%s: failed to find key for PoW", __func__);
45  } else {
46  if (!GetKeyIDFromUTXO(block.vtx[1].vout[1], keyID))
47  return error("%s: failed to find key for PoS", __func__);
48  }
49 
50  CKey key;
51  if (!keystore.GetKey(keyID, key))
52  return error("%s: failed to get key from keystore", __func__);
53 
54  return SignBlockWithKey(block, key);
55 }
56 
57 bool CheckBlockSignature(const CBlock& block)
58 {
59  if (block.IsProofOfWork())
60  return block.vchBlockSig.empty();
61 
62  if (block.vchBlockSig.empty())
63  return error("%s: vchBlockSig is empty!", __func__);
64 
68  CPubKey pubkey;
69  txnouttype whichType;
70  std::vector<valtype> vSolutions;
71  const CTxOut& txout = block.vtx[1].vout[1];
72  if (!Solver(txout.scriptPubKey, whichType, vSolutions))
73  return false;
74  if (whichType == TX_PUBKEY || whichType == TX_PUBKEYHASH) {
75  valtype& vchPubKey = vSolutions[0];
76  pubkey = CPubKey(vchPubKey);
77  }
78 
79  if (!pubkey.IsValid())
80  return error("%s: invalid pubkey %s", __func__, pubkey.GetHex());
81 
82  return pubkey.Verify(block.GetHash(), block.vchBlockSig);
83 }
CKeyStore
A virtual base class for key stores.
Definition: keystore.h:21
CBlock::IsProofOfWork
bool IsProofOfWork() const
Definition: block.h:218
SignBlock
bool SignBlock(CBlock &block, const CKeyStore &keystore)
Definition: blocksignature.cpp:32
CKeyID
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:29
CBlockHeader::GetHash
uint256 GetHash() const
Definition: block.cpp:80
CPubKey::GetHex
std::string GetHex() const
Definition: pubkey.h:193
SignBlockWithKey
bool SignBlockWithKey(CBlock &block, const CKey &key)
Definition: blocksignature.cpp:9
TX_PUBKEYHASH
@ TX_PUBKEYHASH
Definition: standard.h:62
CTxOut
An output of a transaction.
Definition: transaction.h:164
CTxOut::scriptPubKey
CScript scriptPubKey
Definition: transaction.h:168
CPubKey::Verify
bool Verify(const uint256 &hash, const std::vector< unsigned char > &vchSig) const
Verify a DER signature (~72 bytes).
Definition: pubkey.cpp:15
TX_PUBKEY
@ TX_PUBKEY
Definition: standard.h:61
blocksignature.h
CBlock::vtx
std::vector< CTransaction > vtx
Definition: block.h:146
CBlock
Definition: block.h:142
CKeyStore::GetKey
virtual bool GetKey(const CKeyID &address, CKey &keyOut) const =0
uint160
160-bit unsigned big integer.
Definition: uint256.h:27
CPubKey
An encapsulated public key.
Definition: pubkey.h:37
CKey
An encapsulated private key.
Definition: key.h:39
main.h
Solver
bool Solver(const CKeyStore &keystore, const CScript &scriptPubKey, uint256 hash, int nHashType, CScript &scriptSigRet, txnouttype &whichTypeRet)
Sign scriptPubKey with private keys stored in keystore, given transaction hash and hash type.
Definition: sign.cpp:50
key
CKey key
Definition: bip38tooldialog.cpp:173
CKey::Sign
bool Sign(const uint256 &hash, std::vector< unsigned char > &vchSig, uint32_t test_case=0) const
Create a DER-serialized signature.
Definition: key.cpp:91
valtype
std::vector< unsigned char > valtype
Definition: interpreter.cpp:19
txnouttype
txnouttype
Definition: standard.h:57
CPubKey::IsValid
bool IsValid() const
Definition: pubkey.h:159
CheckBlockSignature
bool CheckBlockSignature(const CBlock &block)
Definition: blocksignature.cpp:57
GetKeyIDFromUTXO
bool GetKeyIDFromUTXO(const CTxOut &txout, CKeyID &keyID)
Definition: blocksignature.cpp:17
CBlock::vchBlockSig
std::vector< unsigned char > vchBlockSig
Definition: block.h:152
error
bool error(const char *fmt, const Args &... args)
Definition: util.h:61
CPubKey::GetID
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:143