ton-connect / sdk

SDK for TON Connect 2.0 — a comprehensive communication protocol between wallets and apps in TON ecosystem
Apache License 2.0
326 stars 92 forks source link

fix the `tryGetWindowKeys` method to get the currently injected wallet #256

Open fish-yan opened 2 months ago

fish-yan commented 2 months ago

fix method

export function tryGetWindowKeys(): string[] {
    const window = getWindow();
    if (!window) {
        return [];
    }

    try {
        return Object.keys(window);
    } catch {
        return [];
    }
}

to

export function tryGetWindowKeys(): [string, any][] {
    const window = getWindow();
    if (!window) {
        return [];
    }

    try {
        return Object.entries(window);
    } catch {
        return [];
    }
}

in injected-provider.ts file windowKeys is to get the keys and entries of the window.

export class InjectedProvider<T extends string = string> implements InternalProvider {

    ...

    public static getCurrentlyInjectedWallets(): WalletInfoCurrentlyInjected[] {
        if (!this.window) {
            return [];
        }

        const windowKeys = tryGetWindowKeys();
        const wallets = windowKeys.filter(([_, value]) =>
            isJSBridgeWithMetadata(value)
        ) as unknown as [string, { tonconnect: InjectedWalletApi }][];

    ...

}