Skip to content

Commit

Permalink
Include PUSHDATA opcode in Script.toString(). Also, smallNums are pri…
Browse files Browse the repository at this point in the history
…nted in their decoded form.
  • Loading branch information
schildbach authored and mikehearn committed May 22, 2014
1 parent c236ae4 commit ff8d76c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
8 changes: 6 additions & 2 deletions core/src/main/java/com/google/bitcoin/script/Script.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,15 @@ public String toString() {
if (chunk.isOpCode()) {
buf.append(getOpCodeName(chunk.opcode));
buf.append(" ");
} else {
} else if (chunk.data != null) {
// Data chunk
buf.append(getPushDataName(chunk.opcode));
buf.append("[");
buf.append(chunk.data != null ? bytesToHexString(chunk.data) : "null");
buf.append(bytesToHexString(chunk.data));
buf.append("] ");
} else {
// Small num
buf.append(decodeFromOpN(chunk.opcode));
}
}
return buf.toString().trim();
Expand Down
10 changes: 10 additions & 0 deletions core/src/main/java/com/google/bitcoin/script/ScriptOpCodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,16 @@ public static String getOpCodeName(int opcode) {
return "NON_OP(" + opcode + ")";
}

/**
* Converts the given pushdata OpCode into a string (eg "PUSHDATA2", or "PUSHDATA(23)")
*/
public static String getPushDataName(int opcode) {
if (opCodeMap.containsKey(opcode))
return opCodeMap.get(opcode);

return "PUSHDATA(" + opcode + ")";
}

/**
* Converts the given OpCodeName into an int
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void testScriptPubKey() throws Exception {
// Check we can extract the to address
byte[] pubkeyBytes = Hex.decode(pubkeyProg);
Script pubkey = new Script(pubkeyBytes);
assertEquals("DUP HASH160 [33e81a941e64cda12c6a299ed322ddbdd03f8d0e] EQUALVERIFY CHECKSIG", pubkey.toString());
assertEquals("DUP HASH160 PUSHDATA(20)[33e81a941e64cda12c6a299ed322ddbdd03f8d0e] EQUALVERIFY CHECKSIG", pubkey.toString());
Address toAddr = new Address(params, pubkey.getPubKeyHash());
assertEquals("mkFQohBpy2HDXrCwyMrYL5RtfrmeiuuPY2", toAddr.toString());
}
Expand Down

0 comments on commit ff8d76c

Please sign in to comment.