diff --git a/src/address.js b/src/address.js index d7e5c6d..656ec37 100644 --- a/src/address.js +++ b/src/address.js @@ -15,15 +15,6 @@ function fromBase58Check (address) { return { hash: hash, version: version } } -function fromOutputScript (scriptPubKey, network) { - network = network || networks.bitcoin - - if (bscript.isPubKeyHashOutput(scriptPubKey)) return toBase58Check(bscript.compile(scriptPubKey).slice(3, 23), network.pubKeyHash) - if (bscript.isScriptHashOutput(scriptPubKey)) return toBase58Check(bscript.compile(scriptPubKey).slice(2, 22), network.scriptHash) - - throw new Error(bscript.toASM(scriptPubKey) + ' has no matching Address') -} - function toBase58Check (hash, version) { typeforce(types.tuple(types.Hash160bit, types.UInt8), arguments) @@ -34,6 +25,15 @@ function toBase58Check (hash, version) { return bs58check.encode(payload) } +function fromOutputScript (scriptPubKey, network) { + network = network || networks.bitcoin + + if (bscript.isPubKeyHashOutput(scriptPubKey)) return toBase58Check(bscript.compile(scriptPubKey).slice(3, 23), network.pubKeyHash) + if (bscript.isScriptHashOutput(scriptPubKey)) return toBase58Check(bscript.compile(scriptPubKey).slice(2, 22), network.scriptHash) + + throw new Error(bscript.toASM(scriptPubKey) + ' has no matching Address') +} + function toOutputScript (address, network) { network = network || networks.bitcoin diff --git a/src/crypto.js b/src/crypto.js index 3c1cb73..1bb39f1 100644 --- a/src/crypto.js +++ b/src/crypto.js @@ -1,13 +1,5 @@ var createHash = require('create-hash') -function hash160 (buffer) { - return ripemd160(sha256(buffer)) -} - -function hash256 (buffer) { - return sha256(sha256(buffer)) -} - function ripemd160 (buffer) { return createHash('rmd160').update(buffer).digest() } @@ -20,6 +12,14 @@ function sha256 (buffer) { return createHash('sha256').update(buffer).digest() } +function hash160 (buffer) { + return ripemd160(sha256(buffer)) +} + +function hash256 (buffer) { + return sha256(sha256(buffer)) +} + module.exports = { hash160: hash160, hash256: hash256, diff --git a/src/script.js b/src/script.js index 07f8551..1592a39 100644 --- a/src/script.js +++ b/src/script.js @@ -15,32 +15,6 @@ var REVERSE_OPS = (function () { var OP_INT_BASE = OPS.OP_RESERVED // OP_1 - 1 -function toASM (chunks) { - if (Buffer.isBuffer(chunks)) { - chunks = decompile(chunks) - } - - return chunks.map(function (chunk) { - // data? - if (Buffer.isBuffer(chunk)) return chunk.toString('hex') - - // opcode! - return REVERSE_OPS[chunk] - }).join(' ') -} - -function fromASM (asm) { - typeforce(types.String, asm) - - return compile(asm.split(' ').map(function (chunkStr) { - // opcode? - if (OPS[chunkStr] !== undefined) return OPS[chunkStr] - - // data! - return new Buffer(chunkStr, 'hex') - })) -} - function compile (chunks) { // TODO: remove me if (Buffer.isBuffer(chunks)) return chunks @@ -118,6 +92,32 @@ function decompile (buffer) { return chunks } +function toASM (chunks) { + if (Buffer.isBuffer(chunks)) { + chunks = decompile(chunks) + } + + return chunks.map(function (chunk) { + // data? + if (Buffer.isBuffer(chunk)) return chunk.toString('hex') + + // opcode! + return REVERSE_OPS[chunk] + }).join(' ') +} + +function fromASM (asm) { + typeforce(types.String, asm) + + return compile(asm.split(' ').map(function (chunkStr) { + // opcode? + if (OPS[chunkStr] !== undefined) return OPS[chunkStr] + + // data! + return new Buffer(chunkStr, 'hex') + })) +} + function isCanonicalPubKey (buffer) { if (!Buffer.isBuffer(buffer)) return false if (buffer.length < 33) return false @@ -133,13 +133,6 @@ function isCanonicalPubKey (buffer) { return false } -function isCanonicalSignature (buffer) { - if (!Buffer.isBuffer(buffer)) return false - if (!isDefinedHashType(buffer[buffer.length - 1])) return false - - return bip66.check(buffer.slice(0, -1)) -} - function isDefinedHashType (hashType) { var hashTypeMod = hashType & ~0x80 @@ -147,6 +140,13 @@ function isDefinedHashType (hashType) { return hashTypeMod > 0x00 && hashTypeMod < 0x04 } +function isCanonicalSignature (buffer) { + if (!Buffer.isBuffer(buffer)) return false + if (!isDefinedHashType(buffer[buffer.length - 1])) return false + + return bip66.check(buffer.slice(0, -1)) +} + function isPubKeyHashInput (script) { var chunks = decompile(script) diff --git a/src/types.js b/src/types.js index 69ab787..735f770 100644 --- a/src/types.js +++ b/src/types.js @@ -1,7 +1,7 @@ var typeforce = require('typeforce') function nBuffer (value, n) { - typeforce(types.Buffer, value) + typeforce(typeforce.Buffer, value) if (value.length !== n) throw new typeforce.TfTypeError('Expected ' + (n * 8) + '-bit Buffer, got ' + (value.length * 8) + '-bit Buffer') return true