import type { Observable } from "rxjs" import { mergeMap, startWith } from "rxjs/operators" import type { IFrameEthereumProvider } from "@ledgerhq/iframe-provider" import type { Maybe, ConnectionState, EthereumProviderConnectionResult } from "@rarible/connector" import { cache, connectToWeb3, AbstractConnectionProvider, getStateConnecting, } from "@rarible/connector" type IframeInstance = IFrameEthereumProvider const PROVIDER_ID = "iframe" as const export class IframeConnectionProvider extends AbstractConnectionProvider { private readonly instance: Observable private readonly connection: Observable> constructor() { super() this.instance = cache(() => connect()) this.connection = this.instance.pipe( mergeMap(instance => connectToWeb3(instance)), startWith(getStateConnecting({ providerId: PROVIDER_ID })), ) } getId(): string { return PROVIDER_ID } getConnection() { return this.connection } getOption(): Promise> { return Promise.resolve(PROVIDER_ID) } async isAutoConnected(): Promise { return true } async isConnected(): Promise { return true } } async function connect(): Promise { const { IFrameEthereumProvider } = await import("@ledgerhq/iframe-provider") return new IFrameEthereumProvider() }