coerce wherever necessary for now

This commit is contained in:
Daniel Cousens 2015-08-14 10:26:24 +10:00
parent 2aff7bd899
commit 812d74aa0a
2 changed files with 16 additions and 3 deletions

View file

@ -20,8 +20,9 @@ function fromBase58Check (address) {
function fromOutputScript (script, network) { function fromOutputScript (script, network) {
network = network || networks.bitcoin network = network || networks.bitcoin
if (scripts.isPubKeyHashOutput(script)) return toBase58Check(script[2], network.pubKeyHash) var chunks = Script.decompile(script)
if (scripts.isScriptHashOutput(script)) return toBase58Check(script[1], network.scriptHash) if (scripts.isPubKeyHashOutput(chunks)) return toBase58Check(chunks[2], network.pubKeyHash)
if (scripts.isScriptHashOutput(chunks)) return toBase58Check(chunks[1], network.scriptHash)
throw new Error(Script.toASM(script) + ' has no matching Address') throw new Error(Script.toASM(script) + ' has no matching Address')
} }

View file

@ -4,7 +4,15 @@ var typeforce = require('typeforce')
var types = require('./types') var types = require('./types')
function coerceChunks (chunks) { function coerceChunks (chunks) {
return types.Array(chunks) ? chunks : decompile(chunks) if (types.Array(chunks)) return chunks
return decompile(chunks)
}
function coerceBuffer (buffer) {
if (types.Buffer(buffer)) return buffer
return compile(buffer)
} }
function toASM (chunks) { function toASM (chunks) {
@ -41,6 +49,8 @@ function fromASM (asm) {
} }
function compile (chunks) { function compile (chunks) {
chunks = coerceChunks(chunks)
typeforce(types.Array, chunks) typeforce(types.Array, chunks)
var bufferSize = chunks.reduce(function (accum, chunk) { var bufferSize = chunks.reduce(function (accum, chunk) {
@ -76,6 +86,8 @@ function compile (chunks) {
} }
function decompile (buffer) { function decompile (buffer) {
buffer = coerceBuffer(buffer)
typeforce(types.Buffer, buffer) typeforce(types.Buffer, buffer)
var chunks = [] var chunks = []