Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request/Response Middleware #119

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions SIPS/sip-17.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
---
sip: (To be assigned)
tolbrino marked this conversation as resolved.
Show resolved Hide resolved
title: JSON RPC provider
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
title: JSON RPC provider
title: JSON RPC Middleware

I think Middleware is more appropriate here, given that we want to forward the result to our selected provider.

status: Draft
author: Ronny Esterluss <[email protected]> (@esterlus)
created: 2023-11-15
---

## Abstract

Introduce a new endowment that would allow a snap to intercept outgoing traffic and act as a JSON RPC Provider according to [JSON-RPC 2.0 Specification](https://www.jsonrpc.org/specification).

## Motivation

We want to build a snap that gives users full privacy when doing JSON RPC calls using Metamask.
This snap will leverage the power of [RPCh](https://rpch.net/).
The original JSON RPC provider is still the target of the request, but the request will be routed through [HOPRNet](https://network.hoprnet.org/) to eliminate Metadata.


## Specification

> Formal specifications are written in Typescript.

### Language

The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
"SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and
"OPTIONAL" written in uppercase in this document are to be interpreted as described in [RFC 2119](https://www.ietf.org/rfc/rfc2119.txt)

### Snaps permissions

This SIP specifies a permission named `endowment:json-rpc-provider`.
The permission signals to the platform that the snap wants to act as a JSON RPC Provider and intercept outgoing JSON RPC requests.

This permission is specified as follows in `snap.manifest.json` files:

```json
{
"initialPermissions": {
"endowment:json-rpc-provider": {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"endowment:json-rpc-provider": {}
"endowment:json-rpc-middleware": {}

}
}
```

### Snaps exports

This SIP specifies a new exported event handler `onOutgoingJSONRpcRequest`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This SIP specifies a new exported event handler `onOutgoingJSONRpcRequest`.
This SIP specifies a new export: `onOutboundJSONRpcRequest`.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onOutboundRpcRequest maybe, so it's more consistent with the existing onRpcRequest.

The event handler MUST be called whenever Metamask makes an outgoing JSON RPC request.

#### Parameters

An object containing:

request - The JSON-RPC request
provider - The target JSON RPC Provider

* request

This parameter MUST be present and MUST adhere to the official [JSON-RPC 2.0 Specification](https://www.jsonrpc.org/specification).

* provider

This parameter SHOULD be present. It contains the RPC URL of the intented RPC Provider (e.g. `https://mainnet.infura.io/v3/`).
Metamask SHOULD use the configured RPC provider in the currently selected network.

#### Returns

A promise resolving to the response of the request or rejecting with an error.

#### Example

```typescript
import { OnOutgoingJSONRpcRequestHandler } from '@metamask/snaps-types';
import RPChSDK from '@rpch/sdk';

const sdk = new RPChSDK("<ClientId>");

export const onOutgoingJSONRpcRequest: OnOutgoingJSONRpcRequestHandler = async ({
request,
provider,
}) => {
return sdk.send(request, { provider });
};
```

#### Type definitions

```typescript

type OnOutgoingJSONRpcRequestHandler =
(args: { request: JSONRPCRequest, provider: string }) => Promise<JSONRPCResponse>;

type JSONRPCRequest = {
readonly jsonrpc: '2.0';
id?: string | number | null;
method: string;
params?: any[] | object;
};

type JSONRPCResponse = JSONRPCResult | JSONRPCError;

type JSONRPCResult = {
readonly jsonrpc: '2.0';
id?: string | number | null;
result: any;
};

type JSONRPCError = {
readonly jsonrpc: '2.0';
id?: string | number | null;
error: {
code: number;
message: string;
data?: any;
};
};
```

## Copyright

Copyright and related rights waived via [CC0](../LICENSE).
Loading