Skip to content

Commit 7dc4f9d

Browse files
committed
remove CNumScript from pegin witness logic
1 parent abafbac commit 7dc4f9d

4 files changed

Lines changed: 32 additions & 11 deletions

File tree

qa/rpc-tests/pegging.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def sync_all(sidechain, sidechain2):
153153
pegtxid = sidechain.claimpegin(raw, proof, sidechain.getnewaddress())
154154
raise Exception("Peg-in with non-matching claim_script should fail.")
155155
except JSONRPCException as e:
156-
assert("Given claim_script is not a valid v0 witness program" in e.error["message"])
156+
assert("Given claim_script does not match the given Bitcoin transaction." in e.error["message"])
157157
pass
158158

159159
# 12 confirms allows in mempool

src/script/script.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -325,12 +325,6 @@ class CScriptNum
325325
return m_value;
326326
}
327327

328-
// Only used for peg-in witness values
329-
int64_t getint64() const
330-
{
331-
return m_value;
332-
}
333-
334328
std::vector<unsigned char> getvch() const
335329
{
336330
return serialize(m_value);

src/validation.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2375,8 +2375,17 @@ bool IsValidPeginWitness(const CScriptWitness& pegin_witness, const COutPoint& p
23752375
return false;
23762376
}
23772377

2378-
// Get output value. Special 8 byte length to capture possible bitcoin values
2379-
CAmount value = CScriptNum(stack[0], true, 8).getint64();
2378+
CDataStream stream(stack[0], SER_NETWORK, PROTOCOL_VERSION);
2379+
CAmount value;
2380+
try {
2381+
stream >> value;
2382+
} catch (...) {
2383+
return false;
2384+
}
2385+
2386+
if (!MoneyRange(value)) {
2387+
return false;
2388+
}
23802389

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

24742483
// Constructs unblinded output to be used in amount and scriptpubkey checks during pegin
24752484
CTxOut GetPeginOutputFromWitness(const CScriptWitness& pegin_witness) {
2476-
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()));
2485+
CDataStream stream(pegin_witness.stack[0], SER_NETWORK, PROTOCOL_VERSION);
2486+
CAmount value;
2487+
stream >> value;
2488+
2489+
return CTxOut(CAsset(pegin_witness.stack[1]), value, CScript(pegin_witness.stack[3].begin(), pegin_witness.stack[3].end()));
24772490
}
24782491

24792492
// Protected by cs_main

src/wallet/rpcwallet.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3579,6 +3579,9 @@ UniValue createrawpegin(const JSONRPCRequest& request)
35793579
throw JSONRPCError(RPC_INVALID_PARAMETER, "Given claim_script is not a valid v0 witness program.");
35803580
}
35813581
nOut = GetPeginTxnOutputIndex(txBTC, witnessProgScript);
3582+
if (nOut == txBTC.vout.size()) {
3583+
throw JSONRPCError(RPC_INVALID_PARAMETER, "Given claim_script does not match the given Bitcoin transaction.");
3584+
}
35823585
}
35833586
else {
35843587
// Look through address book for pegin contract value by extracting the unlderlying witness program from p2sh-p2wpkh
@@ -3611,6 +3614,17 @@ UniValue createrawpegin(const JSONRPCRequest& request)
36113614

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

3617+
CDataStream stream(0, 0);
3618+
try {
3619+
stream << value;
3620+
} catch (...) {
3621+
throw JSONRPCError(RPC_INVALID_PARAMETER, "Amount serialization is invalid.");
3622+
}
3623+
// Need to reinterpret bytes as unsigned chars before adding to witness
3624+
char* buf = stream.data();
3625+
unsigned char* membuf = reinterpret_cast<unsigned char*>(buf);
3626+
std::vector<unsigned char> value_bytes(membuf, membuf + stream.size());
3627+
36143628
uint256 genesisBlockHash = Params().ParentGenesisBlockHash();
36153629

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

0 commit comments

Comments
 (0)