From 4a6b6bfb03764f944af618fc1ee851b01c256e95 Mon Sep 17 00:00:00 2001 From: robertlincecum Date: Mon, 17 Jun 2024 16:47:05 -0500 Subject: [PATCH] add getSigner back to browser provider --- src/providers/provider-browser.ts | 41 +++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/src/providers/provider-browser.ts b/src/providers/provider-browser.ts index dce43b13..d44066a7 100644 --- a/src/providers/provider-browser.ts +++ b/src/providers/provider-browser.ts @@ -1,11 +1,9 @@ import { assertArgument } from '../utils/index.js'; -import { JsonRpcApiProvider } from './provider-jsonrpc.js'; +import { JsonRpcApiProvider, JsonRpcSigner } from './provider-jsonrpc.js'; -import type { - JsonRpcError, JsonRpcPayload, JsonRpcResult -} from "./provider-jsonrpc.js"; -import type { Networkish } from "./network.js"; +import type { JsonRpcError, JsonRpcPayload, JsonRpcResult } from './provider-jsonrpc.js'; +import type { Networkish } from './network.js'; /** * The interface to an [EIP-1193](https://eips.ethereum.org/EIPS/eip-1193) provider, which is a standard used by most @@ -73,6 +71,20 @@ export class BrowserProvider extends JsonRpcApiProvider { }; } + async hasSigner(address: number | string): Promise { + if (address == null) { + address = 0; + } + + const accounts = await this.send('quai_accounts', []); + if (typeof address === 'number') { + return accounts.length > address; + } + + address = address.toLowerCase(); + return accounts.filter((a: string) => a.toLowerCase() === address).length !== 0; + } + async send(method: string, params: Array | Record): Promise { await this._start(); @@ -112,6 +124,25 @@ export class BrowserProvider extends JsonRpcApiProvider { return super.getRpcError(payload, error); } + async getSigner(address?: number | string): Promise { + if (address == null) { + address = 0; + } + + if (!(await this.hasSigner(address))) { + try { + //const resp = + await this.#request('quai_requestAccounts', []); + //console.log("RESP", resp); + } catch (error: any) { + const payload = error.payload; + throw this.getRpcError(payload, { id: payload.id, error }); + } + } + + return await super.getSigner(address); + } + /** * Resolves to `true` if the provider manages the `address`. *