Daniel Cousens
10 years ago
14 changed files with 365 additions and 403 deletions
@ -1,16 +1,16 @@ |
|||
module.exports = { |
|||
Address: require('./address'), |
|||
Block: require('./block'), |
|||
bufferutils: require('./bufferutils'), |
|||
crypto: require('./crypto'), |
|||
ECPair: require('./ecpair'), |
|||
ECSignature: require('./ecsignature'), |
|||
message: require('./message'), |
|||
opcodes: require('./opcodes'), |
|||
HDNode: require('./hdnode'), |
|||
Script: require('./script'), |
|||
scripts: require('./scripts'), |
|||
Transaction: require('./transaction'), |
|||
TransactionBuilder: require('./transaction_builder'), |
|||
networks: require('./networks') |
|||
|
|||
bufferutils: require('./bufferutils'), |
|||
crypto: require('./crypto'), |
|||
message: require('./message'), |
|||
networks: require('./networks'), |
|||
opcodes: require('./opcodes'), |
|||
scripts: require('./scripts') |
|||
} |
|||
|
@ -1,129 +0,0 @@ |
|||
var bufferutils = require('./bufferutils') |
|||
var opcodes = require('./opcodes') |
|||
var typeforce = require('typeforce') |
|||
var types = require('./types') |
|||
|
|||
function toASM (chunks) { |
|||
if (types.Buffer(chunks)) { |
|||
chunks = decompile(chunks) |
|||
} |
|||
|
|||
return chunks.map(function (chunk) { |
|||
// data chunk
|
|||
if (Buffer.isBuffer(chunk)) { |
|||
return chunk.toString('hex') |
|||
|
|||
// opcode
|
|||
} else { |
|||
return reverseOps[chunk] |
|||
} |
|||
}).join(' ') |
|||
} |
|||
|
|||
function fromASM (asm) { |
|||
typeforce(types.String, asm) |
|||
|
|||
var strChunks = asm.split(' ') |
|||
var chunks = strChunks.map(function (strChunk) { |
|||
// opcode
|
|||
if (strChunk in opcodes) { |
|||
return opcodes[strChunk] |
|||
|
|||
// data chunk
|
|||
} else { |
|||
return new Buffer(strChunk, 'hex') |
|||
} |
|||
}) |
|||
|
|||
return compile(chunks) |
|||
} |
|||
|
|||
function compile (chunks) { |
|||
// TODO: remove me
|
|||
if (types.Buffer(chunks)) return chunks |
|||
|
|||
typeforce(types.Array, chunks) |
|||
|
|||
var bufferSize = chunks.reduce(function (accum, chunk) { |
|||
// data chunk
|
|||
if (Buffer.isBuffer(chunk)) { |
|||
return accum + bufferutils.pushDataSize(chunk.length) + chunk.length |
|||
} |
|||
|
|||
// opcode
|
|||
return accum + 1 |
|||
}, 0.0) |
|||
|
|||
var buffer = new Buffer(bufferSize) |
|||
var offset = 0 |
|||
|
|||
chunks.forEach(function (chunk) { |
|||
// data chunk
|
|||
if (Buffer.isBuffer(chunk)) { |
|||
offset += bufferutils.writePushDataInt(buffer, chunk.length, offset) |
|||
|
|||
chunk.copy(buffer, offset) |
|||
offset += chunk.length |
|||
|
|||
// opcode
|
|||
} else { |
|||
buffer.writeUInt8(chunk, offset) |
|||
offset += 1 |
|||
} |
|||
}) |
|||
|
|||
if (offset !== buffer.length) throw new Error('Could not decode chunks') |
|||
return buffer |
|||
} |
|||
|
|||
function decompile (buffer) { |
|||
// TODO: remove me
|
|||
if (types.Array(buffer)) return buffer |
|||
|
|||
typeforce(types.Buffer, buffer) |
|||
|
|||
var chunks = [] |
|||
var i = 0 |
|||
|
|||
while (i < buffer.length) { |
|||
var opcode = buffer.readUInt8(i) |
|||
|
|||
// data chunk
|
|||
if ((opcode > opcodes.OP_0) && (opcode <= opcodes.OP_PUSHDATA4)) { |
|||
var d = bufferutils.readPushDataInt(buffer, i) |
|||
|
|||
// did reading a pushDataInt fail? empty script
|
|||
if (d === null) return [] |
|||
i += d.size |
|||
|
|||
// attempt to read too much data? empty script
|
|||
if (i + d.number > buffer.length) return [] |
|||
|
|||
var data = buffer.slice(i, i + d.number) |
|||
i += d.number |
|||
|
|||
chunks.push(data) |
|||
|
|||
// opcode
|
|||
} else { |
|||
chunks.push(opcode) |
|||
|
|||
i += 1 |
|||
} |
|||
} |
|||
|
|||
return chunks |
|||
} |
|||
|
|||
var reverseOps = [] |
|||
for (var op in opcodes) { |
|||
var code = opcodes[op] |
|||
reverseOps[code] = op |
|||
} |
|||
|
|||
module.exports = { |
|||
compile: compile, |
|||
decompile: decompile, |
|||
fromASM: fromASM, |
|||
toASM: toASM |
|||
} |
@ -1,96 +0,0 @@ |
|||
{ |
|||
"valid": [ |
|||
{ |
|||
"asm": "031f1e68f82112b373f0fe980b3a89d212d2b5c01fb51eb25acb8b4c4b4299ce95 OP_CHECKSIG", |
|||
"description": "pay-to-PubKey", |
|||
"hash": "26e645ab170255f2a0a82d29e48f35b14ae7c826", |
|||
"hex": "21031f1e68f82112b373f0fe980b3a89d212d2b5c01fb51eb25acb8b4c4b4299ce95ac", |
|||
"pubKey": "031f1e68f82112b373f0fe980b3a89d212d2b5c01fb51eb25acb8b4c4b4299ce95" |
|||
}, |
|||
{ |
|||
"asm": "OP_HASH160 e8c300c87986efa84c37c0519929019ef86eb5b4 OP_EQUAL", |
|||
"description": "P2SH ScriptPubKey", |
|||
"hash": "0ba47b56a573bab4b430ad6ed3ec79270e04b066", |
|||
"hex": "a914e8c300c87986efa84c37c0519929019ef86eb5b487" |
|||
}, |
|||
{ |
|||
"asm": "OP_DUP OP_HASH160 5a3acbc7bbcc97c5ff16f5909c9d7d3fadb293a8 OP_EQUALVERIFY OP_CHECKSIG", |
|||
"description": "PubKeyHash ScriptPubKey", |
|||
"hash": "a5313f33d5c7b81674b35f7f3febc3522ef234db", |
|||
"hex": "76a9145a3acbc7bbcc97c5ff16f5909c9d7d3fadb293a888ac" |
|||
}, |
|||
{ |
|||
"asm": "304502206becda98cecf7a545d1a640221438ff8912d9b505ede67e0138485111099f696022100ccd616072501310acba10feb97cecc918e21c8e92760cd35144efec7622938f301 040cd2d2ce17a1e9b2b3b2cb294d40eecf305a25b7e7bfdafae6bb2639f4ee399b3637706c3d377ec4ab781355add443ae864b134c5e523001c442186ea60f0eb8", |
|||
"description": "pubKeyHash scriptSig", |
|||
"hash": "b9bac2a5c5c29bb27c382d41fa3d179c646c78fd", |
|||
"hex": "48304502206becda98cecf7a545d1a640221438ff8912d9b505ede67e0138485111099f696022100ccd616072501310acba10feb97cecc918e21c8e92760cd35144efec7622938f30141040cd2d2ce17a1e9b2b3b2cb294d40eecf305a25b7e7bfdafae6bb2639f4ee399b3637706c3d377ec4ab781355add443ae864b134c5e523001c442186ea60f0eb8" |
|||
}, |
|||
{ |
|||
"asm": "304502206becda98cecf7a545d1a640221438ff8912d9b505ede67e0138485111099f696022100ccd616072501310acba10feb97cecc918e21c8e92760cd35144efec7622938f301", |
|||
"description": "pubKey scriptSig", |
|||
"hash": "44d9982c3e79452e02ef5816976a0e20a0ec1cba", |
|||
"hex": "48304502206becda98cecf7a545d1a640221438ff8912d9b505ede67e0138485111099f696022100ccd616072501310acba10feb97cecc918e21c8e92760cd35144efec7622938f301", |
|||
"signature": "304502206becda98cecf7a545d1a640221438ff8912d9b505ede67e0138485111099f696022100ccd616072501310acba10feb97cecc918e21c8e92760cd35144efec7622938f301" |
|||
}, |
|||
{ |
|||
"asm": "OP_TRUE 032487c2a32f7c8d57d2a93906a6457afd00697925b0e6e145d89af6d3bca33016 02308673d16987eaa010e540901cc6fe3695e758c19f46ce604e174dac315e685a OP_2 OP_CHECKMULTISIG", |
|||
"description": "Valid multisig script", |
|||
"hash": "f1c98f0b74ecabcf78ae20dfa224bb6666051fbe", |
|||
"hex": "5121032487c2a32f7c8d57d2a93906a6457afd00697925b0e6e145d89af6d3bca330162102308673d16987eaa010e540901cc6fe3695e758c19f46ce604e174dac315e685a52ae" |
|||
}, |
|||
{ |
|||
"asm": "OP_0 304402202b29881db1b4cc128442d955e906d41c77365ed9a8392b584be12d980b236459022009da4bc60d09280aa26f4f981bfbed94eb7263d92920961e48a7f3f0991895b101 3045022100871708a7597c1dbebff2a5527a56a1f2b49d73e35cd825a07285f5f29f5766d8022003bd7ac25334e9a6d6020cc8ba1be67a8c70dca8e7063ea0547d79c45b9bc12601", |
|||
"description": "mutisig scriptSig", |
|||
"hash": "b1ef3ae2c77b356eff81049aad7dfd2eeb34c6f5", |
|||
"hex": "0047304402202b29881db1b4cc128442d955e906d41c77365ed9a8392b584be12d980b236459022009da4bc60d09280aa26f4f981bfbed94eb7263d92920961e48a7f3f0991895b101483045022100871708a7597c1dbebff2a5527a56a1f2b49d73e35cd825a07285f5f29f5766d8022003bd7ac25334e9a6d6020cc8ba1be67a8c70dca8e7063ea0547d79c45b9bc12601" |
|||
}, |
|||
{ |
|||
"asm": "OP_RETURN 06deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474", |
|||
"description": "OP_RETURN script", |
|||
"hash": "ec88f016655477663455fe6a8e83508c348ea145", |
|||
"hex": "6a2606deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474" |
|||
}, |
|||
{ |
|||
"asm": "OP_HASH256 6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000 OP_EQUAL", |
|||
"description": "Non standard script", |
|||
"hash": "3823382e70d1930989813d3459988e0d7c2861d8", |
|||
"hex": "aa206fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d619000000000087" |
|||
}, |
|||
{ |
|||
"asm": "OP_0 OP_0 OP_0 OP_CHECKMULTISIG", |
|||
"description": "Invalid multisig script", |
|||
"hash": "62ede8963f9387544935f168745262f703dab1fb", |
|||
"hex": "000000ae" |
|||
}, |
|||
{ |
|||
"asm": "", |
|||
"description": "Not enough data: OP_1", |
|||
"hash": "c51b66bced5e4491001bd702669770dccf440982", |
|||
"hex": "01" |
|||
}, |
|||
{ |
|||
"asm": "", |
|||
"description": "Not enough data: OP_2", |
|||
"hash": "d48ce86c698f246829921ba9fb2a844ae2adba67", |
|||
"hex": "0201" |
|||
}, |
|||
{ |
|||
"asm": "", |
|||
"description": "Not enough data: OP_PUSHDATA1 0x02", |
|||
"hash": "b663ef01a96ff65bec84a3fb14688d6ff7fc617c", |
|||
"hex": "4c0201" |
|||
}, |
|||
{ |
|||
"asm": "", |
|||
"description": "Not enough data: OP_PUSHDATA2 0xffff", |
|||
"hash": "b4d2fac2836232e59d7b1628f64f24bce3cb4478", |
|||
"hex": "4dffff01" |
|||
}, |
|||
{ |
|||
"asm": "", |
|||
"description": "Not enough data: OP_PUSHDATA4 0xffffffff", |
|||
"hash": "941db1ca32faf29e1338fb966bb56d98fbce4823", |
|||
"hex": "4effffffff01" |
|||
} |
|||
] |
|||
} |
@ -1,43 +0,0 @@ |
|||
/* global describe, it */ |
|||
/* eslint-disable no-new */ |
|||
|
|||
var assert = require('assert') |
|||
var Script = require('../src/script') |
|||
|
|||
var fixtures = require('./fixtures/script.json') |
|||
|
|||
describe('Script', function () { |
|||
describe('fromASM/toASM', function () { |
|||
fixtures.valid.forEach(function (f) { |
|||
if (!f.asm) return |
|||
|
|||
it('decodes/encodes ' + f.description, function () { |
|||
var script = Script.fromASM(f.asm) |
|||
|
|||
assert.strictEqual(Script.toASM(script), f.asm) |
|||
}) |
|||
}) |
|||
}) |
|||
|
|||
describe('compile', function () { |
|||
fixtures.valid.forEach(function (f) { |
|||
if (!f.asm) return |
|||
|
|||
it('decodes/encodes ' + f.description, function () { |
|||
var script = Script.fromASM(f.asm) |
|||
|
|||
assert.strictEqual(Script.compile(script).toString('hex'), f.hex) |
|||
}) |
|||
}) |
|||
}) |
|||
|
|||
describe('decompile', function () { |
|||
fixtures.valid.forEach(function (f) { |
|||
it('decodes/encodes ' + f.description, function () { |
|||
var script = Script.decompile(new Buffer(f.hex, 'hex')) |
|||
|
|||
assert.strictEqual(Script.toASM(script), f.asm) |
|||
}) |
|||
}) |
|||
}) |
|||
}) |
Loading…
Reference in new issue