diff --git a/src/pegins.cpp b/src/pegins.cpp index 167d1cd3dd..3dea1bc3ef 100644 --- a/src/pegins.cpp +++ b/src/pegins.cpp @@ -496,3 +496,45 @@ std::vector> GetValidFedpegScripts(const CBlockIndex fedpegscripts.resize(std::min(fedpegscripts.size(), params.total_valid_epochs)); return fedpegscripts; } + +template +CScriptWitness CreatePeginWitnessInner(const CAmount& value, const CAsset& asset, const uint256& genesis_hash, const CScript& claim_script, const T_tx_ref& tx_ref, const T_merkle_block& merkle_block) +{ + std::vector value_bytes; + CVectorWriter ss_val(0, 0, value_bytes, 0); + try { + ss_val << value; + } catch (...) { + throw std::ios_base::failure("Amount serialization is invalid."); + } + + // Strip witness data for proof inclusion since only TXID-covered fields matters + CDataStream ss_tx(SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS); + ss_tx << tx_ref; + std::vector tx_data_stripped(ss_tx.begin(), ss_tx.end()); + + // Serialize merkle block + CDataStream ss_txout_proof(SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS); + ss_txout_proof << merkle_block; + std::vector txout_proof_bytes(ss_txout_proof.begin(), ss_txout_proof.end()); + + // Construct pegin proof + CScriptWitness pegin_witness; + std::vector >& stack = pegin_witness.stack; + stack.push_back(value_bytes); + stack.push_back(std::vector(asset.begin(), asset.end())); + stack.push_back(std::vector(genesis_hash.begin(), genesis_hash.end())); + stack.push_back(std::vector(claim_script.begin(), claim_script.end())); + stack.push_back(tx_data_stripped); + stack.push_back(txout_proof_bytes); + return pegin_witness; +} + +CScriptWitness CreatePeginWitness(const CAmount& value, const CAsset& asset, const uint256& genesis_hash, const CScript& claim_script, const CTransactionRef& tx_ref, const CMerkleBlock& merkle_block) +{ + return CreatePeginWitnessInner(value, asset, genesis_hash, claim_script, tx_ref, merkle_block); +} +CScriptWitness CreatePeginWitness(const CAmount& value, const CAsset& asset, const uint256& genesis_hash, const CScript& claim_script, const Sidechain::Bitcoin::CTransactionRef& tx_ref, const Sidechain::Bitcoin::CMerkleBlock& merkle_block) +{ + return CreatePeginWitnessInner(value, asset, genesis_hash, claim_script, tx_ref, merkle_block); +} diff --git a/src/pegins.h b/src/pegins.h index e3e2f80082..86ca57eef4 100644 --- a/src/pegins.h +++ b/src/pegins.h @@ -7,7 +7,9 @@ #include #include +#include #include +#include #include #include