From 8433d73d063dbca9a532bcef3a9857b144419a15 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Wed, 4 Jun 2014 15:16:19 +1000 Subject: [PATCH 1/4] convert: use Buffers and add more tests --- src/convert.js | 22 +++++++++++----------- src/crypto.js | 11 +++++------ test/convert.js | 34 +++++++++++++++------------------- test/fixtures/convert.json | 25 +++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 36 deletions(-) create mode 100644 test/fixtures/convert.json diff --git a/src/convert.js b/src/convert.js index 5bcb4a7..87852b4 100644 --- a/src/convert.js +++ b/src/convert.js @@ -2,11 +2,11 @@ var assert = require('assert') var Crypto = require('crypto-js') var WordArray = Crypto.lib.WordArray -function bytesToWords(bytes) { - assert(Array.isArray(bytes) || Buffer.isBuffer(bytes), 'Input must be a byte array') +function bufferToWords(buffer) { + assert(Buffer.isBuffer(buffer), 'Expected Buffer, got' + buffer) var words = [] - for (var i = 0, b = 0; i < bytes.length; i++, b += 8) { - words[b >>> 5] |= bytes[i] << (24 - b % 32) + for (var i = 0, b = 0; i < buffer.length; i++, b += 8) { + words[b >>> 5] |= buffer[i] << (24 - b % 32) } return words } @@ -19,12 +19,12 @@ function wordsToBytes(words) { return bytes } -function bytesToWordArray(bytes) { - return new WordArray.init(bytesToWords(bytes), bytes.length) +function bufferToWordArray(buffer) { + return new WordArray.init(bufferToWords(buffer), buffer.length) } -function wordArrayToBytes(wordArray) { - return wordsToBytes(wordArray.words) +function wordArrayToBuffer(wordArray) { + return new Buffer(wordsToBytes(wordArray.words)) } function reverseEndian(hex) { @@ -35,9 +35,9 @@ function reverseEndian(hex) { } module.exports = { - bytesToWords: bytesToWords, + bufferToWords: bufferToWords, wordsToBytes: wordsToBytes, - bytesToWordArray: bytesToWordArray, - wordArrayToBytes: wordArrayToBytes, + bufferToWordArray: bufferToWordArray, + wordArrayToBuffer: wordArrayToBuffer, reverseEndian: reverseEndian } diff --git a/src/crypto.js b/src/crypto.js index 38dd569..3400958 100644 --- a/src/crypto.js +++ b/src/crypto.js @@ -5,13 +5,12 @@ var crypto = require('crypto') var convert = require('./convert') function hash160(buffer) { - var step1 = sha256(buffer) - var step2a = convert.bytesToWordArray(step1) + var step2a = convert.bufferToWordArray(step1) var step2b = CryptoJS.RIPEMD160(step2a) - return new Buffer(convert.wordArrayToBytes(step2b)) + return convert.wordArrayToBuffer(step2b) } function hash256(buffer) { @@ -35,12 +34,12 @@ function HmacSHA512(data, secret) { assert(Buffer.isBuffer(data), 'Expected Buffer for data, got ' + data) assert(Buffer.isBuffer(secret), 'Expected Buffer for secret, got ' + secret) - var dataWords = convert.bytesToWordArray(data) - var secretWords = convert.bytesToWordArray(secret) + var dataWords = convert.bufferToWordArray(data) + var secretWords = convert.bufferToWordArray(secret) var hash = CryptoJS.HmacSHA512(dataWords, secretWords) - return new Buffer(convert.wordArrayToBytes(hash)) + return convert.wordArrayToBuffer(hash) } module.exports = { diff --git a/test/convert.js b/test/convert.js index 7c94b4c..b9d086f 100644 --- a/test/convert.js +++ b/test/convert.js @@ -1,30 +1,26 @@ var assert = require('assert') var convert = require('../src/convert') +var fixtures = require('./fixtures/convert') + describe('convert', function() { - describe('byte array and word array conversions', function(){ - var bytes, wordArray + describe('bufferToWordArray', function() { + fixtures.valid.forEach(function(f) { + it('converts ' + f.hex + ' correctly', function() { + var buffer = new Buffer(f.hex, 'hex') + var result = convert.bufferToWordArray(buffer) - beforeEach(function(){ - bytes = [ - 98, 233, 7, 177, 92, 191, 39, 213, 66, 83, - 153, 235, 246, 240, 251, 80, 235, 184, 143, 24 - ] - wordArray = { - words: [1659439025, 1556031445, 1112775147, -151979184, -340226280], - sigBytes: 20 - } - }) - - describe('bytesToWords', function() { - it('works', function() { - assert.deepEqual(convert.bytesToWordArray(bytes), wordArray) + assert.deepEqual(result, f.wordArray) }) }) + }) - describe('bytesToWords', function() { - it('works', function() { - assert.deepEqual(convert.wordArrayToBytes(wordArray), bytes) + describe('wordArrayToBuffer', function() { + fixtures.valid.forEach(function(f) { + it('converts to ' + f.hex + ' correctly', function() { + var resultHex = convert.wordArrayToBuffer(f.wordArray).toString('hex') + + assert.deepEqual(resultHex, f.hex) }) }) }) diff --git a/test/fixtures/convert.json b/test/fixtures/convert.json new file mode 100644 index 0000000..b759004 --- /dev/null +++ b/test/fixtures/convert.json @@ -0,0 +1,25 @@ +{ + "valid": [ + { + "hex": "0000000000000000000000000000000000000000", + "wordArray": { + "words": [0, 0, 0, 0, 0], + "sigBytes": 20 + } + }, + { + "hex": "62e907b15cbf27d5425399ebf6f0fb50ebb88f18", + "wordArray": { + "words": [1659439025, 1556031445, 1112775147, -151979184, -340226280], + "sigBytes": 20 + } + }, + { + "hex": "ffffffffffffffffffffffffffffffffffffffff", + "wordArray": { + "words": [-1, -1, -1, -1, -1], + "sigBytes": 20 + } + } + ] +} From 3b988a08725aa6db0a0b46b33ce626e8af37617f Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Wed, 4 Jun 2014 15:20:45 +1000 Subject: [PATCH 2/4] convert: use Buffer internally and remove unused functions --- src/convert.js | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/src/convert.js b/src/convert.js index 87852b4..a1d907e 100644 --- a/src/convert.js +++ b/src/convert.js @@ -2,29 +2,28 @@ var assert = require('assert') var Crypto = require('crypto-js') var WordArray = Crypto.lib.WordArray -function bufferToWords(buffer) { - assert(Buffer.isBuffer(buffer), 'Expected Buffer, got' + buffer) +function bufferToWordArray(buffer) { + assert(Buffer.isBuffer(buffer), 'Expected Buffer, got', buffer) + var words = [] for (var i = 0, b = 0; i < buffer.length; i++, b += 8) { words[b >>> 5] |= buffer[i] << (24 - b % 32) } - return words -} -function wordsToBytes(words) { - var bytes = [] - for (var b = 0; b < words.length * 32; b += 8) { - bytes.push((words[b >>> 5] >>> (24 - b % 32)) & 0xFF) - } - return bytes -} - -function bufferToWordArray(buffer) { - return new WordArray.init(bufferToWords(buffer), buffer.length) + return new WordArray.init(words, buffer.length) } function wordArrayToBuffer(wordArray) { - return new Buffer(wordsToBytes(wordArray.words)) + assert(Array.isArray(wordArray.words), 'Expected WordArray, got' + wordArray) + + var words = wordArray.words + var buffer = new Buffer(words.length * 4) + + words.forEach(function(value, i) { + buffer.writeInt32BE(value & -1, i * 4) + }) + + return buffer } function reverseEndian(hex) { @@ -35,8 +34,6 @@ function reverseEndian(hex) { } module.exports = { - bufferToWords: bufferToWords, - wordsToBytes: wordsToBytes, bufferToWordArray: bufferToWordArray, wordArrayToBuffer: wordArrayToBuffer, reverseEndian: reverseEndian From 27f9e7dc1ed894ad6d7a425376aee1c6a221d15d Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Wed, 4 Jun 2014 15:21:12 +1000 Subject: [PATCH 3/4] convert: remove reverseEndian --- src/convert.js | 10 +--------- test/convert.js | 9 --------- 2 files changed, 1 insertion(+), 18 deletions(-) diff --git a/src/convert.js b/src/convert.js index a1d907e..8548866 100644 --- a/src/convert.js +++ b/src/convert.js @@ -26,15 +26,7 @@ function wordArrayToBuffer(wordArray) { return buffer } -function reverseEndian(hex) { - var buffer = new Buffer(hex, 'hex') - Array.prototype.reverse.call(buffer) - - return buffer.toString('hex') -} - module.exports = { bufferToWordArray: bufferToWordArray, - wordArrayToBuffer: wordArrayToBuffer, - reverseEndian: reverseEndian + wordArrayToBuffer: wordArrayToBuffer } diff --git a/test/convert.js b/test/convert.js index b9d086f..0b06e44 100644 --- a/test/convert.js +++ b/test/convert.js @@ -24,13 +24,4 @@ describe('convert', function() { }) }) }) - - describe('reverseEndian', function() { - it('works', function() { - var bigEndian = "6a4062273ac4f9ea4ffca52d9fd102b08f6c32faa0a4d1318e3a7b2e437bb9c7" - var littleEdian = "c7b97b432e7b3a8e31d1a4a0fa326c8fb002d19f2da5fc4feaf9c43a2762406a" - assert.deepEqual(convert.reverseEndian(bigEndian), littleEdian) - assert.deepEqual(convert.reverseEndian(littleEdian), bigEndian) - }) - }) }) From 943621f4663b20ef5dd7432243a76209f740987a Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Wed, 4 Jun 2014 15:30:53 +1000 Subject: [PATCH 4/4] Address/HDNode: fix test descriptions --- test/address.js | 2 +- test/hdnode.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/address.js b/test/address.js index 9ab99ab..4e09322 100644 --- a/test/address.js +++ b/test/address.js @@ -29,7 +29,7 @@ describe('Address', function() { }) fixtures.invalid.fromBase58Check.forEach(function(f) { - it('throws on ' + f.descpription, function() { + it('throws on ' + f.description, function() { assert.throws(function() { Address.fromBase58Check(f.base58check) }, new RegExp(f.exception)) diff --git a/test/hdnode.js b/test/hdnode.js index 71a87c7..02ee85f 100644 --- a/test/hdnode.js +++ b/test/hdnode.js @@ -136,7 +136,7 @@ describe('HDNode', function() { }) fixtures.invalid.fromBuffer.forEach(function(f) { - it('throws on ' + f.string, function() { + it('throws on ' + f.hex, function() { assert.throws(function() { HDNode.fromHex(f.hex) }, new RegExp(f.exception))