Skip to content
This repository has been archived by the owner on Mar 31, 2019. It is now read-only.

Commit

Permalink
add OP_RETURN and PUSHDATA support. Fixes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
JBaczuk committed Oct 10, 2018
1 parent d4e6e2f commit 4619cae
Show file tree
Hide file tree
Showing 4 changed files with 296 additions and 7 deletions.
18 changes: 15 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,24 @@ const rOps = require('bitcoin-ops/map')

let parseRawScriptHex = function (rawScript, parsedScript) {
let opStr = rawScript.substring(0,2)
let op = parseInt(`0x${opStr}`)
let op = parseInt(opStr, 16)
// Custom descriptors (not necessarily op codes)
if (op > 0 && op < 76) {
parsedScript += `PUSHDATA(${op})[${rawScript.substring(2, 2+(2*op))}] `
rawScript = rawScript.substring(2+(2*op))
} else {
} else if (op === 76) {
let bytesToPush = parseInt(rawScript.substring(2, 4), 16)
parsedScript += `PUSHDATA(${bytesToPush})[${rawScript.substring(4, 4 + (2 * bytesToPush))}] `
rawScript = rawScript.substring(4 + (2 * bytesToPush))
} else if (op === 77) {
let bytesToPush = parseInt(rawScript.substring(2, 6), 16)
parsedScript += `PUSHDATA(${bytesToPush})[${rawScript.substring(6, 6 + (2 * bytesToPush))}] `
rawScript = rawScript.substring(6 + (2 * bytesToPush))
} else if (op === 78) {
let bytesToPush = parseInt(rawScript.substring(2, 8), 16)
parsedScript += `PUSHDATA(${bytesToPush})[${rawScript.substring(8, 8 + (2 * bytesToPush))}] `
rawScript = rawScript.substring(8 + (2 * bytesToPush))
} else { // TODO: 77 and 78
parsedScript += `${rOps[op]} `
rawScript = rawScript.substring(2)
}
Expand All @@ -21,7 +33,7 @@ let parseRawScriptHex = function (rawScript, parsedScript) {

// TODO: Could also include a dissasembly https://defuse.ca/online-x86-assembler.htm#disassembly2

let parseRawScript = function (rawScript, format) {
let parseRawScript = function (rawScript, format='hex') {
let asm = ''
switch (format) {
case 'hex':
Expand Down
254 changes: 254 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-bitcoin-script",
"version": "1.0.1",
"version": "1.0.2",
"description": "Node Bitcoin Script Parser",
"main": "index.js",
"scripts": {
Expand All @@ -20,7 +20,6 @@
"bitcoin-ops": "^1.4.1"
},
"devDependencies": {
"chai": "^4.1.2",
"mocha": "^5.2.0"
"chai": "^4.1.2"
}
}
26 changes: 25 additions & 1 deletion test/parseTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,34 @@ describe('Parse Tests', function() {
// TODO: P2WSH
// TODO: P2SH(P2WSH)
})

describe('OP_RETURN scriptPubkey', function () {
it('returns expected assembly for less than 76 bytes', function() {
parsedScript = script.parseRawScript('6a13636861726c6579206c6f766573206865696469', 'hex')
expect(parsedScript).to.equal('OP_RETURN PUSHDATA(19)[636861726c6579206c6f766573206865696469]')
})

it('returns expected assembly for OP_PUSHDATA1', function() {
parsedScript = script.parseRawScript('6a4c4f7b6e616d653a5465737479204d63546573746572736f6e2c70686f6e653a2b3434353535353535353535352c656d61696c3a74657374796d63746573746572736f6e3538323140616f6c2e636f6d7d')
expect(parsedScript).to.equal('OP_RETURN PUSHDATA(79)[7b6e616d653a5465737479204d63546573746572736f6e2c70686f6e653a2b3434353535353535353535352c656d61696c3a74657374796d63746573746572736f6e3538323140616f6c2e636f6d7d]')
})

it('returns expected assembly for OP_PUSHDATA2', function() {
parsedScript = script.parseRawScript('6a4d01007b6e616d653a5465737479204d63546573746572736f6e2c70686f6e653a2b3434353535353535353535352c656d61696c3a74657374796d63746573746572736f6e3538323140616f6c2e636f6d7d7b6e616d653a5465737479204d63546573746572736f6e2c70686f6e653a2b3434353535353535353535352c656d61696c3a74657374796d63746573746572736f6e3538323140616f6c2e636f6d7d7b6e616d653a5465737479204d63546573746572736f6e2c70686f6e653a2b3434353535353535353535352c656d61696c3a74657374796d63746573746572736f6e3538323140616f6c2e636f6d7d7b6e616d653a5465737479204d635465737465')
expect(parsedScript).to.equal('OP_RETURN PUSHDATA(256)[7b6e616d653a5465737479204d63546573746572736f6e2c70686f6e653a2b3434353535353535353535352c656d61696c3a74657374796d63746573746572736f6e3538323140616f6c2e636f6d7d7b6e616d653a5465737479204d63546573746572736f6e2c70686f6e653a2b3434353535353535353535352c656d61696c3a74657374796d63746573746572736f6e3538323140616f6c2e636f6d7d7b6e616d653a5465737479204d63546573746572736f6e2c70686f6e653a2b3434353535353535353535352c656d61696c3a74657374796d63746573746572736f6e3538323140616f6c2e636f6d7d7b6e616d653a5465737479204d635465737465]')
})

it('TODO: returns expected assembly for OP_PUSHDATA4', function() {
// TODO: make a test fixture
//parsedScript = script.parseRawScript('6a4e')
//expect(parsedScript).to.equal('OP_RETURN PUSHDATA(65536)[]')
})
})

})

describe('parseAsmScript Tests', function() {
it('returns 0 * 4 = 4', function() {
it('does something', function() {
// TODO
})
})
Expand Down

0 comments on commit 4619cae

Please sign in to comment.