Skip to content

Commit

Permalink
enable to webhook restriction
Browse files Browse the repository at this point in the history
  • Loading branch information
Tsuyoshi-Ishikawa committed Mar 7, 2023
1 parent 1dbfb34 commit 127031f
Show file tree
Hide file tree
Showing 7 changed files with 371 additions and 69 deletions.
10 changes: 7 additions & 3 deletions src/entities/Webhook.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export interface WebhookResponse {
status: number;
error?: any;
import { Transaction } from 'ethers';
import { JsonrpcRequestBody, RequestContext } from 'src/entities';

export interface WebhookTransferData {
requestContext: RequestContext;
body: JsonrpcRequestBody;
tx: Transaction;
}
86 changes: 74 additions & 12 deletions src/services/__tests__/proxy.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
AllowCheckService,
RateLimitService,
TypeCheckService,
WebhookService,
} from 'src/services';
import { JsonrpcError } from 'src/entities';
import { DatastoreService } from 'src/repositories';
Expand Down Expand Up @@ -87,6 +88,7 @@ describe('ProxyService', () => {
AllowCheckService,
TypeCheckService,
RateLimitService,
WebhookService,
DatastoreService,
],
})
Expand Down Expand Up @@ -707,7 +709,12 @@ describe('ProxyService', () => {
const allowedMethods: RegExp[] = [/^.*$/];
const datastore = 'redis';
const method = 'eth_sendRawTransaction';
const ip = '1.2.3.4';
const headers = { host: 'localhost' };
const requestContext = {
ip,
headers,
};
const body = {
jsonrpc: '2.0',
id: 1,
Expand Down Expand Up @@ -769,7 +776,7 @@ describe('ProxyService', () => {
);

try {
await proxyService.sendTransaction(headers, body);
await proxyService.sendTransaction(requestContext, body);
} catch (e) {
expect(e).toEqual(error);
expect(parseRawTx).not.toHaveBeenCalled();
Expand All @@ -785,7 +792,12 @@ describe('ProxyService', () => {
const allowedMethods: RegExp[] = [/^.*$/];
const datastore = 'redis';
const method = 'eth_sendRawTransaction';
const ip = '1.2.3.4';
const headers = { host: 'localhost' };
const requestContext = {
ip,
headers,
};
const body = {
jsonrpc: '2.0',
id: 1,
Expand Down Expand Up @@ -868,7 +880,7 @@ describe('ProxyService', () => {
);

try {
await proxyService.sendTransaction(headers, body);
await proxyService.sendTransaction(requestContext, body);
} catch (e) {
expect(e).toEqual(error);
expect(parseRawTx).toHaveBeenCalled();
Expand All @@ -885,7 +897,12 @@ describe('ProxyService', () => {
const allowedMethods: RegExp[] = [/^.*$/];
const datastore = 'redis';
const method = 'eth_sendRawTransaction';
const ip = '1.2.3.4';
const headers = { host: 'localhost' };
const requestContext = {
ip,
headers,
};
const body = {
jsonrpc: '2.0',
id: 1,
Expand Down Expand Up @@ -957,7 +974,7 @@ describe('ProxyService', () => {
);

try {
await proxyService.sendTransaction(headers, body);
await proxyService.sendTransaction(requestContext, body);
} catch (e) {
expect(e).toEqual(error);
expect(parseRawTx).toHaveBeenCalled();
Expand All @@ -973,7 +990,12 @@ describe('ProxyService', () => {
const allowedMethods: RegExp[] = [/^.*$/];
const datastore = 'redis';
const method = 'eth_sendRawTransaction';
const ip = '1.2.3.4';
const headers = { host: 'localhost' };
const requestContext = {
ip,
headers,
};
const body = {
jsonrpc: '2.0',
id: 1,
Expand Down Expand Up @@ -1048,7 +1070,7 @@ describe('ProxyService', () => {
);

try {
await proxyService.sendTransaction(headers, body);
await proxyService.sendTransaction(requestContext, body);
} catch (e) {
expect(e).toEqual(error);
expect(parseRawTx).toHaveBeenCalled();
Expand All @@ -1064,7 +1086,12 @@ describe('ProxyService', () => {
const allowedMethods: RegExp[] = [/^.*$/];
const datastore = 'redis';
const method = 'eth_sendRawTransaction';
const ip = '1.2.3.4';
const headers = { host: 'localhost' };
const requestContext = {
ip,
headers,
};
const body = {
jsonrpc: '2.0',
id: 1,
Expand Down Expand Up @@ -1130,7 +1157,7 @@ describe('ProxyService', () => {
datastoreService,
);

const result = await proxyService.sendTransaction(headers, body);
const result = await proxyService.sendTransaction(requestContext, body);
expect(result).toEqual(postResponse);
expect(parseRawTx).toHaveBeenCalled();
expect(checkContractDeploy).toHaveBeenCalled();
Expand All @@ -1144,7 +1171,12 @@ describe('ProxyService', () => {
const allowedMethods: RegExp[] = [/^.*$/];
const datastore = 'redis';
const method = 'eth_sendRawTransaction';
const ip = '1.2.3.4';
const headers = { host: 'localhost' };
const requestContext = {
ip,
headers,
};
const body = {
jsonrpc: '2.0',
id: 1,
Expand Down Expand Up @@ -1203,7 +1235,7 @@ describe('ProxyService', () => {
);

try {
await proxyService.sendTransaction(headers, body);
await proxyService.sendTransaction(requestContext, body);
} catch (e) {
expect(e).toEqual(error);
expect(parseRawTx).toHaveBeenCalled();
Expand All @@ -1221,7 +1253,12 @@ describe('ProxyService', () => {
const allowedMethods: RegExp[] = [/^.*$/];
const datastore = 'redis';
const method = 'eth_sendRawTransaction';
const ip = '1.2.3.4';
const headers = { host: 'localhost' };
const requestContext = {
ip,
headers,
};
const body = {
jsonrpc: '2.0',
id: 1,
Expand Down Expand Up @@ -1281,7 +1318,7 @@ describe('ProxyService', () => {
);

try {
await proxyService.sendTransaction(headers, body);
await proxyService.sendTransaction(requestContext, body);
} catch (e) {
expect(e).toEqual(error);
expect(parseRawTx).toHaveBeenCalled();
Expand All @@ -1297,7 +1334,12 @@ describe('ProxyService', () => {
const allowedMethods: RegExp[] = [/^.*$/];
const datastore = 'redis';
const method = 'eth_sendRawTransaction';
const ip = '1.2.3.4';
const headers = { host: 'localhost' };
const requestContext = {
ip,
headers,
};
const body = {
jsonrpc: '2.0',
id: 1,
Expand Down Expand Up @@ -1372,7 +1414,7 @@ describe('ProxyService', () => {
);

try {
await proxyService.sendTransaction(headers, body);
await proxyService.sendTransaction(requestContext, body);
} catch (e) {
expect(e).toEqual(error);
expect(parseRawTx).toHaveBeenCalled();
Expand All @@ -1388,7 +1430,12 @@ describe('ProxyService', () => {
const allowedMethods: RegExp[] = [/^.*$/];
const datastore = 'redis';
const method = 'eth_sendRawTransaction';
const ip = '1.2.3.4';
const headers = { host: 'localhost' };
const requestContext = {
ip,
headers,
};
const body = {
jsonrpc: '2.0',
id: 1,
Expand Down Expand Up @@ -1447,7 +1494,7 @@ describe('ProxyService', () => {
);

try {
await proxyService.sendTransaction(headers, body);
await proxyService.sendTransaction(requestContext, body);
} catch (e) {
expect(e).toEqual(error);
expect(parseRawTx).toHaveBeenCalled();
Expand All @@ -1463,7 +1510,12 @@ describe('ProxyService', () => {
const allowedMethods: RegExp[] = [/^.*$/];
const datastore = 'redis';
const method = 'eth_sendRawTransaction';
const ip = '1.2.3.4';
const headers = { host: 'localhost' };
const requestContext = {
ip,
headers,
};
const body = {
jsonrpc: '2.0',
id: 1,
Expand Down Expand Up @@ -1530,7 +1582,7 @@ describe('ProxyService', () => {
);

try {
await proxyService.sendTransaction(headers, body);
await proxyService.sendTransaction(requestContext, body);
} catch (e) {
expect(e).toEqual(error);
expect(parseRawTx).toHaveBeenCalled();
Expand All @@ -1546,7 +1598,12 @@ describe('ProxyService', () => {
const allowedMethods: RegExp[] = [/^.*$/];
const datastore = 'redis';
const method = 'eth_sendRawTransaction';
const ip = '1.2.3.4';
const headers = { host: 'localhost' };
const requestContext = {
ip,
headers,
};
const body = {
jsonrpc: '2.0',
id: 1,
Expand Down Expand Up @@ -1612,7 +1669,7 @@ describe('ProxyService', () => {
datastoreService,
);

const result = await proxyService.sendTransaction(headers, body);
const result = await proxyService.sendTransaction(requestContext, body);
expect(result).toEqual(postResponse);
expect(parseRawTx).toHaveBeenCalled();
expect(checkContractDeploy).not.toHaveBeenCalled();
Expand All @@ -1625,7 +1682,12 @@ describe('ProxyService', () => {
it('tx is successful and rateLimit is not set', async () => {
const allowedMethods: RegExp[] = [/^.*$/];
const method = 'eth_sendRawTransaction';
const ip = '1.2.3.4';
const headers = { host: 'localhost' };
const requestContext = {
ip,
headers,
};
const body = {
jsonrpc: '2.0',
id: 1,
Expand Down Expand Up @@ -1685,7 +1747,7 @@ describe('ProxyService', () => {
datastoreService,
);

const result = await proxyService.sendTransaction(headers, body);
const result = await proxyService.sendTransaction(requestContext, body);
expect(result).toEqual(postResponse);
expect(parseRawTx).toHaveBeenCalled();
expect(checkContractDeploy).not.toHaveBeenCalled();
Expand Down
2 changes: 2 additions & 0 deletions src/services/__tests__/rateLimit.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
VerseService,
AllowCheckService,
RateLimitService,
WebhookService,
} from 'src/services';
import { DatastoreService } from 'src/repositories';
import { JsonrpcError } from 'src/entities';
Expand All @@ -22,6 +23,7 @@ describe('RateLimitService', () => {
VerseService,
AllowCheckService,
RateLimitService,
WebhookService,
TransactionService,
DatastoreService,
],
Expand Down
Loading

0 comments on commit 127031f

Please sign in to comment.