Skip to content

Commit

Permalink
Merge pull request #215 from xmtp/kele/remove-ethers
Browse files Browse the repository at this point in the history
chore: remove ethers dependency
  • Loading branch information
cameronvoell authored Feb 1, 2024
2 parents 78f6f47 + b2b0256 commit 670f54d
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 471 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@
"license": "MIT",
"homepage": "https://github.com/xmtp/xmtp-react-native#readme",
"dependencies": {
"@ethersproject/bytes": "^5.7.0",
"@msgpack/msgpack": "^3.0.0-beta2",
"@xmtp/proto": "^3.25.0",
"buffer": "^6.0.3",
"ethers": "^5.7.2"
"buffer": "^6.0.3"
},
"devDependencies": {
"@babel/plugin-proposal-export-namespace-from": "^7.18.9",
Expand All @@ -58,7 +58,8 @@
"expo-modules-core": "^1.5.9",
"prettier": "^3.1.0",
"semantic-release": "^21.0.1",
"typedoc": "^0.25.2"
"typedoc": "^0.25.2",
"viem": "^2.4.0"
},
"peerDependencies": {
"expo": "*",
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useClient.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Signer } from 'ethers'
import { useCallback, useRef, useState } from 'react'

import { useXmtp } from './useXmtp'
import { Client, ClientOptions } from '../lib/Client'
import { Signer } from '../lib/Signer'

interface InitializeClientOptions {
signer: Signer
signer: Signer | null
options?: ClientOptions
}

Expand Down
44 changes: 26 additions & 18 deletions src/lib/Client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Signer, utils } from 'ethers'
import { splitSignature } from '@ethersproject/bytes'
import { Subscription } from 'expo-modules-core'
import type { WalletClient } from 'viem'

import Contacts from './Contacts'
import type {
Expand All @@ -11,6 +12,7 @@ import Conversations from './Conversations'
import { DecodedMessage } from './DecodedMessage'
import { TextCodec } from './NativeCodecs/TextCodec'
import { Query } from './Query'
import { Signer, getSigner } from './Signer'
import { hexToBytes } from './util'
import * as XMTPModule from '../index'

Expand All @@ -29,9 +31,8 @@ export class Client<ContentTypes> {
conversations: Conversations<ContentTypes>
contacts: Contacts
codecRegistry: { [key: string]: XMTPModule.ContentCodec<unknown> }
private static signSubscription: Subscription | null = null;
private static authSubscription: Subscription | null = null;

private static signSubscription: Subscription | null = null
private static authSubscription: Subscription | null = null

/**
* Creates a new instance of the Client class using the provided signer.
Expand All @@ -45,7 +46,7 @@ export class Client<ContentTypes> {
static async create<
ContentCodecs extends XMTPModule.ContentCodec<any>[] = [],
>(
signer: Signer,
wallet: Signer | WalletClient | null,
opts?: Partial<ClientOptions> & { codecs?: ContentCodecs }
): Promise<
Client<
Expand All @@ -55,6 +56,10 @@ export class Client<ContentTypes> {
const options = defaultOptions(opts)
const { enableSubscription, createSubscription } =
this.setupSubscriptions(options)
const signer = getSigner(wallet)
if (!signer) {
throw new Error('Signer is not configured')
}
return new Promise<
Client<
ExtractDecodedType<[...ContentCodecs, TextCodec][number]> | undefined
Expand All @@ -67,7 +72,7 @@ export class Client<ContentTypes> {
const request: { id: string; message: string } = message
try {
const signatureString = await signer.signMessage(request.message)
const eSig = utils.splitSignature(signatureString)
const eSig = splitSignature(signatureString)
const r = hexToBytes(eSig.r)
const s = hexToBytes(eSig.s)
const sigBytes = new Uint8Array(65)
Expand All @@ -90,14 +95,17 @@ export class Client<ContentTypes> {
}
)

this.authSubscription = XMTPModule.emitter.addListener('authed', async () => {
this.removeSubscription(enableSubscription)
this.removeSubscription(createSubscription)
this.removeSignSubscription()
this.removeAuthSubscription()
const address = await signer.getAddress()
resolve(new Client(address, opts?.codecs || []))
})
this.authSubscription = XMTPModule.emitter.addListener(
'authed',
async () => {
this.removeSubscription(enableSubscription)
this.removeSubscription(createSubscription)
this.removeSignSubscription()
this.removeAuthSubscription()
const address = await signer.getAddress()
resolve(new Client(address, opts?.codecs || []))
}
)
XMTPModule.auth(
await signer.getAddress(),
options.env,
Expand All @@ -111,15 +119,15 @@ export class Client<ContentTypes> {

private static removeSignSubscription(): void {
if (this.signSubscription) {
this.signSubscription.remove();
this.signSubscription = null;
this.signSubscription.remove()
this.signSubscription = null
}
}

private static removeAuthSubscription(): void {
if (this.authSubscription) {
this.authSubscription.remove();
this.authSubscription = null;
this.authSubscription.remove()
this.authSubscription = null
}
}

Expand Down
41 changes: 41 additions & 0 deletions src/lib/Signer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import type { WalletClient } from 'viem'

export interface Signer {
getAddress: () => Promise<string>
signMessage: (message: string) => Promise<string>
}

export function getSigner(wallet: Signer | WalletClient | null): Signer | null {
if (!wallet) {
return null
}
if (isWalletClient(wallet)) {
return convertWalletClientToSigner(wallet)
}
if (typeof wallet.getAddress !== 'function') {
throw new Error('Unknown wallet type')
}
return wallet
}

function isWalletClient(wallet: Signer | WalletClient): wallet is WalletClient {
return 'type' in wallet && wallet.type === 'walletClient'
}

export function convertWalletClientToSigner(
walletClient: WalletClient
): Signer {
const { account } = walletClient
if (!account || !account.address) {
throw new Error('WalletClient is not configured')
}

return {
getAddress: async () => account.address,
signMessage: async (message: string | Uint8Array) =>
walletClient.signMessage({
message: typeof message === 'string' ? message : { raw: message },
account,
}),
}
}
Loading

0 comments on commit 670f54d

Please sign in to comment.