|
| 1 | +import { type AccountChangeEventHandler } from "@starknet-io/get-starknet-core" |
| 2 | +import type { |
| 3 | + RequestFnCall, |
| 4 | + RpcMessage, |
| 5 | + RpcTypeToMessageMap, |
| 6 | + StarknetWindowObject, |
| 7 | +} from "@starknet-io/types-js" |
| 8 | +import type { |
| 9 | + AccountInterface, |
| 10 | + ProviderInterface, |
| 11 | + ProviderOptions, |
| 12 | +} from "starknet" |
| 13 | +import { |
| 14 | + Connector, |
| 15 | + type ConnectArgs, |
| 16 | + type ConnectorData, |
| 17 | + type ConnectorIcons, |
| 18 | +} from "../connector" |
| 19 | +import { Keplr } from "../injected/keplr" |
| 20 | +import { type InjectedConnectorOptions } from "../injected" |
| 21 | +import { KEPLR_MOBILE_APP_ICON } from "./constants" |
| 22 | +import { isInKeplrMobileAppBrowser } from "./helpers/inAppBrowser" |
| 23 | + |
| 24 | +export class KeplrMobileBaseConnector extends Connector { |
| 25 | + private _wallet: StarknetWindowObject | null = null |
| 26 | + |
| 27 | + constructor() { |
| 28 | + super() |
| 29 | + } |
| 30 | + |
| 31 | + available(): boolean { |
| 32 | + return true |
| 33 | + } |
| 34 | + |
| 35 | + async ready(): Promise<boolean> { |
| 36 | + // return true to be compatible with starknet-react |
| 37 | + // will need to be implemented |
| 38 | + return true |
| 39 | + } |
| 40 | + |
| 41 | + get id(): string { |
| 42 | + return "keplrMobile" |
| 43 | + } |
| 44 | + |
| 45 | + get name(): string { |
| 46 | + return "Keplr (mobile)" |
| 47 | + } |
| 48 | + |
| 49 | + get icon(): ConnectorIcons { |
| 50 | + return { |
| 51 | + dark: KEPLR_MOBILE_APP_ICON, |
| 52 | + light: KEPLR_MOBILE_APP_ICON, |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + get wallet(): StarknetWindowObject { |
| 57 | + throw new Error("not implemented") |
| 58 | + } |
| 59 | + |
| 60 | + async connect(_args: ConnectArgs = {}): Promise<ConnectorData> { |
| 61 | + await this.ensureWallet() |
| 62 | + |
| 63 | + // will return empty data, connect will only open keplr mobile app |
| 64 | + // will require to implement the wallet connection |
| 65 | + return { |
| 66 | + account: "", |
| 67 | + chainId: BigInt(0), |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + async disconnect(): Promise<void> { |
| 72 | + throw new Error("not implemented") |
| 73 | + } |
| 74 | + |
| 75 | + async account( |
| 76 | + _: ProviderOptions | ProviderInterface, |
| 77 | + ): Promise<AccountInterface> { |
| 78 | + throw new Error("not implemented") |
| 79 | + } |
| 80 | + |
| 81 | + async chainId(): Promise<bigint> { |
| 82 | + throw new Error("not implemented") |
| 83 | + } |
| 84 | + |
| 85 | + async request<T extends RpcMessage["type"]>( |
| 86 | + call: RequestFnCall<T>, |
| 87 | + ): Promise<RpcTypeToMessageMap[T]["result"]> { |
| 88 | + throw new Error("not implemented") |
| 89 | + } |
| 90 | + |
| 91 | + // needed, methods required by starknet-react. Otherwise an exception is throwd |
| 92 | + async initEventListener(_: AccountChangeEventHandler) { |
| 93 | + throw new Error("not implemented") |
| 94 | + } |
| 95 | + |
| 96 | + // needed, methods required by starknet-react. Otherwise an exception is throwd |
| 97 | + async removeEventListener(_: AccountChangeEventHandler) { |
| 98 | + throw new Error("not implemented") |
| 99 | + } |
| 100 | + |
| 101 | + private async ensureWallet(): Promise<void> { |
| 102 | + window.open( |
| 103 | + `https://deeplink.keplr.app/web-browser?url=${encodeURIComponent(window.origin)}`, |
| 104 | + "_blank", |
| 105 | + ) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +export interface KeplrMobileConnectorInitParams { |
| 110 | + inAppBrowserOptions?: Omit<InjectedConnectorOptions, "id"> |
| 111 | +} |
| 112 | + |
| 113 | +export class KeplrMobileConnector { |
| 114 | + static init(params?: KeplrMobileConnectorInitParams): Connector { |
| 115 | + const { inAppBrowserOptions } = params || {} |
| 116 | + if (isInKeplrMobileAppBrowser()) { |
| 117 | + return new Keplr(inAppBrowserOptions) |
| 118 | + } else { |
| 119 | + return new KeplrMobileBaseConnector() |
| 120 | + } |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +export { isInKeplrMobileAppBrowser } |
0 commit comments