Skip to content

Commit

Permalink
Refactor example createClient
Browse files Browse the repository at this point in the history
  • Loading branch information
rygine committed Nov 15, 2024
1 parent d7e39b8 commit 78bfcc2
Showing 1 changed file with 11 additions and 23 deletions.
34 changes: 11 additions & 23 deletions examples/react-vite-browser-sdk/src/createClient.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,25 @@
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<typeof createWallet>;

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) {
throw new Error("VITE_ENCRYPTION_KEY must be set in the environment");
}
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;
};

0 comments on commit 78bfcc2

Please sign in to comment.