From 3c4f89b6c0612328ef7164ea6324ce9e0017fb6c Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Fri, 13 Nov 2015 14:23:13 -0500 Subject: [PATCH 1/4] Script: Add test data for ASM with pushdata_1 opcode --- test/script/script.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/script/script.js b/test/script/script.js index 2434b7b63..41c8bc536 100644 --- a/test/script/script.js +++ b/test/script/script.js @@ -209,6 +209,11 @@ describe('Script', function() { script.toASM().should.equal('OP_DUP OP_HASH160 f4c03610e60ad15100929cc23da2f3a799af1725 OP_EQUALVERIFY OP_CHECKSIG'); }); + it('should output this known script with pushdata1 opcode as ASM', function() { + var script = Script.fromHex('00483045022100beb1d83771c04faaeb40bded4f031ed0e0730aaab77cf70102ecd05734a1762002206f168fb00f3b9d7c04b8c78e1fc11e81b9caa49885a904bf22780a7e14a8373101483045022100a319839e37828bf164ff45de34a3fe22d542ebc8297c5d87dbc56fc3068ff9d5022077081a877b6e7f104d8a2fe0985bf2eb7de2e08edbac9499fc3710a353f65461014c69522103a70ae7bde64333461fb88aaafe12ad6c67ca17c8213642469ae191e0aabc7251210344a62338c8ddf138771516d38187146242db50853aa588bcb10a5e49c86421a52102b52a1aed304c4d6cedcf82911f90ca6e1ffed0a5b8f7f19c68213d6fcbde677e53ae'); + script.toASM().should.equal('0 3045022100beb1d83771c04faaeb40bded4f031ed0e0730aaab77cf70102ecd05734a1762002206f168fb00f3b9d7c04b8c78e1fc11e81b9caa49885a904bf22780a7e14a8373101 3045022100a319839e37828bf164ff45de34a3fe22d542ebc8297c5d87dbc56fc3068ff9d5022077081a877b6e7f104d8a2fe0985bf2eb7de2e08edbac9499fc3710a353f6546101 522103a70ae7bde64333461fb88aaafe12ad6c67ca17c8213642469ae191e0aabc7251210344a62338c8ddf138771516d38187146242db50853aa588bcb10a5e49c86421a52102b52a1aed304c4d6cedcf82911f90ca6e1ffed0a5b8f7f19c68213d6fcbde677e53ae'); + }); + }); describe('toHex', function() { From f84834f7a8ff679b5cb60fd2bb389c969b821a54 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Fri, 13 Nov 2015 14:25:59 -0500 Subject: [PATCH 2/4] Script: Added comments to test with reference to the txid --- test/script/script.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/script/script.js b/test/script/script.js index 41c8bc536..1ff68cdc2 100644 --- a/test/script/script.js +++ b/test/script/script.js @@ -210,6 +210,8 @@ describe('Script', function() { }); it('should output this known script with pushdata1 opcode as ASM', function() { + // network: livenet + // txid: dd6fabd2d879be7b8394ad170ff908e9a36b5d5d0b394508df0cca36d2931589 var script = Script.fromHex('00483045022100beb1d83771c04faaeb40bded4f031ed0e0730aaab77cf70102ecd05734a1762002206f168fb00f3b9d7c04b8c78e1fc11e81b9caa49885a904bf22780a7e14a8373101483045022100a319839e37828bf164ff45de34a3fe22d542ebc8297c5d87dbc56fc3068ff9d5022077081a877b6e7f104d8a2fe0985bf2eb7de2e08edbac9499fc3710a353f65461014c69522103a70ae7bde64333461fb88aaafe12ad6c67ca17c8213642469ae191e0aabc7251210344a62338c8ddf138771516d38187146242db50853aa588bcb10a5e49c86421a52102b52a1aed304c4d6cedcf82911f90ca6e1ffed0a5b8f7f19c68213d6fcbde677e53ae'); script.toASM().should.equal('0 3045022100beb1d83771c04faaeb40bded4f031ed0e0730aaab77cf70102ecd05734a1762002206f168fb00f3b9d7c04b8c78e1fc11e81b9caa49885a904bf22780a7e14a8373101 3045022100a319839e37828bf164ff45de34a3fe22d542ebc8297c5d87dbc56fc3068ff9d5022077081a877b6e7f104d8a2fe0985bf2eb7de2e08edbac9499fc3710a353f6546101 522103a70ae7bde64333461fb88aaafe12ad6c67ca17c8213642469ae191e0aabc7251210344a62338c8ddf138771516d38187146242db50853aa588bcb10a5e49c86421a52102b52a1aed304c4d6cedcf82911f90ca6e1ffed0a5b8f7f19c68213d6fcbde677e53ae'); }); From 0a052355bbb6ab99e4267d28ad2a968a9a908a99 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Mon, 16 Nov 2015 12:49:31 -0500 Subject: [PATCH 3/4] Script: Account for reverseMap name inconsistencies There are a few cases where the opcode name doesn't match the name exactly. These are mostly related with data pushes, and the cases for `OP_0` and `OP_1NEGATE` were not handled as data pushes as the buf.length was 0. This adds these exceptions to the `_chunkToString` method on Script. References: - https://github.com/bitcoin/bitcoin/blob/e54ebbf6009716a7abcd4d8d3f7bd910e88decdc/src/script/script.cpp#L13 - https://github.com/bitcoin/bitcoin/blob/e54ebbf6009716a7abcd4d8d3f7bd910e88decdc/src/core_write.cpp#L75 --- lib/script/script.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/script/script.js b/lib/script/script.js index 176efdb35..24c31c9d4 100644 --- a/lib/script/script.js +++ b/lib/script/script.js @@ -228,7 +228,21 @@ Script.prototype._chunkToString = function(chunk, type) { if (!chunk.buf) { // no data chunk if (typeof Opcode.reverseMap[opcodenum] !== 'undefined') { - str = str + ' ' + Opcode(opcodenum).toString(); + if (asm) { + // A few cases where the opcode name differs from reverseMap + // aside from 1 to 16 data pushes. + if (opcodenum === 0) { + // OP_0 -> 0 + str = str + ' 0'; + } else if(opcodenum === 79) { + // OP_1NEGATE -> 1 + str = str + ' -1'; + } else { + str = str + ' ' + Opcode(opcodenum).toString(); + } + } else { + str = str + ' ' + Opcode(opcodenum).toString(); + } } else { var numstr = opcodenum.toString(16); if (numstr.length % 2 !== 0) { @@ -242,7 +256,7 @@ Script.prototype._chunkToString = function(chunk, type) { } } else { // data chunk - if (opcodenum === Opcode.OP_PUSHDATA1 || + if (!asm && opcodenum === Opcode.OP_PUSHDATA1 || opcodenum === Opcode.OP_PUSHDATA2 || opcodenum === Opcode.OP_PUSHDATA4) { str = str + ' ' + Opcode(opcodenum).toString(); From 36b781d2eb54c5a4a6fff7e930f4fad355e2f856 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Mon, 16 Nov 2015 13:12:28 -0500 Subject: [PATCH 4/4] Script: Added test for `OP_1NEGATE` -> `-1` for ASM --- test/script/script.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/script/script.js b/test/script/script.js index 1ff68cdc2..1fb5053db 100644 --- a/test/script/script.js +++ b/test/script/script.js @@ -216,6 +216,11 @@ describe('Script', function() { script.toASM().should.equal('0 3045022100beb1d83771c04faaeb40bded4f031ed0e0730aaab77cf70102ecd05734a1762002206f168fb00f3b9d7c04b8c78e1fc11e81b9caa49885a904bf22780a7e14a8373101 3045022100a319839e37828bf164ff45de34a3fe22d542ebc8297c5d87dbc56fc3068ff9d5022077081a877b6e7f104d8a2fe0985bf2eb7de2e08edbac9499fc3710a353f6546101 522103a70ae7bde64333461fb88aaafe12ad6c67ca17c8213642469ae191e0aabc7251210344a62338c8ddf138771516d38187146242db50853aa588bcb10a5e49c86421a52102b52a1aed304c4d6cedcf82911f90ca6e1ffed0a5b8f7f19c68213d6fcbde677e53ae'); }); + it('should OP_1NEGATE opcode as -1 with ASM', function() { + var script = Script.fromString('OP_1NEGATE'); + script.toASM().should.equal('-1'); + }); + }); describe('toHex', function() {