@pragma-web-utils/connectors
v0.4.0
Published
core utils
Downloads
6
Readme
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',
}
Properties
chainId
- connected chainIdaccount
- connected addressisActivating
- 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 experienceactiveChainIds
- list of chain supported of connector. Connector disconnect from an unknown chain
Methods
signMessage
- sign provider message. Trow error ifConnector.isConnected
equalsfalse
connect
- try to connect to blockchain provider and listen currentaccount
andchainId
changesdisconnect
- reset all properties and unsubscribe onaccount
andchainId
listenerswitchNetwork
- request provider switch networksetupNetwork
- add network tosupportedNetworks
add chain id to activeChainIdsgetProvider
- return related blockchain provider, or null if not connectedgetChainId
- return current chainIdgetAccount
- return connected accountsubscribe
- subscribe onaccount
andchainId
changes. On detect changes provide callback to fieldnext
, on listen errors provide callback to fielderror
, on listen close connection provide callback to fieldcomplete
(on call methoddisconnect
, or from providerdisconnect
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
andsignMessageV2
. Firstly try to usesignMessageV2
but if catch error or connectedTronWeb
doesn't support it, usesign
method for sign provided text message