rename scripts to script

This commit is contained in:
Daniel Cousens 2015-08-20 13:31:29 +10:00
parent 18e9cdcc02
commit 6ec687deba
13 changed files with 120 additions and 123 deletions

View file

@ -1,11 +1,11 @@
var base58check = require('bs58check')
var bs58check = require('bs58check')
var script = require('./script')
var networks = require('./networks')
var scripts = require('./scripts')
var typeforce = require('typeforce')
var types = require('./types')
function fromBase58Check (address) {
var payload = base58check.decode(address)
var payload = bs58check.decode(address)
if (payload.length < 21) throw new TypeError(address + ' is too short')
if (payload.length > 21) throw new TypeError(address + ' is too long')
@ -15,14 +15,14 @@ function fromBase58Check (address) {
return { hash: hash, version: version }
}
function fromOutputScript (script, network) {
function fromOutputScript (scriptPubKey, network) {
network = network || networks.bitcoin
var chunks = scripts.decompile(script)
if (scripts.isPubKeyHashOutput(chunks)) return toBase58Check(chunks[2], network.pubKeyHash)
if (scripts.isScriptHashOutput(chunks)) return toBase58Check(chunks[1], network.scriptHash)
var chunks = script.decompile(scriptPubKey)
if (script.isPubKeyHashOutput(chunks)) return toBase58Check(chunks[2], network.pubKeyHash)
if (script.isScriptHashOutput(chunks)) return toBase58Check(chunks[1], network.scriptHash)
throw new Error(scripts.toASM(chunks) + ' has no matching Address')
throw new Error(script.toASM(chunks) + ' has no matching Address')
}
function toBase58Check (hash, version) {
@ -32,21 +32,21 @@ function toBase58Check (hash, version) {
payload.writeUInt8(version, 0)
hash.copy(payload, 1)
return base58check.encode(payload)
return bs58check.encode(payload)
}
function toOutputScript (address, network) {
network = network || networks.bitcoin
var payload = base58check.decode(address)
var payload = bs58check.decode(address)
if (payload.length < 21) throw new TypeError(address + ' is too short')
if (payload.length > 21) throw new TypeError(address + ' is too long')
var version = payload.readUInt8(0)
var hash = payload.slice(1)
if (version === network.pubKeyHash) return scripts.pubKeyHashOutput(hash)
if (version === network.scriptHash) return scripts.scriptHashOutput(hash)
if (version === network.pubKeyHash) return script.pubKeyHashOutput(hash)
if (version === network.scriptHash) return script.scriptHashOutput(hash)
throw new Error(address + ' has no matching Script')
}

View file

@ -12,5 +12,5 @@ module.exports = {
message: require('./message'),
networks: require('./networks'),
opcodes: require('./opcodes'),
scripts: require('./scripts')
script: require('./script')
}

View file

@ -1,7 +1,7 @@
var bufferutils = require('./bufferutils')
var crypto = require('./crypto')
var opcodes = require('./opcodes')
var scripts = require('./scripts')
var script = require('./script')
var typeforce = require('typeforce')
var types = require('./types')
@ -99,7 +99,7 @@ Transaction.isCoinbaseHash = function (buffer) {
var EMPTY_SCRIPT = new Buffer(0)
Transaction.prototype.addInput = function (hash, index, sequence, script) {
Transaction.prototype.addInput = function (hash, index, sequence, scriptSig) {
typeforce(types.tuple(
types.Hash256bit,
types.UInt32,
@ -111,13 +111,11 @@ Transaction.prototype.addInput = function (hash, index, sequence, script) {
sequence = Transaction.DEFAULT_SEQUENCE
}
script = script || EMPTY_SCRIPT
// Add the input and return the input's index
return (this.ins.push({
hash: hash,
index: index,
script: script,
script: scriptSig || EMPTY_SCRIPT,
sequence: sequence
}) - 1)
}
@ -133,8 +131,8 @@ Transaction.prototype.addOutput = function (scriptPubKey, value) {
}
Transaction.prototype.byteLength = function () {
function scriptSize (script) {
var length = script.length
function scriptSize (someScript) {
var length = someScript.length
return bufferutils.varIntSize(length) + length
}
@ -193,7 +191,7 @@ Transaction.prototype.hashForSignature = function (inIndex, prevOutScript, hashT
// in case concatenating two scripts ends up with two codeseparators,
// or an extra one at the end, this prevents all those possible incompatibilities.
var hashScript = scripts.compile(scripts.decompile(prevOutScript).filter(function (x) {
var hashScript = script.compile(script.decompile(prevOutScript).filter(function (x) {
return x !== opcodes.OP_CODESEPARATOR
}))
var i
@ -320,10 +318,10 @@ Transaction.prototype.toHex = function () {
return this.toBuffer().toString('hex')
}
Transaction.prototype.setInputScript = function (index, script) {
Transaction.prototype.setInputScript = function (index, scriptSig) {
typeforce(types.tuple(types.Number, types.Buffer), arguments)
this.ins[index].script = script
this.ins[index].script = scriptSig
}
module.exports = Transaction

View file

@ -3,7 +3,7 @@ var bcrypto = require('./crypto')
var bufferutils = require('./bufferutils')
var networks = require('./networks')
var ops = require('./opcodes')
var scripts = require('./scripts')
var script = require('./script')
var ECPair = require('./ecpair')
var ECSignature = require('./ecsignature')
@ -12,21 +12,21 @@ var Transaction = require('./transaction')
function extractInput (txIn) {
var redeemScript
var scriptSig = txIn.script
var scriptSigChunks = scripts.decompile(scriptSig)
var scriptSigChunks = script.decompile(scriptSig)
var prevOutScript
var prevOutType = scripts.classifyInput(scriptSig, true)
var prevOutType = script.classifyInput(scriptSig, true)
var scriptType
// Re-classify if scriptHash
if (prevOutType === 'scripthash') {
redeemScript = scriptSigChunks.slice(-1)[0]
prevOutScript = scripts.scriptHashOutput(bcrypto.hash160(redeemScript))
prevOutScript = script.scriptHashOutput(bcrypto.hash160(redeemScript))
scriptSig = scripts.compile(scriptSigChunks.slice(0, -1))
scriptSig = script.compile(scriptSigChunks.slice(0, -1))
scriptSigChunks = scriptSigChunks.slice(0, -1)
scriptType = scripts.classifyInput(scriptSig, true)
scriptType = script.classifyInput(scriptSig, true)
} else {
scriptType = prevOutType
}
@ -34,7 +34,7 @@ function extractInput (txIn) {
// pre-empt redeemScript decompilation
var redeemScriptChunks
if (redeemScript) {
redeemScriptChunks = scripts.decompile(redeemScript)
redeemScriptChunks = script.decompile(redeemScript)
}
// Extract hashType, pubKeys and signatures
@ -46,7 +46,7 @@ function extractInput (txIn) {
hashType = parsed.hashType
pubKeys = scriptSigChunks.slice(1)
signatures = [parsed.signature]
prevOutScript = scripts.pubKeyHashOutput(bcrypto.hash160(pubKeys[0]))
prevOutScript = script.pubKeyHashOutput(bcrypto.hash160(pubKeys[0]))
break
@ -147,8 +147,8 @@ TransactionBuilder.prototype.addInput = function (txHash, vout, sequence, prevOu
var input = {}
if (prevOutScript) {
var prevOutScriptChunks = scripts.decompile(prevOutScript)
var prevOutType = scripts.classifyOutput(prevOutScriptChunks)
var prevOutScriptChunks = script.decompile(prevOutScript)
var prevOutType = script.classifyOutput(prevOutScriptChunks)
// if we can, extract pubKey information
switch (prevOutType) {
@ -240,7 +240,7 @@ TransactionBuilder.prototype.__build = function (allowIncomplete) {
switch (scriptType) {
case 'pubkeyhash':
var pkhSignature = input.signatures[0].toScriptSignature(input.hashType)
scriptSig = scripts.pubKeyHashInput(pkhSignature, input.pubKeys[0])
scriptSig = script.pubKeyHashInput(pkhSignature, input.pubKeys[0])
break
case 'multisig':
@ -262,12 +262,12 @@ TransactionBuilder.prototype.__build = function (allowIncomplete) {
}
var redeemScript = allowIncomplete ? undefined : input.redeemScript
scriptSig = scripts.multisigInput(msSignatures, redeemScript)
scriptSig = script.multisigInput(msSignatures, redeemScript)
break
case 'pubkey':
var pkSignature = input.signatures[0].toScriptSignature(input.hashType)
scriptSig = scripts.pubKeyInput(pkSignature)
scriptSig = script.pubKeyInput(pkSignature)
break
}
}
@ -276,7 +276,7 @@ TransactionBuilder.prototype.__build = function (allowIncomplete) {
if (scriptSig) {
// wrap as scriptHash if necessary
if (input.prevOutType === 'scripthash') {
scriptSig = scripts.scriptHashInput(scriptSig, input.redeemScript)
scriptSig = script.scriptHashInput(scriptSig, input.redeemScript)
}
tx.setInputScript(index, scriptSig)
@ -318,14 +318,14 @@ TransactionBuilder.prototype.sign = function (index, keyPair, redeemScript, hash
if (input.prevOutScript) {
if (input.prevOutType !== 'scripthash') throw new Error('PrevOutScript must be P2SH')
var scriptHash = scripts.decompile(input.prevOutScript)[1]
var scriptHash = script.decompile(input.prevOutScript)[1]
if (!bufferutils.equal(scriptHash, bcrypto.hash160(redeemScript))) throw new Error('RedeemScript does not match ' + scriptHash.toString('hex'))
}
var scriptType = scripts.classifyOutput(redeemScript)
var scriptType = script.classifyOutput(redeemScript)
if (!canSignTypes[scriptType]) throw new Error('RedeemScript not supported (' + scriptType + ')')
var redeemScriptChunks = scripts.decompile(redeemScript)
var redeemScriptChunks = script.decompile(redeemScript)
var pubKeys = []
switch (scriptType) {
case 'multisig':
@ -347,7 +347,7 @@ TransactionBuilder.prototype.sign = function (index, keyPair, redeemScript, hash
// if we don't have a prevOutScript, generate a P2SH script
if (!input.prevOutScript) {
input.prevOutScript = scripts.scriptHashOutput(bcrypto.hash160(redeemScript))
input.prevOutScript = script.scriptHashOutput(bcrypto.hash160(redeemScript))
input.prevOutType = 'scripthash'
}
@ -365,7 +365,7 @@ TransactionBuilder.prototype.sign = function (index, keyPair, redeemScript, hash
// we know nothin' Jon Snow, assume pubKeyHash
} else {
input.prevOutScript = scripts.pubKeyHashOutput(bcrypto.hash160(keyPair.getPublicKeyBuffer()))
input.prevOutScript = script.pubKeyHashOutput(bcrypto.hash160(keyPair.getPublicKeyBuffer()))
input.prevOutType = 'pubkeyhash'
input.pubKeys = [kpPubKey]
input.scriptType = input.prevOutType