From 4d5a699c6e7fd70d3f9ca0b42fedd9a2a4a9cc49 Mon Sep 17 00:00:00 2001 From: Kagami Hiiragi Date: Sat, 3 Jan 2015 00:24:36 +0300 Subject: [PATCH] Expand feature matrix, improve consistency --- README.md | 30 +++++++++++---- lib/index.js | 8 ++-- lib/struct.js | 4 +- test.js | 102 +++++++++++++++++++++++++------------------------- 4 files changed, 80 insertions(+), 64 deletions(-) diff --git a/README.md b/README.md index dbd15c1..94bc4e7 100644 --- a/README.md +++ b/README.md @@ -33,14 +33,32 @@ With the help of browserify `bitmessage` provides different implementations for - [ ] ECIES - [ ] AES-256-CBC - [ ] HMAC-SHA-256 -- [ ] Core structures +- [ ] Common structures + - [ ] message - [x] var_int - [x] var_str - [x] var_int_list - - [ ] inv_vect - [ ] net_addr + - [ ] inv_vect + - [ ] encrypted + - [ ] unencrypted + - [ ] encoding - [ ] bitfield -- [ ] High-level objects +- [ ] Message types + - [ ] version + - [ ] verack + - [ ] addr + - [ ] inv + - [ ] getdata + - [ ] object +- [ ] Object types + - [ ] getpubkey + - [ ] pubkey + - [ ] msg + - [ ] broadcast +- [x] WIF +- [ ] POW +- [ ] High-level classes - [ ] Address - [x] encode - [x] decode @@ -48,10 +66,8 @@ With the help of browserify `bitmessage` provides different implementations for - [x] fromRandom - [ ] fromPassphrase - [ ] Message - - [ ] encode - - [ ] decode -- [x] WIF -- [ ] POW + - [ ] encrypt + - [ ] decrypt - [ ] Parse PyBitmessage configs - [ ] decode keys.dat - [ ] decode knownnodes.dat diff --git a/lib/index.js b/lib/index.js index 52e446e..383b89b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -5,9 +5,9 @@ "use strict"; +/** Common structures. */ +exports.struct = require("./struct"); +/** Working with WIF. */ +exports.WIF = require("./wif"); /** Working with addresses. */ exports.Address = require("./address"); -/** Working with WIF. */ -exports.wif = require("./wif"); -/** Core structures. */ -exports.struct = require("./struct"); diff --git a/lib/struct.js b/lib/struct.js index 70da705..72e1154 100644 --- a/lib/struct.js +++ b/lib/struct.js @@ -1,5 +1,5 @@ /** - * Implements core structures. + * Implements common structures. * @see {@link https://bitmessage.org/wiki/Protocol_specification#Common_structures} * @module bitmessage/struct */ @@ -154,7 +154,7 @@ exports.var_int_list = { rest = decoded.rest; sumLength += decoded.length; } - return {list: list, length: sumLength, rest: rest} + return {list: list, length: sumLength, rest: rest}; }, /** diff --git a/test.js b/test.js index 9c1716a..a3092ea 100644 --- a/test.js +++ b/test.js @@ -3,15 +3,58 @@ var allTests = typeof window === "undefined" ? !!process.env.ALL_TESTS : window.ALL_TESTS; +var bmcrypto = require("./lib/crypto"); var bitmessage = require("./lib"); -var Address = bitmessage.Address; -var wif = bitmessage.wif; var var_int = bitmessage.struct.var_int; var var_str = bitmessage.struct.var_str; var var_int_list = bitmessage.struct.var_int_list; -var bmcrypto = require("./lib/crypto"); +var WIF = bitmessage.WIF; +var Address = bitmessage.Address; -describe("Core structures", function() { +describe("Crypto", function() { + it("should implement SHA-512 hash", function() { + return bmcrypto.sha512(Buffer("test")).then(function(res) { + expect(res.toString("hex")).to.equal("ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff"); + }); + }); + + it("should implement SHA-256 hash", function() { + return bmcrypto.sha256(Buffer("test")).then(function(res) { + expect(res.toString("hex")).to.equal("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"); + }); + }); + + it("should implement RIPEMD-160 hash", function() { + return bmcrypto.ripemd160(Buffer("test")).then(function(res) { + expect(res.toString("hex")).to.equal("5e52fee47e6b070565f74372468cdc699de89107"); + }); + }); + + it("should implement cryptographically secure PRNG", function() { + var size = 100; + var rnd = bmcrypto.randomBytes(size); + expect(Buffer.isBuffer(rnd)).to.be.true; + expect(rnd.length).to.equal(size); + // Very simple statistical test. + var bytes = {}; + var sum = 0; + var value; + for (var i = 0; i < size; i++) { + value = rnd[i]; + sum += value; + if (!bytes[value]) { + bytes[value] = 0; + } + bytes[value]++; + expect(bytes[value]).to.be.below(7); + } + // Ideal sum = (255 / 2) * size = 12750 + expect(sum).to.be.above(10000); + expect(sum).to.be.below(15000); + }); +}); + +describe("Common structures", function() { describe("var_int", function() { it("should decode", function() { var res; @@ -122,12 +165,12 @@ describe("WIF", function() { var encPrivateKey = Buffer("9f9969c93c2d186787a7653f70e49be34c03c4a853e6ad0c867db0946bc433c6", "hex"); it("should decode", function() { - return wif.decode(wifSign) + return WIF.decode(wifSign) .then(function(key1) { expect(Buffer.isBuffer(key1)).to.be.true; expect(key1.length).to.equal(32); expect(key1.toString("hex")).to.equal(signPrivateKey.toString("hex")); - return wif.decode(wifEnc).then(function(key2) { + return WIF.decode(wifEnc).then(function(key2) { expect(Buffer.isBuffer(key2)).to.be.true; expect(key2.length).to.equal(32); expect(key2.toString("hex")).to.equal(encPrivateKey.toString("hex")); @@ -139,58 +182,15 @@ describe("WIF", function() { }); it("should encode", function() { - return wif.encode(signPrivateKey).then(function(wif1) { + return WIF.encode(signPrivateKey).then(function(wif1) { expect(wif1).to.equal(wifSign); - return wif.encode(encPrivateKey); + return WIF.encode(encPrivateKey); }).then(function(wif2) { expect(wif2).to.equal(wifEnc); }); }); }); -describe("Crypto", function() { - it("should implement SHA-512 hash", function() { - return bmcrypto.sha512(Buffer("test")).then(function(res) { - expect(res.toString("hex")).to.equal("ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff"); - }); - }); - - it("should implement SHA-256 hash", function() { - return bmcrypto.sha256(Buffer("test")).then(function(res) { - expect(res.toString("hex")).to.equal("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"); - }); - }); - - it("should implement RIPEMD-160 hash", function() { - return bmcrypto.ripemd160(Buffer("test")).then(function(res) { - expect(res.toString("hex")).to.equal("5e52fee47e6b070565f74372468cdc699de89107"); - }); - }); - - it("should implement cryptographically secure PRNG", function() { - var size = 100; - var rnd = bmcrypto.randomBytes(size); - expect(Buffer.isBuffer(rnd)).to.be.true; - expect(rnd.length).to.equal(size); - // Very simple statistical test. - var bytes = {}; - var sum = 0; - var value; - for (var i = 0; i < size; i++) { - value = rnd[i]; - sum += value; - if (!bytes[value]) { - bytes[value] = 0; - } - bytes[value]++; - expect(bytes[value]).to.be.below(7); - } - // Ideal sum = (255 / 2) * size = 12750 - expect(sum).to.be.above(10000); - expect(sum).to.be.below(15000); - }); -}); - describe("Address", function() { it("should decode Bitmessage address", function() { return Address.decode("BM-2cTux3PGRqHTEH6wyUP2sWeT4LrsGgy63z")