From 78bfcc2bba6a46a77958911076a496ad2281fb46 Mon Sep 17 00:00:00 2001 From: Ry Racherbaumer Date: Fri, 15 Nov 2024 10:46:50 -0600 Subject: [PATCH] Refactor example createClient --- .../src/createClient.ts | 34 ++++++------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/examples/react-vite-browser-sdk/src/createClient.ts b/examples/react-vite-browser-sdk/src/createClient.ts index 261ffd8d8..87a198a6e 100644 --- a/examples/react-vite-browser-sdk/src/createClient.ts +++ b/examples/react-vite-browser-sdk/src/createClient.ts @@ -1,20 +1,7 @@ -import { Client, SignatureRequestType } from "@xmtp/browser-sdk"; +import { Client, type Signer } from "@xmtp/browser-sdk"; import { toBytes } from "viem/utils"; import { createWallet } from "./wallets"; -type Wallet = ReturnType; - -export const getSignature = async (client: Client, wallet: Wallet) => { - const signatureText = await client.getCreateInboxSignatureText(); - if (signatureText) { - const signature = await wallet.signMessage({ - message: signatureText, - }); - return toBytes(signature); - } - return null; -}; - export const createClient = async (walletKey: string) => { const encryptionKeyHex = import.meta.env.VITE_ENCRYPTION_KEY; if (!encryptionKeyHex) { @@ -22,16 +9,17 @@ export const createClient = async (walletKey: string) => { } const encryptionBytes = toBytes(encryptionKeyHex); const wallet = createWallet(walletKey); - const client = await Client.create(wallet.account.address, encryptionBytes, { + const signer: Signer = { + getAddress: () => wallet.account.address, + signMessage: async (message: string) => { + const signature = await wallet.signMessage({ + message, + }); + return toBytes(signature); + }, + }; + const client = await Client.create(signer, encryptionBytes, { env: "local", }); - const isRegistered = await client.isRegistered(); - if (!isRegistered) { - const signature = await getSignature(client, wallet); - if (signature) { - await client.addSignature(SignatureRequestType.CreateInbox, signature); - } - await client.registerIdentity(); - } return client; };