Giter VIP home page Giter VIP logo

bitmessage's Introduction

bitmessage Build Status

NPM

JavaScript Bitmessage library for both browserify and node. The goal of this project is to implement Bitmessage protocol v3 for both platforms at the maximum possible level (we still can't create TCP connections or listen for incoming connections in the Browser but the Proof of work and crypto is fully doable).

Public library API is currently in alpha stage, breaking changes are very likely to happen.

API documentation is available here.

References

Feature matrix

  • Crypto
    • SHA-1
    • SHA-256
    • SHA-512
    • RIPEMD-160
    • PRNG
    • ECC keys handling
    • ECDSA
    • ECIES
  • Common structures
    • message
    • object
    • var_int
    • var_str
    • var_int_list
    • net_addr
    • inv_vect
    • encrypted
    • service features
    • pubkey features
  • Message types
    • version
    • addr
    • inv
    • getdata
    • error
  • Object types
    • getpubkey
    • pubkey
    • msg
    • broadcast
  • WIF
  • POW
  • High-level classes
    • Address
    • UserAgent
  • PyBitmessage configs parsing
    • keys.dat
    • knownnodes.dat
    • messages.dat

Usage

Address

var Address = require("bitmessage").Address;

// Generate a new random Bitmessage identity.
var addr1 = Address.fromRandom();
console.log("New random Bitmessage address:", addr1.encode());

// Or create it from passphrase.
var addr2 = Address.fromPassphrase("test");
console.log("Deterministic Bitmessage address:", addr2.encode());

Structures

var structs = require("bitmessage").structs;

var encoded = Buffer.concat([
  structs.var_int.encode(4),
  Buffer("test"),
  structs.var_str.encode("test2"),
  structs.var_int_list.encode([1, 2, 3]),
]);

var decoded1 = structs.var_str.decode(encoded);
console.log(decoded1.str);  // test
var decoded2 = structs.var_str.decode(decoded1.rest);
console.log(decoded2.str);  // test2
var decoded3 = structs.var_int.decode(decoded2.rest);
console.log(decoded3.value);  // 3
var decoded4 = structs.var_int_list.decode(decoded2.rest);
console.log(decoded4.list);  // [1, 2, 3]

Messages

var structs = require("bitmessage").structs;
var messages = require("bitmessage").messages;

// Simple encoding and decoding:
var vermsg = messages.version.encode({
  remoteHost: "1.1.1.1",
  remotePort: 8444,
});
console.log(messages.version.decode(vermsg).remoteHost);  // 1.1.1.1

// Low-level encoding and decoding:
var addrPayload = messages.addr.encodePayload([
  {host: "2.2.2.2", port: 28444},
]);
var addrmsg = structs.message.encode("addr", addrPayload);
var decoded = structs.message.decode(addrmsg);
console.log(decoded.command);  // addr
var addr = messages.addr.decodePayload(decoded.payload);
console.log(addr.addrs[0].host);  // 2.2.2.2

// Encode with empty payload:
var verackmsg = structs.message.encode("verack");
console.log(structs.message.decode(verackmsg).command);  // verack

Networking

You will need to install bitmessage-transports library.

var messages = require("bitmessage").messages;
var TcpTransport = require("bitmessage-transports").TcpTransport;

var tcp = new TcpTransport({
  dnsSeeds: [["bootstrap8444.bitmessage.org", 8444]],
});

tcp.bootstrap().then(function(nodes) {
  var remoteHost = nodes[0][0];
  var remotePort = nodes[0][1];
  console.log("Connecting to", nodes[0]);
  tcp.connect(remotePort, remoteHost);
});

tcp.on("established", function(version) {
  console.log("Connection established to", version.userAgent);

  tcp.on("message", function(command, payload) {
    console.log("Got new", command, "message");
    var decoded;
    if (command === "addr") {
      decoded = messages.addr.decodePayload(payload);
      console.log("Got", decoded.addrs.length, "node addresses");
    }
  });
});

License

bitmessage - JavaScript Bitmessage library

Written in 2014-2015 by Kagami Hiiragi [email protected]

To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.

You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see http://creativecommons.org/publicdomain/zero/1.0/.

bitmessage's People

Contributors

echom avatar kagami avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bitmessage's Issues

Documentation

Generate documentation using JSDoc (with the Markdown plugin) and host it at GitHub Pages for this repository.

Better objects validation

Decoded objects should be validated as much as possible. Existing validation in PyBitmessage may help. Examples:

  • Messages must be decrypted using identity with the same ripe as in the destination_ripe field [↱]
  • Broadcasts must be decrypted using identity with the same ripe as the one constructed from the keys fields [↱]
  • Better checks for version ranges, streams; also sender's version should justify used object's version (msg, broadcast) [↱]

Pure JS bignum fallback

bignum currently is in optional dependencies, but library is actually not usable without it.

Think through the API, make it more consistent

  • var_int.value vs var_str.str vs var_list.list
  • net_addr.addrs vs inv.inventory vs error.vector
  • pubkey.needed vs msg.identities vs broadcast.subscriptions
  • bootstrap/connect/listen API: it's better to always use objects instead of positional arguments. (E.g. {host: "1.2.3.4", port: 8444})
  • connection pass 3 positional arguments (should it pass transport, {host, port} instead?)
  • error message fatal field is nonintuitive (better to use something like type)
  • version message version field is nonintuitive (better to use something like protoVersion)
  • version message streamNumbers field is awkward
  • High-level checkings (like connection to self detection) probably should lie in transport code
  • Exceptions in transport callbacks propogated further: do we want to auto-catch and warn about them?
  • Somehow make it clear that methods like tryDecode and validate don't throw exceptions

Benchmarks

Create the directory with various benchmarks in order to fully understand the bottlenecks:

  • SHA512/SHA256: WebCryptoAPI vs OpenSSL vs hash.js vs some-other-pure-js-implementation
  • RIPEMD160: OpenSSL vs CryptoJS vs hash.js
  • getPublic: secp256k1 vs elliptic vs ecurve
  • keys2ripe/tryKey: Node vs Browser

POW speed

Comparision of POW speed in different environments

Implementation sources: https://gist.github.com/Kagami/e15186b5d73224ca8c47

POW

target = 297422593171
initialHash = "8ff2d685db89a0af2e3dbfd3f700ae96ef4d9a1eac72fd778bbb368c7510cddda349e03207e1c4965bd95c6f7265e8f1a481a08afab3874eaafb9ade09a10880" (hex)
nonce = 21997550
trialValue = 120283692955

fastcpu (OpenSSL, 8 threads)

Run test POW for ~10-60 seconds, please be patient
Result nonce should be equal to 45833041
Done in 4.78248214722s: nonce = 21997550 with trial value 120283692955 < 297422593171 target

(5 seconds)

PyBitmessage (8 processes)

@@@ CALCULATE TARGET 297422593171 = 18446744073709551616 1000 628 8 1000 2418984 628 8 1000 65536
@@@ START AT: 1420638468
@@@ INPUT: 297422593171 8ff2d685db89a0af2e3dbfd3f700ae96ef4d9a1eac72fd778bbb368c7510cddda349e03207e1c4965bd95c6f7265e8f1a481a08afab3874eaafb9ade09a10880
@@@ END AT: 1420638483
@@@ RESULT: 120283692955 21997550

(15 seconds)

C (OpenSSL, 1 thread)

nonce = 21997550, trial = 120283692955 <= 297422593171 target
Resulting hash is: 0000001c01777f9bd7215852b3ff39ed5cd1b67ae81df304d05b136e38ae59330173f630fcf46665be38d5415d02ae48adcbea9bc8e29862c9888bcf22789308
./pow  19.28s user 0.00s system 99% cpu 19.278 total

(20 seconds)

JS (sha.js, 8 web workers)

Chrome

runPow()
undefined
index.browserify.js:17 pow: 132104.604ms
index.browserify.js:18 Result nonce: 21997550

(132 seconds, 2.25 minutes)

Firefox

pow: timer started index.browserify.js:5
pow: 230391.8ms index.browserify.js:17
"Result nonce: 21997550"

(230 seconds, 4 minutes)

JS (sha.js, 1 thread)

Chrome

runPow()
index.browserify.js:45 pow: 632656.147ms
21997550

(633 seconds, 10.5 minutes)

Automatically generate acknowledgment for msg object

When encoding msg object, library should automatically generate acknowledgement object by default (user should be able to provide his/her own object or include empty ack). It should also parse and validate ack's POW on decode.

Document conventions

Conventions used throughout the library should be documented in CONVENTIONS.md or somewhere in the docs. Some of them are:

  • Structure of the modules
  • Structs/messages/objects namespaces
  • decode/encode common behavior: input, output, exceptions
  • Exception-free methods like tryDecode and validate

Better error handling

Library should provide better error handling interface so the end-user may easily detect and process various errors events. Probably we should use custom Error (exception) classes.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.