-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
11 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |