@biolimitless/connectors
TypeScript icon, indicating that this package has built-in type declarations

0.2.1 • Public • Published

@biolimitless/connectors

Install

npm i @biolimitless/connectors

Connector - class implemented interface to connect related blockchain provider api. All connectors inherit abstract BaseConnector.

BaseConnector

All types of connectors implement BaseConnector interface and have similar behavior

Interface:

interface BaseConnector<T extends BaseProvider = BaseProvider> {
  readonly chainId: number | undefined
  readonly account: IAddress | undefined
  readonly isActivating: boolean
  readonly isActive: boolean
  readonly isConnected: boolean
  supportedNetworks: NetworkDetails[]
  activeChainIds: number[]

  signMessage(message: string): Promise<string>
  connect(chainId?: number): Promise<ConnectResultEnum>
  disconnect(): Promise<void>
  switchNetwork(chainId?: number): Promise<void>
  setupNetwork(networkDetails: NetworkDetails): Promise<void>
  getProvider(): T
  getChainId(): number | undefined
  getAccount(): IAddress | undefined
  subscribe(listener: Listener): Destructor
}

type BaseProvider = { // blockchain provider
  connected?: boolean
  sendAsync?(payload: JsonRpcPayload, callback: (error: Error | null, result?: JsonRpcResponse) => void): void
  send?(payload: JsonRpcPayload, callback: (error: Error | null, result?: JsonRpcResponse) => void): void
  request?(args: RequestArguments): Promise<unknown>
}

interface IAddress {
  set(address?: string): void
  toString(): string
  toHex(): string
  toBase58(): string
  isEmpty(): boolean
}

interface NetworkDetails {
  rpc: string
  chainId: number
  chainName: string
  nativeCurrency: {
    name: string
    symbol: string
    decimals: number
  }
}
 
type Destructor = () => void

interface Listener {
  next?: (event: ConnectionInfo) => void
  error?: (error: unknown) => void
  complete?: () => void
}

interface ConnectionInfo {
  chainId?: number
  account?: Address
  isConnected: boolean
  isActive: boolean
  isActivating: boolean
  provider: BaseProvider
}

enum ConnectResultEnum {
  SUCCESS = 'SUCCESS',
  FAIL = 'FAIL',
  ALREADY_CONNECTED = 'ALREADY_CONNECTED',
}

Address - see core package

Properties

  • chainId - connected chainId
  • account - connected address
  • isActivating - connecting status (true if connecting in progress)
  • isActive - connecting status (true if connected, and connected chain in the list of activeChainIds)
  • isConnected - connecting status (true if connected to any chain)
  • supportedNetworks - list of networks supported of connector. On switch chain, connector asks to setup network from provider if network is not configured yet. Array can be empty, but if provided correctly increase user experience
  • activeChainIds - list of chain supported of connector. Connector disconnect from an unknown chain

Methods

  • signMessage - sign provider message. Trow error if Connector.isConnected equals false
  • connect - try to connect to blockchain provider and listen current account and chainId changes
  • disconnect - reset all properties and unsubscribe on account and chainId listener
  • switchNetwork - request provider switch network
  • setupNetwork - add network to supportedNetworks add chain id to activeChainIds
  • getProvider - return related blockchain provider, or null if not connected
  • getChainId - return current chainId
  • getAccount - return connected account
  • subscribe - subscribe on account and chainId changes. On detect changes provide callback to field next, on listen errors provide callback to field error, on listen close connection provide callback to field complete (on call method disconnect, or from provider disconnect event)

Listen changes example

const someConnector: BaseConnector = new SomeConnector()

someConnector.subscribe({
  next: (data) => console.log('next', data),
  error: (error) => console.warn('error', error),
  complete: () => console.log('complete')
})

someConnector.connect()

// 'next' {  chainId: undefined, account: undefined, isConnected: false, isActive: false, isActivating: true, provider: null }
// 'next' {  chainId: 123, account: { _hex: 0x123..789 }, isConnected: true, isActive: true, isActivating: false, provider: {} }

someConnector.switchNetwork(321)

// 'next' {  chainId: 321, account: { _hex: 0x123..789 }, isConnected: true, isActive: true, isActivating: false, provider: {} }

someConnector.disconnect()

// 'next' {  chainId: undefined, account: undefined, isConnected: false, isActive: false, isActivating: false, provider: null }
// 'complete'

InjectedConnector, MetamaskConnector, CoinbaseConnector, and WalletConnectConnector

These connectors are for ethereum provider integration. InjectedConnector is the most common connector for connecting any type of extension or native app provider. MetamaskConnector, CoinbaseConnector, and WalletConnectConnector connectors try to connect related provider if it possible.

ⓘ Info

For ethereum connectors using personal_sign method for sign provided text message

TronConnector

This connector only for tron related provider

ⓘ Info

Tron has two methods for sign message sign and signMessageV2. Firstly try to use signMessageV2 but if catch error or connected TronWeb doesn't support it, use sign method for sign provided text message

Readme

Keywords

Package Sidebar

Install

npm i @biolimitless/connectors

Weekly Downloads

0

Version

0.2.1

License

MIT

Unpacked Size

115 kB

Total Files

73

Last publish

Collaborators

  • jumojet5
  • martushka