@node-dlc/messaging
Version:
DLC Messaging Protocol
46 lines • 1.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Address = void 0;
const bufio_1 = require("@node-dlc/bufio");
const MessageType_1 = require("../MessageType");
class Address {
static deserialize(buf) {
const reader = new bufio_1.BufferReader(buf);
reader.readBigSize(); // read off type
reader.readBigSize(); // read size
const hostLen = reader.readBigSize();
const hostBuf = reader.readBytes(Number(hostLen));
const host = hostBuf.toString();
const port = reader.readUInt16BE();
const instance = new Address(host, port);
return instance;
}
/**
* Base class representing a network address
*/
constructor(host, port) {
this.type = Address.type;
this.host = host;
this.port = port;
}
toString() {
return `${this.host}:${this.port}`;
}
/**
* Serializes the dlc_transactions_v0 message into a Buffer
*/
serialize() {
const writer = new bufio_1.BufferWriter();
writer.writeBigSize(this.type);
const dataWriter = new bufio_1.BufferWriter();
dataWriter.writeBigSize(this.host.length);
dataWriter.writeBytes(Buffer.from(this.host));
dataWriter.writeUInt16BE(this.port);
writer.writeBigSize(dataWriter.size);
writer.writeBytes(dataWriter.toBuffer());
return writer.toBuffer();
}
}
exports.Address = Address;
Address.type = MessageType_1.MessageType.NodeAnnouncementAddress;
//# sourceMappingURL=Address.js.map