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

Fix types #127

Merged
merged 1 commit into from
Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
101 changes: 42 additions & 59 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
// You may have to bring your own Node types (e.g. @types/node) for these imports.
import { EventEmitter } from 'events';
import { Duplex } from 'stream';
import { JsonRpcRequest, JsonRpcResponse } from 'json-rpc-engine';

export interface MetaMaskInpageProviderOptions {

/**
* The name of the stream used to connect to the wallet.
*/
jsonRpcStreamName?: string;

/**
* The logging API to use.
* @default console
*/
logger?: Pick<Console, 'log' | 'warn' | 'error' | 'debug' | 'info' | 'trace'>;

/**
* The maximum number of event listeners.
* @default 100
*/
maxEventListeners?: number;

/**
* Whether the provider should send page metadata.
* @default true
*/
shouldSendMetadata?: boolean;
}
Expand Down Expand Up @@ -46,31 +49,31 @@ export class MetaMaskInpageProvider extends EventEmitter {
* Submits an RPC request per the given JSON-RPC request object.
*/
sendAsync (
payload: JsonRpcRequest,
callback: (error: Error | null, result?: JsonRpcResponse) => void,
payload: JsonRpcRequest<unknown>,
callback: (error: Error | null, result?: JsonRpcResponse<unknown>) => void,
): void;

/**
* Submits an RPC request for the given method, with the given params.
* @deprecated Use {@link request} instead.
*/
send (method: string, params?: unknown[]): Promise<JsonRpcResponse>;
send (method: string, params?: unknown[]): Promise<JsonRpcResponse<unknown>>;

/**
* Submits an RPC request per the given JSON-RPC request object.
* @deprecated Use {@link request} instead.
*/
send (
payload: JsonRpcRequest,
callback: (error: Error | null, result?: JsonRpcResponse) => void,
payload: JsonRpcRequest<unknown>,
callback: (error: Error | null, result?: JsonRpcResponse<unknown>) => void,
): void;

/**
* Accepts a JSON-RPC request object, and synchronously returns the cached result
* for the given method. Only supports 4 specific methods.
* @deprecated Use {@link request} instead.
*/
send (payload: SendSyncJsonRpcRequest): JsonRpcResponse;
send (payload: SendSyncJsonRpcRequest): JsonRpcResponse<unknown>;

/**
* Indicating that this provider is a MetaMask provider.
Expand All @@ -97,22 +100,31 @@ export class MetaMaskInpageProvider extends EventEmitter {
readonly chainId: string | undefined;
}

interface InitializeProviderOptions extends MetaMaskInpageProviderOptions {

/**
* The stream used to connect to the wallet.
*/
connectionStream: Duplex;

/**
* Whether the provider should be set as window.ethereum.
*/
shouldSetOnWindow?: boolean;

/**
* Whether the window.web3 shim should be set.
*/
shouldShimWeb3?: boolean;
}

/**
* Initializes a MetaMaskInpageProvider and (optionally) assigns it as window.ethereum.
*
* @returns The initialized provider (whether set or not).
*/
export function initializeProvider (
options: Pick<MetaMaskInpageProviderOptions, 'maxEventListeners' | 'shouldSendMetadata'> & {

/** A Node.js duplex stream. */
connectionStream: Duplex;

/**
* Whether the provider should be set as window.ethereum.
* @default true
*/
shouldSetOnWindow?: boolean;
}
options: InitializeProviderOptions,
): MetaMaskInpageProvider;

/**
Expand All @@ -123,53 +135,24 @@ export function initializeProvider (
*/
export function setGlobalProvider (providerInstance: MetaMaskInpageProvider): void;

export interface RequestArguments {

/** The RPC method to request. */
method: string;

/** The params of the RPC method, if any. */
params?: unknown[];
}
/**
* If no existing window.web3 is found, this function injects a web3 "shim" to
* not break dapps that rely on window.web3.currentProvider.
*
* @param provider - The provider to set as window.web3.currentProvider.
* @param log - The logging API to use.
*/
export function shimWeb3 (provider: MetaMaskInpageProvider, log: typeof console): void;

export interface JsonRpcRequest {
export interface RequestArguments {

/** The RPC method to request. */
method: string;

/** The params of the RPC method, if any. */
params?: unknown[];

/** For spec compliance; handled if not provided. */
id?: string | number;

/** For spec compliance; handled if not provided. */
jsonrpc?: '2.0';
params?: unknown[] | Record<string, unknown>;
}

export interface SendSyncJsonRpcRequest extends JsonRpcRequest {
method: 'eth_accounts' | 'eth_coinbase' | 'eth_uninstallFilter' | 'net_version';
}

interface JsonRpcResponseBase {

/** Equal to the corresponding JSON-RPC request object. */
id?: string | number;

/** Equal to the corresponding JSON-RPC request. */
jsonrpc?: '2.0';
}

export interface JsonRpcErrorResponse extends JsonRpcResponseBase {
error: {
code: number;
message: string;
data?: unknown;
};
}

export interface JsonRpcSuccessResponse extends JsonRpcResponseBase {
result: unknown;
}

export type JsonRpcResponse = JsonRpcSuccessResponse | JsonRpcErrorResponse;
2 changes: 1 addition & 1 deletion src/MetaMaskInpageProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module.exports = class MetaMaskInpageProvider extends SafeEventEmitter {
* @param {Object} connectionStream - A Node.js duplex stream
* @param {Object} options - An options bag
* @param {string} [options.jsonRpcStreamName] - The name of the internal JSON-RPC stream.
* Default: metamask_provider
* Default: metamask-provider
* @param {ConsoleLike} [options.logger] - The logging API to use. Default: console
* @param {number} [options.maxEventListeners] - The maximum number of event
* listeners. Default: 100
Expand Down
2 changes: 1 addition & 1 deletion src/initializeProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ function initializeProvider ({
let provider = new MetaMaskInpageProvider(
connectionStream,
{
logger,
jsonRpcStreamName,
logger,
maxEventListeners,
shouldSendMetadata,
},
Expand Down