Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ node_js:
- "0.11"
- "0.10"
env:
- TEST_SUITE=unit
- TEST_SUITE=integration
- TEST_SUITE=coveralls
- TEST_SUITE=integration
- TEST_SUITE=standard
- TEST_SUITE=unit
script: "npm run-script $TEST_SUITE"
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,10 @@ The below examples are implemented as integration tests, they should be very eas
- [Coinkite](https://coinkite.com)
- [Coinpunk](https://coinpunk.com)
- [Dark Wallet](https://darkwallet.unsystem.net)
- [DecentralBank](http://decentralbank.co)
- [DecentralBank](http://decentralbank.com/)
- [Dogechain Wallet](https://dogechain.info)
- [GreenAddress](https://greenaddress.it)
- [Hive Wallet](https://www.hivewallet.com)
- [Justchain Exchange](https://justcoin.com)
- [QuickCoin](https://wallet.quickcoin.co)
- [Robocoin](https://wallet.robocoin.com)
- [Skyhook ATM](http://projectskyhook.com)
Expand Down
19 changes: 0 additions & 19 deletions jshint.json

This file was deleted.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bitcoinjs-lib",
"version": "1.4.5",
"version": "1.5.0",
"description": "Client-side Bitcoin JavaScript library",
"main": "./src/index.js",
"keywords": [
Expand Down Expand Up @@ -36,7 +36,7 @@
"coverage": "istanbul cover _mocha -- test/*.js",
"coveralls": "npm run-script coverage && coveralls < coverage/lcov.info",
"integration": "mocha --reporter list test/integration/*.js",
"jshint": "jshint --config jshint.json src/*.js ; true",
"standard": "standard",
"test": "npm run-script unit",
"unit": "istanbul test mocha -- --reporter list test/*.js"
},
Expand All @@ -57,9 +57,9 @@
"cb-helloblock": "^0.4.10",
"coveralls": "^2.11.2",
"istanbul": "^0.3.5",
"jshint": "^2.5.11",
"mocha": "^2.1.0",
"mocha-lcov-reporter": "0.0.1",
"sinon": "^1.12.2"
"sinon": "^1.12.2",
"standard": "^2.7.3"
}
}
10 changes: 5 additions & 5 deletions src/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var typeForce = require('typeforce')
var networks = require('./networks')
var scripts = require('./scripts')

function findScriptTypeByVersion(version) {
function findScriptTypeByVersion (version) {
for (var networkName in networks) {
var network = networks[networkName]

Expand All @@ -13,7 +13,7 @@ function findScriptTypeByVersion(version) {
}
}

function Address(hash, version) {
function Address (hash, version) {
typeForce('Buffer', hash)

assert.strictEqual(hash.length, 20, 'Invalid hash length')
Expand All @@ -23,15 +23,15 @@ function Address(hash, version) {
this.version = version
}

Address.fromBase58Check = function(string) {
Address.fromBase58Check = function (string) {
var payload = base58check.decode(string)
var version = payload.readUInt8(0)
var hash = payload.slice(1)

return new Address(hash, version)
}

Address.fromOutputScript = function(script, network) {
Address.fromOutputScript = function (script, network) {
network = network || networks.bitcoin

if (scripts.isPubKeyHashOutput(script)) return new Address(script.chunks[2], network.pubKeyHash)
Expand All @@ -48,7 +48,7 @@ Address.prototype.toBase58Check = function () {
return base58check.encode(payload)
}

Address.prototype.toOutputScript = function() {
Address.prototype.toOutputScript = function () {
var scriptType = findScriptTypeByVersion(this.version)

if (scriptType === 'pubkeyhash') return scripts.pubKeyHashOutput(this.hash)
Expand Down
18 changes: 0 additions & 18 deletions src/base58check.js

This file was deleted.

31 changes: 15 additions & 16 deletions src/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ var bufferutils = require('./bufferutils')
var crypto = require('./crypto')

var Transaction = require('./transaction')
var Script = require('./script')

function Block() {
function Block () {
this.version = 1
this.prevHash = null
this.merkleRoot = null
Expand All @@ -14,16 +13,16 @@ function Block() {
this.nonce = 0
}

Block.fromBuffer = function(buffer) {
Block.fromBuffer = function (buffer) {
assert(buffer.length >= 80, 'Buffer too small (< 80 bytes)')

var offset = 0
function readSlice(n) {
function readSlice (n) {
offset += n
return buffer.slice(offset - n, offset)
}

function readUInt32() {
function readUInt32 () {
var i = buffer.readUInt32LE(offset)
offset += 4
return i
Expand All @@ -39,14 +38,14 @@ Block.fromBuffer = function(buffer) {

if (buffer.length === 80) return block

function readVarInt() {
function readVarInt () {
var vi = bufferutils.readVarInt(buffer, offset)
offset += vi.size
return vi.number
}

// FIXME: poor performance
function readTransaction() {
function readTransaction () {
var tx = Transaction.fromBuffer(buffer.slice(offset), true)

offset += tx.toBuffer().length
Expand All @@ -64,35 +63,35 @@ Block.fromBuffer = function(buffer) {
return block
}

Block.fromHex = function(hex) {
Block.fromHex = function (hex) {
return Block.fromBuffer(new Buffer(hex, 'hex'))
}

Block.prototype.getHash = function() {
Block.prototype.getHash = function () {
return crypto.hash256(this.toBuffer(true))
}

Block.prototype.getId = function() {
Block.prototype.getId = function () {
return bufferutils.reverse(this.getHash()).toString('hex')
}

Block.prototype.getUTCDate = function() {
Block.prototype.getUTCDate = function () {
var date = new Date(0) // epoch
date.setUTCSeconds(this.timestamp)

return date
}

Block.prototype.toBuffer = function(headersOnly) {
Block.prototype.toBuffer = function (headersOnly) {
var buffer = new Buffer(80)

var offset = 0
function writeSlice(slice) {
function writeSlice (slice) {
slice.copy(buffer, offset)
offset += slice.length
}

function writeUInt32(i) {
function writeUInt32 (i) {
buffer.writeUInt32LE(i, offset)
offset += 4
}
Expand All @@ -107,14 +106,14 @@ Block.prototype.toBuffer = function(headersOnly) {
if (headersOnly || !this.transactions) return buffer

var txLenBuffer = bufferutils.varIntBuffer(this.transactions.length)
var txBuffers = this.transactions.map(function(tx) {
var txBuffers = this.transactions.map(function (tx) {
return tx.toBuffer()
})

return Buffer.concat([buffer, txLenBuffer].concat(txBuffers))
}

Block.prototype.toHex = function(headersOnly) {
Block.prototype.toHex = function (headersOnly) {
return this.toBuffer(headersOnly).toString('hex')
}

Expand Down
49 changes: 29 additions & 20 deletions src/bufferutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ var assert = require('assert')
var opcodes = require('./opcodes')

// https://github.com/feross/buffer/blob/master/index.js#L1127
function verifuint(value, max) {
function verifuint (value, max) {
assert(typeof value === 'number', 'cannot write a non-number as a number')
assert(value >= 0, 'specified a negative value for writing an unsigned value')
assert(value <= max, 'value is larger than maximum value for type')
assert(Math.floor(value) === value, 'value has a fractional component')
}

function pushDataSize(i) {
function pushDataSize (i) {
return i < opcodes.OP_PUSHDATA1 ? 1
: i < 0xff ? 2
: i < 0xffff ? 3
: 5
: i < 0xff ? 2
: i < 0xffff ? 3
: 5
}

function readPushDataInt(buffer, offset) {
function readPushDataInt (buffer, offset) {
var opcode = buffer.readUInt8(offset)
var number, size

Expand All @@ -41,7 +41,6 @@ function readPushDataInt(buffer, offset) {

number = buffer.readUInt32LE(offset + 1)
size = 5

}

return {
Expand All @@ -51,7 +50,7 @@ function readPushDataInt(buffer, offset) {
}
}

function readUInt64LE(buffer, offset) {
function readUInt64LE (buffer, offset) {
var a = buffer.readUInt32LE(offset)
var b = buffer.readUInt32LE(offset + 4)
b *= 0x100000000
Expand All @@ -61,7 +60,7 @@ function readUInt64LE(buffer, offset) {
return b + a
}

function readVarInt(buffer, offset) {
function readVarInt (buffer, offset) {
var t = buffer.readUInt8(offset)
var number, size

Expand Down Expand Up @@ -92,7 +91,7 @@ function readVarInt(buffer, offset) {
}
}

function writePushDataInt(buffer, number, offset) {
function writePushDataInt (buffer, number, offset) {
var size = pushDataSize(number)

// ~6 bit
Expand All @@ -113,27 +112,26 @@ function writePushDataInt(buffer, number, offset) {
} else {
buffer.writeUInt8(opcodes.OP_PUSHDATA4, offset)
buffer.writeUInt32LE(number, offset + 1)

}

return size
}

function writeUInt64LE(buffer, value, offset) {
function writeUInt64LE (buffer, value, offset) {
verifuint(value, 0x001fffffffffffff)

buffer.writeInt32LE(value & -1, offset)
buffer.writeUInt32LE(Math.floor(value / 0x100000000), offset + 4)
}

function varIntSize(i) {
return i < 253 ? 1
: i < 0x10000 ? 3
: i < 0x100000000 ? 5
: 9
function varIntSize (i) {
return i < 253 ? 1
: i < 0x10000 ? 3
: i < 0x100000000 ? 5
: 9
}

function writeVarInt(buffer, number, offset) {
function writeVarInt (buffer, number, offset) {
var size = varIntSize(number)

// 8 bit
Expand All @@ -159,21 +157,32 @@ function writeVarInt(buffer, number, offset) {
return size
}

function varIntBuffer(i) {
function varIntBuffer (i) {
var size = varIntSize(i)
var buffer = new Buffer(size)
writeVarInt(buffer, i, 0)

return buffer
}

function reverse(buffer) {
function equal (a, b) {
if (a.length !== b.length) return false

for (var i = 0; i < a.length; ++i) {
if (a[i] !== b[i]) return false
}

return true
}

function reverse (buffer) {
var buffer2 = new Buffer(buffer)
Array.prototype.reverse.call(buffer2)
return buffer2
}

module.exports = {
equal: equal,
pushDataSize: pushDataSize,
readPushDataInt: readPushDataInt,
readUInt64LE: readUInt64LE,
Expand Down
Loading