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

@stargazezone/cosmodal

v1.2.0

Published

A wallet connector with mobile WalletConnect support for the Cosmos ecosystem.

Downloads

6

Readme

@stargazezone/cosmodal

A wallet connector with mobile WalletConnect support for the Cosmos ecosystem.

Example

# Clone the repo.
git clone https://github.com/public-awesome/cosmodal
# Enter the example folder.
cd cosmodal/example

# Start the Next.js dev server.
npm install && npm run dev
# OR
yarn && yarn dev

Setup

  1. Install the Cosmodal package in your React project
npm install --save @stargazezone/cosmodal
# OR
yarn add @stargazezone/cosmodal
  1. Import WalletManagerProvider and wrap it around your whole app. Only include it once as an ancestor of all components that need to access the wallet. Likely you'll want this in your root App component. Check out the example code to see how to define wallets.
const MyApp: FunctionComponent<AppProps> = ({ Component, pageProps }) => (
  <WalletManagerProvider
    defaultChainId={ChainInfoID.Juno1}
    enabledWalletTypes={[WalletType.Keplr, WalletType.WalletConnectKeplr]}
    walletConnectClientMeta={{
      name: "CosmodalExampleDAPP",
      description: "A dapp using the cosmodal library.",
      url: "https://cosmodal.example.app",
      icons: ["https://cosmodal.example.app/walletconnect.png"],
    }}
  >
    <Component {...pageProps} />
  </WalletManagerProvider>
)

export default MyApp
  1. Manage the wallet by using the useWalletManager hook in your pages and components. You can use the hook in as many components as you want since the same objects are always returned (as long as there is only one WalletManagerProvider ancestor).
const Home: NextPage = () => {
  const { connect, disconnect, connectedWallet, error } = useWalletManager()

  return connectedWallet ? (
    <div>
      <p>
        Name: <b>{connectedWallet.name}</b>
      </p>
      <p>
        Address: <b>{connectedWallet.address}</b>
      </p>
      <button onClick={disconnect}>Disconnect</button>
    </div>
  ) : (
    <div>
      <button onClick={connect}>Connect</button>
      {error && <p>{error.message}</p>}
    </div>
  )
}

export default Home

API

WalletManagerProvider

This component takes the following properties:

| Property | Type | Required | Description | | --------------------------------- | ---------------------------------------------------------------- | -------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- | | enabledWalletTypes | WalletType[] | ☑ | Wallet types available for connection. | | defaultChainId | string | ☑ | Chain ID to initially connect to and selected by default if nothing is passed to the hook. Must be present in one of the objects in chainInfoList. | | chainInfoOverrides | ChainInfoOverrides \| undefined | | List or getter of additional or replacement ChainInfo objects. These will take precedent over internal definitions by comparing chainId. | | classNames | ModalClassNames | | Class names applied to various components for custom theming. | | closeIcon | ReactNode | | Custom close icon. | | walletConnectClientMeta | IClientMeta | | Descriptive info about the React app which gets displayed when enabling a WalletConnect wallet (e.g. name, image, etc.). | | renderLoader | () => ReactNode | | A custom loader to display in a few modals, such as when enabling the wallet. | | preselectedWalletType | WalletType | | When set to a valid wallet type, the connect function will skip the selection modal and attempt to connect to this wallet immediately. | | localStorageKey | string | | localStorage key for saving, loading, and auto connecting to a wallet. | | onKeplrKeystoreChangeEvent | (event: Event) => unknown | | Callback that will be attached as a listener to the keplr_keystorechange event on the window object. | | getSigningCosmWasmClientOptions | SigningClientGetter<SigningCosmWasmClientOptions> \| undefined | | Getter for options passed to SigningCosmWasmClient on connection. | | getSigningStargateClientOptions | SigningClientGetter<SigningStargateClientOptions> \| undefined | | Getter for options passed to SigningStargateClient on connection. |

useWalletManager

This hook returns the following fields (IWalletManagerContext):

| Property | Type | Description | | --------------------------------- | ---------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | connect | () => void | Function to begin the connection process. This will either display the wallet picker modal or immediately attempt to connect to a wallet depending on the props passed to WalletManagerProvider. | | disconnect | () => Promise<void> | Function that disconnects from the connected wallet. | | connectedWallet | ConnectedWallet \| undefined | Connected wallet info and clients for interacting with the chain. | | status | WalletConnectionStatus | Status of cosmodal. | | error | unknown | Error encountered during the connection process. | | isEmbeddedKeplrMobileWeb | boolean | If this app is running inside the Keplr Mobile web interface. | | chainInfoOverrides | ChainInfoOverrides \| undefined | List or getter of additional or replacement ChainInfo objects. These will take precedent over internal definitions by comparing chainId. This is passed through from the provider props to allow composition of your own hooks, and for use in the built-in useWallet hook. | | getSigningCosmWasmClientOptions | SigningClientGetter<SigningCosmWasmClientOptions> \| undefined | Getter for options passed to SigningCosmWasmClient on connection. This is passed through from the provider props to allow composition of your own hooks, and for use in the built-in useWallet hook. | | getSigningStargateClientOptions | SigningClientGetter<SigningStargateClientOptions> \| undefined | Getter for options passed to SigningStargateClient on connection. This is passed through from the provider props to allow composition of your own hooks, and for use in the built-in useWallet hook. |

useWallet

This hooks returns the following fields (ConnectedWallet with status and error added):

| Property | Type | Description | | ----------------------- | ------------------------------------ | -------------------------------------------------------- | | status | WalletConnectionStatus | Status of connection. | | error | unknown | Error encountered during the connection process. | | wallet | Wallet \| undefined | Wallet. | | walletClient | WalletClient \| undefined | Wallet client. | | chainInfo | ChainInfo \| undefined | Chain info the clients are connected to. | | offlineSigner | OfflineSigner \| undefined | Offline signer for the wallet client. | | name | string \| undefined | User's name for their wallet. | | address | string \| undefined | Wallet address. | | signingCosmWasmClient | SigningCosmWasmClient \| undefined | Signing client for interacting with CosmWasm chain APIs. | | signingStargateClient | SigningStargateClient \| undefined | Signing client for interacting with Stargate chain APIs. |

Relevant types

interface ModalClassNames {
  modalContent?: string
  modalOverlay?: string
  modalHeader?: string
  modalSubheader?: string
  modalCloseButton?: string
  walletList?: string
  wallet?: string
  walletImage?: string
  walletInfo?: string
  walletName?: string
  walletDescription?: string
  textContent?: string
}

interface IClientMeta {
  description: string
  url: string
  icons: string[]
  name: string
}

type WalletClient = Keplr | KeplrWalletConnectV1

enum WalletType {
  Keplr = "keplr",
  WalletConnectKeplr = "walletconnect_keplr",
}

interface ConnectedWallet {
  // Wallet.
  wallet: Wallet
  // Wallet client.
  walletClient: WalletClient
  // Chain info the clients are connected to.
  chainInfo: ChainInfo
  // Offline signer for the wallet client.
  offlineSigner: OfflineSigner
  // Name of wallet.
  name: string
  // Wallet address.
  address: string
  // Signing client for interacting with CosmWasm chain APIs.
  signingCosmWasmClient: SigningCosmWasmClient
  // Signing client for interacting with Stargate chain APIs.
  signingStargateClient: SigningStargateClient
}

enum WalletConnectionStatus {
  Initializing,
  AttemptingAutoConnection,
  // Don't call connect until this state is reached.
  ReadyForConnection,
  Connecting,
  Connected,
  Resetting,
  Errored,
}

type SigningClientGetter<T> = (
  chainInfo: ChainInfo
) => T | Promise<T | undefined> | undefined

type ChainInfoOverrides =
  | ChainInfo[]
  | (() => undefined | ChainInfo[] | Promise<undefined | ChainInfo[]>)

interface IWalletManagerContext {
  // Function to begin the connection process. This will either display
  // the wallet picker modal or immediately attempt to connect to a wallet
  // when `preselectedWalletType` is set.
  connect: () => void
  // Function that disconnects from the connected wallet.
  disconnect: () => Promise<void>
  // Connected wallet info and clients for interacting with the chain.
  connectedWallet?: ConnectedWallet
  // Status of cosmodal.
  status: WalletConnectionStatus
  // Error encountered during the connection process.
  error?: unknown
  // If this app is running inside the Keplr Mobile web interface.
  isEmbeddedKeplrMobileWeb: boolean
  // List or getter of additional or replacement ChainInfo objects. These
  // will take precedent over internal definitions by comparing `chainId`.
  // This is passed through from the provider props to allow composition
  // of your own hooks, and for use in the built-in useWallet hook.
  chainInfoOverrides?: ChainInfoOverrides
  // Getter for options passed to SigningCosmWasmClient on connection.
  // This is passed through from the provider props to allow composition
  // of your own hooks, and for use in the built-in useWallet hook.
  getSigningCosmWasmClientOptions?: SigningClientGetter<SigningCosmWasmClientOptions>
  // Getter for options passed to SigningStargateClient on connection.
  // This is passed through from the provider props to allow composition
  // of your own hooks, and for use in the built-in useWallet hook.
  getSigningStargateClientOptions?: SigningClientGetter<SigningStargateClientOptions>
}

interface WalletManagerProviderProps {
  // Wallet types available for connection.
  enabledWalletTypes: WalletType[]
  // Chain ID to initially connect to and selected by default if nothing
  // is passed to the hook. Must be present in one of the objects in
  // `chainInfoList`.
  defaultChainId: string
  // List or getter of additional or replacement ChainInfo objects. These
  // will take precedent over internal definitions by comparing `chainId`.
  chainInfoOverrides?: ChainInfoOverrides
  // Class names applied to various components for custom theming.
  classNames?: ModalClassNames
  // Custom close icon.
  closeIcon?: ReactNode
  // Descriptive info about the webapp which gets displayed when enabling a
  // WalletConnect wallet (e.g. name, image, etc.).
  walletConnectClientMeta?: IClientMeta
  // A custom loader to display in the modals, such as enabling the wallet.
  renderLoader?: () => ReactNode
  // When set to a valid wallet type, the connect function will skip the
  // selection modal and attempt to connect to this wallet immediately.
  preselectedWalletType?: `${WalletType}`
  // localStorage key for saving, loading, and auto connecting to a wallet.
  localStorageKey?: string
  // Callback that will be attached as a listener to the
  // `keplr_keystorechange` event on the window object.
  onKeplrKeystoreChangeEvent?: (event: Event) => unknown
  // Getter for options passed to SigningCosmWasmClient on connection.
  getSigningCosmWasmClientOptions?: SigningClientGetter<SigningCosmWasmClientOptions>
  // Getter for options passed to SigningStargateClient on connection.
  getSigningStargateClientOptions?: SigningClientGetter<SigningStargateClientOptions>
}