Skip to content

Commit

Permalink
Merge bitcoin#230: Fix short names in ASM decode
Browse files Browse the repository at this point in the history
0d7f3f6 Correcting the instance where the prior opcode is a OP_NAME*, but the referenced name is shorter than 5 bytes. As in issue#140, this converts the asm decode into interpreting the name value as an integer by accident. (Midnight Magic)

Pull request description:

  Very short names should still be decoded as strings and not numbers when they are part of a name script.

  This is a rebased and squashed version of bitcoin#162, fixing bitcoin#140.

Tree-SHA512: 904518a1f4492356f97d5cd6ca983f4ffb7a41db784d5f169d537bd37a57bfcadae470bf15f6938732806f12ef37895dba6fd5c765154206032350431dc17de3
  • Loading branch information
domob1812 committed Jun 25, 2018
2 parents 67d9a00 + 0d7f3f6 commit f9b96c9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/core_write.cpp
Expand Up @@ -83,6 +83,7 @@ std::string ScriptToAsmStr(const CScript& script, const bool fAttemptSighashDeco
{
std::string str;
opcodetype opcode;
opcodetype lastOpcode = OP_0;
std::vector<unsigned char> vch;
CScript::const_iterator pc = script.begin();
while (pc < script.end()) {
Expand All @@ -95,7 +96,14 @@ std::string ScriptToAsmStr(const CScript& script, const bool fAttemptSighashDeco
}
if (0 <= opcode && opcode <= OP_PUSHDATA4) {
if (vch.size() <= static_cast<std::vector<unsigned char>::size_type>(4)) {
str += strprintf("%d", CScriptNum(vch, false).getint());
if ((lastOpcode == OP_NAME_NEW
|| lastOpcode == OP_NAME_UPDATE
|| lastOpcode == OP_NAME_FIRSTUPDATE)
&& !vch.empty()) {
str += HexStr(vch);
} else {
str += strprintf("%d", CScriptNum(vch, false).getint());
}
} else {
// the IsUnspendable check makes sure not to try to decode OP_RETURN data that may match the format of a signature
if (fAttemptSighashDecode && !script.IsUnspendable()) {
Expand All @@ -119,6 +127,7 @@ std::string ScriptToAsmStr(const CScript& script, const bool fAttemptSighashDeco
} else {
str += GetOpName(opcode);
}
lastOpcode = opcode;
}
return str;
}
Expand Down
5 changes: 5 additions & 0 deletions test/functional/rpc_decodescript.py
Expand Up @@ -192,6 +192,11 @@ def decoderawtransaction_asm_sighashtype(self):
assert_equal('OP_DUP OP_HASH160 3011020701010101010101020601010101010101 OP_EQUALVERIFY OP_CHECKSIG', rpc_result['vout'][0]['scriptPubKey']['asm'])
assert_equal('OP_HASH160 3011020701010101010101020601010101010101 OP_EQUAL', rpc_result['vout'][1]['scriptPubKey']['asm'])

# verify that names shorter than 5 bytes aren't interpreted into asm as integers (issue #140)
tx = '0071000002c323b5df3dbd501ef8e26683749fd13f785eac6d4f4c2083196a33262605e6ec010000008b48304502207cc397996cf41a4be7ce229226c2e3862557256e372dd325dfefaba26a4a44a20221008e71fc315563d04d7d92d6e2b0ef9c5e095df50777664819623156dcf3ec1c1e0141045bc24b6e33ecbe341e8195b4e8fcb3f8c7937ca6f671f799644968dc00e82d0160c5e14b687cba83840381dda04711939a225588eb0e2401d4f14dc9d3a5ccdaffffffff882be8eb700e1c6eaef2682839293ae9d5bfacf82cee80510d4dee6eed359c98000000004847304402206c6115eb47b4f3010a6c0e79ea5ee7f036fc1da22af172c4bd39a460758bb34402204cc40302837a3a61d9b1e12a99e73572f95da7b5005e5ce842a7b8b4ba90d7f901ffffffff02e007646300000000434104fb090d04c8f110012a9f4545b6cb1f93a6f22db1b35bef7e1545a4a0aef0124cd25c522851ba2120f12b124785adc6f09b13f8eaa294cfe54a601dc75f11df29ac40420f00000000005a5303662f6a394269746d65737361676520616464726573733a20424d2d3263554755687335436a6973526975514756574447334a514a47717a5967713134356d7576a9147368feca713f2a9d7780343b74007e26a4fcfcea88ac00000000'
rpc_result = self.nodes[0].decoderawtransaction(tx)
assert_equal('OP_NAME_UPDATE 662f6a 4269746d65737361676520616464726573733a20424d2d3263554755687335436a6973526975514756574447334a514a47717a596771313435 OP_2DROP OP_DROP OP_DUP OP_HASH160 7368feca713f2a9d7780343b74007e26a4fcfcea OP_EQUALVERIFY OP_CHECKSIG', rpc_result['vout'][1]['scriptPubKey']['asm'])

# some more full transaction tests of varying specific scriptSigs. used instead of
# tests in decodescript_script_sig because the decodescript RPC is specifically
# for working on scriptPubKeys (argh!).
Expand Down

0 comments on commit f9b96c9

Please sign in to comment.