Skip to content

Commit

Permalink
feat: change isXmtpEnabled to check any address given an initialized …
Browse files Browse the repository at this point in the history
…random client (#30)

* feat: change isXmtpEnabled to check any address given an initialized random client
  • Loading branch information
albertfolch-redeemeum authored Aug 18, 2022
1 parent 6365e71 commit 463663d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
14 changes: 10 additions & 4 deletions src/xmtp/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Signer } from "ethers";
import { Signer, Wallet } from "ethers";
import {
Client,
Conversation,
Expand Down Expand Up @@ -53,10 +53,16 @@ export class XmtpClient {
* Check if input corresponds to a known
* XMTP key bundle (i.e. exists already)
* @param address - wallet address
* @param envName - environment name (e.g. "production", "test", etc)
* @returns boolean
*/
public async isXmtpEnabled(address: string): Promise<boolean> {
return await this.client.canMessage(address);
public static async isXmtpEnabled(
address: string,
envName: string
): Promise<boolean> {
const wallet: Wallet = Wallet.createRandom();
const bosonXmtp = await XmtpClient.initialise(wallet, envName);
return await bosonXmtp.client.canMessage(address);
}

/**
Expand Down Expand Up @@ -93,7 +99,7 @@ export class XmtpClient {
* @returns Conversation - {@link Conversation}
*/
public async startConversation(counterparty: string): Promise<Conversation> {
if (!(await this.isXmtpEnabled(counterparty))) {
if (!(await XmtpClient.isXmtpEnabled(counterparty, this.envName))) {
throw new Error(`${counterparty} has not initialised their XMTP client`);
}
return await this.client.conversations.newConversation(counterparty);
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/xmtp/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ describe("xmtp-client", () => {

test("XmtpClient isXmtpEnabled(): Expect true", async () => {
const address: string = walletAddress;
const isEnabled: boolean = await xmtpClient.isXmtpEnabled(address);
const isEnabled: boolean = await XmtpClient.isXmtpEnabled(address, envName);
expect(isEnabled).toBe(true);
});

test("XmtpClient isXmtpEnabled(): Expect false", async () => {
const address: string = nullAddress();
const isEnabled: boolean = await xmtpClient.isXmtpEnabled(address);
const isEnabled: boolean = await XmtpClient.isXmtpEnabled(address, envName);
expect(isEnabled).toBe(false);
});

Expand Down

0 comments on commit 463663d

Please sign in to comment.