From 5eec04d3bbfa53d4f444b07a55b181cee12b638e Mon Sep 17 00:00:00 2001 From: Long Li Date: Wed, 9 Oct 2024 17:18:24 +0900 Subject: [PATCH 1/4] Fixed Script.fromASM support PUSHDATA --- packages/bitcore-lib/lib/script/script.js | 21 ++++++++++++++++----- packages/bitcore-lib/test/script/script.js | 19 +++++++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/packages/bitcore-lib/lib/script/script.js b/packages/bitcore-lib/lib/script/script.js index 07f5ea8ffa2..92f5dd66e7d 100644 --- a/packages/bitcore-lib/lib/script/script.js +++ b/packages/bitcore-lib/lib/script/script.js @@ -157,12 +157,23 @@ Script.fromASM = function(str) { var opcode = Opcode(token); var opcodenum = opcode.toNumber(); - if (opcodenum == null) { + if (_.isUndefined(opcodenum)) { var buf = Buffer.from(tokens[i], 'hex'); + if (buf.length < 0x4c){ + opcodenum=buf.length + }else if (buf.length <= 0x100) { + opcodenum=Opcode.OP_PUSHDATA1 + }else if(buf.length <= 0x10000){ + opcodenum=Opcode.OP_PUSHDATA2 + }else if(buf.length <= 0x100000000){ + opcodenum=Opcode.OP_PUSHDATA4 + }else{ + throw new Error('Pushdata data is too long'); + } script.chunks.push({ buf: buf, len: buf.length, - opcodenum: buf.length + opcodenum: opcodenum }); i = i + 1; } else if (opcodenum === Opcode.OP_PUSHDATA1 || @@ -952,7 +963,7 @@ Script.buildWitnessV1Out = function(to, scriptTree) { to = Address.fromString(to); } } - + function buildTree(tree) { if (Array.isArray(tree)) { const [left, leftH] = buildTree(tree[0]); @@ -980,11 +991,11 @@ Script.buildWitnessV1Out = function(to, scriptTree) { } let taggedHash = null; - if (scriptTree) { + if (scriptTree) { const [_, h] = buildTree(scriptTree); taggedHash = h; } - + let tweakedPubKey; if (to instanceof PublicKey) { tweakedPubKey = to.createTapTweak(taggedHash).tweakedPubKey; diff --git a/packages/bitcore-lib/test/script/script.js b/packages/bitcore-lib/test/script/script.js index 6bb3e622c65..30b7acbf3ca 100644 --- a/packages/bitcore-lib/test/script/script.js +++ b/packages/bitcore-lib/test/script/script.js @@ -205,6 +205,25 @@ describe('Script', function() { }); }); + describe('#fromASM PUSHDATA', function() { + it('should parse this known script in ASM', function() { + const data0 = '01'.repeat(0x4b) + const data1 = '01'.repeat(0x100) + const data2 = '01'.repeat(0x10000) + const data3 = '01'.repeat(0x10000+1) + var asm = `${data0} ${data1} ${data2} ${data3}`; + var script = Script.fromASM(asm); + console.log("🚀 ~ it ~ script:", script) + script.chunks[0].opcodenum.should.equal(0x4b); + script.chunks[1].opcodenum.should.equal(Opcode.OP_PUSHDATA1); + script.chunks[1].len.should.equal(0x100); + script.chunks[2].opcodenum.should.equal(Opcode.OP_PUSHDATA2); + script.chunks[2].len.should.equal(0x10000); + script.chunks[3].opcodenum.should.equal(Opcode.OP_PUSHDATA4); + script.chunks[3].len.should.equal(0x10000+1); + }); + }); + describe('#fromString', function() { it('should parse these known scripts', function() { From 28e2706faacb384f9d3b6a4cfc76fbe65db3ba33 Mon Sep 17 00:00:00 2001 From: Long Li Date: Thu, 10 Oct 2024 10:46:03 +0900 Subject: [PATCH 2/4] Modify Script PushData test code --- packages/bitcore-lib/test/script/script.js | 29 ++++++++++++---------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/packages/bitcore-lib/test/script/script.js b/packages/bitcore-lib/test/script/script.js index 30b7acbf3ca..1503f345d43 100644 --- a/packages/bitcore-lib/test/script/script.js +++ b/packages/bitcore-lib/test/script/script.js @@ -203,24 +203,27 @@ describe('Script', function() { script.chunks[3].opcodenum.should.equal(Opcode.OP_EQUALVERIFY); script.chunks[4].opcodenum.should.equal(Opcode.OP_CHECKSIG); }); - }); - describe('#fromASM PUSHDATA', function() { - it('should parse this known script in ASM', function() { + it('should parse OP_PUSHDATA in ASM', function() { const data0 = '01'.repeat(0x4b) - const data1 = '01'.repeat(0x100) - const data2 = '01'.repeat(0x10000) - const data3 = '01'.repeat(0x10000+1) - var asm = `${data0} ${data1} ${data2} ${data3}`; + const data1 = '01'.repeat(0x4c) + const data2 = '01'.repeat(0x100) + const data3 = '01'.repeat(0x100+1) + const data4 = '01'.repeat(0x10000) + const data5 = '01'.repeat(0x10000+1) + var asm = `${data0} ${data1} ${data2} ${data3} ${data4} ${data5}`; var script = Script.fromASM(asm); - console.log("🚀 ~ it ~ script:", script) script.chunks[0].opcodenum.should.equal(0x4b); script.chunks[1].opcodenum.should.equal(Opcode.OP_PUSHDATA1); - script.chunks[1].len.should.equal(0x100); - script.chunks[2].opcodenum.should.equal(Opcode.OP_PUSHDATA2); - script.chunks[2].len.should.equal(0x10000); - script.chunks[3].opcodenum.should.equal(Opcode.OP_PUSHDATA4); - script.chunks[3].len.should.equal(0x10000+1); + script.chunks[1].len.should.equal(0x4c); + script.chunks[2].opcodenum.should.equal(Opcode.OP_PUSHDATA1); + script.chunks[2].len.should.equal(0x100); + script.chunks[3].opcodenum.should.equal(Opcode.OP_PUSHDATA2); + script.chunks[3].len.should.equal(0x100+1); + script.chunks[4].opcodenum.should.equal(Opcode.OP_PUSHDATA2); + script.chunks[4].len.should.equal(0x10000); + script.chunks[5].opcodenum.should.equal(Opcode.OP_PUSHDATA4); + script.chunks[5].len.should.equal(0x10000+1); }); }); From 8454815e013ff89222545793a3a2fb00970432ff Mon Sep 17 00:00:00 2001 From: Long Li Date: Thu, 10 Oct 2024 10:48:03 +0900 Subject: [PATCH 3/4] rollack Script file indent --- packages/bitcore-lib/lib/script/script.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/bitcore-lib/lib/script/script.js b/packages/bitcore-lib/lib/script/script.js index 92f5dd66e7d..4d0ca41c7ea 100644 --- a/packages/bitcore-lib/lib/script/script.js +++ b/packages/bitcore-lib/lib/script/script.js @@ -963,6 +963,7 @@ Script.buildWitnessV1Out = function(to, scriptTree) { to = Address.fromString(to); } } + function buildTree(tree) { if (Array.isArray(tree)) { @@ -991,11 +992,11 @@ Script.buildWitnessV1Out = function(to, scriptTree) { } let taggedHash = null; - if (scriptTree) { + if (scriptTree) { const [_, h] = buildTree(scriptTree); taggedHash = h; } - + let tweakedPubKey; if (to instanceof PublicKey) { tweakedPubKey = to.createTapTweak(taggedHash).tweakedPubKey; From ad2071d50c45b56a927a471670cf3da37956d4ee Mon Sep 17 00:00:00 2001 From: Long Li Date: Thu, 10 Oct 2024 11:37:14 +0900 Subject: [PATCH 4/4] JS will not provide strings longer than 2^30, so there is no need to throw an exception. Signed-off-by: Long Li --- packages/bitcore-lib/lib/script/script.js | 2 -- packages/bitcore-lib/test/script/script.js | 5 ++++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/bitcore-lib/lib/script/script.js b/packages/bitcore-lib/lib/script/script.js index 4d0ca41c7ea..fe6a213e9ad 100644 --- a/packages/bitcore-lib/lib/script/script.js +++ b/packages/bitcore-lib/lib/script/script.js @@ -167,8 +167,6 @@ Script.fromASM = function(str) { opcodenum=Opcode.OP_PUSHDATA2 }else if(buf.length <= 0x100000000){ opcodenum=Opcode.OP_PUSHDATA4 - }else{ - throw new Error('Pushdata data is too long'); } script.chunks.push({ buf: buf, diff --git a/packages/bitcore-lib/test/script/script.js b/packages/bitcore-lib/test/script/script.js index 1503f345d43..b4371016712 100644 --- a/packages/bitcore-lib/test/script/script.js +++ b/packages/bitcore-lib/test/script/script.js @@ -211,7 +211,8 @@ describe('Script', function() { const data3 = '01'.repeat(0x100+1) const data4 = '01'.repeat(0x10000) const data5 = '01'.repeat(0x10000+1) - var asm = `${data0} ${data1} ${data2} ${data3} ${data4} ${data5}`; + const data6 = '01'.repeat(1) + var asm = `${data0} ${data1} ${data2} ${data3} ${data4} ${data5} ${data6}`; var script = Script.fromASM(asm); script.chunks[0].opcodenum.should.equal(0x4b); script.chunks[1].opcodenum.should.equal(Opcode.OP_PUSHDATA1); @@ -224,6 +225,8 @@ describe('Script', function() { script.chunks[4].len.should.equal(0x10000); script.chunks[5].opcodenum.should.equal(Opcode.OP_PUSHDATA4); script.chunks[5].len.should.equal(0x10000+1); + script.chunks[6].opcodenum.should.equal(1); + script.chunks[6].len.should.equal(1); }); });