Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Script.fromASM support PUSHDATA #3807

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions packages/bitcore-lib/lib/script/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,23 @@ Script.fromASM = function(str) {
var opcode = Opcode(token);
var opcodenum = opcode.toNumber();

if (opcodenum == null) {
if (_.isUndefined(opcodenum)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency and for linting, would you mind just copying the code from the bitcore-lib-cash script.js? Feel free to clean it up a little (don't need to re-declare opcodenum, for example), but your code here has some inconsistent formatting/linting with the rest of the codebase.

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 ||
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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;
Expand Down
19 changes: 19 additions & 0 deletions packages/bitcore-lib/test/script/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,25 @@ describe('Script', function() {
});
});

describe('#fromASM PUSHDATA', function() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is already a #fromASM describe in this file, and bitcore-lib-cash already has some pushdata tests. Would you please copy those tests to the existing #fromASM describe?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

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() {
Expand Down