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

hedera-wallet-connect-rn

v1.0.1

Published

A library to facilitate integrating Hedera with WalletConnect

Downloads

10

Readme

Overview

This library is the result of Hedera community collaboration to bring Hedera into the WalletConnect ecosystem and vice versa.

The goal of this repository is to be a reference for wallets and dApps integrating the WalletConnect <> Hedera JSON-RPC reference. Additionally, this library is meant to be included in projects supporting WalletConnect and Hedera, providing utility functions useful to validating requests and resposes in both the WalletConnect JSON-RPC context as well as the Hedera context.

A few useful resources include:

WalletConnect brings the ecosystem together by enabling wallets and apps to securely connect and interact.

-- https://walletconnect.com

Hedera aims to be:

The open source public ledger for everyone

-- https://hedera.com


This package managed by the Hedera community and is intended to be a standard for ecosystem wallets and dApp providers utilizing WalletConnect as a their communications protocol. It utilizes the @hashgraph/sdk and provides functions to facilitate implementing the WalletConnect <> Hedera JSON-RPC spec which has been defined through the collaborative HIP process in HIP-820.

This library facilitates the implementation of the WalletConnect <> Hedera Spec which allows wallets and dApps to natively integrate with Hedera. It provides additional, out of network functionality with the hedera_signMessage function.

In short, it uses the Hedera javascript SDK to build transactions, serialize them, send to wallets for processing and return responses back to dApps.

Please note, this is distinct from the Implementation of Ethereum JSON-RPC APIs for Hedera. At the time of this writing, "the Hedera JSON-RPC relay implementation is in beta, offers limited functionality today, and is only available to developers."

The relay and this library have different intentions and serve different purposes - namely native Hedera integration vs. Ethereum compatability layers to ease developer onboarding for those more familiar with the Ethereum ecosystem.

Set up

To start using WalletConnect, sign up for an account at https://cloud.walletconnect.com. You will use your project id when initializing client libraries.

It is important to understand core WalletConnect concepts when integrating this library. Please reference the WalletConnect documentation.

Usage

Upon successfully configuring your dApp and/or wallet to manage WalletConnect sessions, you can use this library’s functions to easily create and handle requests for the Hedera network.

Installation

npm i --save @hashgraph/hedera-wallet-connect

Example code

DApps

Signer

This library provides a DAppSigner class that implements the @hashgraph/sdk's Signer interface. You may use the DAppSigner class to sign and execute transactions on the Hedera network.

Get Signer

After you have paired your wallet with your dApp, you can get the signer from the DAppConnector instance:

const signer = dAppConnector.signers[0] // DAppSigner

Or, if multiple signers are available, you can find the signer by account ID:

const signer = dAppConnector.signers.find(
  (signer_) => signer_.getAccountId().toString() === '0.0.100',
) // DAppSigner
Sign Transactions
const transaction = new TransferTransaction()
  .addHbarTransfer('0.0.100', new Hbar(-1))
  .addHbarTransfer('0.0.101', new Hbar(1))

await transaction.freezeWithSigner(signer)
const signedTransaction = await signer.signTransaction(transaction)
Sign and Execute Transactions
const transaction = new TransferTransaction()
  .addHbarTransfer('0.0.100', new Hbar(-1))
  .addHbarTransfer('0.0.101', new Hbar(1))

await transaction.freezeWithSigner(signer)
const transactionResponse = await transaction.executeWithSigner(signer)
Sign and verify messages
const text = 'Example message to sign'
const base64String = btoa(text)

const sigMaps = await signer.sign([base64StringToUint8Array(base64String)]) // import { base64StringToUint8Array } from '@hashgraph/hedera-wallet-connect'

// sigMaps[0].publicKey also contains the public key of the signer, but you should obtain a PublicKey you can trust from a mirror node.
const verifiedResult = verifySignerSignature(base64String, sigMaps[0], publicKey) // import { verifySignerSignature } from '@hashgraph/hedera-wallet-connect'

Wallet

This library provides a Wallet class that extends the Web3Wallet class provided by WalletConnect class

Event Listeners

WalletConnect emits various events during a session. Listen to these events to synchronize the state of your application:

// Handle pairing proposals
signClient.on('session_proposal', (event) => {
  // Display session proposal to the user and decide to approve or reject
})

// Handle session requests, like signing transactions or messages
signClient.on('session_request', (event) => {
  // Process the session request
})

// Handle session deletions
signClient.on('session_delete', (event) => {
  // React to session termination
})

Pairing with dApps

Pairing establishes a connection between the wallet and a dApp. Once paired, the dApp can send session requests to the wallet.

a. Pairing via URI

If a dApp shares a URI for pairing:

await signClient.core.pairing.pair({ uri: 'RECEIVED_URI' })

Upon successful pairing, the session_proposal event will be triggered.

b. Pairing via QR Codes

For a better user experience, dApps often share QR codes that wallets can scan to establish a pairing. Use a QR code scanning library to scan and obtain the URI, then proceed with pairing:

const scannedUri = '...' // URI obtained from scanning the QR code
await signClient.core.pairing.pair({ uri: scannedUri })

Handling Session Proposals

Upon receiving a session_proposal event, display the proposal details to the user. Allow them to approve or reject the session:

Handling Session Requests

Upon receiving a session_request event, process the request. For instance, if the dApp requests a transaction to be signed:

Extension popup

By default, it is not possible to directly pop up an extension with Wallet Connect. However, to allow this possibility, the dAppConnector look for extensions. If you create the AppConnector, it will automatically send a message to the extension to detect if it is installed. In case the extension is installed, it will be added to the available extensions and its data can be found at the extensions property of dAppConnector.

To connect an available extension, use the method connectExtension(<extensionId>). This will link the extension to the signer and session. Whenever you use the signer created for this session, the extension will automatically open. You can find out if the extension is available by checking the extensions property.

const dAppConnector = new DAppConnector(
  dAppMetadata,
  LedgerId.TESTNET,
  projectId,
  Object.values(HederaJsonRpcMethod),
  [HederaSessionEvent.ChainChanged, HederaSessionEvent.AccountsChanged],
  [HederaChainId.Testnet]
)

[...]

dAppConnector?.extensions?.forEach((extension) => {
  console.log(extension)
})

const extension = dAppConnector?.extensions?.find((extension) => extension.name === '<Extension name>')
if (extension.available) {
  await dAppConnector!.connectExtension(extension.id);
  const signer = dAppConnector.getSigner(AccountId.fromString('0.0.12345'))

  // This request will open the extension
  const response = await signer.signAndExecuteTransaction(transaction)
}

Wallets that are compatible should be able to receive and respond to the following messages:

  • "hedera-extension-query": The extension is required to respond with "hedera-extension-response" and provide the next set of data in the metadata property.
    let metadata = {
      id: '<extesnionId>',
      name: '<Wallet name>',
      url: '<Wallet url>',
      icon: '<Wallet con>',
      description: '<Wallet url>',
    }
  • "hedera-extension-open-<extensionId>": The extension needs to listen to this message and automatically open.
  • "hedera-extension-connect-<extensionId>": The extension must listen to this message and utilize the pairingString property in order to establish a connection.

This communication protocol between the wallet and web dApps requires an intermediate script to use the Chrome API. Refer to the Chrome Extensions documentation

To enable communication between the extension and a web dApp embedded in an iframe, the wallet must support the following messages:

  • "hedera-iframe-query":The extension is required to respond with "hedera-iframe-response" and provide the next set of data in the metadata property.
    let metadata = {
      id: '<Wallet extension id>',
      name: '<Wallet name>',
      url: '<Wallet url>',
      icon: '<Wallet icon>',
      description: '<Wallet description>',
    }
  • "hedera-iframe-connect": The extension must listen to this message and utilize the pairingString property in order to establish a connection.

The dAppConnector is designed to automatically initiate pairing without any need for user action, in case no sessions are noticed and an iframe extension is detected. To capture this event and the newly established session, you can utilize the onSessionIframeCreated function.

Demo & docs

This repository includes a vanilla html/css/javascript implementation with a dApp and wallet example useful for testing and development while integrating WalletConnect and Hedera.

The docs site utilizes Typedoc to generate a library documentation site at https://wc.hgraph.app/docs/

The demo source code lives in ./demos/typescript and is available at https://wc.hgraph.app

Passing tests

  • git commit -Ss "the commit message"