diff --git a/packages/advanced-logic/src/advanced-logic.ts b/packages/advanced-logic/src/advanced-logic.ts index a122655a00..5066239240 100644 --- a/packages/advanced-logic/src/advanced-logic.ts +++ b/packages/advanced-logic/src/advanced-logic.ts @@ -5,10 +5,10 @@ import { RequestLogicTypes, } from '@requestnetwork/types'; -import contentData from './extensions/content-data'; +import ContentData from './extensions/content-data'; import AddressBasedBtc from './extensions/payment-network/bitcoin/mainnet-address-based'; import AddressBasedTestnetBtc from './extensions/payment-network/bitcoin/testnet-address-based'; -import declarative from './extensions/payment-network/declarative'; +import Declarative from './extensions/payment-network/declarative'; import AddressBasedErc20 from './extensions/payment-network/erc20/address-based'; import FeeProxyContractErc20 from './extensions/payment-network/erc20/fee-proxy-contract'; import ProxyContractErc20 from './extensions/payment-network/erc20/proxy-contract'; @@ -25,9 +25,9 @@ export default class AdvancedLogic implements AdvancedLogicTypes.IAdvancedLogic addressBasedBtc: new AddressBasedBtc(), addressBasedErc20: new AddressBasedErc20(), addressBasedTestnetBtc: new AddressBasedTestnetBtc(), - contentData, + contentData: new ContentData(), anyToErc20Proxy: new AnyToErc20Proxy(), - declarative, + declarative: new Declarative(), ethereumInputData: new EthereumInputData(), feeProxyContractErc20: new FeeProxyContractErc20(), proxyContractErc20: new ProxyContractErc20(), @@ -53,7 +53,7 @@ export default class AdvancedLogic implements AdvancedLogicTypes.IAdvancedLogic ): RequestLogicTypes.IExtensionStates { const id: ExtensionTypes.ID = extensionAction.id; const extension: ExtensionTypes.IExtension | undefined = { - [ExtensionTypes.ID.CONTENT_DATA]: contentData, + [ExtensionTypes.ID.CONTENT_DATA]: this.extensions.contentData, [ExtensionTypes.ID.PAYMENT_NETWORK_BITCOIN_ADDRESS_BASED]: this.extensions.addressBasedBtc, [ExtensionTypes.ID.PAYMENT_NETWORK_TESTNET_BITCOIN_ADDRESS_BASED]: this.extensions .addressBasedTestnetBtc, diff --git a/packages/advanced-logic/src/extensions/abstract-extension.ts b/packages/advanced-logic/src/extensions/abstract-extension.ts index 15729f938d..2ea2debcc4 100644 --- a/packages/advanced-logic/src/extensions/abstract-extension.ts +++ b/packages/advanced-logic/src/extensions/abstract-extension.ts @@ -119,8 +119,11 @@ export default abstract class AbstractExtension { * @param request * @param extensionAction action to apply */ - protected abstract validate( - request: RequestLogicTypes.IRequest, - extensionAction: ExtensionTypes.IAction, - ): void; + protected validate( + // eslint-disable-next-line @typescript-eslint/no-unused-vars + _request: RequestLogicTypes.IRequest, + // eslint-disable-next-line @typescript-eslint/no-unused-vars + _extensionAction: ExtensionTypes.IAction, + // eslint-disable-next-line @typescript-eslint/no-empty-function + ): void {} } diff --git a/packages/advanced-logic/src/extensions/content-data.ts b/packages/advanced-logic/src/extensions/content-data.ts index df4a0df3eb..42eba8d51c 100644 --- a/packages/advanced-logic/src/extensions/content-data.ts +++ b/packages/advanced-logic/src/extensions/content-data.ts @@ -1,78 +1,69 @@ -import { ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types'; - -import Utils from '@requestnetwork/utils'; - -/** - * Implementation of the content data extension - */ -const contentData: ExtensionTypes.ContentData.IContentData = { - applyActionToExtension, - createCreationAction, -}; -export default contentData; +import { ExtensionTypes } from '@requestnetwork/types'; +import AbstractExtension from './abstract-extension'; const CURRENT_VERSION = '0.1.0'; /** - * Creates the extensionsData to create the extension content-data - * Should be called to create the extensionsData of a request - * - * @param extensions IAdvancedLogicExtensionsCreationParameters extensions parameters to create - * - * @returns IExtensionCreationAction the extensionsData to be store in the request - */ -function createCreationAction( - creationParameters: ExtensionTypes.ContentData.ICreationParameters, -): ExtensionTypes.IAction { - if (!creationParameters.content) { - throw Error('No content has been given for the extension content-data'); - } - - return { - action: ExtensionTypes.ContentData.ACTION.CREATE, - id: ExtensionTypes.ID.CONTENT_DATA, - parameters: creationParameters, - version: CURRENT_VERSION, - }; -} - -/** - * Applies the extension action to the request - * Is called to interpret the extensions data when applying the transaction - * - * @param extensionsState IExtensionStates previous state of the extensions - * @param extensionAction IAction action to apply - * @param requestState IRequest request state read-only - * - * @returns state of the request updated + * Implementation of the content data extension */ -function applyActionToExtension( - extensionsState: RequestLogicTypes.IExtensionStates, - extensionAction: ExtensionTypes.IAction, - requestState: RequestLogicTypes.IRequest, -): RequestLogicTypes.IExtensionStates { - if (extensionAction.action !== ExtensionTypes.ContentData.ACTION.CREATE) { - throw Error(`Unknown action: ${extensionAction.action}`); +export default class ContentDataExtension< + TCreationParameters extends ExtensionTypes.ContentData.ICreationParameters = ExtensionTypes.ContentData.ICreationParameters +> extends AbstractExtension { + public constructor( + public extensionId: ExtensionTypes.ID = ExtensionTypes.ID.CONTENT_DATA, + public currentVersion: string = CURRENT_VERSION, + ) { + super(ExtensionTypes.TYPE.CONTENT_DATA, extensionId, currentVersion); } - if (requestState.extensions[ExtensionTypes.ID.CONTENT_DATA]) { - throw Error(`This extension has already been created`); - } + /** + * Creates the extensionsData to create the extension content-data + * Should be called to create the extensionsData of a request + * + * @param extensions IAdvancedLogicExtensionsCreationParameters extensions parameters to create + * + * @returns IExtensionCreationAction the extensionsData to be store in the request + */ + public createCreationAction( + creationParameters: TCreationParameters, + ): ExtensionTypes.IAction { + if (!creationParameters.content) { + throw Error('No content has been given for the extension content-data'); + } - if (!extensionAction.parameters.content) { - throw Error('No content has been given for the extension content-data'); + return super.createCreationAction( + creationParameters, + ) as ExtensionTypes.IAction; } - // Deep copy to not mutate the input parameter - const copiedExtensionState: RequestLogicTypes.IExtensionStates = Utils.deepCopy(extensionsState); + /** + * Applies a creation + * + * @param extensionAction action to apply + * @param timestamp + * + * @returns state of the extension created + */ + protected applyCreation( + extensionAction: ExtensionTypes.IAction, + timestamp: number, + ): ExtensionTypes.IState { + if (!extensionAction.parameters.content) { + throw Error('No content has been given for the extension content-data'); + } - copiedExtensionState[ExtensionTypes.ID.CONTENT_DATA] = { - events: [], - id: ExtensionTypes.ID.CONTENT_DATA, - type: ExtensionTypes.TYPE.CONTENT_DATA, - values: { content: extensionAction.parameters.content }, - version: CURRENT_VERSION, - }; + const genericCreationAction = super.applyCreation(extensionAction, timestamp); - return copiedExtensionState; + return { + ...genericCreationAction, + events: [], + id: this.extensionId, + type: this.extensionType, + values: { + ...genericCreationAction.values, + content: extensionAction.parameters.content, + }, + version: this.currentVersion, + }; + } } diff --git a/packages/advanced-logic/src/extensions/payment-network/declarative.ts b/packages/advanced-logic/src/extensions/payment-network/declarative.ts index 8bf447afc2..9b51a2f730 100644 --- a/packages/advanced-logic/src/extensions/payment-network/declarative.ts +++ b/packages/advanced-logic/src/extensions/payment-network/declarative.ts @@ -1,577 +1,468 @@ import { ExtensionTypes, IdentityTypes, RequestLogicTypes } from '@requestnetwork/types'; import Utils from '@requestnetwork/utils'; - -/** - * Core of the declarative payment network - */ -const pnDeclarative: ExtensionTypes.PnAnyDeclarative.IAnyDeclarative = { - applyActionToExtension, - createAddPaymentInstructionAction, - createAddRefundInstructionAction, - createCreationAction, - createDeclareReceivedPaymentAction, - createDeclareReceivedRefundAction, - createDeclareSentPaymentAction, - createDeclareSentRefundAction, -}; -export default pnDeclarative; +import AbstractExtension from '../abstract-extension'; const CURRENT_VERSION = '0.1.0'; /** - * Creates the extensionsData for the declarative payment network extension - * - * @param parameters parameters to create extension - * - * @returns the extensionsData to be stored in the request - */ -function createCreationAction( - parameters?: ExtensionTypes.PnAnyDeclarative.ICreationParameters, -): ExtensionTypes.IAction { - return { - action: ExtensionTypes.PnAnyDeclarative.ACTION.CREATE, - id: ExtensionTypes.ID.PAYMENT_NETWORK_ANY_DECLARATIVE, - parameters: { - paymentInfo: parameters && parameters.paymentInfo, - refundInfo: parameters && parameters.refundInfo, - }, - version: CURRENT_VERSION, - }; -} - -/** - * Creates the extensionsData to add a sent payment declaration - * - * @param parameters parameters to create sent payment declaration - * - * @returns IAction the extensionsData to be stored in the request - */ -function createDeclareSentPaymentAction( - parameters: ExtensionTypes.PnAnyDeclarative.ISentParameters, -): ExtensionTypes.IAction { - return { - action: ExtensionTypes.PnAnyDeclarative.ACTION.DECLARE_SENT_PAYMENT, - id: ExtensionTypes.ID.PAYMENT_NETWORK_ANY_DECLARATIVE, - parameters: { - amount: parameters.amount.toString(), - note: parameters.note, - }, - }; -} - -/** - * Creates the extensionsData to add a sent refund declaration - * - * @param parameters parameters to create sent refund declaration - * - * @returns IAction the extensionsData to be stored in the request - */ -function createDeclareSentRefundAction( - parameters: ExtensionTypes.PnAnyDeclarative.ISentParameters, -): ExtensionTypes.IAction { - return { - action: ExtensionTypes.PnAnyDeclarative.ACTION.DECLARE_SENT_REFUND, - id: ExtensionTypes.ID.PAYMENT_NETWORK_ANY_DECLARATIVE, - parameters: { - amount: parameters.amount.toString(), - note: parameters.note, - }, - }; -} - -/** - * Creates the extensionsData to add a received payment declaration - * - * @param parameters parameters to create received payment declaration - * - * @returns IAction the extensionsData to be stored in the request - */ -function createDeclareReceivedPaymentAction( - parameters: ExtensionTypes.PnAnyDeclarative.IReceivedParameters, -): ExtensionTypes.IAction { - return { - action: ExtensionTypes.PnAnyDeclarative.ACTION.DECLARE_RECEIVED_PAYMENT, - id: ExtensionTypes.ID.PAYMENT_NETWORK_ANY_DECLARATIVE, - parameters: { - amount: parameters.amount.toString(), - note: parameters.note, - }, - }; -} - -/** - * Creates the extensionsData to add a received refund declaration - * - * @param parameters parameters to create received refund declaration - * - * @returns IAction the extensionsData to be stored in the request - */ -function createDeclareReceivedRefundAction( - parameters: ExtensionTypes.PnAnyDeclarative.IReceivedParameters, -): ExtensionTypes.IAction { - return { - action: ExtensionTypes.PnAnyDeclarative.ACTION.DECLARE_RECEIVED_REFUND, - id: ExtensionTypes.ID.PAYMENT_NETWORK_ANY_DECLARATIVE, - parameters: { - amount: parameters.amount.toString(), - note: parameters.note, - }, - }; -} - -/** - * Creates the extensionsData to add payment instruction - * - * @param extensions extensions parameters to add payment instruction - * - * @returns IAction the extensionsData to be stored in the request - */ -function createAddPaymentInstructionAction( - parameters: ExtensionTypes.PnAnyDeclarative.IAddPaymentInstructionParameters, -): ExtensionTypes.IAction { - return { - action: ExtensionTypes.PnAnyDeclarative.ACTION.ADD_PAYMENT_INSTRUCTION, - id: ExtensionTypes.ID.PAYMENT_NETWORK_ANY_DECLARATIVE, - parameters: { - paymentInfo: parameters.paymentInfo, - }, - }; -} - -/** - * Creates the extensionsData to add refund instruction - * - * @param extensions extensions parameters to add refund instruction - * - * @returns IAction the extensionsData to be stored in the request + * Core of the declarative payment network */ -function createAddRefundInstructionAction( - parameters: ExtensionTypes.PnAnyDeclarative.IAddRefundInstructionParameters, -): ExtensionTypes.IAction { - return { - action: ExtensionTypes.PnAnyDeclarative.ACTION.ADD_REFUND_INSTRUCTION, - id: ExtensionTypes.ID.PAYMENT_NETWORK_ANY_DECLARATIVE, - parameters: { - refundInfo: parameters.refundInfo, - }, - }; -} +export default class DeclarativePaymentNetwork< + TCreationParameters extends ExtensionTypes.PnAnyDeclarative.ICreationParameters = ExtensionTypes.PnAnyDeclarative.ICreationParameters +> extends AbstractExtension { + public constructor( + public extensionId: ExtensionTypes.ID = ExtensionTypes.ID.PAYMENT_NETWORK_ANY_DECLARATIVE, + public currentVersion: string = CURRENT_VERSION, + ) { + super(ExtensionTypes.TYPE.PAYMENT_NETWORK, extensionId, currentVersion); + this.actions = { + ...this.actions, + [ExtensionTypes.PnAnyDeclarative.ACTION + .ADD_PAYMENT_INSTRUCTION]: this.applyAddPaymentInstruction.bind(this), + [ExtensionTypes.PnAnyDeclarative.ACTION + .ADD_REFUND_INSTRUCTION]: this.applyAddRefundInstruction.bind(this), + [ExtensionTypes.PnAnyDeclarative.ACTION + .DECLARE_SENT_PAYMENT]: this.applyDeclareSentPayment.bind(this), + [ExtensionTypes.PnAnyDeclarative.ACTION + .DECLARE_SENT_REFUND]: this.applyDeclareSentRefund.bind(this), + [ExtensionTypes.PnAnyDeclarative.ACTION + .DECLARE_RECEIVED_PAYMENT]: this.applyDeclareReceivedPayment.bind(this), + [ExtensionTypes.PnAnyDeclarative.ACTION + .DECLARE_RECEIVED_REFUND]: this.applyDeclareReceivedRefund.bind(this), + }; + } -/** - * Applies the extension action to the request - * Is called to interpret the extensions data when applying the transaction - * - * @param extensionsState previous state of the extensions - * @param extensionAction action to apply - * @param requestState request state read-only - * @param actionSigner identity of the signer - * @param timestamp timestamp of the action - * - * @returns state of the request updated - */ -function applyActionToExtension( - extensionsState: RequestLogicTypes.IExtensionStates, - extensionAction: ExtensionTypes.IAction, - requestState: RequestLogicTypes.IRequest, - actionSigner: IdentityTypes.IIdentity, - timestamp: number, -): RequestLogicTypes.IExtensionStates { - const copiedExtensionState: RequestLogicTypes.IExtensionStates = Utils.deepCopy(extensionsState); - - if (extensionAction.action === ExtensionTypes.PnAnyDeclarative.ACTION.CREATE) { - if (requestState.extensions[extensionAction.id]) { - throw Error(`This extension has already been created`); - } + /** + * Creates the extensionsData to add a sent payment declaration + * + * @param parameters parameters to create sent payment declaration + * + * @returns IAction the extensionsData to be stored in the request + */ + public createDeclareSentPaymentAction( + parameters: ExtensionTypes.PnAnyDeclarative.ISentParameters, + ): ExtensionTypes.IAction { + return { + action: ExtensionTypes.PnAnyDeclarative.ACTION.DECLARE_SENT_PAYMENT, + id: ExtensionTypes.ID.PAYMENT_NETWORK_ANY_DECLARATIVE, + parameters: { + amount: parameters.amount.toString(), + note: parameters.note, + }, + }; + } - copiedExtensionState[extensionAction.id] = applyCreation(extensionAction, timestamp); + /** + * Creates the extensionsData to add a sent refund declaration + * + * @param parameters parameters to create sent refund declaration + * + * @returns IAction the extensionsData to be stored in the request + */ + public createDeclareSentRefundAction( + parameters: ExtensionTypes.PnAnyDeclarative.ISentParameters, + ): ExtensionTypes.IAction { + return { + action: ExtensionTypes.PnAnyDeclarative.ACTION.DECLARE_SENT_REFUND, + id: ExtensionTypes.ID.PAYMENT_NETWORK_ANY_DECLARATIVE, + parameters: { + amount: parameters.amount.toString(), + note: parameters.note, + }, + }; + } - return copiedExtensionState; + /** + * Creates the extensionsData to add a received payment declaration + * + * @param parameters parameters to create received payment declaration + * + * @returns IAction the extensionsData to be stored in the request + */ + public createDeclareReceivedPaymentAction( + parameters: ExtensionTypes.PnAnyDeclarative.IReceivedParameters, + ): ExtensionTypes.IAction { + return { + action: ExtensionTypes.PnAnyDeclarative.ACTION.DECLARE_RECEIVED_PAYMENT, + id: ExtensionTypes.ID.PAYMENT_NETWORK_ANY_DECLARATIVE, + parameters: { + amount: parameters.amount.toString(), + note: parameters.note, + }, + }; } - // if the action is not "create", the state must have been created before - if (!requestState.extensions[extensionAction.id]) { - throw Error(`This extension must have been already created`); + /** + * Creates the extensionsData to add a received refund declaration + * + * @param parameters parameters to create received refund declaration + * + * @returns IAction the extensionsData to be stored in the request + */ + public createDeclareReceivedRefundAction( + parameters: ExtensionTypes.PnAnyDeclarative.IReceivedParameters, + ): ExtensionTypes.IAction { + return { + action: ExtensionTypes.PnAnyDeclarative.ACTION.DECLARE_RECEIVED_REFUND, + id: ExtensionTypes.ID.PAYMENT_NETWORK_ANY_DECLARATIVE, + parameters: { + amount: parameters.amount.toString(), + note: parameters.note, + }, + }; } - if (extensionAction.action === ExtensionTypes.PnAnyDeclarative.ACTION.DECLARE_SENT_PAYMENT) { - copiedExtensionState[extensionAction.id] = applyDeclareSentPayment( - copiedExtensionState[extensionAction.id], - extensionAction, - requestState, - actionSigner, - timestamp, - ); - return copiedExtensionState; + /** + * Creates the extensionsData to add payment instruction + * + * @param extensions extensions parameters to add payment instruction + * + * @returns IAction the extensionsData to be stored in the request + */ + public createAddPaymentInstructionAction( + parameters: ExtensionTypes.PnAnyDeclarative.IAddPaymentInstructionParameters, + ): ExtensionTypes.IAction { + return { + action: ExtensionTypes.PnAnyDeclarative.ACTION.ADD_PAYMENT_INSTRUCTION, + id: ExtensionTypes.ID.PAYMENT_NETWORK_ANY_DECLARATIVE, + parameters: { + paymentInfo: parameters.paymentInfo, + }, + }; } - if (extensionAction.action === ExtensionTypes.PnAnyDeclarative.ACTION.DECLARE_SENT_REFUND) { - copiedExtensionState[extensionAction.id] = applyDeclareSentRefund( - copiedExtensionState[extensionAction.id], - extensionAction, - requestState, - actionSigner, - timestamp, - ); - return copiedExtensionState; + /** + * Creates the extensionsData to add refund instruction + * + * @param extensions extensions parameters to add refund instruction + * + * @returns IAction the extensionsData to be stored in the request + */ + public createAddRefundInstructionAction( + parameters: ExtensionTypes.PnAnyDeclarative.IAddRefundInstructionParameters, + ): ExtensionTypes.IAction { + return { + action: ExtensionTypes.PnAnyDeclarative.ACTION.ADD_REFUND_INSTRUCTION, + id: ExtensionTypes.ID.PAYMENT_NETWORK_ANY_DECLARATIVE, + parameters: { + refundInfo: parameters.refundInfo, + }, + }; } - if (extensionAction.action === ExtensionTypes.PnAnyDeclarative.ACTION.DECLARE_RECEIVED_PAYMENT) { - copiedExtensionState[extensionAction.id] = applyDeclareReceivedPayment( - copiedExtensionState[extensionAction.id], - extensionAction, - requestState, - actionSigner, - timestamp, - ); - return copiedExtensionState; + /** Applies a creation + * + * @param extensionAction action to apply + * @param timestamp timestamp of the action + * + * @returns state of the extension created + */ + protected applyCreation( + extensionAction: ExtensionTypes.IAction, + timestamp: number, + ): ExtensionTypes.IState { + return { + events: [ + { + name: 'create', + parameters: { + paymentInfo: extensionAction.parameters.paymentInfo, + refundInfo: extensionAction.parameters.refundInfo, + }, + timestamp, + }, + ], + id: extensionAction.id, + type: ExtensionTypes.TYPE.PAYMENT_NETWORK, + values: { + paymentInfo: extensionAction.parameters.paymentInfo, + receivedPaymentAmount: '0', + receivedRefundAmount: '0', + refundInfo: extensionAction.parameters.refundInfo, + sentPaymentAmount: '0', + sentRefundAmount: '0', + }, + version: CURRENT_VERSION, + }; } - if (extensionAction.action === ExtensionTypes.PnAnyDeclarative.ACTION.DECLARE_RECEIVED_REFUND) { - copiedExtensionState[extensionAction.id] = applyDeclareReceivedRefund( - copiedExtensionState[extensionAction.id], - extensionAction, - requestState, - actionSigner, - timestamp, + /** Applies a declare sent payment + * + * @param extensionsState previous state of the extensions + * @param extensionAction action to apply + * @param requestState request state read-only + * @param actionSigner identity of the signer + * @param timestamp timestamp of the action + * + * @returns state of the extension created + */ + protected applyDeclareSentPayment( + extensionState: ExtensionTypes.IState, + extensionAction: ExtensionTypes.IAction, + requestState: RequestLogicTypes.IRequest, + actionSigner: IdentityTypes.IIdentity, + timestamp: number, + ): ExtensionTypes.IState { + if (!requestState.payer) { + throw Error(`The request must have a payer`); + } + if (!Utils.identity.areEqual(actionSigner, requestState.payer)) { + throw Error(`The signer must be the payer`); + } + if (!Utils.amount.isValid(extensionAction.parameters.amount)) { + throw Error(`The amount is not a valid amount`); + } + + const copiedExtensionState: ExtensionTypes.IState = Utils.deepCopy(extensionState); + + // increment sentPaymentAmount + copiedExtensionState.values.sentPaymentAmount = Utils.amount.add( + copiedExtensionState.values.sentPaymentAmount, + extensionAction.parameters.amount, ); - return copiedExtensionState; - } - if (extensionAction.action === ExtensionTypes.PnAnyDeclarative.ACTION.ADD_PAYMENT_INSTRUCTION) { - copiedExtensionState[extensionAction.id] = applyAddPaymentInstruction( - copiedExtensionState[extensionAction.id], - extensionAction, - requestState, - actionSigner, + // update events + copiedExtensionState.events.push({ + name: ExtensionTypes.PnAnyDeclarative.ACTION.DECLARE_SENT_PAYMENT, + parameters: { + amount: extensionAction.parameters.amount, + note: extensionAction.parameters.note, + }, timestamp, - ); + }); + return copiedExtensionState; } - if (extensionAction.action === ExtensionTypes.PnAnyDeclarative.ACTION.ADD_REFUND_INSTRUCTION) { - copiedExtensionState[extensionAction.id] = applyAddRefundInstruction( - copiedExtensionState[extensionAction.id], - extensionAction, - requestState, - actionSigner, - timestamp, + + /** Applies a declare sent refund + * + * @param extensionsState previous state of the extensions + * @param extensionAction action to apply + * @param requestState request state read-only + * @param actionSigner identity of the signer + * @param timestamp timestamp of the action + * + * @returns state of the extension created + */ + protected applyDeclareSentRefund( + extensionState: ExtensionTypes.IState, + extensionAction: ExtensionTypes.IAction, + requestState: RequestLogicTypes.IRequest, + actionSigner: IdentityTypes.IIdentity, + timestamp: number, + ): ExtensionTypes.IState { + if (!requestState.payee) { + throw Error(`The request must have a payee`); + } + if (!Utils.identity.areEqual(actionSigner, requestState.payee)) { + throw Error(`The signer must be the payee`); + } + if (!Utils.amount.isValid(extensionAction.parameters.amount)) { + throw Error(`The amount is not a valid amount`); + } + + const copiedExtensionState: ExtensionTypes.IState = Utils.deepCopy(extensionState); + + // increment sentRefundAmount + copiedExtensionState.values.sentRefundAmount = Utils.amount.add( + copiedExtensionState.values.sentRefundAmount, + extensionAction.parameters.amount, ); - return copiedExtensionState; - } - throw Error(`Unknown action: ${extensionAction.action}`); -} -/** Applies a creation - * - * @param extensionAction action to apply - * @param timestamp timestamp of the action - * - * @returns state of the extension created - */ -function applyCreation( - extensionAction: ExtensionTypes.IAction, - timestamp: number, -): ExtensionTypes.IState { - return { - events: [ - { - name: 'create', - parameters: { - paymentInfo: extensionAction.parameters.paymentInfo, - refundInfo: extensionAction.parameters.refundInfo, - }, - timestamp, + // update events + copiedExtensionState.events.push({ + name: ExtensionTypes.PnAnyDeclarative.ACTION.DECLARE_SENT_REFUND, + parameters: { + amount: extensionAction.parameters.amount, + note: extensionAction.parameters.note, }, - ], - id: extensionAction.id, - type: ExtensionTypes.TYPE.PAYMENT_NETWORK, - values: { - paymentInfo: extensionAction.parameters.paymentInfo, - receivedPaymentAmount: '0', - receivedRefundAmount: '0', - refundInfo: extensionAction.parameters.refundInfo, - sentPaymentAmount: '0', - sentRefundAmount: '0', - }, - version: CURRENT_VERSION, - }; -} + timestamp, + }); -/** Applies a declare sent payment - * - * @param extensionsState previous state of the extensions - * @param extensionAction action to apply - * @param requestState request state read-only - * @param actionSigner identity of the signer - * @param timestamp timestamp of the action - * - * @returns state of the extension created - */ -function applyDeclareSentPayment( - extensionState: ExtensionTypes.IState, - extensionAction: ExtensionTypes.IAction, - requestState: RequestLogicTypes.IRequest, - actionSigner: IdentityTypes.IIdentity, - timestamp: number, -): ExtensionTypes.IState { - if (!requestState.payer) { - throw Error(`The request must have a payer`); - } - if (!Utils.identity.areEqual(actionSigner, requestState.payer)) { - throw Error(`The signer must be the payer`); - } - if (!Utils.amount.isValid(extensionAction.parameters.amount)) { - throw Error(`The amount is not a valid amount`); + return copiedExtensionState; } - const copiedExtensionState: ExtensionTypes.IState = Utils.deepCopy(extensionState); - - // increment sentPaymentAmount - copiedExtensionState.values.sentPaymentAmount = Utils.amount.add( - copiedExtensionState.values.sentPaymentAmount, - extensionAction.parameters.amount, - ); - - // update events - copiedExtensionState.events.push({ - name: ExtensionTypes.PnAnyDeclarative.ACTION.DECLARE_SENT_PAYMENT, - parameters: { - amount: extensionAction.parameters.amount, - note: extensionAction.parameters.note, - }, - timestamp, - }); - - return copiedExtensionState; -} + /** Applies a declare received payment + * + * @param extensionsState previous state of the extensions + * @param extensionAction action to apply + * @param requestState request state read-only + * @param actionSigner identity of the signer + * @param timestamp timestamp of the action + * + * @returns state of the extension created + */ + protected applyDeclareReceivedPayment( + extensionState: ExtensionTypes.IState, + extensionAction: ExtensionTypes.IAction, + requestState: RequestLogicTypes.IRequest, + actionSigner: IdentityTypes.IIdentity, + timestamp: number, + ): ExtensionTypes.IState { + if (!requestState.payee) { + throw Error(`The request must have a payee`); + } + if (!Utils.identity.areEqual(actionSigner, requestState.payee)) { + throw Error(`The signer must be the payee`); + } + if (!Utils.amount.isValid(extensionAction.parameters.amount)) { + throw Error(`The amount is not a valid amount`); + } -/** Applies a declare sent refund - * - * @param extensionsState previous state of the extensions - * @param extensionAction action to apply - * @param requestState request state read-only - * @param actionSigner identity of the signer - * @param timestamp timestamp of the action - * - * @returns state of the extension created - */ -function applyDeclareSentRefund( - extensionState: ExtensionTypes.IState, - extensionAction: ExtensionTypes.IAction, - requestState: RequestLogicTypes.IRequest, - actionSigner: IdentityTypes.IIdentity, - timestamp: number, -): ExtensionTypes.IState { - if (!requestState.payee) { - throw Error(`The request must have a payee`); - } - if (!Utils.identity.areEqual(actionSigner, requestState.payee)) { - throw Error(`The signer must be the payee`); - } - if (!Utils.amount.isValid(extensionAction.parameters.amount)) { - throw Error(`The amount is not a valid amount`); - } + const copiedExtensionState: ExtensionTypes.IState = Utils.deepCopy(extensionState); - const copiedExtensionState: ExtensionTypes.IState = Utils.deepCopy(extensionState); - - // increment sentRefundAmount - copiedExtensionState.values.sentRefundAmount = Utils.amount.add( - copiedExtensionState.values.sentRefundAmount, - extensionAction.parameters.amount, - ); - - // update events - copiedExtensionState.events.push({ - name: ExtensionTypes.PnAnyDeclarative.ACTION.DECLARE_SENT_REFUND, - parameters: { - amount: extensionAction.parameters.amount, - note: extensionAction.parameters.note, - }, - timestamp, - }); - - return copiedExtensionState; -} + // increment receivedPaymentAmount + copiedExtensionState.values.receivedPaymentAmount = Utils.amount.add( + copiedExtensionState.values.receivedPaymentAmount, + extensionAction.parameters.amount, + ); -/** Applies a declare received payment - * - * @param extensionsState previous state of the extensions - * @param extensionAction action to apply - * @param requestState request state read-only - * @param actionSigner identity of the signer - * @param timestamp timestamp of the action - * - * @returns state of the extension created - */ -function applyDeclareReceivedPayment( - extensionState: ExtensionTypes.IState, - extensionAction: ExtensionTypes.IAction, - requestState: RequestLogicTypes.IRequest, - actionSigner: IdentityTypes.IIdentity, - timestamp: number, -): ExtensionTypes.IState { - if (!requestState.payee) { - throw Error(`The request must have a payee`); - } - if (!Utils.identity.areEqual(actionSigner, requestState.payee)) { - throw Error(`The signer must be the payee`); - } - if (!Utils.amount.isValid(extensionAction.parameters.amount)) { - throw Error(`The amount is not a valid amount`); + // update events + copiedExtensionState.events.push({ + name: ExtensionTypes.PnAnyDeclarative.ACTION.DECLARE_RECEIVED_PAYMENT, + parameters: { + amount: extensionAction.parameters.amount, + note: extensionAction.parameters.note, + }, + timestamp, + }); + + return copiedExtensionState; } - const copiedExtensionState: ExtensionTypes.IState = Utils.deepCopy(extensionState); - - // increment receivedPaymentAmount - copiedExtensionState.values.receivedPaymentAmount = Utils.amount.add( - copiedExtensionState.values.receivedPaymentAmount, - extensionAction.parameters.amount, - ); - - // update events - copiedExtensionState.events.push({ - name: ExtensionTypes.PnAnyDeclarative.ACTION.DECLARE_RECEIVED_PAYMENT, - parameters: { - amount: extensionAction.parameters.amount, - note: extensionAction.parameters.note, - }, - timestamp, - }); - - return copiedExtensionState; -} + /** Applies a declare received refund + * + * @param extensionsState previous state of the extensions + * @param extensionAction action to apply + * @param requestState request state read-only + * @param actionSigner identity of the signer + * @param timestamp timestamp of the action + * + * @returns state of the extension created + */ + protected applyDeclareReceivedRefund( + extensionState: ExtensionTypes.IState, + extensionAction: ExtensionTypes.IAction, + requestState: RequestLogicTypes.IRequest, + actionSigner: IdentityTypes.IIdentity, + timestamp: number, + ): ExtensionTypes.IState { + if (!requestState.payer) { + throw Error(`The request must have a payer`); + } + if (!Utils.identity.areEqual(actionSigner, requestState.payer)) { + throw Error(`The signer must be the payer`); + } + if (!Utils.amount.isValid(extensionAction.parameters.amount)) { + throw Error(`The amount is not a valid amount`); + } -/** Applies a declare received refund - * - * @param extensionsState previous state of the extensions - * @param extensionAction action to apply - * @param requestState request state read-only - * @param actionSigner identity of the signer - * @param timestamp timestamp of the action - * - * @returns state of the extension created - */ -function applyDeclareReceivedRefund( - extensionState: ExtensionTypes.IState, - extensionAction: ExtensionTypes.IAction, - requestState: RequestLogicTypes.IRequest, - actionSigner: IdentityTypes.IIdentity, - timestamp: number, -): ExtensionTypes.IState { - if (!requestState.payer) { - throw Error(`The request must have a payer`); - } - if (!Utils.identity.areEqual(actionSigner, requestState.payer)) { - throw Error(`The signer must be the payer`); - } - if (!Utils.amount.isValid(extensionAction.parameters.amount)) { - throw Error(`The amount is not a valid amount`); - } + const copiedExtensionState: ExtensionTypes.IState = Utils.deepCopy(extensionState); - const copiedExtensionState: ExtensionTypes.IState = Utils.deepCopy(extensionState); - - // increment receivedRefundAmount - copiedExtensionState.values.receivedRefundAmount = Utils.amount.add( - copiedExtensionState.values.receivedRefundAmount, - extensionAction.parameters.amount, - ); - - // update events - copiedExtensionState.events.push({ - name: ExtensionTypes.PnAnyDeclarative.ACTION.DECLARE_RECEIVED_REFUND, - parameters: { - amount: extensionAction.parameters.amount, - note: extensionAction.parameters.note, - }, - timestamp, - }); - - return copiedExtensionState; -} + // increment receivedRefundAmount + copiedExtensionState.values.receivedRefundAmount = Utils.amount.add( + copiedExtensionState.values.receivedRefundAmount, + extensionAction.parameters.amount, + ); -/** Applies an add of payment instruction - * - * @param extensionsState previous state of the extensions - * @param extensionAction action to apply - * @param requestState request state read-only - * @param actionSigner identity of the signer - * @param timestamp timestamp of the action - * - * @returns state of the extension created - */ -function applyAddPaymentInstruction( - extensionState: ExtensionTypes.IState, - extensionAction: ExtensionTypes.IAction, - requestState: RequestLogicTypes.IRequest, - actionSigner: IdentityTypes.IIdentity, - timestamp: number, -): ExtensionTypes.IState { - if (extensionState.values.paymentInfo) { - throw Error(`The payment instruction already given`); - } - if (!requestState.payee) { - throw Error(`The request must have a payee`); - } - if (!Utils.identity.areEqual(actionSigner, requestState.payee)) { - throw Error(`The signer must be the payee`); + // update events + copiedExtensionState.events.push({ + name: ExtensionTypes.PnAnyDeclarative.ACTION.DECLARE_RECEIVED_REFUND, + parameters: { + amount: extensionAction.parameters.amount, + note: extensionAction.parameters.note, + }, + timestamp, + }); + + return copiedExtensionState; } - const copiedExtensionState: ExtensionTypes.IState = Utils.deepCopy(extensionState); + /** Applies an add of payment instruction + * + * @param extensionsState previous state of the extensions + * @param extensionAction action to apply + * @param requestState request state read-only + * @param actionSigner identity of the signer + * @param timestamp timestamp of the action + * + * @returns state of the extension created + */ + protected applyAddPaymentInstruction( + extensionState: ExtensionTypes.IState, + extensionAction: ExtensionTypes.IAction, + requestState: RequestLogicTypes.IRequest, + actionSigner: IdentityTypes.IIdentity, + timestamp: number, + ): ExtensionTypes.IState { + if (extensionState.values.paymentInfo) { + throw Error(`The payment instruction already given`); + } + if (!requestState.payee) { + throw Error(`The request must have a payee`); + } + if (!Utils.identity.areEqual(actionSigner, requestState.payee)) { + throw Error(`The signer must be the payee`); + } - // increment paymentInfo - copiedExtensionState.values.paymentInfo = extensionAction.parameters.paymentInfo; + const copiedExtensionState: ExtensionTypes.IState = Utils.deepCopy(extensionState); - // update events - copiedExtensionState.events.push({ - name: ExtensionTypes.PnAnyDeclarative.ACTION.ADD_PAYMENT_INSTRUCTION, - parameters: { - paymentInfo: extensionAction.parameters.paymentInfo, - }, - timestamp, - }); + // increment paymentInfo + copiedExtensionState.values.paymentInfo = extensionAction.parameters.paymentInfo; - return copiedExtensionState; -} + // update events + copiedExtensionState.events.push({ + name: ExtensionTypes.PnAnyDeclarative.ACTION.ADD_PAYMENT_INSTRUCTION, + parameters: { + paymentInfo: extensionAction.parameters.paymentInfo, + }, + timestamp, + }); -/** Applies an add of refund instruction - * - * @param extensionsState previous state of the extensions - * @param extensionAction action to apply - * @param requestState request state read-only - * @param actionSigner identity of the signer - * @param timestamp timestamp of the action - * - * @returns state of the extension created - */ -function applyAddRefundInstruction( - extensionState: ExtensionTypes.IState, - extensionAction: ExtensionTypes.IAction, - requestState: RequestLogicTypes.IRequest, - actionSigner: IdentityTypes.IIdentity, - timestamp: number, -): ExtensionTypes.IState { - if (extensionState.values.refundInfo) { - throw Error(`The refund instruction already given`); - } - if (!requestState.payer) { - throw Error(`The request must have a payer`); - } - if (!Utils.identity.areEqual(actionSigner, requestState.payer)) { - throw Error(`The signer must be the payer`); + return copiedExtensionState; } - const copiedExtensionState: ExtensionTypes.IState = Utils.deepCopy(extensionState); + /** Applies an add of refund instruction + * + * @param extensionsState previous state of the extensions + * @param extensionAction action to apply + * @param requestState request state read-only + * @param actionSigner identity of the signer + * @param timestamp timestamp of the action + * + * @returns state of the extension created + */ + protected applyAddRefundInstruction( + extensionState: ExtensionTypes.IState, + extensionAction: ExtensionTypes.IAction, + requestState: RequestLogicTypes.IRequest, + actionSigner: IdentityTypes.IIdentity, + timestamp: number, + ): ExtensionTypes.IState { + if (extensionState.values.refundInfo) { + throw Error(`The refund instruction already given`); + } + if (!requestState.payer) { + throw Error(`The request must have a payer`); + } + if (!Utils.identity.areEqual(actionSigner, requestState.payer)) { + throw Error(`The signer must be the payer`); + } + + const copiedExtensionState: ExtensionTypes.IState = Utils.deepCopy(extensionState); - // increment refundInfo - copiedExtensionState.values.refundInfo = extensionAction.parameters.refundInfo; + // increment refundInfo + copiedExtensionState.values.refundInfo = extensionAction.parameters.refundInfo; - // update events - copiedExtensionState.events.push({ - name: ExtensionTypes.PnAnyDeclarative.ACTION.ADD_REFUND_INSTRUCTION, - parameters: { - refundInfo: extensionAction.parameters.refundInfo, - }, - timestamp, - }); + // update events + copiedExtensionState.events.push({ + name: ExtensionTypes.PnAnyDeclarative.ACTION.ADD_REFUND_INSTRUCTION, + parameters: { + refundInfo: extensionAction.parameters.refundInfo, + }, + timestamp, + }); - return copiedExtensionState; + return copiedExtensionState; + } } diff --git a/packages/advanced-logic/src/extensions/payment-network/reference-based.ts b/packages/advanced-logic/src/extensions/payment-network/reference-based.ts index ec5732bfb3..cbc30fce7f 100644 --- a/packages/advanced-logic/src/extensions/payment-network/reference-based.ts +++ b/packages/advanced-logic/src/extensions/payment-network/reference-based.ts @@ -6,11 +6,7 @@ const eightHexRegex = /[0-9a-f]{16,}/; /** * Core of the reference based payment networks - * Abstract implementation of the payment network to pay with a reference - * With this extension, one request can have two addresses (one for payment and one for refund) and a specific value to give as input data - * The value to give as input data is the last 8 bytes of a salted hash of the requestId and the address: `last8Bytes(hash(requestId + salt + address))`: - * The salt should have at least 8 bytes of randomness. A way to generate it is: - * `Math.floor(Math.random() * Math.pow(2, 4 * 8)).toString(16) + Math.floor(Math.random() * Math.pow(2, 4 * 8)).toString(16)` + * This module is called by the reference based payment networks to avoid code redundancy */ export default abstract class ReferenceBasedPaymentNetwork< TCreationParameters extends ExtensionTypes.PnReferenceBased.ICreationParameters = ExtensionTypes.PnReferenceBased.ICreationParameters diff --git a/packages/advanced-logic/test/extensions/content-data.test.ts b/packages/advanced-logic/test/extensions/content-data.test.ts index b6a227ada7..127ee50ce4 100644 --- a/packages/advanced-logic/test/extensions/content-data.test.ts +++ b/packages/advanced-logic/test/extensions/content-data.test.ts @@ -5,13 +5,15 @@ import ContentData from '../../src/extensions/content-data'; import * as TestData from '../utils/test-data-generator'; +const contentData = new ContentData(); + /* eslint-disable @typescript-eslint/no-unused-expressions */ describe('content-data', () => { describe('applyActionToExtension', () => { it('can applyActionToExtensions', () => { const requestCreatedNoExtensionBefore = Utils.deepCopy(TestData.requestCreatedNoExtension); const previousState = {}; - const newExtensionState = ContentData.applyActionToExtension( + const newExtensionState = contentData.applyActionToExtension( previousState, TestData.createContentDataExtensionData, requestCreatedNoExtensionBefore, @@ -29,7 +31,7 @@ describe('content-data', () => { }); it('cannot create state if already state', () => { expect(() => - ContentData.applyActionToExtension( + contentData.applyActionToExtension( TestData.expectedCreatedContentDataState, { action: ExtensionTypes.ContentData.ACTION.CREATE, @@ -46,7 +48,7 @@ describe('content-data', () => { it('cannot create state if action parameters do not have content', () => { expect(() => - ContentData.applyActionToExtension( + contentData.applyActionToExtension( {}, { action: ExtensionTypes.ContentData.ACTION.CREATE, @@ -64,7 +66,7 @@ describe('content-data', () => { it('cannot create state if action unknown', () => { // 'must throw' expect(() => { - ContentData.applyActionToExtension( + contentData.applyActionToExtension( {}, { action: 'unknown action', @@ -76,13 +78,13 @@ describe('content-data', () => { TestData.otherIdRaw.identity, TestData.arbitraryTimestamp, ); - }).toThrowError('Unknown action: unknown action'); + }).toThrowError('The extension should be created before receiving any other action'); }); }); describe('createCreationAction', () => { it('can createCreationAction', () => { - const extensionDataCreated = ContentData.createCreationAction({ + const extensionDataCreated = contentData.createCreationAction({ content: { what: 'ever', it: 'is' }, }); @@ -90,7 +92,7 @@ describe('content-data', () => { expect(extensionDataCreated).toEqual(TestData.createContentDataExtensionData); }); it('cannot create extension data if parameters do not have content', () => { - expect(() => ContentData.createCreationAction({} as any)).toThrowError( + expect(() => contentData.createCreationAction({} as any)).toThrowError( 'No content has been given for the extension content-data', ); }); diff --git a/packages/advanced-logic/test/extensions/payment-network/declarative.test.ts b/packages/advanced-logic/test/extensions/payment-network/declarative.test.ts index bc51fdc611..2c52b7234e 100644 --- a/packages/advanced-logic/test/extensions/payment-network/declarative.test.ts +++ b/packages/advanced-logic/test/extensions/payment-network/declarative.test.ts @@ -5,13 +5,15 @@ import Utils from '@requestnetwork/utils'; import * as TestDataDeclarative from '../../utils/payment-network/any/generator-data-create'; import * as TestData from '../../utils/test-data-generator'; +const pnAnyDeclarative = new PnAnyDeclarative(); + /* eslint-disable @typescript-eslint/no-unused-expressions */ describe('extensions/payment-network/any/declarative', () => { describe('createCreationAction', () => { it('can createCreationAction with payment and refund instruction', () => { // 'extensionsdata is wrong' expect( - PnAnyDeclarative.createCreationAction({ + pnAnyDeclarative.createCreationAction({ paymentInfo: TestDataDeclarative.paymentInfo, refundInfo: TestDataDeclarative.refundInfo, }), @@ -23,7 +25,7 @@ describe('extensions/payment-network/any/declarative', () => { // 'extensionsdata is wrong' expect( Utils.deepCopy( - PnAnyDeclarative.createCreationAction({ + pnAnyDeclarative.createCreationAction({ paymentInfo: TestDataDeclarative.paymentInfo, }), ), @@ -34,26 +36,19 @@ describe('extensions/payment-network/any/declarative', () => { // 'extensionsdata is wrong' expect( Utils.deepCopy( - PnAnyDeclarative.createCreationAction({ + pnAnyDeclarative.createCreationAction({ refundInfo: TestDataDeclarative.refundInfo, }), ), ).toEqual(TestDataDeclarative.actionCreationOnlyRefund); }); - it('can createCreationAction with nothing', () => { - // deep copy to remove the undefined properties to comply deep.equal() - // 'extensionsdata is wrong' - expect(Utils.deepCopy(PnAnyDeclarative.createCreationAction())).toEqual( - TestDataDeclarative.actionCreationEmpty, - ); - }); }); describe('createAddPaymentInstructionAction', () => { it('can createAddPaymentInstructionAction', () => { // 'extensionsdata is wrong' expect( - PnAnyDeclarative.createAddPaymentInstructionAction({ + pnAnyDeclarative.createAddPaymentInstructionAction({ paymentInfo: TestDataDeclarative.paymentInfo, }), ).toEqual(TestDataDeclarative.actionPaymentInstruction); @@ -64,7 +59,7 @@ describe('extensions/payment-network/any/declarative', () => { it('can createAddRefundInstructionAction', () => { // 'extensionsdata is wrong' expect( - PnAnyDeclarative.createAddRefundInstructionAction({ + pnAnyDeclarative.createAddRefundInstructionAction({ refundInfo: TestDataDeclarative.refundInfo, }), ).toEqual(TestDataDeclarative.actionRefundInstruction); @@ -75,7 +70,7 @@ describe('extensions/payment-network/any/declarative', () => { it('can createDeclareSentPaymentAction', () => { // 'extensionsdata is wrong' expect( - PnAnyDeclarative.createDeclareSentPaymentAction({ + pnAnyDeclarative.createDeclareSentPaymentAction({ amount: TestDataDeclarative.amount, note: TestDataDeclarative.note, }), @@ -87,7 +82,7 @@ describe('extensions/payment-network/any/declarative', () => { it('can createDeclareSentRefundAction', () => { // 'extensionsdata is wrong' expect( - PnAnyDeclarative.createDeclareSentRefundAction({ + pnAnyDeclarative.createDeclareSentRefundAction({ amount: TestDataDeclarative.amount, note: TestDataDeclarative.note, }), @@ -99,7 +94,7 @@ describe('extensions/payment-network/any/declarative', () => { it('can createDeclareReceivedPaymentAction', () => { // 'extensionsdata is wrong' expect( - PnAnyDeclarative.createDeclareReceivedPaymentAction({ + pnAnyDeclarative.createDeclareReceivedPaymentAction({ amount: TestDataDeclarative.amount, note: TestDataDeclarative.note, }), @@ -111,7 +106,7 @@ describe('extensions/payment-network/any/declarative', () => { it('can createDeclareReceivedRefundAction', () => { // 'extensionsdata is wrong' expect( - PnAnyDeclarative.createDeclareReceivedRefundAction({ + pnAnyDeclarative.createDeclareReceivedRefundAction({ amount: TestDataDeclarative.amount, note: TestDataDeclarative.note, }), @@ -126,7 +121,7 @@ describe('extensions/payment-network/any/declarative', () => { unknownAction.action = 'unknown action' as any; // 'must throw' expect(() => { - PnAnyDeclarative.applyActionToExtension( + pnAnyDeclarative.applyActionToExtension( TestDataDeclarative.requestStateCreatedEmpty.extensions, unknownAction, TestDataDeclarative.requestStateCreatedEmpty, @@ -141,7 +136,7 @@ describe('extensions/payment-network/any/declarative', () => { it('can applyActionToExtensions of creation', () => { // 'new extension state wrong' expect( - PnAnyDeclarative.applyActionToExtension( + pnAnyDeclarative.applyActionToExtension( TestDataDeclarative.requestStateNoExtensions.extensions, TestDataDeclarative.actionCreationWithPaymentAndRefund, TestDataDeclarative.requestStateNoExtensions, @@ -153,7 +148,7 @@ describe('extensions/payment-network/any/declarative', () => { it('cannot applyActionToExtensions of creation with a previous state', () => { // 'must throw' expect(() => { - PnAnyDeclarative.applyActionToExtension( + pnAnyDeclarative.applyActionToExtension( TestDataDeclarative.requestStateCreatedWithPaymentAndRefund.extensions, TestDataDeclarative.actionCreationWithPaymentAndRefund, TestDataDeclarative.requestStateCreatedWithPaymentAndRefund, @@ -168,7 +163,7 @@ describe('extensions/payment-network/any/declarative', () => { it('can applyActionToExtensions of addPaymentInstruction', () => { // 'new extension state wrong' expect( - PnAnyDeclarative.applyActionToExtension( + pnAnyDeclarative.applyActionToExtension( TestDataDeclarative.requestStateCreatedEmpty.extensions, TestDataDeclarative.actionPaymentInstruction, TestDataDeclarative.requestStateCreatedEmpty, @@ -180,21 +175,21 @@ describe('extensions/payment-network/any/declarative', () => { it('cannot applyActionToExtensions of addPaymentInstruction without a previous state', () => { // 'must throw' expect(() => { - PnAnyDeclarative.applyActionToExtension( + pnAnyDeclarative.applyActionToExtension( TestDataDeclarative.requestStateNoExtensions.extensions, TestDataDeclarative.actionPaymentInstruction, TestDataDeclarative.requestStateNoExtensions, TestData.payeeRaw.identity, TestData.arbitraryTimestamp, ); - }).toThrowError(`This extension must have been already created`); + }).toThrowError(`The extension should be created before receiving any other action`); }); it('cannot applyActionToExtensions of addPaymentInstruction without a payee', () => { const previousState = Utils.deepCopy(TestDataDeclarative.requestStateCreatedEmpty); previousState.payee = undefined; // 'must throw' expect(() => { - PnAnyDeclarative.applyActionToExtension( + pnAnyDeclarative.applyActionToExtension( previousState.extensions, TestDataDeclarative.actionPaymentInstruction, previousState, @@ -207,7 +202,7 @@ describe('extensions/payment-network/any/declarative', () => { const previousState = Utils.deepCopy(TestDataDeclarative.requestStateCreatedEmpty); // 'must throw' expect(() => { - PnAnyDeclarative.applyActionToExtension( + pnAnyDeclarative.applyActionToExtension( previousState.extensions, TestDataDeclarative.actionPaymentInstruction, previousState, @@ -219,7 +214,7 @@ describe('extensions/payment-network/any/declarative', () => { it('cannot applyActionToExtensions of addPaymentInstruction with payment instruction already given', () => { // 'must throw' expect(() => { - PnAnyDeclarative.applyActionToExtension( + pnAnyDeclarative.applyActionToExtension( TestDataDeclarative.requestStateCreatedWithPaymentAndRefund.extensions, TestDataDeclarative.actionPaymentInstruction, TestDataDeclarative.requestStateCreatedWithPaymentAndRefund, @@ -234,7 +229,7 @@ describe('extensions/payment-network/any/declarative', () => { it('can applyActionToExtensions of addRefundInstruction', () => { // 'new extension state wrong' expect( - PnAnyDeclarative.applyActionToExtension( + pnAnyDeclarative.applyActionToExtension( TestDataDeclarative.requestStateCreatedEmpty.extensions, TestDataDeclarative.actionRefundInstruction, TestDataDeclarative.requestStateCreatedEmpty, @@ -246,21 +241,21 @@ describe('extensions/payment-network/any/declarative', () => { it('cannot applyActionToExtensions of addRefundInstruction without a previous state', () => { // 'must throw' expect(() => { - PnAnyDeclarative.applyActionToExtension( + pnAnyDeclarative.applyActionToExtension( TestDataDeclarative.requestStateNoExtensions.extensions, TestDataDeclarative.actionRefundInstruction, TestDataDeclarative.requestStateNoExtensions, TestData.payerRaw.identity, TestData.arbitraryTimestamp, ); - }).toThrowError(`This extension must have been already created`); + }).toThrowError(`The extension should be created before receiving any other action`); }); it('cannot applyActionToExtensions of addRefundInstruction without a payer', () => { const previousState = Utils.deepCopy(TestDataDeclarative.requestStateCreatedEmpty); previousState.payer = undefined; // 'must throw' expect(() => { - PnAnyDeclarative.applyActionToExtension( + pnAnyDeclarative.applyActionToExtension( previousState.extensions, TestDataDeclarative.actionRefundInstruction, previousState, @@ -273,7 +268,7 @@ describe('extensions/payment-network/any/declarative', () => { const previousState = Utils.deepCopy(TestDataDeclarative.requestStateCreatedEmpty); // 'must throw' expect(() => { - PnAnyDeclarative.applyActionToExtension( + pnAnyDeclarative.applyActionToExtension( previousState.extensions, TestDataDeclarative.actionRefundInstruction, previousState, @@ -285,7 +280,7 @@ describe('extensions/payment-network/any/declarative', () => { it('cannot applyActionToExtensions of addRefundInstruction with payment instruction already given', () => { // 'must throw' expect(() => { - PnAnyDeclarative.applyActionToExtension( + pnAnyDeclarative.applyActionToExtension( TestDataDeclarative.requestStateCreatedWithPaymentAndRefund.extensions, TestDataDeclarative.actionRefundInstruction, TestDataDeclarative.requestStateCreatedWithPaymentAndRefund, @@ -300,7 +295,7 @@ describe('extensions/payment-network/any/declarative', () => { it('can applyActionToExtensions of declareSentPayment', () => { // 'new extension state wrong' expect( - PnAnyDeclarative.applyActionToExtension( + pnAnyDeclarative.applyActionToExtension( TestDataDeclarative.requestStateCreatedEmpty.extensions, TestDataDeclarative.actionDeclareSentPayment, TestDataDeclarative.requestStateCreatedEmpty, @@ -312,21 +307,21 @@ describe('extensions/payment-network/any/declarative', () => { it('cannot applyActionToExtensions of declareSentPayment without a previous state', () => { // 'must throw' expect(() => { - PnAnyDeclarative.applyActionToExtension( + pnAnyDeclarative.applyActionToExtension( TestDataDeclarative.requestStateNoExtensions.extensions, TestDataDeclarative.actionDeclareSentPayment, TestDataDeclarative.requestStateNoExtensions, TestData.payerRaw.identity, TestData.arbitraryTimestamp, ); - }).toThrowError(`This extension must have been already created`); + }).toThrowError(`The extension should be created before receiving any other action`); }); it('cannot applyActionToExtensions of declareSentPayment without a payer', () => { const previousState = Utils.deepCopy(TestDataDeclarative.requestStateCreatedEmpty); previousState.payer = undefined; // 'must throw' expect(() => { - PnAnyDeclarative.applyActionToExtension( + pnAnyDeclarative.applyActionToExtension( previousState.extensions, TestDataDeclarative.actionDeclareSentPayment, previousState, @@ -339,7 +334,7 @@ describe('extensions/payment-network/any/declarative', () => { const previousState = Utils.deepCopy(TestDataDeclarative.requestStateCreatedEmpty); // 'must throw' expect(() => { - PnAnyDeclarative.applyActionToExtension( + pnAnyDeclarative.applyActionToExtension( previousState.extensions, TestDataDeclarative.actionDeclareSentPayment, previousState, @@ -353,7 +348,7 @@ describe('extensions/payment-network/any/declarative', () => { // 'must throw' expect(() => { - PnAnyDeclarative.applyActionToExtension( + pnAnyDeclarative.applyActionToExtension( TestDataDeclarative.requestStateCreatedEmpty.extensions, TestDataDeclarative.actionDeclareSentPayment, TestDataDeclarative.requestStateCreatedEmpty, @@ -368,7 +363,7 @@ describe('extensions/payment-network/any/declarative', () => { it('can applyActionToExtensions of declareReceivedRefund', () => { // 'new extension state wrong' expect( - PnAnyDeclarative.applyActionToExtension( + pnAnyDeclarative.applyActionToExtension( TestDataDeclarative.requestStateCreatedEmpty.extensions, TestDataDeclarative.actionDeclareReceivedRefund, TestDataDeclarative.requestStateCreatedEmpty, @@ -380,21 +375,21 @@ describe('extensions/payment-network/any/declarative', () => { it('cannot applyActionToExtensions of declareReceivedRefund without a previous state', () => { // 'must throw' expect(() => { - PnAnyDeclarative.applyActionToExtension( + pnAnyDeclarative.applyActionToExtension( TestDataDeclarative.requestStateNoExtensions.extensions, TestDataDeclarative.actionDeclareReceivedRefund, TestDataDeclarative.requestStateNoExtensions, TestData.payerRaw.identity, TestData.arbitraryTimestamp, ); - }).toThrowError(`This extension must have been already created`); + }).toThrowError(`The extension should be created before receiving any other action`); }); it('cannot applyActionToExtensions of declareReceivedRefund without a payer', () => { const previousState = Utils.deepCopy(TestDataDeclarative.requestStateCreatedEmpty); previousState.payer = undefined; // 'must throw' expect(() => { - PnAnyDeclarative.applyActionToExtension( + pnAnyDeclarative.applyActionToExtension( previousState.extensions, TestDataDeclarative.actionDeclareReceivedRefund, previousState, @@ -407,7 +402,7 @@ describe('extensions/payment-network/any/declarative', () => { const previousState = Utils.deepCopy(TestDataDeclarative.requestStateCreatedEmpty); // 'must throw' expect(() => { - PnAnyDeclarative.applyActionToExtension( + pnAnyDeclarative.applyActionToExtension( previousState.extensions, TestDataDeclarative.actionDeclareReceivedRefund, previousState, @@ -421,7 +416,7 @@ describe('extensions/payment-network/any/declarative', () => { // 'must throw' expect(() => { - PnAnyDeclarative.applyActionToExtension( + pnAnyDeclarative.applyActionToExtension( TestDataDeclarative.requestStateCreatedEmpty.extensions, TestDataDeclarative.actionDeclareReceivedRefund, TestDataDeclarative.requestStateCreatedEmpty, @@ -436,7 +431,7 @@ describe('extensions/payment-network/any/declarative', () => { it('can applyActionToExtensions of declareSentRefund', () => { // 'new extension state wrong' expect( - PnAnyDeclarative.applyActionToExtension( + pnAnyDeclarative.applyActionToExtension( TestDataDeclarative.requestStateCreatedEmpty.extensions, TestDataDeclarative.actionDeclareSentRefund, TestDataDeclarative.requestStateCreatedEmpty, @@ -448,21 +443,21 @@ describe('extensions/payment-network/any/declarative', () => { it('cannot applyActionToExtensions of declareSentRefund without a previous state', () => { // 'must throw' expect(() => { - PnAnyDeclarative.applyActionToExtension( + pnAnyDeclarative.applyActionToExtension( TestDataDeclarative.requestStateNoExtensions.extensions, TestDataDeclarative.actionDeclareSentRefund, TestDataDeclarative.requestStateNoExtensions, TestData.payeeRaw.identity, TestData.arbitraryTimestamp, ); - }).toThrowError(`This extension must have been already created`); + }).toThrowError(`The extension should be created before receiving any other action`); }); it('cannot applyActionToExtensions of declareSentRefund without a payee', () => { const previousState = Utils.deepCopy(TestDataDeclarative.requestStateCreatedEmpty); previousState.payee = undefined; // 'must throw' expect(() => { - PnAnyDeclarative.applyActionToExtension( + pnAnyDeclarative.applyActionToExtension( previousState.extensions, TestDataDeclarative.actionDeclareSentRefund, previousState, @@ -475,7 +470,7 @@ describe('extensions/payment-network/any/declarative', () => { const previousState = Utils.deepCopy(TestDataDeclarative.requestStateCreatedEmpty); // 'must throw' expect(() => { - PnAnyDeclarative.applyActionToExtension( + pnAnyDeclarative.applyActionToExtension( previousState.extensions, TestDataDeclarative.actionDeclareSentRefund, previousState, @@ -489,7 +484,7 @@ describe('extensions/payment-network/any/declarative', () => { // 'must throw' expect(() => { - PnAnyDeclarative.applyActionToExtension( + pnAnyDeclarative.applyActionToExtension( TestDataDeclarative.requestStateCreatedEmpty.extensions, TestDataDeclarative.actionDeclareSentRefund, TestDataDeclarative.requestStateCreatedEmpty, @@ -504,7 +499,7 @@ describe('extensions/payment-network/any/declarative', () => { it('can applyActionToExtensions of declareReceivedPayment', () => { // 'new extension state wrong' expect( - PnAnyDeclarative.applyActionToExtension( + pnAnyDeclarative.applyActionToExtension( TestDataDeclarative.requestStateCreatedEmpty.extensions, TestDataDeclarative.actionDeclareReceivedPayment, TestDataDeclarative.requestStateCreatedEmpty, @@ -516,21 +511,21 @@ describe('extensions/payment-network/any/declarative', () => { it('cannot applyActionToExtensions of declareReceivedPayment without a previous state', () => { // 'must throw' expect(() => { - PnAnyDeclarative.applyActionToExtension( + pnAnyDeclarative.applyActionToExtension( TestDataDeclarative.requestStateNoExtensions.extensions, TestDataDeclarative.actionDeclareReceivedPayment, TestDataDeclarative.requestStateNoExtensions, TestData.payeeRaw.identity, TestData.arbitraryTimestamp, ); - }).toThrowError(`This extension must have been already created`); + }).toThrowError(`The extension should be created before receiving any other action`); }); it('cannot applyActionToExtensions of declareReceivedPayment without a payee', () => { const previousState = Utils.deepCopy(TestDataDeclarative.requestStateCreatedEmpty); previousState.payee = undefined; // 'must throw' expect(() => { - PnAnyDeclarative.applyActionToExtension( + pnAnyDeclarative.applyActionToExtension( previousState.extensions, TestDataDeclarative.actionDeclareReceivedPayment, previousState, @@ -543,7 +538,7 @@ describe('extensions/payment-network/any/declarative', () => { const previousState = Utils.deepCopy(TestDataDeclarative.requestStateCreatedEmpty); // 'must throw' expect(() => { - PnAnyDeclarative.applyActionToExtension( + pnAnyDeclarative.applyActionToExtension( previousState.extensions, TestDataDeclarative.actionDeclareReceivedPayment, previousState, @@ -557,7 +552,7 @@ describe('extensions/payment-network/any/declarative', () => { // 'must throw' expect(() => { - PnAnyDeclarative.applyActionToExtension( + pnAnyDeclarative.applyActionToExtension( TestDataDeclarative.requestStateCreatedEmpty.extensions, TestDataDeclarative.actionDeclareReceivedPayment, TestDataDeclarative.requestStateCreatedEmpty,