Skip to content

Commit

Permalink
rename scripts to script
Browse files Browse the repository at this point in the history
  • Loading branch information
dcousens committed Aug 20, 2015
1 parent a193ffc commit 54083b6
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 123 deletions.
24 changes: 12 additions & 12 deletions src/address.js
Original file line number Diff line number Diff line change
@@ -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')

Expand All @@ -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) {
Expand All @@ -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')
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ module.exports = {
message: require('./message'),
networks: require('./networks'),
opcodes: require('./opcodes'),
scripts: require('./scripts')
script: require('./script')
}
File renamed without changes.
18 changes: 8 additions & 10 deletions src/transaction.js
Original file line number Diff line number Diff line change
@@ -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')

Expand Down Expand Up @@ -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,
Expand All @@ -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)
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
38 changes: 19 additions & 19 deletions src/transaction_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -12,29 +12,29 @@ 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
}

// pre-empt redeemScript decompilation
var redeemScriptChunks
if (redeemScript) {
redeemScriptChunks = scripts.decompile(redeemScript)
redeemScriptChunks = script.decompile(redeemScript)
}

// Extract hashType, pubKeys and signatures
Expand All @@ -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

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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':
Expand All @@ -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
}
}
Expand All @@ -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)
Expand Down Expand Up @@ -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':
Expand All @@ -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'
}

Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions test/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var assert = require('assert')
var baddress = require('../src/address')
var networks = require('../src/networks')
var scripts = require('../src/scripts')
var bscript = require('../src/script')
var fixtures = require('./fixtures/address.json')

describe('address', function () {
Expand All @@ -29,7 +29,7 @@ describe('address', function () {
describe('fromOutputScript', function () {
fixtures.valid.forEach(function (f) {
it('parses ' + f.script.slice(0, 30) + '... (' + f.network + ')', function () {
var script = scripts.fromASM(f.script)
var script = bscript.fromASM(f.script)
var address = baddress.fromOutputScript(script, networks[f.network])

assert.strictEqual(address, f.base58check)
Expand All @@ -38,7 +38,7 @@ describe('address', function () {

fixtures.invalid.fromOutputScript.forEach(function (f) {
it('throws when ' + f.script.slice(0, 30) + '... ' + f.exception, function () {
var script = scripts.fromASM(f.script)
var script = bscript.fromASM(f.script)

assert.throws(function () {
baddress.fromOutputScript(script)
Expand All @@ -64,7 +64,7 @@ describe('address', function () {
it('exports ' + f.script.slice(0, 30) + '... (' + f.network + ')', function () {
var script = baddress.toOutputScript(f.base58check, network)

assert.strictEqual(scripts.toASM(script), f.script)
assert.strictEqual(bscript.toASM(script), f.script)
})
})

Expand Down
8 changes: 4 additions & 4 deletions test/bitcoin.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ describe('Bitcoin-core', function () {
})
})

describe('scripts.fromASM', function () {
describe('script.fromASM', function () {
tx_valid.forEach(function (f) {
// Objects that are only a single string are ignored
if (f.length === 1) return
Expand All @@ -185,7 +185,7 @@ describe('Bitcoin-core', function () {

it('can parse ' + prevOutScriptPubKey, function () {
// TODO: we can probably do better validation than this
bitcoin.scripts.fromASM(prevOutScriptPubKey)
bitcoin.script.fromASM(prevOutScriptPubKey)
})
})
})
Expand Down Expand Up @@ -218,8 +218,8 @@ describe('Bitcoin-core', function () {
assert.strictEqual(transaction.toHex(), txHex)

var script = new Buffer(scriptHex, 'hex')
var scriptChunks = bitcoin.scripts.decompile(script)
assert.strictEqual(bitcoin.scripts.compile(scriptChunks).toString('hex'), scriptHex)
var scriptChunks = bitcoin.script.decompile(script)
assert.strictEqual(bitcoin.script.compile(scriptChunks).toString('hex'), scriptHex)

var hash = transaction.hashForSignature(inIndex, script, hashType)
assert.deepEqual(hash, expectedHash)
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions test/integration/advanced.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('bitcoinjs-lib (advanced)', function () {

var tx = new bitcoin.TransactionBuilder(network)
var data = new Buffer('bitcoinjs-lib')
var dataScript = bitcoin.scripts.nullDataOutput(data)
var dataScript = bitcoin.script.nullDataOutput(data)

var unspent = unspents.pop()

Expand All @@ -55,7 +55,7 @@ describe('bitcoinjs-lib (advanced)', function () {

var actual = bitcoin.Transaction.fromHex(transaction.txHex)
var dataScript2 = actual.outs[0].script
var data2 = bitcoin.scripts.decompile(dataScript2)[1]
var data2 = bitcoin.script.decompile(dataScript2)[1]

assert.deepEqual(dataScript, dataScript2)
assert.deepEqual(data, data2)
Expand Down
4 changes: 2 additions & 2 deletions test/integration/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ describe('bitcoinjs-lib (crypto)', function () {
inputs.forEach(function (input) {
var transaction = transactions[input.txId]
var script = transaction.ins[input.vout].script
var scriptChunks = bitcoin.scripts.decompile(script)
var scriptChunks = bitcoin.script.decompile(script)

assert(bitcoin.scripts.isPubKeyHashInput(scriptChunks), 'Expected pubKeyHash script')
assert(bitcoin.script.isPubKeyHashInput(scriptChunks), 'Expected pubKeyHash script')

var prevOutTxId = bitcoin.bufferutils.reverse(transaction.ins[input.vout].hash).toString('hex')
var prevVout = transaction.ins[input.vout].index
Expand Down
Loading

0 comments on commit 54083b6

Please sign in to comment.