use standardjs formatting

This commit is contained in:
Daniel Cousens 2015-02-23 10:36:57 +11:00
commit 399803affa
41 changed files with 1252 additions and 1177 deletions

View file

@ -1,3 +1,5 @@
/* global describe, it, beforeEach */
var assert = require('assert')
var scripts = require('../src/scripts')
@ -8,19 +10,18 @@ var Script = require('../src/script')
var fixtures = require('./fixtures/transaction')
describe('Transaction', function() {
function fromRaw(raw) {
describe('Transaction', function () {
function fromRaw (raw) {
var tx = new Transaction()
tx.version = raw.version
tx.locktime = raw.locktime
raw.ins.forEach(function(txIn) {
raw.ins.forEach(function (txIn) {
var txHash = new Buffer(txIn.hash, 'hex')
var script
if (txIn.data) {
script = new Script(new Buffer(txIn.data, 'hex'), [])
} else if (txIn.script) {
script = Script.fromASM(txIn.script)
}
@ -28,34 +29,34 @@ describe('Transaction', function() {
tx.addInput(txHash, txIn.index, txIn.sequence, script)
})
raw.outs.forEach(function(txOut) {
raw.outs.forEach(function (txOut) {
tx.addOutput(Script.fromASM(txOut.script), txOut.value)
})
return tx
}
describe('fromBuffer/fromHex', function() {
fixtures.valid.forEach(function(f) {
it('imports ' + f.id + ' correctly', function() {
describe('fromBuffer/fromHex', function () {
fixtures.valid.forEach(function (f) {
it('imports ' + f.id + ' correctly', function () {
var actual = Transaction.fromHex(f.hex)
assert.deepEqual(actual.toHex(), f.hex)
})
})
fixtures.invalid.fromBuffer.forEach(function(f) {
it('throws on ' + f.exception, function() {
assert.throws(function() {
fixtures.invalid.fromBuffer.forEach(function (f) {
it('throws on ' + f.exception, function () {
assert.throws(function () {
Transaction.fromHex(f.hex)
}, new RegExp(f.exception))
})
})
})
describe('toBuffer/toHex', function() {
fixtures.valid.forEach(function(f) {
it('exports ' + f.id + ' correctly', function() {
describe('toBuffer/toHex', function () {
fixtures.valid.forEach(function (f) {
it('exports ' + f.id + ' correctly', function () {
var actual = fromRaw(f.raw)
assert.deepEqual(actual.toHex(), f.hex)
@ -63,81 +64,81 @@ describe('Transaction', function() {
})
})
describe('addInput', function() {
describe('addInput', function () {
// FIXME: not as pretty as could be
// Probably a bit representative of the API
var prevTxHash, prevTxId, prevTx
beforeEach(function() {
beforeEach(function () {
var f = fixtures.valid[0]
prevTx = Transaction.fromHex(f.hex)
prevTxHash = prevTx.getHash()
prevTxId = prevTx.getId()
})
it('accepts a transaction id', function() {
it('accepts a transaction id', function () {
var tx = new Transaction()
tx.addInput(prevTxId, 0)
assert.deepEqual(tx.ins[0].hash, prevTxHash)
})
it('accepts a transaction hash', function() {
it('accepts a transaction hash', function () {
var tx = new Transaction()
tx.addInput(prevTxHash, 0)
assert.deepEqual(tx.ins[0].hash, prevTxHash)
})
it('accepts a Transaction object', function() {
it('accepts a Transaction object', function () {
var tx = new Transaction()
tx.addInput(prevTx, 0)
assert.deepEqual(tx.ins[0].hash, prevTxHash)
})
it('returns an index', function() {
it('returns an index', function () {
var tx = new Transaction()
assert.equal(tx.addInput(prevTxHash, 0), 0)
assert.equal(tx.addInput(prevTxHash, 0), 1)
})
it('defaults to DEFAULT_SEQUENCE', function() {
it('defaults to DEFAULT_SEQUENCE', function () {
var tx = new Transaction()
tx.addInput(prevTxHash, 0)
assert.equal(tx.ins[0].sequence, Transaction.DEFAULT_SEQUENCE)
})
it('defaults to empty script', function() {
it('defaults to empty script', function () {
var tx = new Transaction()
tx.addInput(prevTxHash, 0)
assert.equal(tx.ins[0].script, Script.EMPTY)
})
fixtures.invalid.addInput.forEach(function(f) {
it('throws on ' + f.exception, function() {
fixtures.invalid.addInput.forEach(function (f) {
it('throws on ' + f.exception, function () {
var tx = new Transaction()
var hash = new Buffer(f.hash, 'hex')
assert.throws(function() {
assert.throws(function () {
tx.addInput(hash, f.index)
}, new RegExp(f.exception))
})
})
})
describe('addOutput', function() {
describe('addOutput', function () {
// FIXME: not as pretty as could be
// Probably a bit representative of the API
var destAddressB58, destAddress, destScript
beforeEach(function() {
beforeEach(function () {
destAddressB58 = '15mMHKL96tWAUtqF3tbVf99Z8arcmnJrr3'
destAddress = Address.fromBase58Check(destAddressB58)
destScript = destAddress.toOutputScript()
})
it('accepts an address string', function() {
it('accepts an address string', function () {
var tx = new Transaction()
tx.addOutput(destAddressB58, 40000)
@ -145,7 +146,7 @@ describe('Transaction', function() {
assert.equal(tx.outs[0].value, 40000)
})
it('accepts an Address', function() {
it('accepts an Address', function () {
var tx = new Transaction()
tx.addOutput(destAddress, 40000)
@ -153,7 +154,7 @@ describe('Transaction', function() {
assert.equal(tx.outs[0].value, 40000)
})
it('accepts a scriptPubKey', function() {
it('accepts a scriptPubKey', function () {
var tx = new Transaction()
tx.addOutput(destScript, 40000)
@ -161,35 +162,35 @@ describe('Transaction', function() {
assert.equal(tx.outs[0].value, 40000)
})
it('returns an index', function() {
it('returns an index', function () {
var tx = new Transaction()
assert.equal(tx.addOutput(destScript, 40000), 0)
assert.equal(tx.addOutput(destScript, 40000), 1)
})
})
describe('clone', function() {
fixtures.valid.forEach(function(f) {
describe('clone', function () {
fixtures.valid.forEach(function (f) {
var actual, expected
beforeEach(function() {
beforeEach(function () {
expected = Transaction.fromHex(f.hex)
actual = expected.clone()
})
it('should have value equality', function() {
it('should have value equality', function () {
assert.deepEqual(actual, expected)
})
it('should not have reference equality', function() {
it('should not have reference equality', function () {
assert.notEqual(actual, expected)
})
})
})
describe('getId', function() {
fixtures.valid.forEach(function(f) {
it('should return the id for ' + f.id, function() {
describe('getId', function () {
fixtures.valid.forEach(function (f) {
it('should return the id for ' + f.id, function () {
var tx = Transaction.fromHex(f.hex)
var actual = tx.getId()
@ -198,9 +199,9 @@ describe('Transaction', function() {
})
})
describe('getHash', function() {
fixtures.valid.forEach(function(f) {
it('should return the hash for ' + f.id, function() {
describe('getHash', function () {
fixtures.valid.forEach(function (f) {
it('should return the hash for ' + f.id, function () {
var tx = Transaction.fromHex(f.hex)
var actual = tx.getHash().toString('hex')
@ -213,8 +214,8 @@ describe('Transaction', function() {
// hashForSignature: [Function],
// FIXME: remove in 2.x.y
describe('signInput/validateInput', function() {
it('works for multi-sig redeem script', function() {
describe('signInput/validateInput', function () {
it('works for multi-sig redeem script', function () {
var tx = new Transaction()
tx.addInput('d6f72aab8ff86ff6289842a0424319bf2ddba85dc7c52757912297f948286389', 0)
tx.addOutput('mrCDrCybB6J1vRfbwM5hemdJz73FwDBC8r', 1)
@ -222,13 +223,15 @@ describe('Transaction', function() {
var privKeys = [
'5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAnchuDf',
'5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAvUcVfH'
].map(function(wif) {
].map(function (wif) {
return ECKey.fromWIF(wif)
})
var pubKeys = privKeys.map(function(eck) { return eck.pub })
var pubKeys = privKeys.map(function (eck) {
return eck.pub
})
var redeemScript = scripts.multisigOutput(2, pubKeys)
var signatures = privKeys.map(function(privKey) {
var signatures = privKeys.map(function (privKey) {
return tx.signInput(0, redeemScript, privKey)
})
@ -236,7 +239,7 @@ describe('Transaction', function() {
var scriptSig = scripts.scriptHashInput(redeemScriptSig, redeemScript)
tx.setInputScript(0, scriptSig)
signatures.forEach(function(sig, i){
signatures.forEach(function (sig, i) {
assert(tx.validateInput(0, redeemScript, privKeys[i].pub, sig))
})