Use a single payment header in payments plugin (#393)

* Add client address field to payments manager

* Use a single payment header in payments plugin

* Upgrade ts-nitro packages

* Rename method to authenticateVoucherForSender

* Upgrade package versions

---------

Co-authored-by: Nabarun <nabarun@deepstacksoft.com>
This commit is contained in:
prathamesh0
2023-07-25 13:34:02 +05:30
committed by GitHub
co-authored by nabarun
parent 10fa9d6b4b
commit 6731a640a2
14 changed files with 71 additions and 63 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@cerc-io/cache",
"version": "0.2.48",
"version": "0.2.49",
"description": "Generic object cache",
"main": "dist/index.js",
"scripts": {
+3 -3
View File
@@ -1,6 +1,6 @@
{
"name": "@cerc-io/cli",
"version": "0.2.48",
"version": "0.2.49",
"main": "dist/index.js",
"license": "AGPL-3.0",
"scripts": {
@@ -11,8 +11,8 @@
"chat": "DEBUG='vulcanize:*, laconic:*' node dist/chat.js"
},
"dependencies": {
"@cerc-io/peer": "^0.2.48",
"@cerc-io/util": "^0.2.48",
"@cerc-io/peer": "^0.2.49",
"@cerc-io/util": "^0.2.49",
"@ethersproject/providers": "^5.4.4",
"@graphql-tools/utils": "^9.1.1",
"@ipld/dag-cbor": "^8.0.0",
+2 -2
View File
@@ -1,6 +1,6 @@
{
"name": "@cerc-io/codegen",
"version": "0.2.48",
"version": "0.2.49",
"description": "Code generator",
"private": true,
"main": "index.js",
@@ -20,7 +20,7 @@
},
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
"dependencies": {
"@cerc-io/util": "^0.2.48",
"@cerc-io/util": "^0.2.49",
"@graphql-tools/load-files": "^6.5.2",
"@poanet/solidity-flattener": "https://github.com/vulcanize/solidity-flattener.git",
"@solidity-parser/parser": "^0.13.2",
@@ -41,12 +41,12 @@
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
"dependencies": {
"@apollo/client": "^3.3.19",
"@cerc-io/cli": "^0.2.48",
"@cerc-io/ipld-eth-client": "^0.2.48",
"@cerc-io/solidity-mapper": "^0.2.48",
"@cerc-io/util": "^0.2.48",
"@cerc-io/cli": "^0.2.49",
"@cerc-io/ipld-eth-client": "^0.2.49",
"@cerc-io/solidity-mapper": "^0.2.49",
"@cerc-io/util": "^0.2.49",
{{#if (subgraphPath)}}
"@cerc-io/graph-node": "^0.2.48",
"@cerc-io/graph-node": "^0.2.49",
{{/if}}
"@ethersproject/providers": "^5.4.4",
"apollo-type-bigint": "^0.1.3",
+5 -5
View File
@@ -1,10 +1,10 @@
{
"name": "@cerc-io/graph-node",
"version": "0.2.48",
"version": "0.2.49",
"main": "dist/index.js",
"license": "AGPL-3.0",
"devDependencies": {
"@cerc-io/solidity-mapper": "^0.2.48",
"@cerc-io/solidity-mapper": "^0.2.49",
"@ethersproject/providers": "^5.4.4",
"@graphprotocol/graph-ts": "^0.22.0",
"@nomiclabs/hardhat-ethers": "^2.0.2",
@@ -51,9 +51,9 @@
"dependencies": {
"@apollo/client": "^3.3.19",
"@cerc-io/assemblyscript": "0.19.10-watcher-ts-0.1.2",
"@cerc-io/cache": "^0.2.48",
"@cerc-io/ipld-eth-client": "^0.2.48",
"@cerc-io/util": "^0.2.48",
"@cerc-io/cache": "^0.2.49",
"@cerc-io/ipld-eth-client": "^0.2.49",
"@cerc-io/util": "^0.2.49",
"@types/json-diff": "^0.5.2",
"@types/yargs": "^17.0.0",
"bn.js": "^4.11.9",
+2 -2
View File
@@ -1,6 +1,6 @@
{
"name": "@cerc-io/ipld-eth-client",
"version": "0.2.48",
"version": "0.2.49",
"description": "IPLD ETH Client",
"main": "dist/index.js",
"scripts": {
@@ -20,7 +20,7 @@
"homepage": "https://github.com/cerc-io/watcher-ts#readme",
"dependencies": {
"@apollo/client": "^3.7.1",
"@cerc-io/cache": "^0.2.48",
"@cerc-io/cache": "^0.2.49",
"cross-fetch": "^3.1.4",
"debug": "^4.3.1",
"ethers": "^5.4.4",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@cerc-io/peer",
"version": "0.2.48",
"version": "0.2.49",
"description": "libp2p module",
"main": "dist/index.js",
"exports": "./dist/index.js",
+2 -2
View File
@@ -1,10 +1,10 @@
{
"name": "@cerc-io/solidity-mapper",
"version": "0.2.48",
"version": "0.2.49",
"main": "dist/index.js",
"license": "AGPL-3.0",
"devDependencies": {
"@cerc-io/ipld-eth-client": "^0.2.48",
"@cerc-io/ipld-eth-client": "^0.2.49",
"@ethersproject/abi": "^5.3.0",
"@nomiclabs/hardhat-ethers": "^2.0.2",
"@nomiclabs/hardhat-waffle": "^2.0.1",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@cerc-io/test",
"version": "0.2.48",
"version": "0.2.49",
"main": "dist/index.js",
"license": "AGPL-3.0",
"private": true,
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@cerc-io/tracing-client",
"version": "0.2.48",
"version": "0.2.49",
"description": "ETH VM tracing client",
"main": "dist/index.js",
"scripts": {
+5 -6
View File
@@ -1,13 +1,12 @@
{
"name": "@cerc-io/util",
"version": "0.2.48",
"version": "0.2.49",
"main": "dist/index.js",
"license": "AGPL-3.0",
"dependencies": {
"@apollo/utils.keyvaluecache": "^1.0.1",
"@cerc-io/nitro-client": "^0.1.3",
"@cerc-io/nitro-util": "^0.1.3",
"@cerc-io/solidity-mapper": "^0.2.48",
"@cerc-io/nitro-client": "^0.1.4",
"@cerc-io/solidity-mapper": "^0.2.49",
"@cerc-io/ts-channel": "1.0.3-ts-nitro-0.1.1",
"@ethersproject/providers": "^5.4.4",
"@graphql-tools/schema": "^9.0.10",
@@ -41,8 +40,8 @@
"yargs": "^17.0.1"
},
"devDependencies": {
"@cerc-io/cache": "^0.2.48",
"@cerc-io/ipld-eth-client": "^0.2.48",
"@cerc-io/cache": "^0.2.49",
"@cerc-io/ipld-eth-client": "^0.2.49",
"@nomiclabs/hardhat-waffle": "^2.0.1",
"@types/express": "^4.17.14",
"@types/fs-extra": "^9.0.11",
+33 -24
View File
@@ -1,5 +1,4 @@
import debug from 'debug';
import { ethers } from 'ethers';
import { LRUCache } from 'lru-cache';
import { FieldNode } from 'graphql';
import { ApolloServerPlugin, GraphQLResponse, GraphQLRequestContext } from 'apollo-server-plugin-base';
@@ -7,22 +6,22 @@ import { Response as HTTPResponse } from 'apollo-server-env';
import Channel from '@cerc-io/ts-channel';
import type { ReadWriteChannel } from '@cerc-io/ts-channel';
import type { Client, Signature, Voucher } from '@cerc-io/nitro-client';
import { recoverEthereumMessageSigner, getSignatureFromEthersSignature } from '@cerc-io/nitro-client';
import { hex2Bytes } from '@cerc-io/nitro-util';
import type { Client, Voucher } from '@cerc-io/nitro-client';
import { utils as nitroUtils } from '@cerc-io/nitro-client';
const log = debug('laconic:payments');
const IntrospectionQuery = 'IntrospectionQuery';
const HASH_HEADER_KEY = 'hash';
const SIG_HEADER_KEY = 'sig';
const PAYMENT_HEADER_KEY = 'x-payment';
const PAYMENT_HEADER_REGEX = /vhash:(.*),vsig:(.*)/;
const ERR_FREE_QUOTA_EXHUASTED = 'Free quota exhausted';
const ERR_PAYMENT_NOT_RECEIVED = 'Payment not received';
const HTTP_CODE_PAYMENT_NOT_RECEIVED = 402; // Payment required
const ERR_HEADER_MISSING = 'Header for hash or sig not set';
const HTTP_CODE_HEADER_MISSING = 400; // Bad request
const ERR_HEADER_MISSING = 'Payment header x-payment not set';
const ERR_INVALID_PAYMENT_HEADER = 'Invalid payment header format';
const HTTP_CODE_BAD_REQUEST = 400; // Bad request
const EMPTY_VOUCHER_HASH = '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'; // keccak256('0x')
@@ -38,6 +37,8 @@ const FREE_QUERIES = ['latestBlock'];
const REQUEST_TIMEOUT = 10 * 1000; // 10 seconds
export class PaymentsManager {
clientAddress?: string;
// TODO: Persist data
private remainingFreeQueriesMap: Map<string, number> = new Map();
@@ -57,6 +58,8 @@ export class PaymentsManager {
}
async subscribeToVouchers (client: Client): Promise<void> {
this.clientAddress = client.address;
const receivedVouchersChannel = client.receivedVouchers();
log('Starting voucher subscription...');
@@ -107,7 +110,7 @@ export class PaymentsManager {
}
async allowRequest (voucherHash: string, voucherSig: string): Promise<[boolean, string]> {
const senderAddress = getSenderAddress(voucherHash, voucherSig);
const senderAddress = nitroUtils.getSignerAddress(voucherHash, voucherSig);
if (voucherHash === EMPTY_VOUCHER_HASH) {
let remainingFreeQueries = this.remainingFreeQueriesMap.get(senderAddress);
@@ -128,7 +131,7 @@ export class PaymentsManager {
}
// Check for payment voucher received from the Nitro account
const paymentVoucherRecived = await this.authenticateVoucherForSender(voucherHash, senderAddress);
const paymentVoucherRecived = await this.authenticateVoucher(voucherHash, senderAddress);
if (paymentVoucherRecived) {
log(`Serving a paid query for ${senderAddress}`);
@@ -139,7 +142,7 @@ export class PaymentsManager {
}
}
private async authenticateVoucherForSender (voucherHash:string, senderAddress: string): Promise<boolean> {
async authenticateVoucher (voucherHash:string, senderAddress: string): Promise<boolean> {
if (this.acceptReceivedVouchers(voucherHash, senderAddress)) {
return true;
}
@@ -213,15 +216,28 @@ export const paymentsPlugin = (paymentsManager?: PaymentsManager): ApolloServerP
return null;
}
const hash = requestContext.request.http?.headers.get(HASH_HEADER_KEY);
const sig = requestContext.request.http?.headers.get(SIG_HEADER_KEY);
if (hash == null || sig == null) {
const paymentHeader = requestContext.request.http?.headers.get(PAYMENT_HEADER_KEY);
if (paymentHeader == null) {
return {
errors: [{ message: ERR_HEADER_MISSING }],
http: new HTTPResponse(undefined, {
headers: requestContext.response?.http?.headers,
status: HTTP_CODE_HEADER_MISSING
status: HTTP_CODE_BAD_REQUEST
})
};
}
let vhash: string, vsig: string;
const match = paymentHeader.match(PAYMENT_HEADER_REGEX);
if (match) {
[, vhash, vsig] = match;
} else {
return {
errors: [{ message: ERR_INVALID_PAYMENT_HEADER }],
http: new HTTPResponse(undefined, {
headers: requestContext.response?.http?.headers,
status: HTTP_CODE_BAD_REQUEST
})
};
}
@@ -236,7 +252,7 @@ export const paymentsPlugin = (paymentsManager?: PaymentsManager): ApolloServerP
continue;
}
const [allowRequest, rejectionMessage] = await paymentsManager.allowRequest(hash, sig);
const [allowRequest, rejectionMessage] = await paymentsManager.allowRequest(vhash, vsig);
if (!allowRequest) {
const failResponse: GraphQLResponse = {
errors: [{ message: rejectionMessage }],
@@ -256,10 +272,3 @@ export const paymentsPlugin = (paymentsManager?: PaymentsManager): ApolloServerP
}
};
};
const getSenderAddress = (hash: string, sig: string): string => {
const splitSig = ethers.utils.splitSignature(sig);
const signature: Signature = getSignatureFromEthersSignature(splitSig);
return recoverEthereumMessageSigner(hex2Bytes(hash), signature);
};