Skip to content

Commit

Permalink
Use a single payment header in payments plugin (#393)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
prathamesh0 and nikugogoi authored Jul 25, 2023
1 parent 10fa9d6 commit 6731a64
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 63 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"packages": [
"packages/*"
],
"version": "0.2.48",
"version": "0.2.49",
"npmClient": "yarn",
"useWorkspaces": true,
"command": {
Expand Down
2 changes: 1 addition & 1 deletion packages/cache/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions packages/codegen/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cerc-io/codegen",
"version": "0.2.48",
"version": "0.2.49",
"description": "Code generator",
"private": true,
"main": "index.js",
Expand All @@ -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",
Expand Down
10 changes: 5 additions & 5 deletions packages/codegen/src/templates/package-template.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 5 additions & 5 deletions packages/graph-node/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions packages/ipld-eth-client/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/peer/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions packages/solidity-mapper/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/test/package.json
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/tracing-client/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
11 changes: 5 additions & 6 deletions packages/util/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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",
Expand Down
57 changes: 33 additions & 24 deletions packages/util/src/payments.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
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';
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')

Expand All @@ -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();

Expand All @@ -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...');

Expand Down Expand Up @@ -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);
Expand All @@ -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}`);
Expand All @@ -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;
}
Expand Down Expand Up @@ -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
})
};
}
Expand All @@ -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 }],
Expand All @@ -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);
};
18 changes: 9 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -350,13 +350,13 @@
wherearewe "^2.0.0"
xsalsa20 "^1.1.0"

"@cerc-io/nitro-client@^0.1.3":
version "0.1.3"
resolved "https://git.vdb.to/api/packages/cerc-io/npm/%40cerc-io%2Fnitro-client/-/0.1.3/nitro-client-0.1.3.tgz#a536da9f6c1bc0f95cd3c4ecc2358be5f36bc8f1"
integrity sha512-fZ8+Diz5tDrBz7t56X5R9ul54FE7tLmyAB//5NY2OBncjthpTmkF0kmQvO2KVyQGuriVTe7FbqwJMtIRWKxWEQ==
"@cerc-io/nitro-client@^0.1.4":
version "0.1.4"
resolved "https://git.vdb.to/api/packages/cerc-io/npm/%40cerc-io%2Fnitro-client/-/0.1.4/nitro-client-0.1.4.tgz#7f69fa5fa66beb7eedd6dddc81a29d2589e6d60d"
integrity sha512-2onCl2wygbXXhtjdMotRdeOXovFeiCegenE/y4uFKxOX83LpslTx/iK+d6HqcQHW4Kkc2pExu6PBntaPoKVpLA==
dependencies:
"@cerc-io/libp2p" "0.42.2-laconic-0.1.3"
"@cerc-io/nitro-util" "^0.1.3"
"@cerc-io/nitro-util" "^0.1.4"
"@cerc-io/peer" "^0.2.46"
"@cerc-io/ts-channel" "1.0.3-ts-nitro-0.1.1"
"@libp2p/crypto" "^1.0.4"
Expand All @@ -373,10 +373,10 @@
promjs "^0.4.2"
uint8arrays "^4.0.3"

"@cerc-io/nitro-util@^0.1.3":
version "0.1.3"
resolved "https://git.vdb.to/api/packages/cerc-io/npm/%40cerc-io%2Fnitro-util/-/0.1.3/nitro-util-0.1.3.tgz#1c640221c3936203e8ccf492e6374b50502d60d3"
integrity sha512-NTg8RO/3bzGLZie3pavcJJzLeL9gI1RLxwEMVBtRzsZfniDfsu7CTasoEX+hKgyBiZstEoMtGbfpwVmM1IEQBA==
"@cerc-io/nitro-util@^0.1.4":
version "0.1.4"
resolved "https://git.vdb.to/api/packages/cerc-io/npm/%40cerc-io%2Fnitro-util/-/0.1.4/nitro-util-0.1.4.tgz#101f61692050f0ae850b0a0a12e56252f3ddc5a8"
integrity sha512-dqzFA5czCxo/yyAhHMplzRzTyzf52WpU8U+ntZ9zFIfrWPHvHHyDY4YyjA2yVQEWfcyyIZ0GTzf6wDb/kP8dKg==
dependencies:
assert "^2.0.0"
debug "^4.3.4"
Expand Down

0 comments on commit 6731a64

Please sign in to comment.