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

Commit

Permalink
add support for P2PK, P2SH, and P2SH(P2WPKH)
Browse files Browse the repository at this point in the history
  • Loading branch information
JBaczuk committed Sep 25, 2018
1 parent 828cd1c commit c02888f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
Binary file modified .index.js.swp
Binary file not shown.
12 changes: 2 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,25 @@ const ops = require('bitcoin-ops')
const rOps = require('bitcoin-ops/map')

let parseRawScriptHex = function (rawScript, parsedScript) {
console.debug(`rawScript: ${rawScript}`)
let opStr = rawScript.substring(0,2)
let op = parseInt(`0x${opStr}`)
// Custom descriptors (not necessarily op codes)
if (op > 0 && op < 76) {
console.debug(`op: ${op}`)
parsedScript += `PUSHDATA(${op})[${rawScript.substring(2, 2+(2*op))}] `
console.debug(`parsedScript: ${parsedScript}`)
rawScript = rawScript.substring(2+(2*op))
} else {
console.debug(`op: ${op}`)
parsedScript += `${rOps[op]} `
console.debug(`parsedScript: ${parsedScript}`)
rawScript = rawScript.substring(2)
}
console.debug(`rawScript.length: ${rawScript.length}`)
if (rawScript.length > 0) {
return parseRawScriptHex(rawScript, parsedScript)
} else {
console.debug(`done. parsedScript: ${parsedScript}`)
console.debug(`parsedScript.length: ${parsedScript.length}`)
console.debug(`final: ${parsedScript.substring(0, parsedScript.length -1)}`)
return parsedScript.substring(0, parsedScript.length -1)
}
}

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

let parseRawScript = function (rawScript, format) {
let asm = ''
switch (format) {
Expand All @@ -37,7 +30,6 @@ let parseRawScript = function (rawScript, format) {
return
}
asm = parseRawScriptHex(rawScript, asm)
console.debug(`asm: ${asm}`)
break
default:
console.error('Unrecognized format: ' + format)
Expand Down
Binary file modified test/.parseTests.js.swp
Binary file not shown.
40 changes: 32 additions & 8 deletions test/parseTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,39 @@ const bop = require('../index.js')

describe('Parse Tests', function() {
describe('parseRawScript Tests', function() {
it('returns ', function() {
parsedScript = bop.parseRawScript('a914c664139327b98043febeab6434eba89bb196d1af87', 'hex')
expect(parsedScript).to.equal('OP_HASH160 PUSHDATA(20)[c664139327b98043febeab6434eba89bb196d1af] OP_EQUAL')
});
});
describe('P2PK (Genesis Block) scriptPubkey', function () {
it('returns expected assembly', function() {
parsedScript = bop.parseRawScript('410483a2eb1d9ae027fe47c8ec88e6d9e6a3903f5d0e5602fa232a0fe221dae256b17f8c6207229c6261a6659c12c59265a3299cbb0e45d180d15d7226f29323894dac', 'hex')
// TODO: Could indicate the type of transaction. E.g. P2PK: <asm>
expect(parsedScript).to.equal('PUSHDATA(65)[0483a2eb1d9ae027fe47c8ec88e6d9e6a3903f5d0e5602fa232a0fe221dae256b17f8c6207229c6261a6659c12c59265a3299cbb0e45d180d15d7226f29323894d] OP_CHECKSIG')
})
})

// TODO: P2PKH

describe('P2SH scriptPubkey', function () {
it('returns expected assembly', function() {
parsedScript = bop.parseRawScript('a914c664139327b98043febeab6434eba89bb196d1af87', 'hex')
expect(parsedScript).to.equal('OP_HASH160 PUSHDATA(20)[c664139327b98043febeab6434eba89bb196d1af] OP_EQUAL')
})
})

describe('P2SH(P2WPKH) scriptSig', function () {
it('returns expected assembly', function() {
parsedScript = bop.parseRawScript('160014d3530760601a3af53c7c93b01dfaf08d1ab156e0', 'hex')
expect(parsedScript).to.equal('PUSHDATA(22)[0014d3530760601a3af53c7c93b01dfaf08d1ab156e0]')
// TODO: Maybe show the redeem script, e.g.
// expect(parsedScript).to.equal('PUSHDATA(22)[SEGWIT_VERSION_0(P2WPKH) PUSHDATA(20)[d3530760601a3af53c7c93b01dfaf08d1ab156e0]]')
})
// See https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#P2WPKH
// TODO: P2WSH
// TODO: P2SH(P2WSH)
})
})

describe('parseAsmScript Tests', function() {
it('returns 0 * 4 = 4', function() {
// TODO
});
});
});
})
})
})

0 comments on commit c02888f

Please sign in to comment.