Skip to content

Commit

Permalink
feat: create pay before persist request flow
Browse files Browse the repository at this point in the history
  • Loading branch information
aimensahnoun committed May 6, 2024
1 parent 422fff4 commit 776bb7d
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 1 deletion.
23 changes: 23 additions & 0 deletions packages/request-client.js/src/api/request-network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import * as Types from '../types';
import ContentDataExtension from './content-data-extension';
import Request from './request';
import localUtils from './utils';
import { NoConfirmHttpDataAccess } from '../no-confirm-http-data-access';

/**
* Entry point of the request-client.js library. Create requests, get requests, manipulate requests.
Expand All @@ -34,6 +35,7 @@ export default class RequestNetwork {
private requestLogic: RequestLogicTypes.IRequestLogic;
private transaction: TransactionTypes.ITransactionManager;
private advancedLogic: AdvancedLogicTypes.IAdvancedLogic;
private dataAccess: DataAccessTypes.IDataAccess;

private contentData: ContentDataExtension;
private currencyManager: ICurrencyManager;
Expand All @@ -58,6 +60,7 @@ export default class RequestNetwork {
paymentOptions?: Partial<PaymentNetworkOptions>;
}) {
this.currencyManager = currencyManager || CurrencyManager.getDefault();
this.dataAccess = dataAccess;
this.advancedLogic = new AdvancedLogic(this.currencyManager);
this.transaction = new TransactionManager(dataAccess, decryptionProvider);
this.requestLogic = new RequestLogic(this.transaction, signatureProvider, this.advancedLogic);
Expand Down Expand Up @@ -99,6 +102,8 @@ export default class RequestNetwork {
requestLogicCreateResult,
skipPaymentDetection: parameters.disablePaymentDetection,
disableEvents: parameters.disableEvents,
topics: requestLogicCreateResult.meta.transactionManagerMeta?.topics,
transactionData: requestLogicCreateResult.meta?.transactionManagerMeta.transactionData,
},
);

Expand All @@ -110,6 +115,22 @@ export default class RequestNetwork {
return request;
}

public async persistRequest(
transactionData: DataAccessTypes.ITransaction,
channelId: string,
topics?: string[],
): Promise<DataAccessTypes.IReturnPersistTransaction> {
if (this.dataAccess instanceof NoConfirmHttpDataAccess) {
throw new Error(
'Cannot persist request when skipCreateConfirmation is used. Create a new instance of RequestNetwork without skipCreateConfirmation to persist the request.',
);
}
const result: DataAccessTypes.IReturnPersistTransaction =
await this.dataAccess.persistTransaction(transactionData, channelId, topics);

return result;
}

/**
* Creates an encrypted request.
*
Expand Down Expand Up @@ -143,6 +164,8 @@ export default class RequestNetwork {
requestLogicCreateResult,
skipPaymentDetection: parameters.disablePaymentDetection,
disableEvents: parameters.disableEvents,
topics: requestLogicCreateResult.meta.transactionManagerMeta?.topics,
transactionData: requestLogicCreateResult.meta?.transactionManagerMeta.transactionData,
},
);

Expand Down
15 changes: 15 additions & 0 deletions packages/request-client.js/src/api/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
} from '@requestnetwork/payment-detection';
import {
CurrencyTypes,
DataAccessTypes,
EncryptionTypes,
IdentityTypes,
PaymentTypes,
Expand Down Expand Up @@ -79,6 +80,16 @@ export default class Request {
*/
private currencyManager: ICurrencyManager;

/**
* Transaction data of a in-memory request, necesary for persisting the request later on.
*/
private transactionData: DataAccessTypes.ITransaction | undefined;

/**
* Topics of a in-memory request, necesary for persisting the request later on.
*/
private topics: string[] | undefined;

/**
* Creates an instance of Request
*
Expand All @@ -99,6 +110,8 @@ export default class Request {
requestLogicCreateResult?: RequestLogicTypes.IReturnCreateRequest;
skipPaymentDetection?: boolean;
disableEvents?: boolean;
transactionData?: DataAccessTypes.ITransaction;
topics?: string[];
},
) {
this.requestLogic = requestLogic;
Expand All @@ -109,6 +122,8 @@ export default class Request {
this.skipPaymentDetection = options?.skipPaymentDetection || false;
this.disableEvents = options?.disableEvents || false;
this.currencyManager = currencyManager;
this.topics = options?.topics;
this.transactionData = options?.transactionData;

if (options?.requestLogicCreateResult && !this.disableEvents) {
const originalEmitter = options.requestLogicCreateResult;
Expand Down
12 changes: 11 additions & 1 deletion packages/request-client.js/src/http-request-network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import RequestNetwork from './api/request-network';
import HttpDataAccess, { NodeConnectionConfig } from './http-data-access';
import { MockDataAccess } from '@requestnetwork/data-access';
import { MockStorage } from './mock-storage';
import { NoConfirmHttpDataAccess } from './no-confirm-http-data-access';

/**
* Exposes RequestNetwork module configured to use http-data-access.
Expand All @@ -23,7 +24,8 @@ export default class HttpRequestNetwork extends RequestNetwork {
* @param options.useMockStorage When true, will use a mock storage in memory. Meant to simplify local development and should never be used in production.
* @param options.signatureProvider Module to handle the signature. If not given it will be impossible to create new transaction (it requires to sign).
* @param options.currencies custom currency list
* @param options.currencyManager custom currency manager (will override `currencies`)
* @param options.currencyManager custom currency manager (will override `currencies`)http
* @param options.skipCreateConfirmation allows to create a transaction without persisting it.
*/
constructor(
{
Expand All @@ -35,6 +37,7 @@ export default class HttpRequestNetwork extends RequestNetwork {
currencies,
currencyManager,
paymentOptions,
skipCreateConfirmation,
}: {
decryptionProvider?: DecryptionProviderTypes.IDecryptionProvider;
httpConfig?: Partial<ClientTypes.IHttpDataAccessConfig>;
Expand All @@ -44,13 +47,20 @@ export default class HttpRequestNetwork extends RequestNetwork {
currencies?: CurrencyInput[];
currencyManager?: ICurrencyManager;
paymentOptions?: Partial<PaymentNetworkOptions>;
skipCreateConfirmation?: boolean;
} = {
httpConfig: {},
useMockStorage: false,
skipCreateConfirmation: false,
},
) {
const dataAccess: DataAccessTypes.IDataAccess = useMockStorage
? new MockDataAccess(new MockStorage())
: skipCreateConfirmation
? new NoConfirmHttpDataAccess({
httpConfig,
nodeConnectionConfig,
})
: new HttpDataAccess({ httpConfig, nodeConnectionConfig });

if (!currencyManager) {
Expand Down
45 changes: 45 additions & 0 deletions packages/request-client.js/src/no-confirm-http-data-access.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import HttpDataAccess, { NodeConnectionConfig } from './http-data-access';
import { ClientTypes, DataAccessTypes, StorageTypes } from '@requestnetwork/types';
import { EventEmitter } from 'events';

export class NoConfirmHttpDataAccess extends HttpDataAccess {
constructor(
{
httpConfig,
nodeConnectionConfig,
}: {
httpConfig?: Partial<ClientTypes.IHttpDataAccessConfig>;
nodeConnectionConfig?: Partial<NodeConnectionConfig>;
} = {
httpConfig: {},
nodeConnectionConfig: {},
},
) {
super({ httpConfig, nodeConnectionConfig });
}

async persistTransaction(
transactionData: DataAccessTypes.ITransaction,
channelId: string,
topics?: string[],
): Promise<DataAccessTypes.IReturnPersistTransaction> {
const data: DataAccessTypes.IReturnPersistTransactionRaw = {
meta: {
topics: topics || [],
transactionStorageLocation: '',
storageMeta: {
state: StorageTypes.ContentState.PENDING,
timestamp: Date.now() / 1000,
},
},
result: {},
};

const result: DataAccessTypes.IReturnPersistTransaction = Object.assign(
new EventEmitter() as DataAccessTypes.PersistTransactionEmitter,
data,
);

return result;
}
}
2 changes: 2 additions & 0 deletions packages/transaction-manager/src/transaction-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ export default class TransactionManager implements TransactionTypes.ITransaction
meta: {
dataAccessMeta: persistResult.meta,
encryptionMethod: channelEncryptionMethod,
transactionData: transaction,
topics: topics.concat([hash]),
},
result: {},
});
Expand Down

0 comments on commit 776bb7d

Please sign in to comment.