Skip to content

Commit

Permalink
test: manually create wallet client
Browse files Browse the repository at this point in the history
  • Loading branch information
neekolas committed Sep 24, 2023
1 parent 7d0f639 commit 9ef6181
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions src/utils/viem.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { WalletClient } from 'viem'
import { Signer } from '../types/Signer'
import { providers } from 'ethers'

export function getSigner(wallet: Signer | WalletClient | null): Signer | null {
if (!wallet) {
Expand All @@ -9,7 +8,7 @@ export function getSigner(wallet: Signer | WalletClient | null): Signer | null {
if (isWalletClient(wallet)) {
return convertWalletClientToSigner(wallet)
}
if (!(typeof wallet.getAddress !== 'function')) {
if (typeof wallet.getAddress !== 'function') {
throw new Error('Unknown wallet type')
}
return wallet
Expand All @@ -19,23 +18,19 @@ function isWalletClient(wallet: Signer | WalletClient): wallet is WalletClient {
return 'type' in wallet && wallet.type === 'walletClient'
}

// Borrowed from https://wagmi.sh/react/ethers-adapters
export function convertWalletClientToSigner(
walletClient: WalletClient
): Signer {
const { account, chain, transport } = walletClient

if (!account || !chain) {
const { account } = walletClient
if (!account || !account.address) {
throw new Error('WalletClient is not configured')
}

const network = {
chainId: chain.id,
name: chain.name,
ensAddress: chain.contracts?.ensRegistry?.address,
return {
getAddress: async () => account.address,
signMessage: async (message: string) => {
const signature = await walletClient.signMessage({ message, account })
return signature
},
}

const provider = new providers.Web3Provider(transport, network)
const signer = provider.getSigner(account.address)
return signer
}

0 comments on commit 9ef6181

Please sign in to comment.