Skip to content

Commit

Permalink
chore(utils): replace default export with named exports (#1023)
Browse files Browse the repository at this point in the history
* Replace default export with named exports in utils package to enable tree shaking (fixes #1015)

This change replaces the default export in the utils package with named exports, which allows for better tree shaking and smaller build sizes in user code applications. It also enforces the no-default-export ESLint rule to ensure consistent code style

* running refactored files through prehook (#1015)

* Apply changes to utils package to monorepo

This commit applies the changes made to the utils package to the rest of the monorepo. The changes include replacing the default export with named exports and enforcing the no-default-export ESLint rule.

NOTE: Not all changes could be tested (#1029, #1030, #1031)

* Apply changes to utils package to payment-processor/test/payment/any-to-near.test.ts

* feat: tombchain (#1024)

* feat: tombchain (#1024)

* running refactored files through prehook (#1015)

* Apply changes to utils package to monorepo

This commit applies the changes made to the utils package to the rest of the monorepo. The changes include replacing the default export with named exports and enforcing the no-default-export ESLint rule.

NOTE: Not all changes could be tested (#1029, #1030, #1031)

* Apply changes to utils package to payment-processor/test/payment/any-to-near.test.ts

* fix circular dependency by importing from the corresponding package rather from the same package barrel file

* Resolves #1023: Rename ambiguous functions for clarity

As discussed with @alexandre-abrioux and @benjlevesque, many of the functions in the utils module have been refactored and need to be renamed for improved code readability.

Signed-off-by: marcohefti <marco@heftiweb.ch>

* Fix README, identity, and signature file updates
-Removed list of utils from README to avoid staleness
-Changed 'hasError' to 'identityHasError'
-Changed 'sign' back to original name 'sign'
-Changed 'recover' to 'recoverSigner'

Addressed comments by @MantisClone

Signed-off-by: marcohefti <marco@heftiweb.ch>

* Refactor crypto and ec-utils modules

-Removed the named const from crypto-wrapper.ts and exported functions individually
-Removed the named const from ec-utils.ts and exported functions individually
-Renamed recover() to recoverSigner() in ec-utils.ts
-Prefixed functions with 'ec' to prevent duplicate variables
-Reverted normalizeData() to normalize()

Signed-off-by: marcohefti <marco@heftiweb.ch>

Signed-off-by: marcohefti <marco@heftiweb.ch>
Co-authored-by: MantisClone <david.huntmateo@request.network>
Co-authored-by: leoslr <50319677+leoslr@users.noreply.github.com>
  • Loading branch information
3 people authored Jan 17, 2023
1 parent 880ccd7 commit ae5862a
Show file tree
Hide file tree
Showing 167 changed files with 1,139 additions and 1,237 deletions.
5 changes: 2 additions & 3 deletions packages/advanced-logic/src/extensions/abstract-extension.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ExtensionTypes, IdentityTypes, RequestLogicTypes } from '@requestnetwork/types';
import Utils from '@requestnetwork/utils';
import { deepCopy } from '@requestnetwork/utils';

/**
* Abstract class to create extension
Expand Down Expand Up @@ -53,8 +53,7 @@ export abstract class AbstractExtension<TCreationParameters> implements Extensio
): RequestLogicTypes.IExtensionStates {
this.validate(requestState, extensionAction);

const copiedExtensionState: RequestLogicTypes.IExtensionStates =
Utils.deepCopy(extensionsState);
const copiedExtensionState: RequestLogicTypes.IExtensionStates = deepCopy(extensionsState);

if (extensionAction.action === ExtensionTypes.PnFeeReferenceBased.ACTION.CREATE) {
if (requestState.extensions[extensionAction.id]) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CurrencyManager, UnsupportedCurrencyError } from '@requestnetwork/currency';
import { ExtensionTypes, IdentityTypes, RequestLogicTypes } from '@requestnetwork/types';
import Utils from '@requestnetwork/utils';
import { areEqualIdentities, deepCopy } from '@requestnetwork/utils';
import DeclarativePaymentNetwork from './declarative';

/**
Expand Down Expand Up @@ -194,11 +194,11 @@ export default abstract class AddressBasedPaymentNetwork<
if (!requestState.payee) {
throw Error(`The request must have a payee`);
}
if (!Utils.identity.areEqual(actionSigner, requestState.payee)) {
if (!areEqualIdentities(actionSigner, requestState.payee)) {
throw Error(`The signer must be the payee`);
}

const copiedExtensionState: ExtensionTypes.IState = Utils.deepCopy(extensionState);
const copiedExtensionState: ExtensionTypes.IState = deepCopy(extensionState);

// update payment address
copiedExtensionState.values.paymentAddress = extensionAction.parameters.paymentAddress;
Expand Down Expand Up @@ -241,11 +241,11 @@ export default abstract class AddressBasedPaymentNetwork<
if (!requestState.payer) {
throw Error(`The request must have a payer`);
}
if (!Utils.identity.areEqual(actionSigner, requestState.payer)) {
if (!areEqualIdentities(actionSigner, requestState.payer)) {
throw Error(`The signer must be the payer`);
}

const copiedExtensionState: ExtensionTypes.IState = Utils.deepCopy(extensionState);
const copiedExtensionState: ExtensionTypes.IState = deepCopy(extensionState);

// update refund address
copiedExtensionState.values.refundAddress = extensionAction.parameters.refundAddress;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ExtensionTypes, IdentityTypes, RequestLogicTypes } from '@requestnetwork/types';
import Utils from '@requestnetwork/utils';
import { addAmount, areEqualIdentities, deepCopy, isValidAmount } from '@requestnetwork/utils';
import { AbstractExtension } from '../abstract-extension';

const CURRENT_VERSION = '0.1.0';
Expand Down Expand Up @@ -239,14 +239,14 @@ export default class DeclarativePaymentNetwork<
timestamp: number,
): ExtensionTypes.IState {
this.checkIdentities(extensionState, requestState, actionSigner, RequestLogicTypes.ROLE.PAYER);
if (!Utils.amount.isValid(extensionAction.parameters.amount)) {
if (!isValidAmount(extensionAction.parameters.amount)) {
throw Error(`The amount is not a valid amount`);
}

const copiedExtensionState: ExtensionTypes.IState = Utils.deepCopy(extensionState);
const copiedExtensionState: ExtensionTypes.IState = deepCopy(extensionState);

// increment sentPaymentAmount
copiedExtensionState.values.sentPaymentAmount = Utils.amount.add(
copiedExtensionState.values.sentPaymentAmount = addAmount(
copiedExtensionState.values.sentPaymentAmount,
extensionAction.parameters.amount,
);
Expand Down Expand Up @@ -285,14 +285,14 @@ export default class DeclarativePaymentNetwork<
timestamp: number,
): ExtensionTypes.IState {
this.checkIdentities(extensionState, requestState, actionSigner, RequestLogicTypes.ROLE.PAYEE);
if (!Utils.amount.isValid(extensionAction.parameters.amount)) {
if (!isValidAmount(extensionAction.parameters.amount)) {
throw Error(`The amount is not a valid amount`);
}

const copiedExtensionState: ExtensionTypes.IState = Utils.deepCopy(extensionState);
const copiedExtensionState: ExtensionTypes.IState = deepCopy(extensionState);

// increment sentRefundAmount
copiedExtensionState.values.sentRefundAmount = Utils.amount.add(
copiedExtensionState.values.sentRefundAmount = addAmount(
copiedExtensionState.values.sentRefundAmount,
extensionAction.parameters.amount,
);
Expand Down Expand Up @@ -331,14 +331,14 @@ export default class DeclarativePaymentNetwork<
timestamp: number,
): ExtensionTypes.IState {
this.checkIdentities(extensionState, requestState, actionSigner, RequestLogicTypes.ROLE.PAYEE);
if (!Utils.amount.isValid(extensionAction.parameters.amount)) {
if (!isValidAmount(extensionAction.parameters.amount)) {
throw Error(`The amount is not a valid amount`);
}

const copiedExtensionState: ExtensionTypes.IState = Utils.deepCopy(extensionState);
const copiedExtensionState: ExtensionTypes.IState = deepCopy(extensionState);

// increment receivedPaymentAmount
copiedExtensionState.values.receivedPaymentAmount = Utils.amount.add(
copiedExtensionState.values.receivedPaymentAmount = addAmount(
copiedExtensionState.values.receivedPaymentAmount,
extensionAction.parameters.amount,
);
Expand Down Expand Up @@ -377,14 +377,14 @@ export default class DeclarativePaymentNetwork<
timestamp: number,
): ExtensionTypes.IState {
this.checkIdentities(extensionState, requestState, actionSigner, RequestLogicTypes.ROLE.PAYER);
if (!Utils.amount.isValid(extensionAction.parameters.amount)) {
if (!isValidAmount(extensionAction.parameters.amount)) {
throw Error(`The amount is not a valid amount`);
}

const copiedExtensionState: ExtensionTypes.IState = Utils.deepCopy(extensionState);
const copiedExtensionState: ExtensionTypes.IState = deepCopy(extensionState);

// increment receivedRefundAmount
copiedExtensionState.values.receivedRefundAmount = Utils.amount.add(
copiedExtensionState.values.receivedRefundAmount = addAmount(
copiedExtensionState.values.receivedRefundAmount,
extensionAction.parameters.amount,
);
Expand Down Expand Up @@ -427,7 +427,7 @@ export default class DeclarativePaymentNetwork<
}
this.checkIdentities(extensionState, requestState, actionSigner, RequestLogicTypes.ROLE.PAYEE);

const copiedExtensionState: ExtensionTypes.IState = Utils.deepCopy(extensionState);
const copiedExtensionState: ExtensionTypes.IState = deepCopy(extensionState);

// assign paymentInfo
copiedExtensionState.values.paymentInfo = extensionAction.parameters.paymentInfo;
Expand Down Expand Up @@ -463,9 +463,9 @@ export default class DeclarativePaymentNetwork<
timestamp: number,
): ExtensionTypes.IState {
let delegateStr: string;
if (Utils.identity.areEqual(actionSigner, requestState.payee)) {
if (areEqualIdentities(actionSigner, requestState.payee)) {
delegateStr = 'payeeDelegate';
} else if (Utils.identity.areEqual(actionSigner, requestState.payer)) {
} else if (areEqualIdentities(actionSigner, requestState.payer)) {
delegateStr = 'payerDelegate';
} else {
throw Error(`The signer must be the payee or the payer`);
Expand All @@ -475,7 +475,7 @@ export default class DeclarativePaymentNetwork<
throw Error(`The ${delegateStr} is already assigned`);
}

const copiedExtensionState: ExtensionTypes.IState = Utils.deepCopy(extensionState);
const copiedExtensionState: ExtensionTypes.IState = deepCopy(extensionState);

// assign payeeDelegate or payerDelegate
copiedExtensionState.values[delegateStr] = extensionAction.parameters.delegate;
Expand Down Expand Up @@ -515,7 +515,7 @@ export default class DeclarativePaymentNetwork<
}
this.checkIdentities(extensionState, requestState, actionSigner, RequestLogicTypes.ROLE.PAYER);

const copiedExtensionState: ExtensionTypes.IState = Utils.deepCopy(extensionState);
const copiedExtensionState: ExtensionTypes.IState = deepCopy(extensionState);

// assign refundInfo
copiedExtensionState.values.refundInfo = extensionAction.parameters.refundInfo;
Expand Down Expand Up @@ -567,8 +567,8 @@ export default class DeclarativePaymentNetwork<
throw Error(`The request must have a ${requestRoleStr}`);
}
if (
!Utils.identity.areEqual(actionSigner, requestRole) &&
!Utils.identity.areEqual(actionSigner, requestRoleDelegate)
!areEqualIdentities(actionSigner, requestRole) &&
!areEqualIdentities(actionSigner, requestRoleDelegate)
) {
throw Error(`The signer must be the ${requestRoleStr} or the ${requestRoleStr}Delegate`);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ExtensionTypes, RequestLogicTypes, TypesUtils } from '@requestnetwork/types';
import ReferenceBasedPaymentNetwork from '../reference-based';
import Utils from '@requestnetwork/utils';
import { isValidAmount } from '@requestnetwork/utils';
const CURRENT_VERSION = '0.1.0';

/**
Expand Down Expand Up @@ -92,15 +92,15 @@ export default class Erc777StreamPaymentNetwork<
if (
!extensionAction.parameters.expectedStartDate ||
(extensionAction.parameters.expectedStartDate &&
!Utils.amount.isValid(extensionAction.parameters.expectedStartDate))
!isValidAmount(extensionAction.parameters.expectedStartDate))
) {
throw Error('expectedStartDate is empty or invalid');
}

if (
!extensionAction.parameters.expectedFlowRate ||
(extensionAction.parameters.expectedFlowRate &&
!Utils.amount.isValid(extensionAction.parameters.expectedFlowRate))
!isValidAmount(extensionAction.parameters.expectedFlowRate))
) {
throw Error('expectedFlowRate is empty or invalid');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ExtensionTypes, IdentityTypes, RequestLogicTypes } from '@requestnetwork/types';
import ReferenceBasedPaymentNetwork from './reference-based';
import Utils from '@requestnetwork/utils';
import { areEqualIdentities, deepCopy, isValidAmount } from '@requestnetwork/utils';

/**
* Core of the reference based with fee payment networks
Expand Down Expand Up @@ -35,7 +35,7 @@ export abstract class FeeReferenceBasedPaymentNetwork<
throw Error('feeAddress is not a valid address');
}

if (creationParameters.feeAmount && !Utils.amount.isValid(creationParameters.feeAmount)) {
if (creationParameters.feeAmount && !isValidAmount(creationParameters.feeAmount)) {
throw Error('feeAmount is not a valid amount');
}

Expand Down Expand Up @@ -65,7 +65,7 @@ export abstract class FeeReferenceBasedPaymentNetwork<
throw Error('feeAddress is not a valid address');
}

if (addFeeParameters.feeAmount && !Utils.amount.isValid(addFeeParameters.feeAmount)) {
if (addFeeParameters.feeAmount && !isValidAmount(addFeeParameters.feeAmount)) {
throw Error('feeAmount is not a valid amount');
}

Expand Down Expand Up @@ -103,7 +103,7 @@ export abstract class FeeReferenceBasedPaymentNetwork<
}
if (
extensionAction.parameters.feeAmount &&
!Utils.amount.isValid(extensionAction.parameters.feeAmount)
!isValidAmount(extensionAction.parameters.feeAmount)
) {
throw Error('feeAmount is not a valid amount');
}
Expand Down Expand Up @@ -162,7 +162,7 @@ export abstract class FeeReferenceBasedPaymentNetwork<
}
if (
extensionAction.parameters.feeAmount &&
!Utils.amount.isValid(extensionAction.parameters.feeAmount)
!isValidAmount(extensionAction.parameters.feeAmount)
) {
throw Error('feeAmount is not a valid amount');
}
Expand All @@ -172,11 +172,11 @@ export abstract class FeeReferenceBasedPaymentNetwork<
if (!requestState.payee) {
throw Error(`The request must have a payee`);
}
if (!Utils.identity.areEqual(actionSigner, requestState.payee)) {
if (!areEqualIdentities(actionSigner, requestState.payee)) {
throw Error(`The signer must be the payee`);
}

const copiedExtensionState: ExtensionTypes.IState = Utils.deepCopy(extensionState);
const copiedExtensionState: ExtensionTypes.IState = deepCopy(extensionState);

// update fee address and amount
copiedExtensionState.values.feeAddress = extensionAction.parameters.feeAddress;
Expand Down
12 changes: 5 additions & 7 deletions packages/advanced-logic/test/advanced-logic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as DataBTCCreate from './utils/payment-network/bitcoin/generator-data-c
import * as DataDeclarativeCreate from './utils/payment-network/any/generator-data-create';
import * as DataTestnetBTCCreate from './utils/payment-network/bitcoin/testnet-generator-data-create';

import Utils from '@requestnetwork/utils';
import { deepCopy } from '@requestnetwork/utils';

import { AdvancedLogic } from '../src/index';

Expand All @@ -19,7 +19,7 @@ describe('advanced-logic.ts', () => {
});
describe('applyActionToExtensions', () => {
it('can applyActionToExtensions', () => {
const requestCreatedNoExtensionBefore = Utils.deepCopy(TestData.requestCreatedNoExtension);
const requestCreatedNoExtensionBefore = deepCopy(TestData.requestCreatedNoExtension);
const previousState = {};

const newExtensionState = advancedLogic.applyActionToExtensions(
Expand All @@ -39,9 +39,7 @@ describe('advanced-logic.ts', () => {
});

it('can applyActionToExtensions with pn bitcoin address based', () => {
const requestCreatedNoExtensionBefore = Utils.deepCopy(
DataBTCCreate.requestStateNoExtensions,
);
const requestCreatedNoExtensionBefore = deepCopy(DataBTCCreate.requestStateNoExtensions);

const newExtensionState = advancedLogic.applyActionToExtensions(
requestCreatedNoExtensionBefore.extensions,
Expand All @@ -58,7 +56,7 @@ describe('advanced-logic.ts', () => {
});

it('can applyActionToExtensions with pn testnet bitcoin address based', () => {
const requestCreatedNoExtensionBefore = Utils.deepCopy(
const requestCreatedNoExtensionBefore = deepCopy(
DataTestnetBTCCreate.requestStateNoExtensions,
);

Expand All @@ -79,7 +77,7 @@ describe('advanced-logic.ts', () => {
});

it('can applyActionToExtensions with declarative payment network', () => {
const requestCreatedNoExtensionBefore = Utils.deepCopy(
const requestCreatedNoExtensionBefore = deepCopy(
DataDeclarativeCreate.requestStateNoExtensions,
);

Expand Down
4 changes: 2 additions & 2 deletions packages/advanced-logic/test/extensions/content-data.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ExtensionTypes } from '@requestnetwork/types';
import Utils from '@requestnetwork/utils';
import { deepCopy } from '@requestnetwork/utils';

import ContentData from '../../src/extensions/content-data';

Expand All @@ -11,7 +11,7 @@ const contentData = new ContentData();
describe('content-data', () => {
describe('applyActionToExtension', () => {
it('can applyActionToExtensions', () => {
const requestCreatedNoExtensionBefore = Utils.deepCopy(TestData.requestCreatedNoExtension);
const requestCreatedNoExtensionBefore = deepCopy(TestData.requestCreatedNoExtension);
const previousState = {};
const newExtensionState = contentData.applyActionToExtension(
previousState,
Expand Down
Loading

0 comments on commit ae5862a

Please sign in to comment.