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

@concordium/browser-wallet-api-types

v0.1.1

Published

This package includes the types for the API to be used in web applications for communicating with the Concordium browser wallet.

Downloads

6

Readme

browser-wallet-api-types

This package includes the types for the API to be used in web applications for communicating with the Concordium browser wallet.

This package changes the type of the window object, to include the injected api that is available on window.concordium.

Note that this package only contains the types and a description of the API, which is injected by the Concordium browser wallet.

The actual implementation can be found In the Concordium browser wallet repository.

Using the API

The API is automatically injected into web applications if the Concordium browser wallet extension is installed in the browser. It is made accessible by window.concordium. A callback (concordiumReady), which needs to be defined globally, is called by the injected script to notify applications that the API is ready for use. The following exemplifies how this can be done.

// Should be included as high in head tag of application HTML as possible to ensure the callback is defined before the script is injected.
window.concordiumReady = async () => {
    // The API is ready for use.
    const accountAddress = await window.concordium.connect();
};

To include the injected types in the window object's type, one can include this package's extend-window.d.ts file when building. This can be acheived by adding the following to your project's tsconfig.json file:

{
    ...
    "include": [..., "path/to/node_modules/@concordium/browser-wallet-api-types/extend-window.d.ts"]
}

API instance methods

connect

To request a connection to the wallet from the user, the connect method has to be invoked. The method returns a Promise resolving with information related to the currently selected account in the wallet, or rejecting if the request is rejected in the wallet. If this is not called, it will be called as part of any other request (f.x. sendTransaction or signMessage) made by the API.

const accountAddress = await window.concordium.connect();

sendTransaction

To send a transaction, two arguments need to be provided: A transaction type and a corresponding payload. Invoking sendTransaction returns a Promise, which resolves with the transaction hash for the submitted transaction.

The following exemplifies how to create a simple transfer of funds from one account (selected account in the wallet) to another. Please note that @concordium/web-sdk is used to provide the correct formats and types for the transaction payload.

const txHash = await window.concordium.sendTransaction(concordiumSDK.AccountTransactionType.SimpleTransfer, {
    amount: new concordiumSDK.GtuAmount(1n),
    toAddress: new concordiumSDK.AccountAddress('39bKAuC7sXCZQfo7DmVQTMbiUuBMQJ5bCfsS7sva1HvDnUXp13'),
});

In the case of a smart contract init/update, parameters for the specific function and a corresponding schema for serializing the parameters can be defined.

const txHash = await window.concordium.sendTransaction(
    concordiumSDK.AccountTransactionType.UpdateSmartContractInstance,
    {
        amount: new concordiumSDK.GtuAmount(1n),
        contractAddress: {
            index: 11n,
            subindex: 0n,
        },
        receiveName: 'two-step-transfer.receive',
        maxContractExecutionEnergy: 30000n,
    },
    {
        RequestTransfer: ['1000', '1', '3Y1RLgi5pW3x96xZ7CiDiKsTL9huU92qn6mfxpebwmtkeku8ry'],
    },
    'AQAAABEAAAB0d28tc3RlcC10cmFuc2ZlcgEUAAIAAAALAAAAaW5pdF9wYXJhbXMUAAMAAAAPAAAAYWNjb3VudF9ob2xkZXJzEQALHAAAAHRyYW5zZmVyX2FncmVlbWVudF90aHJlc2hvbGQCFAAAAHRyYW5zZmVyX3JlcXVlc3RfdHRsDggAAAByZXF1ZXN0cxIBBRQABAAAAA8AAAB0cmFuc2Zlcl9hbW91bnQKDgAAAHRhcmdldF9hY2NvdW50CwwAAAB0aW1lc19vdXRfYXQNCgAAAHN1cHBvcnRlcnMRAgsBFAADAAAADwAAAGFjY291bnRfaG9sZGVycxEACxwAAAB0cmFuc2Zlcl9hZ3JlZW1lbnRfdGhyZXNob2xkAhQAAAB0cmFuc2Zlcl9yZXF1ZXN0X3R0bA4BAAAABwAAAHJlY2VpdmUVAgAAAA8AAABSZXF1ZXN0VHJhbnNmZXIBAwAAAAUKCw8AAABTdXBwb3J0VHJhbnNmZXIBAwAAAAUKCw=='
);

signMessage

It is possible to sign arbitrary messages using the keys for an account stored in the wallet, by invoking the signMessage method. This method returns a Promise resolving with a signature of the message.

The following exemplifies requesting a signature of a message:

const signature = await window.concordium.signMessage('This is a message to be signed');

addChangeAccountListener

To react when the selected account in the wallet changes, a handler function can be assigned through addChangeAccountListener. This does not return the currently selected account when the handler is initially assigned. This can be obtained by invoking the connect method.

let selectedAccountAddress: string | undefined = undefined;
window.concordium.addChangeAccountListener((address) => (selectedAccountAddress = address));