All files / ethers.js/src.ts/providers provider-browser.ts

33.23% Statements 111/334
0% Branches 0/1
0% Functions 0/1
33.23% Lines 111/334

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 3351x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                                                                                                                                                                                                                                                                                                                                                                                                                
 
import { assertArgument, makeError } from "../utils/index.js";
 
import { JsonRpcApiPollingProvider } from "./provider-jsonrpc.js";
 
import type {
    JsonRpcApiProviderOptions,
    JsonRpcError, JsonRpcPayload, JsonRpcResult,
    JsonRpcSigner
} from "./provider-jsonrpc.js";
import type { Network, Networkish } from "./network.js";
 
/**
 *  The interface to an [[link-eip-1193]] provider, which is a standard
 *  used by most injected providers, which the [[BrowserProvider]] accepts
 *  and exposes the API of.
 */
export interface Eip1193Provider {
    /**
     *  See [[link-eip-1193]] for details on this method.
     */
    request(request: { method: string, params?: Array<any> | Record<string, any> }): Promise<any>;
};
 
/**
 *  The possible additional events dispatched when using the ``"debug"``
 *  event on a [[BrowserProvider]].
 */
export type DebugEventBrowserProvider = {
    action: "sendEip1193Payload",
    payload: { method: string, params: Array<any> }
} | {
    action: "receiveEip1193Result",
    result: any
} | {
    action: "receiveEip1193Error",
    error: Error
};
 
/**
 *  Provider info provided by the [[link-eip-6963]] discovery mechanism.
 */
export interface Eip6963ProviderInfo {
    uuid: string;
    name: string;
    icon: string;
    rdns: string;
}
 
interface Eip6963ProviderDetail {
    info: Eip6963ProviderInfo;
    provider: Eip1193Provider;
}
 
interface Eip6963Announcement {
    type: "eip6963:announceProvider";
    detail: Eip6963ProviderDetail
}
 
export type BrowserProviderOptions = {
    polling?: boolean;
    staticNetwork?: null | boolean | Network;
 
    cacheTimeout?: number;
    pollingInterval?: number;
 
    providerInfo?: Eip6963ProviderInfo;
};
 
/**
 *  Specifies how [[link-eip-6963]] discovery should proceed.
 *
 *  See: [[BrowserProvider-discover]]
 */
export interface BrowserDiscoverOptions {
    /**
     *  Override provider detection with this provider.
     */
    provider?: Eip1193Provider;
 
    /**
     *  Duration to wait to detect providers. (default: 300ms)
     */
    timeout?: number;
 
    /**
     *  Return the first detected provider. Otherwise wait for %%timeout%%
     *  and allowing filtering before selecting the desired provider.
     */
    anyProvider?: boolean;
 
    /**
     *  Use the provided window context. Useful in non-standard
     *  environments or to hijack where a provider comes from.
     */
    window?: any;
 
    /**
     *  Explicitly choose which provider to used once scanning is complete.
     */
    filter?: (found: Array<Eip6963ProviderInfo>) => null | BrowserProvider |
      Eip6963ProviderInfo;
}
 
 
/**
 *  A **BrowserProvider** is intended to wrap an injected provider which
 *  adheres to the [[link-eip-1193]] standard, which most (if not all)
 *  currently do.
 */
export class BrowserProvider extends JsonRpcApiPollingProvider {
    #request: (method: string, params: Array<any> | Record<string, any>) => Promise<any>;

    #providerInfo: null | Eip6963ProviderInfo;

    /**
     *  Connect to the %%ethereum%% provider, optionally forcing the
     *  %%network%%.
     */
    constructor(ethereum: Eip1193Provider, network?: Networkish, _options?: BrowserProviderOptions) {

        // Copy the options
        const options: JsonRpcApiProviderOptions = Object.assign({ },
          ((_options != null) ? _options: { }),
          { batchMaxCount: 1 });

        assertArgument(ethereum && ethereum.request, "invalid EIP-1193 provider", "ethereum", ethereum);

        super(network, options);

        this.#providerInfo = null;
        if (_options && _options.providerInfo) {
            this.#providerInfo = _options.providerInfo;
        }

        this.#request = async (method: string, params: Array<any> | Record<string, any>) => {
            const payload = { method, params };
            this.emit("debug", { action: "sendEip1193Request", payload });
            try {
                const result = await ethereum.request(payload);
                this.emit("debug", { action: "receiveEip1193Result", result });
                return result;
            } catch (e: any) {
                const error = new Error(e.message);
                (<any>error).code = e.code;
                (<any>error).data = e.data;
                (<any>error).payload = payload;
                this.emit("debug", { action: "receiveEip1193Error", error });
                throw error;
            }
        };
    }

    get providerInfo(): null | Eip6963ProviderInfo {
        return this.#providerInfo;
    }

    async send(method: string, params: Array<any> | Record<string, any>): Promise<any> {
        await this._start();

        return await super.send(method, params);
    }

    async _send(payload: JsonRpcPayload | Array<JsonRpcPayload>): Promise<Array<JsonRpcResult | JsonRpcError>> {
        assertArgument(!Array.isArray(payload), "EIP-1193 does not support batch request", "payload", payload);

        try {
            const result = await this.#request(payload.method, payload.params || [ ]);
            return [ { id: payload.id, result } ];
        } catch (e: any) {
            return [ {
                id: payload.id,
                error: { code: e.code, data: e.data, message: e.message }
            } ];
        }
    }

    getRpcError(payload: JsonRpcPayload, error: JsonRpcError): Error {

        error = JSON.parse(JSON.stringify(error));

        // EIP-1193 gives us some machine-readable error codes, so rewrite
        // them into Ethers standard errors.
        switch (error.error.code || -1) {
            case 4001:
                error.error.message = `ethers-user-denied: ${ error.error.message }`;
                break;
            case 4200:
                error.error.message = `ethers-unsupported: ${ error.error.message }`;
                break;
        }

        return super.getRpcError(payload, error);
    }

    /**
     *  Resolves to ``true`` if the provider manages the %%address%%.
     */
    async hasSigner(address: number | string): Promise<boolean> {
        if (address == null) { address = 0; }

        const accounts = await this.send("eth_accounts", [ ]);
        if (typeof(address) === "number") {
            return (accounts.length > address);
        }

        address = address.toLowerCase();
        return accounts.filter((a: string) => (a.toLowerCase() === address)).length !== 0;
    }

    async getSigner(address?: number | string): Promise<JsonRpcSigner> {
        if (address == null) { address = 0; }

        if (!(await this.hasSigner(address))) {
            try {
                await this.#request("eth_requestAccounts", [ ]);

            } catch (error: any) {
                const payload = error.payload;
                throw this.getRpcError(payload, { id: payload.id, error });
            }
        }

        return await super.getSigner(address);
    }

    /**
     *  Discover and connect to a Provider in the Browser using the
     *  [[link-eip-6963]] discovery mechanism. If no providers are
     *  present, ``null`` is resolved.
     */
    static async discover(options?: BrowserDiscoverOptions): Promise<null | BrowserProvider> {
        if (options == null) { options = { }; }

        if (options.provider) {
            return new BrowserProvider(options.provider);
        }

        const context = options.window ? options.window:
            (typeof(window) !== "undefined") ? window: null;

        if (context == null) { return null; }

        const anyProvider = options.anyProvider;
        if (anyProvider && context.ethereum) {
            return new BrowserProvider(context.ethereum);
        }

        if (!("addEventListener" in context && "dispatchEvent" in context
          && "removeEventListener" in context)) {
            return null;
        }

        const timeout = options.timeout ? options.timeout: 300;
        if (timeout === 0) { return null; }

        return await (new Promise((resolve, reject) => {
            let found: Array<Eip6963ProviderDetail> = [ ];

            const addProvider = (event: Eip6963Announcement) => {
                found.push(event.detail);
                if (anyProvider) { finalize(); }
            };

            const finalize = () => {
                clearTimeout(timer);

                if (found.length) {

                    // If filtering is provided:
                    if (options && options.filter) {

                        // Call filter, with a copies of found provider infos
                        const filtered = options.filter(found.map(i =>
                          Object.assign({ }, (i.info))));

                        if (filtered == null) {
                            // No provider selected
                            resolve(null);

                        } else if (filtered instanceof BrowserProvider) {
                            // Custom provider created
                            resolve(filtered);

                        } else {
                            // Find the matching provider
                            let match: null | Eip6963ProviderDetail = null;
                            if (filtered.uuid) {
                                const matches = found.filter(f =>
                                  (filtered.uuid === f.info.uuid));
                                // @TODO: What should happen if multiple values
                                //        for the same UUID?
                                match = matches[0];
                            }

                            if (match) {
                                const { provider, info } = match;
                                resolve(new BrowserProvider(provider, undefined, {
                                    providerInfo: info
                                }));
                            } else {
                                reject(makeError("filter returned unknown info", "UNSUPPORTED_OPERATION", {
                                    value: filtered
                                }));
                            }
                        }

                    } else {

                        // Pick the first found provider
                        const { provider, info } = found[0];
                        resolve(new BrowserProvider(provider, undefined, {
                            providerInfo: info
                        }));
                    }

                } else {
                    // Nothing found
                    resolve(null);
                }

                context.removeEventListener(<any>"eip6963:announceProvider",
                  addProvider);
            };

            const timer = setTimeout(() => { finalize(); }, timeout);

            context.addEventListener(<any>"eip6963:announceProvider",
              addProvider);

            context.dispatchEvent(new Event("eip6963:requestProvider"));
        }));
    }
}