-
-
Notifications
You must be signed in to change notification settings - Fork 446
feat(prover): support non-mutated verification provider in prover #6727
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
fbce3bd
Restructure the assertions
nazarhussain 0df5b04
Add an inspector to run the logic to detect providers
nazarhussain f77426a
Update web3 provdier logic to use inspetor
nazarhussain 095c676
Fix the types for proxy
nazarhussain 0cffea3
Make the default type for mutation
nazarhussain 8bb22c9
Rename elrpc to elrpcprovider
nazarhussain d05530b
Apply suggestions from code review
nazarhussain cef6fc3
Fix build error
nazarhussain 574f50c
Update the readme doc
nazarhussain b0fa5c9
Apply suggestions from code review
nazarhussain 73b3187
Fix the docs linting
nazarhussain 346f18d
Add missing words
nazarhussain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
packages/prover/src/provider_types/eip1193_provider_type.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import {Web3ProviderType} from "../interfaces.js"; | ||
| import {JsonRpcRequestOrBatch, JsonRpcResponseOrBatch} from "../types.js"; | ||
|
|
||
| // Modern providers uses this structure e.g. Web3 4.x | ||
| export interface EIP1193Provider { | ||
| request: (payload: JsonRpcRequestOrBatch) => Promise<JsonRpcResponseOrBatch>; | ||
| } | ||
| export default { | ||
| name: "eip1193", | ||
| matched(provider): provider is EIP1193Provider { | ||
| return ( | ||
| "request" in provider && | ||
| typeof provider.request === "function" && | ||
| provider.request.constructor.name === "AsyncFunction" | ||
| ); | ||
| }, | ||
| handler(provider) { | ||
| const request = provider.request.bind(provider); | ||
|
|
||
| return async (payload: JsonRpcRequestOrBatch): Promise<JsonRpcResponseOrBatch | undefined> => { | ||
| const response = await request(payload); | ||
| return response; | ||
| }; | ||
| }, | ||
| mutateProvider(provider, newHandler) { | ||
| Object.assign(provider, { | ||
| request: async function newRequest(payload: JsonRpcRequestOrBatch): Promise<JsonRpcResponseOrBatch | undefined> { | ||
| return newHandler(payload); | ||
| }, | ||
| }); | ||
| }, | ||
| } as Web3ProviderType<EIP1193Provider>; |
44 changes: 44 additions & 0 deletions
44
packages/prover/src/provider_types/ethers_provider_type.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import {Web3ProviderType} from "../interfaces.js"; | ||
| import {JsonRpcRequestOrBatch, JsonRpcResponse, JsonRpcResponseOrBatch} from "../types.js"; | ||
| import {isBatchRequest} from "../utils/json_rpc.js"; | ||
| import web3JsProviderType from "./web3_js_provider_type.js"; | ||
|
|
||
| export interface EthersProvider { | ||
| // Ethers provider does not have a public interface for batch requests | ||
| send(method: string, params: Array<unknown>): Promise<JsonRpcResponse>; | ||
| } | ||
| export default { | ||
| name: "ethers", | ||
| matched(provider): provider is EthersProvider { | ||
| return ( | ||
| !web3JsProviderType.matched(provider) && | ||
| "send" in provider && | ||
| typeof provider.send === "function" && | ||
| provider.send.length > 1 && | ||
| provider.send.constructor.name === "AsyncFunction" | ||
| ); | ||
| }, | ||
| handler(provider) { | ||
| const send = provider.send.bind(provider); | ||
|
|
||
| return async (payload: JsonRpcRequestOrBatch): Promise<JsonRpcResponseOrBatch | undefined> => { | ||
| // Because ethers provider public interface does not support batch requests | ||
| // so we need to handle it manually | ||
| if (isBatchRequest(payload)) { | ||
| const responses = []; | ||
| for (const request of payload) { | ||
| responses.push(await send(request.method, request.params)); | ||
| } | ||
| return responses; | ||
| } | ||
| return send(payload.method, payload.params); | ||
| }; | ||
| }, | ||
| mutateProvider(provider, newHandler) { | ||
| Object.assign(provider, { | ||
| send: function newSend(method: string, params: Array<unknown>): Promise<JsonRpcResponseOrBatch | undefined> { | ||
| return newHandler({jsonrpc: "2.0", id: 0, method, params}); | ||
| }, | ||
| }); | ||
| }, | ||
| } as Web3ProviderType<EthersProvider>; |
123 changes: 123 additions & 0 deletions
123
packages/prover/src/provider_types/legacy_provider_type.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| import {AnyWeb3Provider, Web3ProviderType} from "../interfaces.js"; | ||
| import {JsonRpcRequest, JsonRpcRequestOrBatch, JsonRpcResponse, JsonRpcResponseOrBatch} from "../types.js"; | ||
| import web3JsProviderType from "./web3_js_provider_type.js"; | ||
|
|
||
| // Some providers uses `request` instead of the `send`. e.g. Ganache | ||
| interface RequestProvider { | ||
| request( | ||
| payload: JsonRpcRequestOrBatch, | ||
| callback: (err: Error | undefined, response: JsonRpcResponseOrBatch) => void | ||
| ): void; | ||
| } | ||
| // The legacy Web3 1.x use this structure | ||
| interface SendProvider { | ||
| send(payload: JsonRpcRequest, callback: (err?: Error | null, response?: JsonRpcResponse) => void): void; | ||
| } | ||
| // Some legacy providers use this very old structure | ||
| interface SendAsyncProvider { | ||
| sendAsync(payload: JsonRpcRequestOrBatch): Promise<JsonRpcResponseOrBatch>; | ||
| } | ||
|
|
||
| type LegacyProvider = RequestProvider | SendProvider | SendAsyncProvider; | ||
|
|
||
| export default { | ||
| name: "legacy", | ||
| matched(provider): provider is LegacyProvider { | ||
| return isRequestProvider(provider) || isSendProvider(provider) || isSendAsyncProvider(provider); | ||
| }, | ||
| handler(provider) { | ||
| if (isRequestProvider(provider)) { | ||
| const request = provider.request.bind(provider); | ||
| return function newHandler(payload: JsonRpcRequestOrBatch): Promise<JsonRpcResponseOrBatch | undefined> { | ||
| return new Promise((resolve, reject) => { | ||
| request(payload, (err, response) => { | ||
| if (err) { | ||
| reject(err); | ||
| } else { | ||
| resolve(response); | ||
| } | ||
| }); | ||
| }); | ||
| }; | ||
| } | ||
| if (isSendProvider(provider)) { | ||
| const send = provider.send.bind(provider); | ||
| return function newHandler(payload: JsonRpcRequestOrBatch): Promise<JsonRpcResponseOrBatch | undefined> { | ||
| return new Promise((resolve, reject) => { | ||
| // web3 providers supports batch requests but don't have valid types | ||
| send(payload as JsonRpcRequest, (err, response) => { | ||
| if (err) { | ||
| reject(err); | ||
| } else { | ||
| resolve(response); | ||
| } | ||
| }); | ||
| }); | ||
| }; | ||
| } | ||
|
|
||
| // sendAsync provider | ||
| const sendAsync = provider.sendAsync.bind(provider); | ||
| return async function newHandler(payload: JsonRpcRequestOrBatch): Promise<JsonRpcResponseOrBatch | undefined> { | ||
| const response = await sendAsync(payload); | ||
| return response; | ||
| }; | ||
| }, | ||
| mutateProvider(provider, newHandler) { | ||
| if (isRequestProvider(provider)) { | ||
| const newRequest = function newRequest( | ||
| payload: JsonRpcRequestOrBatch, | ||
| callback: (err?: Error | null, response?: JsonRpcResponseOrBatch) => void | ||
| ): void { | ||
| newHandler(payload) | ||
| .then((response) => callback(undefined, response)) | ||
| .catch((err) => callback(err, undefined)); | ||
| }; | ||
|
|
||
| Object.assign(provider, {request: newRequest}); | ||
| } | ||
|
|
||
| if (isSendProvider(provider)) { | ||
| const newSend = function newSend( | ||
| payload: JsonRpcRequestOrBatch, | ||
| callback: (err?: Error | null, response?: JsonRpcResponseOrBatch) => void | ||
| ): void { | ||
| newHandler(payload) | ||
| .then((response) => callback(undefined, response)) | ||
| .catch((err) => callback(err, undefined)); | ||
| }; | ||
|
|
||
| Object.assign(provider, {send: newSend}); | ||
| } | ||
|
|
||
| // sendAsync provider | ||
| Object.assign(provider, {sendAsync: newHandler}); | ||
| }, | ||
| } as Web3ProviderType<LegacyProvider>; | ||
|
|
||
| function isSendProvider(provider: AnyWeb3Provider): provider is SendProvider { | ||
| return ( | ||
| !web3JsProviderType.matched(provider) && | ||
| "send" in provider && | ||
| typeof provider.send === "function" && | ||
| provider.send.length > 1 && | ||
| provider.send.constructor.name !== "AsyncFunction" | ||
| ); | ||
| } | ||
|
|
||
| function isRequestProvider(provider: AnyWeb3Provider): provider is RequestProvider { | ||
| return ( | ||
| !web3JsProviderType.matched(provider) && | ||
| "request" in provider && | ||
| typeof provider.request === "function" && | ||
| provider.request.length > 1 | ||
| ); | ||
| } | ||
|
|
||
| function isSendAsyncProvider(provider: AnyWeb3Provider): provider is SendAsyncProvider { | ||
| return ( | ||
| "sendAsync" in provider && | ||
| typeof provider.sendAsync === "function" && | ||
| provider.sendAsync.constructor.name === "AsyncFunction" | ||
| ); | ||
| } |
35 changes: 35 additions & 0 deletions
35
packages/prover/src/provider_types/web3_js_provider_type.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import {AnyWeb3Provider, Web3ProviderType} from "../interfaces.js"; | ||
| import {JsonRpcRequest, JsonRpcRequestOrBatch, JsonRpcResponse, JsonRpcResponseOrBatch} from "../types.js"; | ||
| import {isBatchRequest} from "../utils/json_rpc.js"; | ||
|
|
||
| export interface Web3jsProvider { | ||
| request: (payload: JsonRpcRequest) => Promise<JsonRpcResponse>; | ||
| } | ||
|
|
||
| export default { | ||
| name: "web3js", | ||
| matched(provider): provider is Web3jsProvider { | ||
| return ( | ||
| "isWeb3Provider" in provider.constructor && | ||
| (provider.constructor as {isWeb3Provider: (provider: AnyWeb3Provider) => boolean}).isWeb3Provider(provider) | ||
| ); | ||
| }, | ||
| handler(provider) { | ||
| const request = provider.request.bind(provider); | ||
|
|
||
| return async (payload: JsonRpcRequestOrBatch): Promise<JsonRpcResponseOrBatch | undefined> => { | ||
| if (isBatchRequest(payload)) { | ||
| return Promise.all(payload.map((p) => request(p))); | ||
| } | ||
|
|
||
| return request(payload); | ||
| }; | ||
| }, | ||
| mutateProvider(provider, newHandler) { | ||
| Object.assign(provider, { | ||
| request: async function newRequest(payload: JsonRpcRequestOrBatch): Promise<JsonRpcResponseOrBatch | undefined> { | ||
| return newHandler(payload); | ||
| }, | ||
| }); | ||
| }, | ||
| } as Web3ProviderType<Web3jsProvider>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.