Skip to content

Commit

Permalink
move checksum to a util function
Browse files Browse the repository at this point in the history
  • Loading branch information
LePremierHomme committed Jan 14, 2021
1 parent 725f380 commit 6d167d9
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
3 changes: 2 additions & 1 deletion lib/p2p/Framer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { randomBytes } from '../utils/cryptoUtils';
import errors from './errors';
import Network from './Network';
import Packet from './packets/Packet';
import { calcChecksum } from './packets/utils';

type WireMsgHeader = {
magic?: number;
Expand Down Expand Up @@ -69,7 +70,7 @@ class Framer {
msg.writeUInt32LE(packet.type, 8);

// checksum
msg.writeUInt32LE(packet.checksum(packetRaw), 12);
msg.writeUInt32LE(calcChecksum(packetRaw), 12);

// payload
packetRaw.copy(msg, 16);
Expand Down
3 changes: 2 additions & 1 deletion lib/p2p/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Framer, { WireMsgHeader } from './Framer';
import { PacketType } from './packets';
import Packet, { isPacket } from './packets/Packet';
import * as packetTypes from './packets/types';
import { calcChecksum } from './packets/utils';

interface Parser {
on(event: 'packet', packet: (order: Packet) => void): this;
Expand Down Expand Up @@ -181,7 +182,7 @@ class Parser extends EventEmitter {
}

const packet = packetOrPbObj;
if (header.checksum && header.checksum !== packet.checksum(payload)) {
if (header.checksum && header.checksum !== calcChecksum(payload)) {
throw errors.PARSER_DATA_INTEGRITY_ERR(`${PacketType[header.type]} ${JSON.stringify(packet)}`);
}

Expand Down
8 changes: 0 additions & 8 deletions lib/p2p/packets/Packet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { createHash } from 'crypto';
import uuidv1 from 'uuid/v1';
import PacketType from './PacketType';

Expand Down Expand Up @@ -97,13 +96,6 @@ abstract class Packet<T = any> implements PacketInterface {
public toRaw = (): Buffer => {
return Buffer.from(this.serialize().buffer as ArrayBuffer);
};

/**
* Calculating the packet checksum using its binary representation hash first 4 bytes.
*/
public checksum = (bytes: Uint8Array): number => {
return createHash('sha256').update(bytes).digest().readUInt32LE(0);
};
}

function isPacket(val: any): val is Packet {
Expand Down
8 changes: 8 additions & 0 deletions lib/p2p/packets/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { createHash } from "crypto";
import * as pb from '../../proto/xudp2p_pb';
import { removeUndefinedProps, convertKvpArrayToKvps, setObjectToMap } from '../../utils/utils';
import { NodeState } from '../types';
Expand Down Expand Up @@ -67,3 +68,10 @@ export const serializeNodeState = (nodeState: NodeState): pb.NodeState => {
}
return pbNodeState;
};

/**
* Calculate checksum using the bytes sha256-hash first 4 bytes.
*/
export const calcChecksum = (bytes: Uint8Array): number => {
return createHash('sha256').update(bytes).digest().readUInt32LE(0);
};

0 comments on commit 6d167d9

Please sign in to comment.