@neo-one/node-protocol
Version:
NEO•ONE NEO node and consensus protocol.
67 lines (65 loc) • 2.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NetworkAddress = void 0;
const client_common_1 = require("@neo-one/client-common");
const node_core_1 = require("@neo-one/node-core");
const ip_address_1 = require("ip-address");
const BYTE_LENGTH = 16;
class NetworkAddress {
constructor({ host, port, timestamp, services }) {
this.serializeWire = client_common_1.createSerializeWire(this.serializeWireBase.bind(this));
this.host = host;
this.port = port;
this.timestamp = timestamp;
this.services = services;
}
static deserializeWireBase({ reader }) {
const timestamp = reader.readUInt32LE();
const services = reader.readUInt64LE();
const address = ip_address_1.Address6.fromByteArray([...reader.readBytes(16)]);
const port = reader.readUInt16BE();
const canonical = address == undefined ? '' : address.canonicalForm();
return new this({
timestamp,
services,
host: canonical == undefined ? '' : canonical,
port,
});
}
static deserializeWire(options) {
return this.deserializeWireBase({
context: options.context,
reader: new node_core_1.BinaryReader(options.buffer),
});
}
static isValid(host) {
const address = this.getAddress6(host);
if (address == undefined) {
return false;
}
try {
address.toByteArray();
return true;
}
catch (_a) {
return false;
}
}
static getAddress6(host) {
const parts = host.split('.');
return parts.length === 4 ? ip_address_1.Address6.fromAddress4(host) : new ip_address_1.Address6(host);
}
serializeWireBase(writer) {
const address = NetworkAddress.getAddress6(this.host);
if (address == undefined) {
throw new client_common_1.InvalidFormatError('Network IP address undefined');
}
writer.writeUInt32LE(this.timestamp);
writer.writeUInt64LE(this.services);
const addressSerialized = Buffer.from(address.toByteArray());
writer.writeBytes(Buffer.concat([Buffer.alloc(BYTE_LENGTH - addressSerialized.length, 0), addressSerialized]));
writer.writeUInt16BE(this.port);
}
}
exports.NetworkAddress = NetworkAddress;
//# sourceMappingURL=NetworkAddress.js.map