Skip to content

chainx-org/chainx-signer-connector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@chainx/signer-connector is used for connecting and interacting with ChainX signer.

Installation

npm install @chainx/signer-connector

Or

yarn add @chainx/signer-connector

How to use

import Connector from '@chainx/signer-connector'

// Create the signer connector instance
const connector = new Connector('dapp')

;(async () => {
  try {
    await connector.link()
  } catch (e) {
    // handle the linking failure case
  }

  // Get current account from signer. If the returned value is not empty, then it have `name` and `address` fields.
  const account = await connector.getCurrentAccount()

  // Get current node from signer. The returned value have `name` and `url` fields.
  const node = await connector.getCurrentNode()

  function accountChangeListener({ from, to }) {
    // `to` is the changed account, you may set this account to your dapp
  }

  function nodeChangeListener({ from, to }) {
    // `to` is the changed node, you may re-init the ChainX instance with this node
  }

  function networkChangeListener({ from, to }) {
    // `to` is the changed network, you may restart your dapp with this network
  }

  connector.listenAccountChange(accountChangeListener)
  connector.listenNodeChange(nodeChangeListener)
  connector.listenNetworkChange(networkChangeListener)

  // Make sure to remove the listener when you don't need them
  connector.removeAccountChangeListener(accountChangeListener)
  connector.removeNodeChangeListener(nodeChangeListener)
  connector.removeNetworkChangeListener(networkChangeListener)
})()

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published