Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { BufferReader, BufferWriter } from '@node-dlc/bufio';
import { crc32c } from '@node-dlc/checksum';
import { MessageType } from '../MessageType';
const MAX_DATA_LEN = 190;
export abstract class IrcMessage {
public static deserialize(buf: Buffer): IrcMessageV0 {
const reader = new BufferReader(buf);
const type = Number(reader.readUInt16BE());
switch (type) {
case MessageType.IrcMessageV0:
return IrcMessageV0.deserialize(buf);
default:
throw new Error(`Irc message type must be IrcMessageV0`);
}
}
public abstract type: number;
public abstract serialize(): Buffer;
}
/**
* IrcMessage contains information for Irc message packet.
*/
export class IrcMessageV0 extends IrcMessage {
public static type = MessageType.IrcMessageV0;
public static fromBuffer(buf: Buffer, pubkey: Buffer): IrcMessageV0[] {
const instances: IrcMessageV0[] = [];
const checksum = crc32c(buf);
const sequenceLength = Math.ceil(buf.length / MAX_DATA_LEN);
const reader = new BufferReader(buf);
const bufLen = buf.length;
const currentTime = Math.floor(new Date().getTime() / 1000);
for (let i = 0; i < sequenceLength; i++) {
const sequenceNumber = i;
const instance = new IrcMessageV0();
instance.sequenceLength = BigInt(sequenceLength);
instance.sequenceNumber = BigInt(sequenceNumber);
instance.checksum = checksum;
instance.pubkey = pubkey;
instance.timestamp = currentTime;
instance.data = reader.readBytes(
Math.min(MAX_DATA_LEN, bufLen - reader.position),
);
instances.push(instance);
}
return instances;
}
public static fromString(str: string, pubkey: Buffer): IrcMessageV0[] {
const buf = Buffer.from(str);
return IrcMessageV0.fromBuffer(buf, pubkey);
}
/**
* Deserializes an irc message
* @param buf
*/
public static deserialize(buf: Buffer): IrcMessageV0 {
const instance = new IrcMessageV0();
const reader = new BufferReader(buf);
reader.readBigSize(); // read type
instance.length = reader.readBigSize();
instance.sequenceLength = reader.readBigSize();
instance.sequenceNumber = reader.readBigSize();
instance.checksum = reader.readUInt32BE();
instance.signature = reader.readBytes(64);
instance.pubkey = reader.readBytes(33);
instance.timestamp = reader.readUInt32BE();
const dataLen = reader.readUInt16BE();
instance.data = reader.readBytes(dataLen);
return instance;
}
public type = IrcMessageV0.type;
public length: bigint;
public sequenceLength: bigint;
public sequenceNumber: bigint;
public checksum: number;
public signature: Buffer;
public pubkey: Buffer;
public timestamp: number;
public data: Buffer;
public serializeWithoutSig(): Buffer {
const writer = new BufferWriter();
writer.writeBigSize(this.sequenceLength);
writer.writeBigSize(this.sequenceNumber);
writer.writeUInt64BE(this.checksum);
writer.writeBytes(this.pubkey);
writer.writeUInt32BE(this.timestamp);
writer.writeUInt16BE(this.data.length);
writer.writeBytes(this.data);
return writer.toBuffer();
}
/**
* Serializes the irc_message_v0
*/
public serialize(): Buffer {
const writer = new BufferWriter();
writer.writeBigSize(this.type);
const dataWriter = new BufferWriter();
dataWriter.writeBigSize(this.sequenceLength);
dataWriter.writeBigSize(this.sequenceNumber);
dataWriter.writeUInt32BE(this.checksum);
dataWriter.writeBytes(this.signature);
dataWriter.writeBytes(this.pubkey);
dataWriter.writeUInt32BE(this.timestamp);
dataWriter.writeUInt16BE(this.data.length);
dataWriter.writeBytes(this.data);
writer.writeBigSize(dataWriter.size);
writer.writeBytes(dataWriter.toBuffer());
return writer.toBuffer();
}
}
export class IrcMessageWithoutSig {
constructor(
readonly sequenceLength: bigint,
readonly sequenceNumber: bigint,
readonly checksum: number,
readonly signature: Buffer,
readonly pubkey: Buffer,
readonly data: Buffer,
) {}
}
|