npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@multiversx/sdk-webview-provider

v2.0.0-alpha.0

Published

SDK Webview Provider

Downloads

2,477

Readme

mx-sdk-js-webview-provider

This provider is used to interact with the MultiversX dApps in a webview environment. It facilitates the communication between the host application (wallet) and the dApp.

Distribution

npm

Installation

sdk-webview-provider is delivered via npm, therefore it can be installed as follows:

npm install @multiversx/sdk-webview-provider

Building the library

In order to compile the library, run the following:

npm install
npm run compile

Usage

Parent window

<html>

<head>
    <meta charset="utf-8">
    <title>Webview Provider Example</title>
</head>

<body>
    <iframe id="webviewChild" src="http://child_URL/webviewChild.html" style="width: 100%; height: 200px;"></iframe>

    <script>
        // SigningProvider is one of the possible signing providers (extension, hardware wallet, etc.)
        const signingProvider = new SigningProvider();

        const webviewChild = document.getElementById("webviewChild");
        const targetWindow = webviewChild.contentWindow;

        window.addEventListener("message", async e => {
            const { type, payload } = e.data;

            switch (type) {
                case "LOGIN_REQUEST":
                    const address = await signingProvider.login();
                    
                    // generate accessToken using native auth client
                    const accessToken = "ZXJkMXdqeXRmbjZ6aHFmY3NlanZod3Y3cTR1c2F6czVyeWMzajhoYzc4ZmxkZ2pueWN0OHdlanFrYXN1bmM.Ykc5allXeG9iM04wLmY2ODE3NzUxMDc1NmVkY2U0NWVjYTg0Yjk0NTQ0YTZlYWNkZmEzNmU2OWRmZDNiOGYyNGM0MDEwZDE5OTA3NTEuMzAwLmV5SjBhVzFsYzNSaGJYQWlPakUyTnpNNU56SXlORFI5.a29dba3f0d2fb4712cb662bc8050c87bf9f0e7fd28f3c98efe0fda074be5b087bd75c075243e832c2985a9da044496b2cff3c852c8c963f9d3d840aed8799c07";

                    targetWindow?.postMessage({
                        type: "LOGIN_RESPONSE",
                        payload: {
                            data: {
                                address,
                                accessToken
                            }
                        },
                    })
                    break;
                case "SIGN_TRANSACTIONS_REQUEST":
                    const transactions = payload;
                    const signedTransactions = await signingProvider.signTransactions(transactions);

                    targetWindow?.postMessage({
                        type: "SIGN_TRANSACTIONS_RESPONSE",
                        payload: {
                            data: signedTransactions
                        },
                    })
                    break;
                case "SIGN_MESSAGE_REQUEST":
                    const messageToSign = payload?.message;

                    const signedMessage = await signingProvider.signMessage(messageToSign);

                    targetWindow?.postMessage({
                        type: "SIGN_MESSAGE_RESPONSE",
                        payload: {
                            data: {
                                signature: signedMessage.signature,
                                status: "signed"
                            }
                        },
                    })
                    break;
                default:
                    // handle other message types
                    break;
            }
        })
    </script>
</body>
</html>

Child window

<html>

<head>
    <meta charset="utf-8">
    <title>Webview Provider Example (Child Window)</title>
</head>

<body>
    <div>
        <button onclick="webview.relogin()">Relogin</button>
        <button onclick="webview.signTransactions()">Sign transactions</button>
        <button onclick="webview.signMessage()">Sign message</button>
        <button onclick="webview.logout()">Logout</button>
    </div>

    <script>
        const webview = new WebviewProvider.getInstance({
            resetStateCallback: () => console.log("Reset state callback called"),
        });
    </script>
</body>

</html>

!!! Important !!!

Using WKWebView, you need to register an object to be the handler for a particular message: jsHandler.

Every message (action like login, signMessage, etc.) sent from the child webview is using the jsHandler object to send the message to the parent window. Eg. window.webkit.messageHandlers.jsHandler.postMessage({ type: "LOGIN_REQUEST" });

It's developer's responsibility to handle the message in the parent window and call the appropriate method on the webview object.