Skip to content

Commit

Permalink
remove CNumScript from pegin witness logic
Browse files Browse the repository at this point in the history
  • Loading branch information
instagibbs committed Nov 6, 2017
1 parent abafbac commit 7dc4f9d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
2 changes: 1 addition & 1 deletion qa/rpc-tests/pegging.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def sync_all(sidechain, sidechain2):
pegtxid = sidechain.claimpegin(raw, proof, sidechain.getnewaddress())
raise Exception("Peg-in with non-matching claim_script should fail.")
except JSONRPCException as e:
assert("Given claim_script is not a valid v0 witness program" in e.error["message"])
assert("Given claim_script does not match the given Bitcoin transaction." in e.error["message"])
pass

# 12 confirms allows in mempool
Expand Down
6 changes: 0 additions & 6 deletions src/script/script.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,12 +325,6 @@ class CScriptNum
return m_value;
}

// Only used for peg-in witness values
int64_t getint64() const
{
return m_value;
}

std::vector<unsigned char> getvch() const
{
return serialize(m_value);
Expand Down
19 changes: 16 additions & 3 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2375,8 +2375,17 @@ bool IsValidPeginWitness(const CScriptWitness& pegin_witness, const COutPoint& p
return false;
}

// Get output value. Special 8 byte length to capture possible bitcoin values
CAmount value = CScriptNum(stack[0], true, 8).getint64();
CDataStream stream(stack[0], SER_NETWORK, PROTOCOL_VERSION);
CAmount value;
try {
stream >> value;
} catch (...) {
return false;
}

if (!MoneyRange(value)) {
return false;
}

// Get asset type
if (stack[1].size() != 32) {
Expand Down Expand Up @@ -2473,7 +2482,11 @@ bool IsValidPeginWitness(const CScriptWitness& pegin_witness, const COutPoint& p

// Constructs unblinded output to be used in amount and scriptpubkey checks during pegin
CTxOut GetPeginOutputFromWitness(const CScriptWitness& pegin_witness) {
return CTxOut(CAsset(pegin_witness.stack[1]), CScriptNum(pegin_witness.stack[0], true, 8).getint64(), CScript(pegin_witness.stack[3].begin(), pegin_witness.stack[3].end()));
CDataStream stream(pegin_witness.stack[0], SER_NETWORK, PROTOCOL_VERSION);
CAmount value;
stream >> value;

return CTxOut(CAsset(pegin_witness.stack[1]), value, CScript(pegin_witness.stack[3].begin(), pegin_witness.stack[3].end()));
}

// Protected by cs_main
Expand Down
16 changes: 15 additions & 1 deletion src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3579,6 +3579,9 @@ UniValue createrawpegin(const JSONRPCRequest& request)
throw JSONRPCError(RPC_INVALID_PARAMETER, "Given claim_script is not a valid v0 witness program.");
}
nOut = GetPeginTxnOutputIndex(txBTC, witnessProgScript);
if (nOut == txBTC.vout.size()) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Given claim_script does not match the given Bitcoin transaction.");
}
}
else {
// Look through address book for pegin contract value by extracting the unlderlying witness program from p2sh-p2wpkh
Expand Down Expand Up @@ -3611,6 +3614,17 @@ UniValue createrawpegin(const JSONRPCRequest& request)

CAmount value = txBTC.vout[nOut].nValue;

CDataStream stream(0, 0);
try {
stream << value;
} catch (...) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Amount serialization is invalid.");
}
// Need to reinterpret bytes as unsigned chars before adding to witness
char* buf = stream.data();
unsigned char* membuf = reinterpret_cast<unsigned char*>(buf);
std::vector<unsigned char> value_bytes(membuf, membuf + stream.size());

uint256 genesisBlockHash = Params().ParentGenesisBlockHash();

// Manually construct peg-in transaction, sign it, and send it off.
Expand Down Expand Up @@ -3639,7 +3653,7 @@ UniValue createrawpegin(const JSONRPCRequest& request)
// Construct pegin proof
CScriptWitness pegin_witness;
std::vector<std::vector<unsigned char> >& stack = pegin_witness.stack;
stack.push_back(CScriptNum::serialize(value));
stack.push_back(value_bytes);
stack.push_back(std::vector<unsigned char>(Params().GetConsensus().pegged_asset.begin(), Params().GetConsensus().pegged_asset.end()));
stack.push_back(std::vector<unsigned char>(genesisBlockHash.begin(), genesisBlockHash.end()));
stack.push_back(std::vector<unsigned char>(witnessProgScript.begin(), witnessProgScript.end()));
Expand Down

0 comments on commit 7dc4f9d

Please sign in to comment.