xud
Version:
Exchange Union Daemon
91 lines • 3.63 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.isPacketTypeArray = exports.isPacketType = exports.isPacket = exports.PacketDirection = void 0;
const PacketType_1 = __importDefault(require("./PacketType"));
const crypto_1 = require("crypto");
const v1_1 = __importDefault(require("uuid/v1"));
const json_stable_stringify_1 = __importDefault(require("json-stable-stringify"));
function isPacketInterface(obj) {
if (obj) {
const header = obj.header;
return header !== undefined && typeof header.id === 'string';
}
return false;
}
var PacketDirection;
(function (PacketDirection) {
/** A packet that is pushed to a peer without expecting any response. */
PacketDirection[PacketDirection["Unilateral"] = 0] = "Unilateral";
/** A packet requesting a response. */
PacketDirection[PacketDirection["Request"] = 1] = "Request";
/** A packet that is sent in response to an incoming packet. */
PacketDirection[PacketDirection["Response"] = 2] = "Response";
})(PacketDirection || (PacketDirection = {}));
exports.PacketDirection = PacketDirection;
function isPacketType(val) {
return val !== undefined && typeof val === 'number' && PacketType_1.default[val] !== undefined;
}
exports.isPacketType = isPacketType;
function isPacketTypeArray(val) {
return val !== undefined && val instanceof Array && val.every(v => isPacketType(v));
}
exports.isPacketTypeArray = isPacketTypeArray;
/**
* Represents a packet of data that can be transmitted as part of the p2p xud protocol. Packets
* are serialized using protobuf, optionally encrypted, and transmitted to peers. Each packet
* represents a discrete chunk of information that either sends data to or requests data from a
* peer.
*/
class Packet {
constructor(bodyOrPacket, reqId) {
this.toJSON = () => {
return json_stable_stringify_1.default({ header: this.header, body: this.body });
};
/**
* Serialize this packet to binary Buffer.
* @returns Buffer representation of the packet
*/
this.toRaw = () => {
return Buffer.from(this.serialize().buffer);
};
/**
* Calculating the packet checksum using its JSON representation hash first 4 bytes.
*/
this.checksum = () => {
return crypto_1.createHash('sha256')
.update(this.toJSON())
.digest()
.readUInt32LE(0);
};
if (isPacketInterface(bodyOrPacket)) {
// we are constructing the deserialized packet
this.header = bodyOrPacket.header;
if (bodyOrPacket.body) {
this.body = bodyOrPacket.body;
}
}
else {
// we are constructing a new outgoing packet from a body
this.header = { id: v1_1.default() };
if (reqId) {
this.header.reqId = reqId;
}
if (bodyOrPacket) {
this.body = bodyOrPacket;
}
}
}
}
function isPacket(val) {
const p = val;
return (p.toRaw !== undefined && typeof p.toRaw === 'function'
&& p.serialize !== undefined && typeof p.serialize === 'function'
&& p.type !== undefined && typeof p.type === 'number'
&& p.direction !== undefined && typeof p.direction === 'number');
}
exports.isPacket = isPacket;
exports.default = Packet;
//# sourceMappingURL=Packet.js.map