aboutsummaryrefslogtreecommitdiffstats
path: root/packages/instant/src/util/env.ts
blob: 448ad526282143375f779ab9e4aeecc37eade5a6 (plain) (blame)
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
import * as bowser from 'bowser';
import { Provider } from 'ethereum-types';
import * as _ from 'lodash';

import { Browser, OperatingSystem, ProviderType } from '../types';

export const envUtil = {
    getBrowser(): Browser {
        if (bowser.chrome) {
            return Browser.Chrome;
        } else if (bowser.firefox) {
            return Browser.Firefox;
        } else if (bowser.opera) {
            return Browser.Opera;
        } else if (bowser.msedge) {
            return Browser.Edge;
        } else if (bowser.safari) {
            return Browser.Safari;
        } else {
            return Browser.Other;
        }
    },
    isMobileOperatingSystem(): boolean {
        return bowser.mobile;
    },
    getOperatingSystem(): OperatingSystem {
        if (bowser.android) {
            return OperatingSystem.Android;
        } else if (bowser.ios) {
            return OperatingSystem.iOS;
        } else if (bowser.mac) {
            return OperatingSystem.Mac;
        } else if (bowser.windows) {
            return OperatingSystem.Windows;
        } else if (bowser.windowsphone) {
            return OperatingSystem.WindowsPhone;
        } else if (bowser.linux) {
            return OperatingSystem.Linux;
        } else {
            return OperatingSystem.Other;
        }
    },
    getProviderName(provider: Provider): ProviderType | string {
        const constructorName = provider.constructor.name;
        let parsedProviderName = constructorName;
        // https://ethereum.stackexchange.com/questions/24266/elegant-way-to-detect-current-provider-int-web3-js
        switch (constructorName) {
            case 'EthereumProvider':
                parsedProviderName = ProviderType.Mist;
                break;

            default:
                parsedProviderName = constructorName;
                break;
        }
        if ((provider as any).isParity) {
            parsedProviderName = ProviderType.Parity;
        } else if ((provider as any).isMetaMask) {
            parsedProviderName = ProviderType.MetaMask;
        } else if (!_.isUndefined(_.get(window, 'SOFA'))) {
            parsedProviderName = ProviderType.CoinbaseWallet;
        } else if (!_.isUndefined(_.get(window, '__CIPHER__'))) {
            parsedProviderName = ProviderType.Cipher;
        }
        return parsedProviderName;
    },
};