forked from ioncoincore/ion
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
41 changed files
with
1,460 additions
and
740 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
|
||
#include "blocksignature.h" | ||
#include "main.h" | ||
|
||
bool SignBlockWithKey(CBlock& block, const CKey& key) | ||
{ | ||
if (!key.Sign(block.GetHash(), block.vchBlockSig)) | ||
return error("%s: failed to sign block hash with key", __func__); | ||
|
||
return true; | ||
} | ||
|
||
bool GetKeyIDFromUTXO(const CTxOut& txout, CKeyID& keyID) | ||
{ | ||
std::vector<valtype> vSolutions; | ||
txnouttype whichType; | ||
if (!Solver(txout.scriptPubKey, whichType, vSolutions)) | ||
return false; | ||
if (whichType == TX_PUBKEY) { | ||
keyID = CPubKey(vSolutions[0]).GetID(); | ||
} else if (whichType == TX_PUBKEYHASH) { | ||
keyID = CKeyID(uint160(vSolutions[0])); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
bool SignBlock(CBlock& block, const CKeyStore& keystore) | ||
{ | ||
CKeyID keyID; | ||
if (block.IsProofOfWork()) { | ||
bool fFoundID = false; | ||
for (const CTxOut& txout :block.vtx[0].vout) { | ||
if (!GetKeyIDFromUTXO(txout, keyID)) | ||
continue; | ||
fFoundID = true; | ||
break; | ||
} | ||
if (!fFoundID) | ||
return error("%s: failed to find key for PoW", __func__); | ||
} else { | ||
if (!GetKeyIDFromUTXO(block.vtx[1].vout[1], keyID)) | ||
return error("%s: failed to find key for PoS", __func__); | ||
} | ||
|
||
CKey key; | ||
if (!keystore.GetKey(keyID, key)) | ||
return error("%s: failed to get key from keystore", __func__); | ||
|
||
return SignBlockWithKey(block, key); | ||
} | ||
|
||
bool CheckBlockSignature(const CBlock& block) | ||
{ | ||
if (block.IsProofOfWork()) | ||
return block.vchBlockSig.empty(); | ||
|
||
if (block.vchBlockSig.empty()) | ||
return error("%s: vchBlockSig is empty!", __func__); | ||
|
||
/** Each block is signed by the private key of the input that is staked. This can be either zPIV or normal UTXO | ||
* zPIV: Each zPIV has a keypair associated with it. The serial number is a hash of the public key. | ||
* UTXO: The public key that signs must match the public key associated with the first utxo of the coinstake tx. | ||
*/ | ||
CPubKey pubkey; | ||
bool fzPIVStake = block.vtx[1].IsZerocoinSpend(); | ||
if (fzPIVStake) { | ||
libzerocoin::CoinSpend spend = TxInToZerocoinSpend(block.vtx[1].vin[0]); | ||
pubkey = spend.getPubKey(); | ||
LogPrintf("%s spend version=%d\n", __func__, spend.getVersion()); | ||
} else { | ||
txnouttype whichType; | ||
std::vector<valtype> vSolutions; | ||
const CTxOut& txout = block.vtx[1].vout[1]; | ||
if (!Solver(txout.scriptPubKey, whichType, vSolutions)) | ||
return false; | ||
if (whichType == TX_PUBKEY || whichType == TX_PUBKEYHASH) { | ||
valtype& vchPubKey = vSolutions[0]; | ||
pubkey = CPubKey(vchPubKey); | ||
} | ||
} | ||
|
||
if (!pubkey.IsValid()) | ||
return error("%s: invalid pubkey %s", __func__, pubkey.GetHex()); | ||
|
||
return pubkey.Verify(block.GetHash(), block.vchBlockSig); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
|
||
|
||
#ifndef PIVX_BLOCKSIGNATURE_H | ||
#define PIVX_BLOCKSIGNATURE_H | ||
|
||
#include "key.h" | ||
#include "primitives/block.h" | ||
#include "keystore.h" | ||
|
||
bool SignBlockWithKey(CBlock& block, const CKey& key); | ||
bool SignBlock(CBlock& block, const CKeyStore& keystore); | ||
bool CheckBlockSignature(const CBlock& block); | ||
|
||
#endif //PIVX_BLOCKSIGNATURE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.