Skip to content

Commit

Permalink
Merge branch 'main' into cv/group-spike
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronvoell committed Feb 1, 2024
2 parents cd4e28d + 670f54d commit 5366aa1
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 456 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
12 changes: 9 additions & 3 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 Down Expand Up @@ -49,7 +51,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 @@ -59,6 +61,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 @@ -71,7 +77,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 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 5366aa1

Please sign in to comment.