Skip to content

Commit

Permalink
feat: add extension data at request creation (#897)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjlevesque committed Aug 25, 2022
1 parent f0ee19f commit 1ca2363
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export abstract class AbstractExtension<TCreationParameters> {
}

// if the action is not "create", the state must have been created before
if (!requestState.extensions[extensionAction.id]) {
if (!extensionsState[extensionAction.id]) {
throw Error(`The extension should be created before receiving any other action`);
}

Expand Down
8 changes: 4 additions & 4 deletions packages/request-client.js/src/api/request-network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,10 +405,6 @@ export default class RequestNetwork {
const contentData = parameters.contentData;
const topics = parameters.topics?.slice() || [];

if (requestParameters.extensionsData) {
throw new Error('extensionsData in request parameters must be empty');
}

// If ERC20, validate that the value is a checksum address
if (requestParameters.currency.type === RequestLogicTypes.CURRENCY.ERC20) {
if (!this.validERC20Address(requestParameters.currency.value)) {
Expand Down Expand Up @@ -455,6 +451,10 @@ export default class RequestNetwork {
topics.push(copiedRequestParameters.payer);
}

if (requestParameters.extensionsData) {
copiedRequestParameters.extensionsData.push(...requestParameters.extensionsData);
}

return { requestParameters: copiedRequestParameters, topics, paymentNetwork };
}

Expand Down
13 changes: 0 additions & 13 deletions packages/request-client.js/test/api/request-network.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,6 @@ describe('api/request-network', () => {
expect(typeof requestnetwork.fromRequestId).toBe('function');
});

describe('createRequest', () => {
it('cannot createRequest() with extensionsData', async () => {
const requestnetwork = new RequestNetwork({ dataAccess: mockDataAccess });

await expect(
requestnetwork.createRequest({
requestInfo: { extensionsData: ['not expected'] } as any,
signer: {} as any,
}),
).rejects.toThrowError('extensionsData in request parameters must be empty');
});
});

describe('fromRequestId', () => {
it('can get request with payment network fromRequestId', async () => {
const mockDataAccessWithTxs: DataAccessTypes.IDataAccess = {
Expand Down
35 changes: 35 additions & 0 deletions packages/request-client.js/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,41 @@ describe('index', () => {
expect(mock.history.post).toHaveLength(1);
});

it('allows to create a request with delegate', async () => {
const requestNetwork = new RequestNetwork({
useMockStorage: true,
signatureProvider: TestData.fakeSignatureProvider,
});

const paymentNetwork: PaymentTypes.IPaymentNetworkCreateParameters = {
id: PaymentTypes.PAYMENT_NETWORK_ID.DECLARATIVE,
parameters: {},
};

const request = await requestNetwork.createRequest({
paymentNetwork,
requestInfo: {
...TestData.parametersWithoutExtensionsData,
extensionsData: [
{
action: ExtensionTypes.PnAnyDeclarative.ACTION.ADD_DELEGATE,
id: ExtensionTypes.ID.PAYMENT_NETWORK_ANY_DECLARATIVE,
parameters: {
delegate: TestData.delegate.identity,
},
},
],
},
signer: TestData.payee.identity,
});
await request.waitForConfirmation();

const requestData = await waitForConfirmation(
request.declareReceivedPayment('10', 'received payment', TestData.delegate.identity),
);
expect(requestData.balance!.balance).toEqual('10');
});

it('allows to declare a received payment from delegate', async () => {
const requestNetwork = new RequestNetwork({
useMockStorage: true,
Expand Down
2 changes: 0 additions & 2 deletions packages/types/src/request-logic-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,4 @@ export enum ROLE {
PAYEE = 'payee',
PAYER = 'payer',
THIRD_PARTY = 'third-party',
PAYEE_DELEGATE = 'payeeDelegate',
PAYER_DELEGATE = 'payerDelegate',
}

0 comments on commit 1ca2363

Please sign in to comment.