Script: adds toASM/fromASM

This commit is contained in:
Daniel Cousens 2014-06-25 15:44:15 +10:00
parent 7e85515e37
commit 13d41f67e9
5 changed files with 78 additions and 38 deletions

View file

@ -12,6 +12,21 @@ function Script(buffer, chunks) {
}
// Import operations
Script.fromASM = function(asm) {
var strChunks = asm.split(' ')
var chunks = strChunks.map(function(strChunk) {
if (strChunk in opcodes) {
return opcodes[strChunk]
} else {
return new Buffer(strChunk, 'hex')
}
})
return Script.fromChunks(chunks)
}
Script.fromBuffer = function(buffer) {
var chunks = []
@ -90,6 +105,23 @@ Script.prototype.without = function(needle) {
}
// Export operations
var reverseOps = []
for (var op in opcodes) {
var code = opcodes[op]
reverseOps[code] = op
}
Script.prototype.toASM = function() {
return this.chunks.map(function(chunk) {
if (Buffer.isBuffer(chunk)) {
return chunk.toString('hex')
} else {
return reverseOps[chunk]
}
}).join(' ')
}
Script.prototype.toBuffer = function() {
return this.buffer
}

View file

@ -6,7 +6,7 @@
"type": "pubkey",
"hash": "26e645ab170255f2a0a82d29e48f35b14ae7c826",
"pubKey": "031f1e68f82112b373f0fe980b3a89d212d2b5c01fb51eb25acb8b4c4b4299ce95",
"asm": "33 031f1e68f82112b373f0fe980b3a89d212d2b5c01fb51eb25acb8b4c4b4299ce95 OP_CHECKSIG",
"asm": "031f1e68f82112b373f0fe980b3a89d212d2b5c01fb51eb25acb8b4c4b4299ce95 OP_CHECKSIG",
"scriptPubKey": true
},
{
@ -14,7 +14,7 @@
"hex": "a914e8c300c87986efa84c37c0519929019ef86eb5b487",
"type": "scripthash",
"hash": "0ba47b56a573bab4b430ad6ed3ec79270e04b066",
"asm": "OP_HASH160 20 e8c300c87986efa84c37c0519929019ef86eb5b4 OP_EQUAL",
"asm": "OP_HASH160 e8c300c87986efa84c37c0519929019ef86eb5b4 OP_EQUAL",
"scriptPubKey": true
},
{
@ -22,7 +22,7 @@
"hex": "76a9145a3acbc7bbcc97c5ff16f5909c9d7d3fadb293a888ac",
"type": "pubkeyhash",
"hash": "a5313f33d5c7b81674b35f7f3febc3522ef234db",
"asm": "OP_DUP OP_HASH160 20 5a3acbc7bbcc97c5ff16f5909c9d7d3fadb293a8 OP_EQUALVERIFY OP_CHECKSIG",
"asm": "OP_DUP OP_HASH160 5a3acbc7bbcc97c5ff16f5909c9d7d3fadb293a8 OP_EQUALVERIFY OP_CHECKSIG",
"scriptPubKey": true
},
{
@ -30,7 +30,7 @@
"hex": "48304502206becda98cecf7a545d1a640221438ff8912d9b505ede67e0138485111099f696022100ccd616072501310acba10feb97cecc918e21c8e92760cd35144efec7622938f30141040cd2d2ce17a1e9b2b3b2cb294d40eecf305a25b7e7bfdafae6bb2639f4ee399b3637706c3d377ec4ab781355add443ae864b134c5e523001c442186ea60f0eb8",
"type": "pubkeyhash",
"hash": "b9bac2a5c5c29bb27c382d41fa3d179c646c78fd",
"asm": "72 304502206becda98cecf7a545d1a640221438ff8912d9b505ede67e0138485111099f696022100ccd616072501310acba10feb97cecc918e21c8e92760cd35144efec7622938f301 65 040cd2d2ce17a1e9b2b3b2cb294d40eecf305a25b7e7bfdafae6bb2639f4ee399b3637706c3d377ec4ab781355add443ae864b134c5e523001c442186ea60f0eb8",
"asm": "304502206becda98cecf7a545d1a640221438ff8912d9b505ede67e0138485111099f696022100ccd616072501310acba10feb97cecc918e21c8e92760cd35144efec7622938f301 040cd2d2ce17a1e9b2b3b2cb294d40eecf305a25b7e7bfdafae6bb2639f4ee399b3637706c3d377ec4ab781355add443ae864b134c5e523001c442186ea60f0eb8",
"scriptPubKey": false
},
{
@ -39,7 +39,7 @@
"type": "pubkey",
"hash": "44d9982c3e79452e02ef5816976a0e20a0ec1cba",
"signature": "304502206becda98cecf7a545d1a640221438ff8912d9b505ede67e0138485111099f696022100ccd616072501310acba10feb97cecc918e21c8e92760cd35144efec7622938f301",
"asm": "72 304502206becda98cecf7a545d1a640221438ff8912d9b505ede67e0138485111099f696022100ccd616072501310acba10feb97cecc918e21c8e92760cd35144efec7622938f301",
"asm": "304502206becda98cecf7a545d1a640221438ff8912d9b505ede67e0138485111099f696022100ccd616072501310acba10feb97cecc918e21c8e92760cd35144efec7622938f301",
"scriptPubKey": false
},
{
@ -47,7 +47,7 @@
"hex": "5121032487c2a32f7c8d57d2a93906a6457afd00697925b0e6e145d89af6d3bca330162102308673d16987eaa010e540901cc6fe3695e758c19f46ce604e174dac315e685a52ae",
"type": "multisig",
"hash": "f1c98f0b74ecabcf78ae20dfa224bb6666051fbe",
"asm": "OP_TRUE 33 032487c2a32f7c8d57d2a93906a6457afd00697925b0e6e145d89af6d3bca33016 33 02308673d16987eaa010e540901cc6fe3695e758c19f46ce604e174dac315e685a OP_2 OP_CHECKMULTISIG",
"asm": "OP_TRUE 032487c2a32f7c8d57d2a93906a6457afd00697925b0e6e145d89af6d3bca33016 02308673d16987eaa010e540901cc6fe3695e758c19f46ce604e174dac315e685a OP_2 OP_CHECKMULTISIG",
"scriptPubKey": true
},
{
@ -55,15 +55,15 @@
"hex": "0047304402202b29881db1b4cc128442d955e906d41c77365ed9a8392b584be12d980b236459022009da4bc60d09280aa26f4f981bfbed94eb7263d92920961e48a7f3f0991895b101483045022100871708a7597c1dbebff2a5527a56a1f2b49d73e35cd825a07285f5f29f5766d8022003bd7ac25334e9a6d6020cc8ba1be67a8c70dca8e7063ea0547d79c45b9bc12601",
"type": "multisig",
"hash": "b1ef3ae2c77b356eff81049aad7dfd2eeb34c6f5",
"asm": "00 47 304402202b29881db1b4cc128442d955e906d41c77365ed9a8392b584be12d980b236459022009da4bc60d09280aa26f4f981bfbed94eb7263d92920961e48a7f3f0991895b101 48 3045022100871708a7597c1dbebff2a5527a56a1f2b49d73e35cd825a07285f5f29f5766d8022003bd7ac25334e9a6d6020cc8ba1be67a8c70dca8e7063ea0547d79c45b9bc12601",
"asm": "OP_0 304402202b29881db1b4cc128442d955e906d41c77365ed9a8392b584be12d980b236459022009da4bc60d09280aa26f4f981bfbed94eb7263d92920961e48a7f3f0991895b101 3045022100871708a7597c1dbebff2a5527a56a1f2b49d73e35cd825a07285f5f29f5766d8022003bd7ac25334e9a6d6020cc8ba1be67a8c70dca8e7063ea0547d79c45b9bc12601",
"scriptPubKey": false
},
{
"description": "OP_RETURN script",
"hex":"6a2606deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474",
"hex": "6a2606deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474",
"type": "nulldata",
"hash": "ec88f016655477663455fe6a8e83508c348ea145",
"asm": "OP_RETURN 38 06deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474",
"asm": "OP_RETURN 06deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474",
"scriptPubKey": true
},
{
@ -71,7 +71,7 @@
"hex": "aa206fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d619000000000087",
"type": "nonstandard",
"hash": "3823382e70d1930989813d3459988e0d7c2861d8",
"asm": "OP_HASH256 32 6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000 OP_EQUAL",
"asm": "OP_HASH256 6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000 OP_EQUAL",
"scriptPubKey": true
},
{
@ -79,7 +79,7 @@
"hex": "000000ae",
"type": "nonstandard",
"hash": "62ede8963f9387544935f168745262f703dab1fb",
"asm": "0 0 0 OP_CHECKMULTISIG",
"asm": "OP_0 OP_0 OP_0 OP_CHECKMULTISIG",
"scriptPubKey": true
}
]

View file

@ -4,15 +4,15 @@
"type": "pubkey",
"pubKey": "02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1",
"signature": "304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801",
"scriptPubKey": "2102359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1ac",
"scriptSig": "47304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801"
"scriptPubKey": "02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1 OP_CHECKSIG",
"scriptSig": "304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801"
},
{
"type": "pubkeyhash",
"pubKey": "02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1",
"signature": "304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801",
"scriptPubKey": "76a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac",
"scriptSig": "47304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca28012102359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1"
"scriptPubKey": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG",
"scriptSig": "304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801 02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1"
},
{
"type": "multisig",
@ -24,8 +24,8 @@
"304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801",
"3045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d140501"
],
"scriptPubKey": "522102359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1210395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a52ae",
"scriptSig": "0047304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801483045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d140501"
"scriptPubKey": "OP_2 02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1 0395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a OP_2 OP_CHECKMULTISIG",
"scriptSig": "OP_0 304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801 3045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d140501"
},
{
"type": "multisig",
@ -39,15 +39,15 @@
"3044022001ab168e80b863fdec694350b587339bb72a37108ac3c989849251444d13ebba02201811272023e3c1038478eb972a82d3ad431bfc2408e88e4da990f1a7ecbb263901",
"3045022100aaeb7204c17eee2f2c4ff1c9f8b39b79e75e7fbf33e92cc67ac51be8f15b75f90220659eee314a4943a6384d2b154fa5821ef7a084814d7ee2c6f9f7f0ffb53be34b01"
],
"scriptPubKey": "53210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f817982102b80011a883a0fd621ad46dfc405df1e74bf075cbaf700fd4aebef6e96f84834021024289801366bcee6172b771cf5a7f13aaecd237a0b9a1ff9d769cabc2e6b70a3453ae",
"scriptSig": "00483045022100fe324541215798b2df68cbd44039615e23c506d4ec1a05572064392a98196b82022068c849fa6699206da2fc6d7848efc1d3804a5816d6293615fe34c1a7f34e1c2f01473044022001ab168e80b863fdec694350b587339bb72a37108ac3c989849251444d13ebba02201811272023e3c1038478eb972a82d3ad431bfc2408e88e4da990f1a7ecbb263901483045022100aaeb7204c17eee2f2c4ff1c9f8b39b79e75e7fbf33e92cc67ac51be8f15b75f90220659eee314a4943a6384d2b154fa5821ef7a084814d7ee2c6f9f7f0ffb53be34b01"
"scriptPubKey": "OP_3 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 02b80011a883a0fd621ad46dfc405df1e74bf075cbaf700fd4aebef6e96f848340 024289801366bcee6172b771cf5a7f13aaecd237a0b9a1ff9d769cabc2e6b70a34 OP_3 OP_CHECKMULTISIG",
"scriptSig": "OP_0 3045022100fe324541215798b2df68cbd44039615e23c506d4ec1a05572064392a98196b82022068c849fa6699206da2fc6d7848efc1d3804a5816d6293615fe34c1a7f34e1c2f01 3044022001ab168e80b863fdec694350b587339bb72a37108ac3c989849251444d13ebba02201811272023e3c1038478eb972a82d3ad431bfc2408e88e4da990f1a7ecbb263901 3045022100aaeb7204c17eee2f2c4ff1c9f8b39b79e75e7fbf33e92cc67ac51be8f15b75f90220659eee314a4943a6384d2b154fa5821ef7a084814d7ee2c6f9f7f0ffb53be34b01"
},
{
"type": "scripthash",
"redeemScript": "522102359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1210395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a52ae",
"redeemScriptSig": "0047304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801483045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d140501",
"scriptSig": "0047304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801483045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d14050147522102359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1210395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a52ae",
"scriptPubKey": "a914722ff0bc2c3f47b35c20df646c395594da24e90e87"
"redeemScript": "OP_2 02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1 0395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a OP_2 OP_CHECKMULTISIG",
"redeemScriptSig": "OP_0 304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801 3045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d140501",
"scriptSig": "OP_0 304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801 3045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d140501 522102359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1210395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a52ae",
"scriptPubKey": "OP_HASH160 722ff0bc2c3f47b35c20df646c395594da24e90e OP_EQUAL"
}
],
"invalid": {

View file

@ -21,6 +21,14 @@ describe('Script', function() {
})
})
describe('fromASM/toASM', function() {
fixtures.valid.forEach(function(f) {
it('decodes/encodes ' + f.description, function() {
assert.equal(Script.fromASM(f.asm).toASM(), f.asm)
})
})
})
describe('fromHex/toHex', function() {
fixtures.valid.forEach(function(f) {
it('decodes/encodes ' + f.description, function() {

View file

@ -13,7 +13,7 @@ describe('Scripts', function() {
if (!f.scriptSig) return
it('classifies ' + f.scriptSig + ' as ' + f.type, function() {
var script = Script.fromHex(f.scriptSig)
var script = Script.fromASM(f.scriptSig)
var type = scripts.classifyInput(script)
assert.equal(type, f.type)
@ -26,7 +26,7 @@ describe('Scripts', function() {
if (!f.scriptPubKey) return
it('classifies ' + f.scriptPubKey + ' as ' + f.type, function() {
var script = Script.fromHex(f.scriptPubKey)
var script = Script.fromASM(f.scriptPubKey)
var type = scripts.classifyOutput(script)
assert.equal(type, f.type)
@ -43,7 +43,7 @@ describe('Scripts', function() {
var signature = new Buffer(f.signature, 'hex')
var scriptSig = scripts.pubKeyInput(signature)
assert.equal(scriptSig.toHex(), f.scriptSig)
assert.equal(scriptSig.toASM(), f.scriptSig)
})
})
@ -52,7 +52,7 @@ describe('Scripts', function() {
var pubKey = ECPubKey.fromHex(f.pubKey)
var scriptPubKey = scripts.pubKeyOutput(pubKey)
assert.equal(scriptPubKey.toHex(), f.scriptPubKey)
assert.equal(scriptPubKey.toASM(), f.scriptPubKey)
})
})
})
@ -70,14 +70,14 @@ describe('Scripts', function() {
var signature = new Buffer(f.signature, 'hex')
var scriptSig = scripts.pubKeyHashInput(signature, pubKey)
assert.equal(scriptSig.toHex(), f.scriptSig)
assert.equal(scriptSig.toASM(), f.scriptSig)
})
})
describe('output script', function() {
it('is generated correctly for ' + address, function() {
var scriptPubKey = scripts.pubKeyHashOutput(address.hash)
assert.equal(scriptPubKey.toHex(), f.scriptPubKey)
assert.equal(scriptPubKey.toASM(), f.scriptPubKey)
})
})
})
@ -91,19 +91,19 @@ describe('Scripts', function() {
var scriptPubKey = scripts.multisigOutput(pubKeys.length, pubKeys)
describe('input script', function() {
it('is generated correctly for ' + scriptPubKey.toHex(), function() {
it('is generated correctly for ' + scriptPubKey.toASM(), function() {
var signatures = f.signatures.map(function(signature) {
return new Buffer(signature, 'hex')
})
var scriptSig = scripts.multisigInput(signatures)
assert.equal(scriptSig.toHex(), f.scriptSig)
assert.equal(scriptSig.toASM(), f.scriptSig)
})
})
describe('output script', function() {
it('is generated correctly for ' + scriptPubKey.toHex(), function() {
assert.equal(scriptPubKey.toHex(), f.scriptPubKey)
it('is generated correctly for ' + scriptPubKey.toASM(), function() {
assert.equal(scriptPubKey.toASM(), f.scriptPubKey)
})
})
})
@ -140,16 +140,16 @@ describe('Scripts', function() {
fixtures.valid.forEach(function(f) {
if (f.type !== 'scripthash') return
var redeemScript = Script.fromHex(f.redeemScript)
var redeemScriptSig = Script.fromHex(f.redeemScriptSig)
var redeemScript = Script.fromASM(f.redeemScript)
var redeemScriptSig = Script.fromASM(f.redeemScriptSig)
var address = Address.fromOutputScript(Script.fromHex(f.scriptPubKey))
var address = Address.fromOutputScript(Script.fromASM(f.scriptPubKey))
describe('input script', function() {
it('is generated correctly for ' + address, function() {
var scriptSig = scripts.scriptHashInput(redeemScriptSig, redeemScript)
assert.equal(scriptSig.toHex(), f.scriptSig)
assert.equal(scriptSig.toASM(), f.scriptSig)
})
})
@ -157,7 +157,7 @@ describe('Scripts', function() {
it('is generated correctly for ' + address, function() {
var scriptPubKey = scripts.scriptHashOutput(redeemScript.getHash())
assert.equal(scriptPubKey.toHex(), f.scriptPubKey)
assert.equal(scriptPubKey.toASM(), f.scriptPubKey)
})
})
})