@neo-one/node-protocol-esnext-esm
Version:
NEO•ONE NEO node and consensus protocol.
63 lines (61 loc) • 2.25 kB
JavaScript
import { createSerializeWire, InvalidFormatError, } from '@neo-one/client-common-esnext-esm';
import { BinaryReader } from '@neo-one/node-core-esnext-esm';
import { Address6 } from 'ip-address';
const BYTE_LENGTH = 16;
export class NetworkAddress {
constructor({ host, port, timestamp, services }) {
this.serializeWire = 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 = 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 BinaryReader(options.buffer),
});
}
static isValid(host) {
const address = this.getAddress6(host);
if (address == undefined) {
return false;
}
try {
address.toByteArray();
return true;
}
catch {
return false;
}
}
static getAddress6(host) {
const parts = host.split('.');
return parts.length === 4 ? Address6.fromAddress4(host) : new Address6(host);
}
serializeWireBase(writer) {
const address = NetworkAddress.getAddress6(this.host);
if (address == undefined) {
throw new 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);
}
}
//# sourceMappingURL=NetworkAddress.js.map