Source: structs.js

  1. /**
  2. * Implements common structures.
  3. * @see {@link
  4. * https://bitmessage.org/wiki/Protocol_specification#Common_structures}
  5. * @module bitmessage/structs
  6. * @example
  7. * var structs = require("bitmessage").structs;
  8. *
  9. * var encoded = Buffer.concat([
  10. * structs.var_int.encode(4),
  11. * Buffer("test"),
  12. * structs.var_str.encode("test2"),
  13. * structs.var_int_list.encode([1, 2, 3]),
  14. * ]);
  15. *
  16. * var decoded1 = structs.var_str.decode(encoded);
  17. * console.log(decoded1.str); // test
  18. * var decoded2 = structs.var_str.decode(decoded1.rest);
  19. * console.log(decoded2.str); // test2
  20. * var decoded3 = structs.var_int.decode(decoded2.rest);
  21. * console.log(decoded3.value); // 3
  22. * var decoded4 = structs.var_int_list.decode(decoded2.rest);
  23. * console.log(decoded4.list); // [1, 2, 3]
  24. */
  25. "use strict";
  26. var objectAssign = Object.assign || require("object-assign");
  27. var bufferEqual = require("buffer-equal");
  28. var bmcrypto = require("./crypto");
  29. var POW = require("./pow");
  30. var util = require("./_util");
  31. var assert = util.assert;
  32. var IPv4_MAPPING = util.IPv4_MAPPING;
  33. var inet_pton = util.inet_pton;
  34. function isAscii(str) {
  35. for (var i = 0; i < str.length; i++) {
  36. if (str.charCodeAt(i) > 127) {
  37. return false;
  38. }
  39. }
  40. return true;
  41. }
  42. // Compute the message checksum for the given data.
  43. function getmsgchecksum(data) {
  44. return bmcrypto.sha512(data).slice(0, 4);
  45. }
  46. // \ :3 /
  47. function findMagic(buf) {
  48. var i;
  49. var len = buf.length;
  50. var firstb = false;
  51. var secondb = false;
  52. var thirdb = false;
  53. for (i = 0; i < len; ++i) {
  54. switch (buf[i]) {
  55. case 0xE9:
  56. firstb = true;
  57. break;
  58. case 0xBE:
  59. if (firstb) { secondb = true; }
  60. break;
  61. case 0xB4:
  62. if (firstb && secondb) { thirdb = true; }
  63. break;
  64. case 0xD9:
  65. if (firstb && secondb && thirdb) { return i - 3; }
  66. break;
  67. default:
  68. firstb = false;
  69. secondb = false;
  70. thirdb = false;
  71. }
  72. }
  73. // If we reached the end of the buffer but part of the magic matches
  74. // we'll still return index of the magic's start position.
  75. if (firstb) {
  76. if (secondb) {
  77. --i;
  78. }
  79. if (thirdb) {
  80. --i;
  81. }
  82. return i - 1; // Compensate for last i's increment
  83. } else {
  84. return -1;
  85. }
  86. }
  87. /**
  88. * Message structure.
  89. * @see {@link https://bitmessage.org/wiki/Protocol_specification#Message_structure}
  90. * @namespace
  91. * @static
  92. */
  93. var message = exports.message = {
  94. /**
  95. * Bitmessage magic value.
  96. * @constant {number}
  97. */
  98. MAGIC: 0xE9BEB4D9,
  99. /**
  100. * @typedef {Object} TryDecodeResult
  101. * @property {Object} message - Decoded message
  102. * @property {string} message.command - Message command
  103. * @property {Buffer} message.payload - Message payload
  104. * @property {number} message.length - Full message length
  105. * @property {Error} error - ...or decoding error
  106. * @property {Buffer} rest - The rest of the input buffer after
  107. * processing message
  108. * @memberof module:bitmessage/structs.message
  109. */
  110. /**
  111. * Decode message in "stream" mode.
  112. * NOTE: message payload and `rest` are copied (so the runtime can GC
  113. * processed buffer data).
  114. * @param {Buffer} buf - Data buffer
  115. * @return {?TryDecodeResult}
  116. * [Decoded result.]{@link module:bitmessage/structs.message.TryDecodeResult}
  117. */
  118. tryDecode: function(buf) {
  119. if (buf.length < 24) {
  120. // Message is not yet fully received, just skip to next process
  121. // cycle.
  122. return;
  123. }
  124. var res = {};
  125. // Magic.
  126. var mindex = findMagic(buf);
  127. if (mindex !== 0) {
  128. if (mindex === -1) {
  129. res.error = new Error("Magic not found, skipping buffer data");
  130. res.rest = new Buffer(0);
  131. } else {
  132. res.error = new Error(
  133. "Magic in the middle of buffer, skipping some data at start"
  134. );
  135. res.rest = new Buffer(buf.length - mindex);
  136. buf.copy(res.rest, 0, mindex);
  137. }
  138. return res;
  139. }
  140. // Payload length.
  141. var payloadLength = buf.readUInt32BE(16, true);
  142. var msgLength = 24 + payloadLength;
  143. // See: <https://github.com/Bitmessage/PyBitmessage/issues/767>.
  144. if (payloadLength > 1600003) {
  145. res.error = new Error("Message is too large, skipping it");
  146. if (buf.length > msgLength) {
  147. res.rest = new Buffer(buf.length - msgLength);
  148. buf.copy(res.rest, 0, msgLength);
  149. } else {
  150. res.rest = new Buffer(0);
  151. }
  152. return res;
  153. }
  154. if (buf.length < msgLength) {
  155. // Message is not yet fully received, just skip to next process
  156. // cycle.
  157. return;
  158. }
  159. // Now we can set `rest` value.
  160. res.rest = new Buffer(buf.length - msgLength);
  161. buf.copy(res.rest, 0, msgLength);
  162. // Command.
  163. var command = buf.slice(4, 16);
  164. var firstNonNull = 0;
  165. var i;
  166. for (i = 11; i >=0; i--) {
  167. if (command[i] > 127) {
  168. res.error = new Error(
  169. "Non-ASCII characters in command, skipping message"
  170. );
  171. return res;
  172. }
  173. if (!firstNonNull && command[i] !== 0) {
  174. firstNonNull = i + 1;
  175. }
  176. }
  177. command = command.slice(0, firstNonNull).toString("ascii");
  178. // Payload.
  179. var payload = new Buffer(payloadLength);
  180. buf.copy(payload, 0, 24, msgLength);
  181. var checksum = buf.slice(20, 24);
  182. if (!bufferEqual(checksum, getmsgchecksum(payload))) {
  183. res.error = new Error("Bad checksum, skipping message");
  184. return res;
  185. }
  186. res.message = {command: command, payload: payload, length: msgLength};
  187. return res;
  188. },
  189. /**
  190. * @typedef {Object} DecodeResult
  191. * @property {string} command - Message command
  192. * @property {Buffer} payload - Message payload
  193. * @property {number} length - Full message length
  194. * @property {Buffer} rest - The rest of the input buffer
  195. * @memberof module:bitmessage/structs.message
  196. */
  197. /**
  198. * Decode message.
  199. * NOTE: `payload` is copied, `rest` references input buffer.
  200. * @param {Buffer} buf - Buffer that starts with encoded message
  201. * @return {DecodeResult}
  202. * [Decoded message structure.]{@link module:bitmessage/structs.message.DecodeResult}
  203. */
  204. decode: function(buf) {
  205. assert(buf.length >= 24, "Buffer is too small");
  206. assert(buf.readUInt32BE(0, true) === message.MAGIC, "Wrong magic");
  207. var command = buf.slice(4, 16);
  208. var firstNonNull = 0;
  209. for (var i = 11; i >=0; i--) {
  210. assert(command[i] <= 127, "Non-ASCII characters in command");
  211. if (!firstNonNull && command[i] !== 0) {
  212. firstNonNull = i + 1;
  213. }
  214. }
  215. // NOTE(Kagami): Command can be empty.
  216. // NOTE(Kagami): "ascii" encoding is not necessary here since we
  217. // already validated the command but that should be quite faster
  218. // than default "utf-8" encoding.
  219. command = command.slice(0, firstNonNull).toString("ascii");
  220. var payloadLength = buf.readUInt32BE(16, true);
  221. assert(payloadLength <= 1600003, "Message payload is too big");
  222. var length = 24 + payloadLength;
  223. assert(buf.length >= length, "Truncated payload");
  224. var checksum = buf.slice(20, 24);
  225. // NOTE(Kagami): We do copy instead of slice to protect against
  226. // possible source buffer modification by user.
  227. var payload = new Buffer(payloadLength);
  228. buf.copy(payload, 0, 24, length);
  229. assert(bufferEqual(checksum, getmsgchecksum(payload)), "Bad checksum");
  230. var rest = buf.slice(length);
  231. return {command: command, payload: payload, length: length, rest: rest};
  232. },
  233. /**
  234. * Encode message.
  235. * @param {string} command - Message command
  236. * @param {Bufer} payload - Message payload
  237. * @return {Buffer} Encoded message structure.
  238. */
  239. encode: function(command, payload) {
  240. assert(command.length <= 12, "Command is too long");
  241. assert(isAscii(command), "Non-ASCII characters in command");
  242. payload = payload || new Buffer(0);
  243. assert(payload.length <= 1600003, "Message payload is too big");
  244. var buf = new Buffer(24 + payload.length);
  245. buf.fill(0);
  246. buf.writeUInt32BE(message.MAGIC, 0, true);
  247. buf.write(command, 4);
  248. buf.writeUInt32BE(payload.length, 16, true);
  249. getmsgchecksum(payload).copy(buf, 20);
  250. payload.copy(buf, 24);
  251. return buf;
  252. },
  253. };
  254. /**
  255. * An `object` is a message which is shared throughout a stream. It is
  256. * the only message which propagates; all others are only between two
  257. * nodes.
  258. * @see {@link https://bitmessage.org/wiki/Protocol_specification#object}
  259. * @namespace
  260. * @static
  261. */
  262. var object = exports.object = {
  263. /**
  264. * [getpubkey]{@link module:bitmessage/objects.getpubkey} object type.
  265. * @constant {number}
  266. */
  267. GETPUBKEY: 0,
  268. /**
  269. * [pubkey]{@link module:bitmessage/objects.pubkey} object type.
  270. * @constant {number}
  271. */
  272. PUBKEY: 1,
  273. /**
  274. * [msg]{@link module:bitmessage/objects.msg} object type.
  275. * @constant {number}
  276. */
  277. MSG: 2,
  278. /**
  279. * [broadcast]{@link module:bitmessage/objects.broadcast} object type.
  280. * @constant {number}
  281. */
  282. BROADCAST: 3,
  283. /**
  284. * @typedef {Object} DecodeResult
  285. * @property {Buffer} nonce - A 8-byte object nonce
  286. * @property {number} ttl - Time to live in seconds
  287. * @property {Date} expires - Object expiration date
  288. * @property {number} type - Object type
  289. * @property {number} version - Object version
  290. * @property {number} stream - Object stream
  291. * @property {number} headerLength - Length of the object header
  292. * @property {Buffer} objectPayload - Object payload
  293. * @memberof module:bitmessage/structs.object
  294. */
  295. /**
  296. * Decode `object` message.
  297. * NOTE: `nonce` and `objectPayload` are copied.
  298. * @param {Buffer} buf - Message
  299. * @param {Object=} opts - Decoding options
  300. * @param {boolean} opts.allowExpired - Allow expired objects
  301. * @param {boolean} opts.skipPow - Do not validate object POW
  302. * @return {DecodeResult} [Decoded `object` structure.]{@link
  303. * module:bitmessage/structs.object.DecodeResult}
  304. * @throws {Error} Invalid object
  305. */
  306. decode: function(buf, opts) {
  307. var decoded = message.decode(buf);
  308. assert(decoded.command === "object", "Given message is not an object");
  309. return object.decodePayload(decoded.payload, opts);
  310. },
  311. /**
  312. * Decode `object` message payload.
  313. * The same as [decode]{@link module:bitmessage/structs.object.decode}.
  314. */
  315. decodePayload: function(buf, opts) {
  316. opts = opts || {};
  317. // 8 + 8 + 4 + (1+) + (1+)
  318. assert(buf.length >= 22, "object message payload is too small");
  319. assert(buf.length <= 262144, "object message payload is too big");
  320. var nonce;
  321. if (!opts._validate) {
  322. nonce = new Buffer(8);
  323. buf.copy(nonce, 0, 0, 8);
  324. }
  325. // TTL.
  326. var expiresTime = util.readTimestamp64BE(buf.slice(8, 16));
  327. var expires = new Date(expiresTime * 1000);
  328. var ttl = expiresTime - util.tnow();
  329. assert(ttl <= 2430000, "expiresTime is too far in the future");
  330. if (!opts.allowExpired) {
  331. assert(ttl >= -3600, "Object expired more than a hour ago");
  332. }
  333. // POW.
  334. if (!opts.skipPow) {
  335. // User may specify trials/payload extra options and we will
  336. // account in here.
  337. var targetOpts = objectAssign({}, opts, {ttl: ttl, payload: buf});
  338. var target = POW.getTarget(targetOpts);
  339. assert(POW.check({target: target, payload: buf}), "Insufficient POW");
  340. }
  341. var type = buf.readUInt32BE(16, true);
  342. var decodedVersion = var_int.decode(buf.slice(20));
  343. var decodedStream = var_int.decode(decodedVersion.rest);
  344. var headerLength = 20 + decodedVersion.length + decodedStream.length;
  345. if (opts._validate) { return {stream: decodedStream.value}; }
  346. var objectPayload = new Buffer(decodedStream.rest.length);
  347. decodedStream.rest.copy(objectPayload);
  348. return {
  349. nonce: nonce,
  350. ttl: ttl,
  351. expires: expires,
  352. type: type,
  353. version: decodedVersion.value,
  354. stream: decodedStream.value,
  355. headerLength: headerLength,
  356. objectPayload: objectPayload,
  357. };
  358. },
  359. /**
  360. * Check whether given `object` message is valid.
  361. * @param {Buffer} buf - Message
  362. * @param {Object=} opts - Any of [object.decode]{@link
  363. * module:bitmessage/structs.object.decode} options and:
  364. * @param {number} opts.stream - Expected object's stream
  365. * @return {?Error} Return an error with description if object is
  366. * invalid.
  367. */
  368. validate: function(buf, opts) {
  369. var decoded;
  370. try {
  371. decoded = message.decode(buf);
  372. } catch(e) {
  373. return e;
  374. }
  375. if (decoded.command !== "object") {
  376. return new Error("Given message is not an object");
  377. }
  378. return object.validatePayload(decoded.payload, opts);
  379. },
  380. /**
  381. * Check whether `object` message payload is valid.
  382. * The same as [validate]{@link
  383. * module:bitmessage/structs.object.validate}.
  384. */
  385. validatePayload: function(buf, opts) {
  386. opts = objectAssign({}, opts, {_validate: true});
  387. var decoded;
  388. try {
  389. decoded = object.decodePayload(buf, opts);
  390. } catch(e) {
  391. return e;
  392. }
  393. if (opts.stream && decoded.stream !== opts.stream) {
  394. return new Error(
  395. "The stream number " + opts.stream +
  396. " is not the one we are interested in"
  397. );
  398. }
  399. },
  400. /**
  401. * Encode `object` message.
  402. * @param {Object} opts - Object options
  403. * @param {Object} opts.nonce - A 8-byte object nonce
  404. * @param {number} opts.ttl - Time to live in seconds
  405. * @param {number} opts.type - Object type
  406. * @param {number} opts.version - Object version
  407. * @param {number=} opts.stream - Object stream (1 by default)
  408. * @param {Buffer} opts.objectPayload - Object payload
  409. * @return {Buffer} Encoded message.
  410. */
  411. encode: function(opts) {
  412. var payload = object.encodePayload(opts);
  413. return message.encode("object", payload);
  414. },
  415. /**
  416. * Encode `object` message payload.
  417. * The same as [encode]{@link module:bitmessage/structs.object.encode}.
  418. */
  419. encodePayload: function(opts) {
  420. // NOTE(Kagami): We do not try to calculate nonce here if it is not
  421. // provided because:
  422. // 1) It's async operation but in `structs` module all operations
  423. // are synchronous.
  424. // 2) It shouldn't be useful because almost all objects signatures
  425. // include object header and POW is computed for entire object so at
  426. // first the object header should be assembled and only then we can
  427. // do a POW.
  428. assert(opts.nonce.length === 8, "Bad nonce");
  429. // NOTE(Kagami): This may be a bit inefficient since we allocate
  430. // twice.
  431. return Buffer.concat([
  432. opts.nonce,
  433. object.encodePayloadWithoutNonce(opts),
  434. ]);
  435. },
  436. /**
  437. * Encode `object` message payload without leading nonce field (may be
  438. * useful if you are going to calculate it later).
  439. * @param {Object} opts - Object options
  440. * @param {number} opts.ttl - Time to live in seconds
  441. * @param {number} opts.type - Object type
  442. * @param {number} opts.version - Object version
  443. * @param {number=} opts.stream - Object stream (1 by default)
  444. * @param {Buffer} opts.objectPayload - Object payload
  445. * @return {Buffer} Encoded payload.
  446. */
  447. encodePayloadWithoutNonce: function(opts) {
  448. assert(opts.ttl > 0, "Bad TTL");
  449. assert(opts.ttl <= 2430000, "TTL may not be larger than 28 days + 3 hours");
  450. var expiresTime = util.tnow() + opts.ttl;
  451. var type = new Buffer(4);
  452. type.writeUInt32BE(opts.type, 0);
  453. var stream = opts.stream || 1;
  454. var obj = Buffer.concat([
  455. util.writeUInt64BE(null, expiresTime),
  456. type,
  457. var_int.encode(opts.version),
  458. var_int.encode(stream),
  459. opts.objectPayload,
  460. ]);
  461. assert(obj.length <= 262136, "object message payload is too big");
  462. return obj;
  463. },
  464. };
  465. /**
  466. * Variable length integer.
  467. * @see {@link https://bitmessage.org/wiki/Protocol_specification#Variable_length_integer}
  468. * @namespace
  469. * @static
  470. */
  471. var var_int = exports.var_int = {
  472. /**
  473. * @typedef {Object} DecodeResult
  474. * @property {number} value - Stored value
  475. * @property {number} length - `var_int` full length
  476. * @property {Buffer} rest - The rest of the input buffer
  477. * @memberof module:bitmessage/structs.var_int
  478. */
  479. /**
  480. * Decode `var_int`.
  481. * NOTE: `rest` references input buffer.
  482. * @param {Buffer} buf - A buffer that starts with encoded `var_int`
  483. * @return {DecodeResult}
  484. * [Decoded `var_int` structure.]{@link module:bitmessage/structs.var_int.DecodeResult}
  485. */
  486. decode: function(buf) {
  487. var value, length;
  488. assert(buf.length > 0, "Empty buffer");
  489. switch (buf[0]) {
  490. case 253:
  491. value = buf.readUInt16BE(1);
  492. assert(value >= 253, "Impractical var_int");
  493. length = 3;
  494. break;
  495. case 254:
  496. value = buf.readUInt32BE(1);
  497. assert(value >= 65536, "Impractical var_int");
  498. length = 5;
  499. break;
  500. case 255:
  501. var hi = buf.readUInt32BE(1);
  502. assert(hi !== 0, "Impractical var_int");
  503. // Max safe number = 2^53 - 1 =
  504. // 0b0000000000011111111111111111111111111111111111111111111111111111
  505. // = 2097151*(2^32) + (2^32 - 1).
  506. // So it's safe until hi <= 2097151. See
  507. // <http://mdn.io/issafeinteger>,
  508. // <https://stackoverflow.com/q/307179> for details.
  509. // TODO(Kagami): We may want to return raw Buffer for
  510. // 2^53 <= value <= 2^64 - 1 range. Probably using the optional
  511. // argument because most of the code expect to get a number when
  512. // calling `var_int.decode`.
  513. assert(hi <= 2097151, "Unsafe integer");
  514. var lo = buf.readUInt32BE(5);
  515. value = hi * 4294967296 + lo;
  516. length = 9;
  517. break;
  518. default:
  519. value = buf[0];
  520. length = 1;
  521. }
  522. var rest = buf.slice(length);
  523. return {value: value, length: length, rest: rest};
  524. },
  525. /**
  526. * Encode number into `var_int`.
  527. * @param {(number|Buffer)} value - Input number
  528. * @return {Buffer} Encoded `var_int`.
  529. */
  530. encode: function(value) {
  531. var buf, targetStart;
  532. if (typeof value === "number") {
  533. assert(value >= 0, "Value cannot be less than zero");
  534. if (value < 253) {
  535. buf = new Buffer([value]);
  536. } else if (value < 65536) {
  537. buf = new Buffer(3);
  538. buf[0] = 253;
  539. buf.writeUInt16BE(value, 1, true);
  540. } else if (value < 4294967296) {
  541. buf = new Buffer(5);
  542. buf[0] = 254;
  543. buf.writeUInt32BE(value, 1, true);
  544. } else {
  545. assert(value <= 9007199254740991, "Unsafe integer");
  546. buf = new Buffer(9);
  547. buf[0] = 255;
  548. buf.writeUInt32BE(Math.floor(value / 4294967296), 1, true); // high32
  549. buf.writeUInt32BE(value % 4294967296, 5, true); // low32
  550. }
  551. } else if (Buffer.isBuffer(value)) {
  552. assert(value.length <= 8, "Buffer is too big");
  553. buf = new Buffer(9);
  554. buf.fill(0);
  555. buf[0] = 255;
  556. targetStart = 1 + (8 - value.length);
  557. value.copy(buf, targetStart);
  558. } else {
  559. throw new Error("Unknown value type");
  560. }
  561. return buf;
  562. },
  563. };
  564. /**
  565. * Variable length string.
  566. * @see {@link https://bitmessage.org/wiki/Protocol_specification#Variable_length_string}
  567. * @namespace
  568. */
  569. exports.var_str = {
  570. /**
  571. * @typedef {Object} DecodeResult
  572. * @property {number} str - The string itself
  573. * @property {number} length - `var_str` full length
  574. * @property {Buffer} rest - The rest of the input buffer
  575. * @memberof module:bitmessage/structs.var_str
  576. */
  577. /**
  578. * Decode `var_str`.
  579. * NOTE: `rest` references input buffer.
  580. * @param {Buffer} buf - A buffer that starts with encoded `var_str`
  581. * @return {DecodeResult}
  582. * [Decoded `var_str` structure.]{@link module:bitmessage/structs.var_str.DecodeResult}
  583. */
  584. decode: function(buf) {
  585. var decoded = var_int.decode(buf);
  586. var strLength = decoded.value;
  587. var length = decoded.length + strLength;
  588. assert(buf.length >= length, "Buffer is too small");
  589. // XXX(Kagami): Spec doesn't mention encoding, using UTF-8.
  590. var str = decoded.rest.slice(0, strLength).toString("utf8");
  591. var rest = decoded.rest.slice(strLength);
  592. return {str: str, length: length, rest: rest};
  593. },
  594. /**
  595. * Encode string into `var_str`.
  596. * @param {string} str - A string
  597. * @return {Buffer} Encoded `var_str`.
  598. */
  599. encode: function(str) {
  600. // XXX(Kagami): Spec doesn't mention encoding, using UTF-8.
  601. var strBuf = new Buffer(str, "utf8");
  602. return Buffer.concat([var_int.encode(strBuf.length), strBuf]);
  603. },
  604. };
  605. /**
  606. * Variable length list of integers.
  607. * @see {@link https://bitmessage.org/wiki/Protocol_specification#Variable_length_list_of_integers}
  608. * @namespace
  609. */
  610. exports.var_int_list = {
  611. /**
  612. * @typedef {Object} DecodeResult
  613. * @property {number} list - Stored numbers
  614. * @property {number} length - `var_int_list` full length
  615. * @property {Buffer} rest - The rest of the input buffer
  616. * @memberof module:bitmessage/structs.var_int_list
  617. */
  618. /**
  619. * Decode `var_int_list`.
  620. * NOTE: `rest` references input buffer.
  621. * @param {Buffer} buf - A buffer that starts with encoded
  622. * `var_int_list`
  623. * @return {DecodeResult}
  624. * [Decoded `var_int_list` structure.]{@link module:bitmessage/structs.var_int_list.DecodeResult}
  625. */
  626. decode: function(buf) {
  627. var decoded = var_int.decode(buf);
  628. var listLength = decoded.value;
  629. var list = new Array(listLength);
  630. var rest = decoded.rest;
  631. var sumLength = decoded.length;
  632. for (var i = 0; i < listLength; i++) {
  633. decoded = var_int.decode(rest);
  634. list[i] = decoded.value;
  635. rest = decoded.rest;
  636. sumLength += decoded.length;
  637. }
  638. return {list: list, length: sumLength, rest: rest};
  639. },
  640. /**
  641. * Encode list of numbers into `var_int_list`.
  642. * @param {number[]} list - A number list
  643. * @return {Buffer} Encoded `var_int_list`.
  644. */
  645. encode: function(list) {
  646. var var_ints = list.map(var_int.encode);
  647. var bufs = [var_int.encode(list.length)].concat(var_ints);
  648. return Buffer.concat(bufs);
  649. },
  650. };
  651. // Very simple inet_ntop(3) equivalent.
  652. function inet_ntop(buf) {
  653. assert(buf.length === 16, "Bad buffer size");
  654. // IPv4 mapped to IPv6.
  655. if (bufferEqual(buf.slice(0, 12), IPv4_MAPPING)) {
  656. return Array.prototype.join.call(buf.slice(12), ".");
  657. // IPv6.
  658. } else {
  659. // TODO(Kagami): Join empty groups to make address looks nicer.
  660. var groups = [];
  661. for (var i = 0; i < 8; i++) {
  662. groups.push(buf.readUInt16BE(i * 2, true).toString(16));
  663. }
  664. return groups.join(":");
  665. }
  666. }
  667. /**
  668. * Network address.
  669. * @see {@link https://bitmessage.org/wiki/Protocol_specification#Network_address}
  670. * @namespace
  671. */
  672. exports.net_addr = {
  673. /**
  674. * @typedef {Object} DecodeResult
  675. * @property {Date} time - Time the node was last active, not included
  676. * in short mode
  677. * @property {number} stream - Stream number of the node, not included
  678. * in short mode
  679. * @property {Object} services -
  680. * [Services]{@link module:bitmessage/structs.ServicesBitfield}
  681. * provided by the node
  682. * @property {string} host - IPv4/IPv6 address of the node
  683. * @property {number} port - Incoming port of the node
  684. * @memberof module:bitmessage/structs.net_addr
  685. */
  686. /**
  687. * Decode `net_addr`.
  688. * @param {Buffer} buf - A buffer that contains encoded `net_addr`
  689. * @param {Object=} opts - Decoding options; use `short` option to
  690. * decode `net_addr` from
  691. * [version message]{@link module:bitmessage/messages.version}
  692. * @return {DecodeResult}
  693. * [Decoded `net_addr` structure.]{@link module:bitmessage/structs.net_addr.DecodeResult}
  694. */
  695. decode: function(buf, opts) {
  696. var short = !!(opts || {}).short;
  697. var res = {};
  698. if (short) {
  699. assert(buf.length === 26, "Bad buffer size");
  700. } else {
  701. assert(buf.length === 38, "Bad buffer size");
  702. var timeHi = buf.readUInt32BE(0, true);
  703. var timeLo = buf.readUInt32BE(4, true);
  704. // JavaScript's Date object can't work with timestamps higher than
  705. // 8640000000000 (~2^43, ~275760 year). Hope JavaScript will
  706. // support 64-bit numbers up to this date.
  707. assert(timeHi <= 2011, "Time is too high");
  708. assert(timeHi !== 2011 || timeLo <= 2820767744, "Time is too high");
  709. res.time = new Date((timeHi * 4294967296 + timeLo) * 1000);
  710. res.stream = buf.readUInt32BE(8, true);
  711. buf = buf.slice(12);
  712. }
  713. res.services = ServicesBitfield(buf.slice(0, 8), {copy: true});
  714. res.host = inet_ntop(buf.slice(8, 24));
  715. res.port = buf.readUInt16BE(24, true);
  716. return res;
  717. },
  718. /**
  719. * Encode `net_addr`.
  720. * @param {Object} opts - Encoding options
  721. * @param {boolean=} opts.short - Encode `net_addr` for
  722. * [version message]{@link module:bitmessage/messages.version}
  723. * (false by default)
  724. * @param {Date=} opts.time - Time the node was last active, not
  725. * included in short mode (current time by default)
  726. * @param {number=} opts.stream - Stream number of the node, not
  727. * included in short mode (1 by default)
  728. * @param {(Object|Buffer)=} opts.services -
  729. * [Services]{@link module:bitmessage/structs.ServicesBitfield}
  730. * provided by the node (`NODE_NETWORK` by default)
  731. * @param {string} opts.host - IPv4/IPv6 address of the node
  732. * @param {number} opts.port - Incoming port of the node
  733. * @return {Buffer} Encoded `net_addr`.
  734. */
  735. encode: function(opts) {
  736. // Be aware of `Buffer.slice` quirk in browserify:
  737. // <http://git.io/lNZF1A> (does not modify parent buffer's memory in
  738. // old browsers). So we use offset instead of `buf = buf.slice`.
  739. var buf, shift;
  740. if (opts.short) {
  741. buf = new Buffer(26);
  742. shift = 0;
  743. } else {
  744. buf = new Buffer(38);
  745. var time = opts.time || new Date();
  746. time = Math.floor(time.getTime() / 1000);
  747. buf.writeUInt32BE(Math.floor(time / 4294967296), 0, true); // high32
  748. buf.writeUInt32BE(time % 4294967296, 4, true); // low32
  749. var stream = opts.stream || 1;
  750. buf.writeUInt32BE(stream, 8);
  751. shift = 12;
  752. }
  753. var services = opts.services ||
  754. ServicesBitfield().set(ServicesBitfield.NODE_NETWORK);
  755. if (Buffer.isBuffer(services)) {
  756. assert(services.length === 8, "Bad services buffer length");
  757. } else {
  758. services = services.buffer;
  759. }
  760. services.copy(buf, shift);
  761. inet_pton(opts.host).copy(buf, shift + 8);
  762. buf.writeUInt16BE(opts.port, shift + 24);
  763. return buf;
  764. },
  765. };
  766. /**
  767. * Inventory vector.
  768. * @see {@link https://bitmessage.org/wiki/Protocol_specification#Inventory_Vectors}
  769. * @namespace
  770. */
  771. exports.inv_vect = {
  772. // NOTE(Kagami): Only encode operation is defined because decoding of
  773. // the encoded vector is impossible.
  774. /**
  775. * Encode inventory vector.
  776. * @param {Buffer} buf - Payload to calculate the inventory vector for
  777. * @return {Buffer} A 32-byte encoded `inv_vect`.
  778. */
  779. encode: function(buf) {
  780. return bmcrypto.sha512(bmcrypto.sha512(buf)).slice(0, 32);
  781. },
  782. };
  783. /**
  784. * Encrypted payload.
  785. * @see {@link https://bitmessage.org/wiki/Protocol_specification#Encrypted_payload}
  786. * @namespace encrypted
  787. * @static
  788. */
  789. /**
  790. * @typedef {Object} DecodeResult
  791. * @property {Buffer} iv - Initialization vector (16 bytes)
  792. * @property {Buffer} ephemPrivateKey - Ephemeral private key (32 bytes)
  793. * @property {Buffer} ciphertext - The result of encryption (variable
  794. * size)
  795. * @property {Buffer} mac - Message authentication code (32 bytes)
  796. * @memberof module:bitmessage/structs.encrypted
  797. */
  798. /**
  799. * Decode encrypted payload.
  800. * NOTE: all structure members are copied.
  801. * @param {Buffer} buf - A buffer that contains encrypted payload
  802. * @return {DecodeResult}
  803. * [Decoded `encrypted` structure.]{@link module:bitmessage/structs.encrypted.DecodeResult}
  804. * @function decode
  805. * @memberof module:bitmessage/structs.encrypted
  806. */
  807. /**
  808. * Encode `encrypted`.
  809. * @param {Object} opts - Encoding options
  810. * @param {Buffer} opts.iv - Initialization vector (16 bytes)
  811. * @param {Buffer} opts.ephemPrivateKey - Ephemeral private key (32
  812. * bytes)
  813. * @param {Buffer} opts.ciphertext - The result of encryption (variable
  814. * size)
  815. * @param {Buffer} opts.mac - Message authentication code (32 bytes)
  816. * @return {Buffer} Encoded `encrypted` payload.
  817. * @function encode
  818. * @memberof module:bitmessage/structs.encrypted
  819. */
  820. // Reexport struct.
  821. exports.encrypted = bmcrypto.encrypted;
  822. // Creates bitfield (MSB 0) class of the specified size.
  823. var Bitfield = function(size) {
  824. var bytesize = size / 8;
  825. // Inspired by <https://github.com/fb55/bitfield>.
  826. function BitfieldInner(buf, opts) {
  827. if (!(this instanceof BitfieldInner)) {
  828. return new BitfieldInner(buf);
  829. }
  830. opts = opts || {};
  831. if (buf) {
  832. assert(buf.length === bytesize, "Bad buffer size");
  833. if (opts.copy) {
  834. var dup = new Buffer(bytesize);
  835. dup.fill(0);
  836. buf.copy(dup);
  837. buf = dup;
  838. }
  839. } else {
  840. buf = new Buffer(bytesize);
  841. buf.fill(0);
  842. }
  843. this.buffer = buf;
  844. }
  845. BitfieldInner.prototype.get = function(bits) {
  846. if (!Array.isArray(bits)) {
  847. bits = [bits];
  848. }
  849. var buf = this.buffer;
  850. return bits.every(function(bit) {
  851. assert(bit >= 0, "Bit number is too low");
  852. assert(bit < size, "Bit number is too high");
  853. var index = Math.floor(bit / 8);
  854. var shift = 7 - (bit % 8);
  855. return (buf[index] & (1 << shift)) !== 0; // jshint ignore:line
  856. });
  857. };
  858. BitfieldInner.prototype.set = function(bits) {
  859. if (!Array.isArray(bits)) {
  860. bits = [bits];
  861. }
  862. var buf = this.buffer;
  863. bits.forEach(function(bit) {
  864. assert(bit >= 0, "Bit number is too low");
  865. assert(bit < size, "Bit number is too high");
  866. var index = Math.floor(bit / 8);
  867. var shift = 7 - (bit % 8);
  868. buf[index] |= 1 << shift; // jshint ignore:line
  869. });
  870. return this;
  871. };
  872. BitfieldInner.prototype.toString = function() {
  873. var i;
  874. var str = "";
  875. for (i = 0; i < this.buffer.length; i++) {
  876. // Should be faster than pushing to array and joining on v8.
  877. str += ("0000000" + this.buffer[i].toString(2)).slice(-8);
  878. }
  879. return "<Bitfield:" + str + ">";
  880. };
  881. return BitfieldInner;
  882. };
  883. /**
  884. * Service features bitfield (MSB 0).
  885. * @see {@link https://bitmessage.org/wiki/Protocol_specification#version}
  886. * @param {Buffer=} buf - A 8-byte bitfield buffer (will be created if
  887. * not provided or will be copied if `opts.copy` is `true`)
  888. * @param {Object=} opts - Options
  889. * @constructor
  890. * @static
  891. * @example
  892. * var ServicesBitfield = require("bitmessage").structs.ServicesBitfield;
  893. * var services = ServicesBitfield().set(ServicesBitfield.NODE_NETWORK);
  894. * console.log(services.get(ServicesBitfield.NODE_NETWORK)); // true
  895. * console.log(services.get(15)); // false
  896. */
  897. // NOTE(Kagami): Since pubkey bitfield uses MSB 0, we use it here too.
  898. // See <https://github.com/Bitmessage/PyBitmessage/issues/769> for
  899. // details.
  900. var ServicesBitfield = exports.ServicesBitfield = objectAssign(Bitfield(64), {
  901. /**
  902. * Returns a boolean indicating whether the bit is set.
  903. * @param {number} index - Bit index (MSB 0)
  904. * @function get
  905. * @instance
  906. * @return {boolean}
  907. * @memberof module:bitmessage/structs.ServicesBitfield
  908. */
  909. /**
  910. * Set the given bit(s) to `1`.
  911. * @param {(number|number[])} index - Bit(s) index (MSB 0)
  912. * @function set
  913. * @instance
  914. * @return {Object} Returns self so methods can be chained.
  915. * @memberof module:bitmessage/structs.ServicesBitfield
  916. */
  917. /**
  918. * The contents of the bitfield.
  919. * @type {Buffer}
  920. * @var buffer
  921. * @instance
  922. * @memberof module:bitmessage/structs.ServicesBitfield
  923. */
  924. /**
  925. * Bit index indicating normal network node.
  926. * @memberof module:bitmessage/structs.ServicesBitfield
  927. * @constant {number}
  928. */
  929. NODE_NETWORK: 63,
  930. /**
  931. * Bit index indicating web/mobile client with limited network
  932. * capabilities (proposal feature).
  933. * @memberof module:bitmessage/structs.ServicesBitfield
  934. * @see {@link https://bitmessage.org/wiki/Mobile_Protocol_specification}
  935. * @constant {number}
  936. */
  937. NODE_MOBILE: 62,
  938. /**
  939. * Bit index indicating node which can work as a WebSocket gateway for
  940. * web/mobile clients (proposal feature).
  941. * @memberof module:bitmessage/structs.ServicesBitfield
  942. * @see {@link https://bitmessage.org/wiki/Mobile_Protocol_specification}
  943. * @constant {number}
  944. */
  945. NODE_GATEWAY: 61,
  946. });
  947. /**
  948. * Pubkey features bitfield (MSB 0).
  949. * @see {@link https://bitmessage.org/wiki/Protocol_specification#Pubkey_bitfield_features}
  950. * @param {Buffer=} buf - A 4-byte bitfield buffer (will be created if
  951. * not provided or will be copied if `opts.copy` is `true`)
  952. * @param {Object=} opts - Options
  953. * @constructor
  954. * @example
  955. * var PubkeyBitfield = require("bitmessage").structs.PubkeyBitfield;
  956. * var behavior = PubkeyBitfield().set([
  957. * PubkeyBitfield.INCLUDE_DESTINATION,
  958. * PubkeyBitfield.DOES_ACK,
  959. * ]).set(1);
  960. * console.log(behavior.get(PubkeyBitfield.DOES_ACK)); // true
  961. * console.log(behavior.get(15)); // false
  962. */
  963. exports.PubkeyBitfield = objectAssign(Bitfield(32), {
  964. /**
  965. * Returns a boolean indicating whether the bit is set.
  966. * @param {number} index - Bit index (MSB 0)
  967. * @function get
  968. * @instance
  969. * @return {boolean}
  970. * @memberof module:bitmessage/structs.PubkeyBitfield
  971. */
  972. /**
  973. * Set the given bit(s) to `1`.
  974. * @param {(number|number[])} index - Bit(s) index (MSB 0)
  975. * @function set
  976. * @instance
  977. * @return {Object} Returns self so methods can be chained.
  978. * @memberof module:bitmessage/structs.PubkeyBitfield
  979. */
  980. /**
  981. * The contents of the bitfield.
  982. * @type {Buffer}
  983. * @var buffer
  984. * @instance
  985. * @memberof module:bitmessage/structs.PubkeyBitfield
  986. */
  987. /**
  988. * Bit index.
  989. * If set, the receiving node does send acknowledgements (rather than
  990. * dropping them).
  991. * @memberof module:bitmessage/structs.PubkeyBitfield
  992. * @constant {number}
  993. */
  994. DOES_ACK: 31,
  995. /**
  996. * Bit index.
  997. * If set, the receiving node expects that the RIPEMD hash encoded in
  998. * their address preceedes the encrypted message data of msg messages
  999. * bound for them.
  1000. * @memberof module:bitmessage/structs.PubkeyBitfield
  1001. * @constant {number}
  1002. */
  1003. INCLUDE_DESTINATION: 30,
  1004. });