Skip to content

Commit

Permalink
chore: use the 'debug' package as the new logger
Browse files Browse the repository at this point in the history
  • Loading branch information
omridan159 committed Feb 28, 2024
1 parent 5e8b101 commit ac14808
Show file tree
Hide file tree
Showing 122 changed files with 1,140 additions and 1,269 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
},
"packageManager": "[email protected]",
"dependencies": {
"debug": "^4.3.4",
"execa": "^5.0.0",
"openai": "^4.24.0",
"ts-node": "^10.9.1"
Expand Down
1 change: 1 addition & 0 deletions packages/sdk-communication-layer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"bufferutil": "^4.0.8",
"cross-fetch": "^3.1.5",
"date-fns": "^2.29.3",
"debug": "^4.3.4",
"eciesjs": "^0.3.16",
"eventemitter2": "^6.4.5",
"socket.io-client": "^4.5.1",
Expand Down
64 changes: 29 additions & 35 deletions packages/sdk-communication-layer/src/ECIES.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Buffer } from 'buffer';
import { decrypt, encrypt, PrivateKey } from 'eciesjs';
import debug from 'debug';
import { loggerEciesLayer } from './utils/logger';

/**
* These properties are optional and should only be used during development for debugging purposes.
Expand All @@ -22,11 +24,9 @@ export class ECIES {

private enabled = true;

private debug = false;

constructor(props?: ECIESProps) {
if (props?.debug) {
this.debug = props.debug;
debug.enable('Ecies:Layer');
}

if (props?.pkey) {
Expand All @@ -35,14 +35,16 @@ export class ECIES {
this.ecies = new PrivateKey();
}

if (this.debug) {
console.info(`[ECIES] initialized secret: `, this.ecies.toHex());
console.info(
`[ECIES] initialized public: `,
this.ecies.publicKey.toHex(),
);
console.info(`[ECIES] init with`, this);
}
loggerEciesLayer(
`[ECIES constructor()] initialized secret: `,
this.ecies.toHex(),
);

loggerEciesLayer(
`[ECIES constructor()] initialized public: `,
this.ecies.publicKey.toHex(),
);
loggerEciesLayer(`[ECIES constructor()] init with`, this);
}

/**
Expand Down Expand Up @@ -74,22 +76,18 @@ export class ECIES {
let encryptedString = data;
if (this.enabled) {
try {
if (this.debug) {
console.debug(
`ECIES::encrypt() using otherPublicKey`,
otherPublicKey,
);
}
loggerEciesLayer(
`[ECIES: encrypt()] using otherPublicKey`,
otherPublicKey,
);
const payload = Buffer.from(data);
const encryptedData = encrypt(otherPublicKey, payload);
encryptedString = Buffer.from(encryptedData).toString('base64');
} catch (err) {
if (this.debug) {
console.error(`error encrypt:`, err);
console.error(`private: `, this.ecies.toHex());
console.error('data: ', data);
console.error(`otherkey: `, otherPublicKey);
}
loggerEciesLayer(`[ECIES: encrypt()] error encrypt:`, err);
loggerEciesLayer(`[ECIES: encrypt()] private: `, this.ecies.toHex());
loggerEciesLayer('[ECIES: encrypt()] data: ', data);
loggerEciesLayer(`[ECIES: encrypt()] otherkey: `, otherPublicKey);
throw err;
}
}
Expand All @@ -106,22 +104,18 @@ export class ECIES {
let decryptedString = encryptedData;
if (this.enabled) {
try {
if (this.debug) {
console.debug(
`ECIES::decrypt() using privateKey`,
this.ecies.toHex(),
);
}
loggerEciesLayer(
`[ECIES: decrypt()] using privateKey`,
this.ecies.toHex(),
);
const payload = Buffer.from(encryptedData.toString(), 'base64');
const decrypted = decrypt(this.ecies.toHex(), payload);

decryptedString = decrypted.toString();
} catch (error) {
if (this.debug) {
console.error(`error decrypt`, error);
console.error(`private: `, this.ecies.toHex());
console.error(`encryptedData: `, encryptedData);
}
loggerEciesLayer(`[ECIES: decrypt()] error decrypt`, error);
loggerEciesLayer(`[ECIES: decrypt()] private: `, this.ecies.toHex());
loggerEciesLayer(`[ECIES: decrypt()] encryptedData: `, encryptedData);
throw error;
}
}
Expand All @@ -137,6 +131,6 @@ export class ECIES {
}

toString() {
console.debug(`ECIES::toString()`, this.getKeyInfo());
loggerEciesLayer(`[ECIES: toString()]`, this.getKeyInfo());
}
}
101 changes: 48 additions & 53 deletions packages/sdk-communication-layer/src/KeyExchange.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { EventEmitter2 } from 'eventemitter2';
import debug from 'debug';
import { ECIES, ECIESProps } from './ECIES';
import { CommunicationLayer } from './types/CommunicationLayer';
import { CommunicationLayerMessage } from './types/CommunicationLayerMessage';
Expand All @@ -7,6 +8,7 @@ import { InternalEventType } from './types/InternalEventType';
import { KeyExchangeMessageType } from './types/KeyExchangeMessageType';
import { KeyInfo } from './types/KeyInfo';
import { CommunicationLayerLoggingOptions } from './types/LoggingOptions';
import { loggerKeyExchangeLayer } from './utils/logger';

export interface KeyExchangeProps {
communicationLayer: CommunicationLayer;
Expand Down Expand Up @@ -50,6 +52,10 @@ export class KeyExchange extends EventEmitter2 {
this.myPublicKey = this.myECIES.getPublicKey();
this.debug = logging?.keyExchangeLayer === true;

if (logging?.keyExchangeLayer) {
debug.enable('KeyExchange:Layer');
}

if (otherPublicKey) {
this.setOtherPublicKey(otherPublicKey);
}
Expand All @@ -63,20 +69,16 @@ export class KeyExchange extends EventEmitter2 {
public onKeyExchangeMessage(keyExchangeMsg: {
message: CommunicationLayerMessage;
}) {
if (this.debug) {
console.debug(
`KeyExchange::${this.context}::onKeyExchangeMessage() keysExchanged=${this.keysExchanged}`,
keyExchangeMsg,
);
}
loggerKeyExchangeLayer(
`[KeyExchange: onKeyExchangeMessage()] context=${this.context} keysExchanged=${this.keysExchanged}`,
keyExchangeMsg,
);

const { message } = keyExchangeMsg;
if (this.keysExchanged) {
if (this.debug) {
console.log(
`KeyExchange::${this.context}::onKeyExchangeMessage received handshake while already exchanged. step=${this.step} otherPubKey=${this.otherPublicKey}`,
);
}
loggerKeyExchangeLayer(
`[KeyExchange: onKeyExchangeMessage()] context=${this.context} received handshake while already exchanged. step=${this.step} otherPubKey=${this.otherPublicKey}`,
);
// FIXME check if correct way / when is it really happening?
// return;
}
Expand All @@ -89,9 +91,10 @@ export class KeyExchange extends EventEmitter2 {
KeyExchangeMessageType.KEY_HANDSHAKE_ACK,
]);

if (this.debug) {
console.debug(`KeyExchange::KEY_HANDSHAKE_SYN`, message);
}
loggerKeyExchangeLayer(
`[KeyExchange: onKeyExchangeMessage()] KEY_HANDSHAKE_SYN`,
message,
);

if (message.pubkey) {
this.setOtherPublicKey(message.pubkey);
Expand All @@ -111,9 +114,9 @@ export class KeyExchange extends EventEmitter2 {
KeyExchangeMessageType.KEY_HANDSHAKE_NONE,
]);

if (this.debug) {
console.debug(`KeyExchange::KEY_HANDSHAKE_SYNACK`);
}
loggerKeyExchangeLayer(
`[KeyExchange: onKeyExchangeMessage()] KEY_HANDSHAKE_SYNACK`,
);

if (message.pubkey) {
this.setOtherPublicKey(message.pubkey);
Expand All @@ -127,11 +130,9 @@ export class KeyExchange extends EventEmitter2 {
this.setStep(KeyExchangeMessageType.KEY_HANDSHAKE_ACK);
this.emit(EventType.KEYS_EXCHANGED);
} else if (message.type === KeyExchangeMessageType.KEY_HANDSHAKE_ACK) {
if (this.debug) {
console.debug(
`KeyExchange::KEY_HANDSHAKE_ACK set keysExchanged to true!`,
);
}
loggerKeyExchangeLayer(
`[KeyExchange: onKeyExchangeMessage()] KEY_HANDSHAKE_ACK set keysExchanged to true!`,
);

this.checkStep([
KeyExchangeMessageType.KEY_HANDSHAKE_ACK,
Expand All @@ -150,11 +151,10 @@ export class KeyExchange extends EventEmitter2 {
}

clean(): void {
if (this.debug) {
console.debug(
`KeyExchange::${this.context}::clean reset handshake state`,
);
}
loggerKeyExchangeLayer(
`[KeyExchange: clean()] context=${this.context} reset handshake state`,
);

this.setStep(KeyExchangeMessageType.KEY_HANDSHAKE_NONE);
this.emit(EventType.KEY_INFO, this.step);
this.keysExchanged = false;
Expand All @@ -169,11 +169,9 @@ export class KeyExchange extends EventEmitter2 {
isOriginator: boolean;
force?: boolean;
}): void {
if (this.debug) {
console.debug(
`KeyExchange::${this.context}::start isOriginator=${isOriginator} step=${this.step} force=${force} keysExchanged=${this.keysExchanged}`,
);
}
loggerKeyExchangeLayer(
`[KeyExchange: start()] context=${this.context} isOriginator=${isOriginator} step=${this.step} force=${force} keysExchanged=${this.keysExchanged}`,
);

if (!isOriginator) {
// force is used to redo keyexchange even if already exchanged.
Expand All @@ -183,9 +181,9 @@ export class KeyExchange extends EventEmitter2 {
type: KeyExchangeMessageType.KEY_HANDSHAKE_START,
});
this.clean();
} else if (this.debug) {
console.debug(
`KeyExchange::start don't send KEY_HANDSHAKE_START -- exchange already done.`,
} else {
loggerKeyExchangeLayer(
`[KeyExchange: start()] don't send KEY_HANDSHAKE_START -- exchange already done.`,
);
}

Expand All @@ -199,24 +197,22 @@ export class KeyExchange extends EventEmitter2 {
!force
) {
// Key exchange can be restarted if the wallet ask for a new key.
if (this.debug) {
console.debug(
`KeyExchange::${this.context}::start -- key exchange already ${
this.keysExchanged ? 'done' : 'in progress'
} -- aborted.`,
this.step,
);
}
return;
}

if (this.debug) {
console.debug(
`KeyExchange::${this.context}::start -- start key exchange (force=${force}) -- step=${this.step}`,
loggerKeyExchangeLayer(
`[KeyExchange: start()] context=${
this.context
} -- key exchange already ${
this.keysExchanged ? 'done' : 'in progress'
} -- aborted.`,
this.step,
);
return;
}

loggerKeyExchangeLayer(
`[KeyExchange: start()] context=${this.context} -- start key exchange (force=${force}) -- step=${this.step}`,
this.step,
);

this.clean();
// except a SYN_ACK for next step
this.setStep(KeyExchangeMessageType.KEY_HANDSHAKE_SYNACK);
Expand All @@ -236,7 +232,7 @@ export class KeyExchange extends EventEmitter2 {
if (stepList.length > 0 && stepList.indexOf(this.step.toString()) === -1) {
// Graceful warning but continue communication
console.warn(
`[KeyExchange] Wrong Step "${this.step}" not within ${stepList}`,
`[KeyExchange: checkStep()] Wrong Step "${this.step}" not within ${stepList}`,
);
}
}
Expand All @@ -258,9 +254,8 @@ export class KeyExchange extends EventEmitter2 {
}

setOtherPublicKey(otherPubKey: string) {
if (this.debug) {
console.debug(`KeyExchange::setOtherPubKey()`, otherPubKey);
}
loggerKeyExchangeLayer(`[KeyExchange: setOtherPubKey()]`, otherPubKey);

this.otherPublicKey = otherPubKey;
}

Expand Down
32 changes: 17 additions & 15 deletions packages/sdk-communication-layer/src/RemoteCommunication.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { EventEmitter2 } from 'eventemitter2';
import debug from 'debug';
import packageJson from '../package.json';
import { ECIESProps } from './ECIES';
import {
Expand Down Expand Up @@ -37,6 +38,7 @@ import {
StorageManagerProps,
} from './types/StorageManager';
import { WalletInfo } from './types/WalletInfo';
import { loggerRemoteLayer } from './utils/logger';

type MetaMaskMobile = 'metamask-mobile';

Expand Down Expand Up @@ -156,6 +158,11 @@ export class RemoteCommunication extends EventEmitter2 {
this.state.storageOptions = storage;
this.state.autoConnectOptions = autoConnect;
this.state.debug = logging?.remoteLayer === true;

if (logging?.remoteLayer === true) {
debug.enable('RemoteCommunication:Layer');
}

this.state.logging = logging;

if (storage?.storageManager) {
Expand Down Expand Up @@ -264,21 +271,17 @@ export class RemoteCommunication extends EventEmitter2 {
}

ping() {
if (this.state.debug) {
console.debug(
`RemoteCommunication::ping() channel=${this.state.channelId}`,
);
}
loggerRemoteLayer(
`[RemoteCommunication: ping()] channel=${this.state.channelId}`,
);

this.state.communicationLayer?.ping();
}

keyCheck() {
if (this.state.debug) {
console.debug(
`RemoteCommunication::keyCheck() channel=${this.state.channelId}`,
);
}
loggerRemoteLayer(
`[RemoteCommunication: keyCheck()] channel=${this.state.channelId}`,
);

this.state.communicationLayer?.keyCheck();
}
Expand Down Expand Up @@ -330,11 +333,10 @@ export class RemoteCommunication extends EventEmitter2 {
}

pause() {
if (this.state.debug) {
console.debug(
`RemoteCommunication::pause() channel=${this.state.channelId}`,
);
}
loggerRemoteLayer(
`[RemoteCommunication: pause()] channel=${this.state.channelId}`,
);

this.state.communicationLayer?.pause();
this.setConnectionStatus(ConnectionStatus.PAUSED);
}
Expand Down
Loading

0 comments on commit ac14808

Please sign in to comment.