JavaScript Bitmessage library
Go to file
2023-01-03 06:19:59 +02:00
.buildbot/node Add a buildbot dir and a script for local testing 2023-01-03 06:19:54 +02:00
lib Replace new Buffer() with Buffer.alloc() in lib/crypto - do it everywhere! 2023-01-03 06:19:59 +02:00
src Fix for latest nan and eccrypto 2015-11-13 21:32:32 +03:00
.gitignore Use webworkify 2015-01-10 16:00:08 +03:00
.jshintignore Use webworkify 2015-01-10 16:00:08 +03:00
.jshintrc Fix for jshint 2.6.3 2015-02-28 22:45:04 +03:00
.npmignore Use webworkify 2015-01-10 16:00:08 +03:00
.travis.yml Use newer GCC 2015-09-29 17:39:34 +03:00
binding.gyp Fix for nan 1.5 2015-01-15 01:07:03 +03:00
COPYING Initial commit 2014-12-11 20:59:48 +03:00
docker-test.sh Add a buildbot dir and a script for local testing 2023-01-03 06:19:54 +02:00
jsdoc.json Documentation with jsdoc 2014-12-30 20:00:28 +03:00
karma.conf.js Extract transports to bitmessage-transports 2015-02-25 13:28:58 +03:00
package.json Update package 2023-01-03 05:04:16 +02:00
README.md Fix badge url 2015-02-28 23:02:04 +03:00
test.js Fix POW test 2015-07-13 22:11:18 +03:00

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 kagami@genshiken.org

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/.