From c02888f5ed6f4efd1a602b83134c7f89bb4fe020 Mon Sep 17 00:00:00 2001 From: Jordan Baczuk Date: Tue, 25 Sep 2018 19:02:48 +0000 Subject: [PATCH] add support for P2PK, P2SH, and P2SH(P2WPKH) --- .index.js.swp | Bin 12288 -> 12288 bytes index.js | 12 ++---------- test/.parseTests.js.swp | Bin 12288 -> 12288 bytes test/parseTests.js | 40 ++++++++++++++++++++++++++++++++-------- 4 files changed, 34 insertions(+), 18 deletions(-) diff --git a/.index.js.swp b/.index.js.swp index 6582b8de4f367db2514b0308979b1140f6f781a0..5fef1e51e57ae9fd1c4a24d3e238a59cf507136f 100644 GIT binary patch delta 453 zcmYk%ze_?<6bJBQ8Chm(3PIB)m$Ke_5|p9@h7|~!L+R{OU-;mu(NoKoB%)u`bcce* zXvihPf1oLXAZQCJqoJiOLTU;6_G9S4hjZ?|=fJtuWHsqXc@p6$^NAip*oZtEwKK=3 z)gaaFwl}Ngs)OjL^Y=;OcGkN(VC2IF2ke;IIr_g@a69)~H2!ypLItef1^23eBWTx>GtXQ4t#%P?8f3rx+Y zwNgf5GRvsCF6&AzrBztASX|Kkl9W;AOS&SaWvP(Y)Vv}r`^E%W|LIZ|#cVO>!r0$M jWrRnDV*Enr;p@Y3?i5CORdC@P;z2ROPsKyNByE2I4~KIn delta 782 zcmb8rJ4nM&6b9f^@KNg{m5NpriS_M6a+UFt3uJmC9#cKXp`6n3PtQ@q2nDK z)k(xfDT1p`ZVn=D;w%ng9TogTL2amt7d{eh&iM}~IVmSOmi>!UAwDSj3CB+4JUcpd zymOX7vDIGUnqTUP9bbOpzdhagzsx|!$__FxkxpdIXR+eCB* zSx{jZo*IdEVG-tG7DCVoR(P=y-NPX)*l3Q%@#2TK2BI6dfD=fA3`;Nr(=Y{tFaUkv z0uG+AlCf6FTC~)>*0UXVAJaW`Y;UNIJ$TN$Bbu7h;))nmmNTnvNlqn3`Ti^(ThdUm z;0;^Ygb;G^IHt;#m@3DOT3t@2lqi#Z4z?~hx6wyNxp7P_<;rj*8Pn5ZTv1okYhM8s z*S~~BHB}M$?@<*QwaDI*SYAksn5HM?jfxpYoB0lN*|RVzm|B0FRvyNSLQj=-HQgAY i=|kLOZ=FOukZn1kKv&J diff --git a/index.js b/index.js index 0a13801..85dba8e 100644 --- a/index.js +++ b/index.js @@ -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) { @@ -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) diff --git a/test/.parseTests.js.swp b/test/.parseTests.js.swp index 3063ecb7bd78e08b724656dd15f047f71ae7d0d3..cedb6e9e8c1fce1e05529e9bb095476a6388f891 100644 GIT binary patch literal 12288 zcmeI2&u<$=6vwBP5KUR*LLb!ZkwPY6f`4!J@(GqnKz$#Gb?MK zVAof#($HiC%aepW^Vv6B@0@*>+){+x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H;b&mD%g4Zw3dHe190kOYK&%DC%s~7esQM)kKY{AJ38Y(r;`KmW0>lA8tO~^P zKr8~pAoYiNH!BJ_@@m&=S~GBh2`I^#lAi|?gwYI~Aj!#o8u=VR)nJ8_Cu+A$meh&m Z$t=-OFG?*bEy^oam|UfyFu6#-4*&=@FysIL diff --git a/test/parseTests.js b/test/parseTests.js index 4dfd122..d6851c7 100644 --- a/test/parseTests.js +++ b/test/parseTests.js @@ -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: + 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 - }); - }); -}); + }) + }) +})