xylabs/sdk-react

View on GitHub
packages/crypto/src/wallets/third-party/hooks/useSigner.tsx

Summary

Maintainability
A
0 mins
Test Coverage
import { EthAddress } from '@xylabs/eth-address'
import { usePromise } from '@xylabs/react-promise'
import { JsonRpcSigner } from 'ethers'

import { EthWalletConnectorBase } from '../classes'

/** Locate the signer on the passed wallet for a given address */
export const useSigner = (ethWalletConnector: EthWalletConnectorBase, localAddress?: EthAddress): JsonRpcSigner | undefined => {
  const [signer] = usePromise(async () => {
    if (ethWalletConnector.installed) {
      try {
        // In a browser context, we should never build a signer without first having an allowed address
        if (localAddress) {
          return await ethWalletConnector.signerFromAddress(localAddress?.toString())
        }
      } catch (ex) {
        console.error(ex)
      }
    }
  }, [localAddress, ethWalletConnector])

  return signer
}