# All files / dist Detector.js

82.5% Statements 33/40
55.77% Branches 29/52
83.33% Functions 5/6
94.29% Lines 33/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    1371x
    12x
    12x
    12x
    12x

    1x
    34x
    34x
    34x

    6x

    6x
    6x
    6x

    6x
    6x
    6x
    4x

    4x
    4x
    4x

    2x
    1x

    1x
    1x

    5x
    5x
    5x

    6x
    5x
    5x

    6x
    6x

    1x
     var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
        Iif (kind === "m") throw new TypeError("Private method is not writable");
        Iif (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
        Iif (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
        return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
    };
    var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
        Iif (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
        Iif (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
        return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
    };
    var _Detector_chainSpecs, _Detector_name, _Detector_isExtension, _Detector_providers;
    import { ApiPromise } from '@polkadot/api';
    import { SmoldotProvider } from './SmoldotProvider/SmoldotProvider.js';
    import { ExtensionProvider } from './ExtensionProvider/ExtensionProvider.js';
    import westend from './specs/westend.json';
    import kusama from './specs/kusama.json';
    import polkadot from './specs/polkadot.json';
    /**
     * Detector is an API for providing an instance of the PolkadotJS API configured
     * to use a WASM-based light client.  It takes care of detecting whether the
     * user has the substrate connect browser extension installed or not and
     * configures the PolkadotJS API appropriately; falling back to a provider
     * which instantiates the WASM light client in the page when the extension is
     * not available.
     *
     * @example
     *
     * ```
     * import { Detector } from '@substrate/connect';
     *
     * // Create a new UApp with a unique name
     * const app = new Detector('burnr-wallet');
     * const westend = await app.detect('westend');
     * const kusama = await app.detect('kusama');
     *
     * await westend.rpc.chain.subscribeNewHeads((lastHeader) => {
     *   console.log(lastHeader.hash);
     * );
     * await kusama.rpc.chain.subscribeNewHeads((lastHeader) => {
     *   console.log(lastHeader.hash);
     * });
     *
     * // Interact with westend and kusama APIs ...
     *
     * await westend.disconnect();
     * await kusama.disconnect();
     * ```
     */
    export class Detector {
        /**
         * @param name - the name of your app.
         *
         * @remarks
         *
         * You should make a best effort to make your app name unique to avoid
         * confusion for users in the extension user interface
         */
        constructor(name) {
            _Detector_chainSpecs.set(this, {
                'polkadot': polkadot,
                'kusama': kusama,
                'westend': westend
            });
            _Detector_name.set(this, void 0);
            _Detector_isExtension.set(this, void 0);
            _Detector_providers.set(this, {});
            /**
             * connect attempts to detect the extension and configures the PolkadotJS
             * API instance to return appropriately.
             *
             * There are 3 bundled networks: "polkadot", "kusama" and "westend" which
             * require no further configuration.
             *
             * Alternatively you may supply a chain spec and options to connect to a
             * custom chain with a light client (in-page only).
             *
             * @param chainName - the name of the blockchain network to connect to
             * @param chainSpec - an optional chainSpec to connect to a different network
             * @param options - any extra API options to passed to PolkadotJS when
             * constructing it.
             * @returns a promise that resolves to an instance of the PolkadotJS API
             *
             * @remarks
             *
             * When providing a custom chain spec, detect will always configure and return
             * an in-page light client and not use the extension
             *
             * Typically options is used to pass custom types to PolkadotJS API.
             *
             * See the PolkadotJS docs for documentation on using PolkadotJS.
             *
             * {@link https://polkadot.js.org/docs/}
             */
            this.connect = async (chainName, chainSpec, options) => {
                let provider = {};
                if (Object.keys(__classPrivateFieldGet(this, _Detector_chainSpecs, "f")).includes(chainName)) {
                    Iif (__classPrivateFieldGet(this, _Detector_isExtension, "f")) {
                        provider = new ExtensionProvider(__classPrivateFieldGet(this, _Detector_name, "f"), chainName);
                    }
                    else Eif (!__classPrivateFieldGet(this, _Detector_isExtension, "f")) {
                        const chainSpec = JSON.stringify(__classPrivateFieldGet(this, _Detector_chainSpecs, "f")[chainName]);
                        provider = new SmoldotProvider(chainSpec);
                    }
                }
                else if (chainSpec) {
                    provider = new SmoldotProvider(chainSpec);
                }
                else Eif (!chainSpec) {
                    throw new Error(`No known Chain was detected and no chainSpec was provided. Either give a known chain name ('${Object.keys(__classPrivateFieldGet(this, _Detector_chainSpecs, "f")).join('\', \'')}') or provide valid chainSpecs.`);
                }
                await provider.connect();
                __classPrivateFieldGet(this, _Detector_providers, "f")[chainName] = provider;
                return await ApiPromise.create(Object.assign(options !== null && options !== void 0 ? options : {}, { provider }));
            };
            /**
             * disconnect disconnects the PolkadotJS API isntance
             *
             * @param chainName - the name of the blockchain network to disconnect from
             */
            this.disconnect = async (chainName) => {
                await __classPrivateFieldGet(this, _Detector_providers, "f")[chainName].disconnect();
                delete __classPrivateFieldGet(this, _Detector_providers, "f")[chainName];
            };
            __classPrivateFieldSet(this, _Detector_isExtension, !!document.getElementById('substrateExtension'), "f");
            __classPrivateFieldSet(this, _Detector_name, name, "f");
        }
        /**
         * name is the name of the app. This is used by the extension to identify
         * your app in the user interface
         */
        get name() {
            return __classPrivateFieldGet(this, _Detector_name, "f");
        }
    }
    _Detector_chainSpecs = new WeakMap(), _Detector_name = new WeakMap(), _Detector_isExtension = new WeakMap(), _Detector_providers = new WeakMap();
    //# sourceMappingURL=Detector.js.map