Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDK updates #732

Merged
merged 14 commits into from
Nov 23, 2024
4 changes: 2 additions & 2 deletions sdks/browser-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@
"@xmtp/content-type-group-updated": "^1.0.1",
"@xmtp/content-type-primitives": "^1.0.3",
"@xmtp/content-type-text": "^1.0.1",
"@xmtp/proto": "^3.72.0",
"@xmtp/wasm-bindings": "^0.0.4",
"@xmtp/proto": "^3.72.3",
"@xmtp/wasm-bindings": "^0.0.7",
"uuid": "^11.0.3"
},
"devDependencies": {
Expand Down
38 changes: 32 additions & 6 deletions sdks/browser-sdk/src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class Client extends ClientWorkerClass {
#encryptionKey: Uint8Array;
#inboxId: string | undefined;
#installationId: string | undefined;
#installationIdBytes: Uint8Array | undefined;
#isReady = false;
#signer: Signer;
options?: ClientOptions;
Expand Down Expand Up @@ -74,6 +75,7 @@ export class Client extends ClientWorkerClass {
});
this.#inboxId = result.inboxId;
this.#installationId = result.installationId;
this.#installationIdBytes = result.installationIdBytes;
this.#isReady = true;
}

Expand Down Expand Up @@ -106,6 +108,10 @@ export class Client extends ClientWorkerClass {
return this.#installationId;
}

get installationIdBytes() {
return this.#installationIdBytes;
}

async #createInboxSignatureText() {
return this.sendMessage("createInboxSignatureText", undefined);
}
Expand Down Expand Up @@ -176,12 +182,6 @@ export class Client extends ClientWorkerClass {
throw new Error("Unable to generate add account signature text");
}

await this.#addSignature(
SignatureRequestType.AddWallet,
signatureText,
this.#signer,
);

await this.#addSignature(
SignatureRequestType.AddWallet,
signatureText,
Expand Down Expand Up @@ -314,4 +314,30 @@ export class Client extends ClientWorkerClass {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return codec.decode(encodedContent, this);
}

signWithInstallationKey(signatureText: string) {
return this.sendMessage("signWithInstallationKey", { signatureText });
}

verifySignedWithInstallationKey(
signatureText: string,
signatureBytes: Uint8Array,
) {
return this.sendMessage("verifySignedWithInstallationKey", {
signatureText,
signatureBytes,
});
}

verifySignedWithPublicKey(
signatureText: string,
signatureBytes: Uint8Array,
publicKey: Uint8Array,
) {
return this.sendMessage("verifySignedWithPublicKey", {
signatureText,
signatureBytes,
publicKey,
});
}
rygine marked this conversation as resolved.
Show resolved Hide resolved
}
4 changes: 4 additions & 0 deletions sdks/browser-sdk/src/Conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export class Conversations {
return this.#client.sendMessage("syncConversations", undefined);
}

async syncAll() {
return this.#client.sendMessage("syncAllConversations", undefined);
}

async getConversationById(id: string) {
return this.#client.sendMessage("getConversationById", {
id,
Expand Down
42 changes: 38 additions & 4 deletions sdks/browser-sdk/src/WorkerClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
verifySignedWithPublicKey,
type Client,
type ConsentEntityType,
type SignatureRequestType,
Expand Down Expand Up @@ -42,6 +43,10 @@ export class WorkerClient {
return this.#client.installationId;
}

get installationIdBytes() {
return this.#client.installationIdBytes;
}

get isRegistered() {
return this.#client.isRegistered;
}
Expand All @@ -56,10 +61,7 @@ export class WorkerClient {

async addAccountSignatureText(accountAddress: string) {
try {
return await this.#client.addWalletSignatureText(
this.#accountAddress,
accountAddress,
);
return await this.#client.addWalletSignatureText(accountAddress);
} catch {
return undefined;
}
Expand Down Expand Up @@ -131,4 +133,36 @@ export class WorkerClient {
get conversations() {
return this.#conversations;
}

signWithInstallationKey(signatureText: string) {
return this.#client.signWithInstallationKey(signatureText);
}

verifySignedWithInstallationKey(
signatureText: string,
signatureBytes: Uint8Array,
) {
try {
this.#client.verifySignedWithInstallationKey(
signatureText,
signatureBytes,
);
return true;
} catch {
return false;
}
}

verifySignedWithPublicKey(
signatureText: string,
signatureBytes: Uint8Array,
publicKey: Uint8Array,
) {
try {
verifySignedWithPublicKey(signatureText, signatureBytes, publicKey);
return true;
} catch {
return false;
}
}
rygine marked this conversation as resolved.
Show resolved Hide resolved
}
4 changes: 4 additions & 0 deletions sdks/browser-sdk/src/WorkerConversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export class WorkerConversations {
return this.#conversations.sync();
}

async syncAll() {
return this.#conversations.syncAllConversations();
}
rygine marked this conversation as resolved.
Show resolved Hide resolved

getConversationById(id: string) {
try {
const group = this.#conversations.findGroupById(id);
Expand Down
34 changes: 34 additions & 0 deletions sdks/browser-sdk/src/types/clientEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export type ClientEvents =
result: {
inboxId: string;
installationId: string;
installationIdBytes: Uint8Array;
};
data: {
address: string;
Expand Down Expand Up @@ -157,6 +158,33 @@ export type ClientEvents =
address: string;
};
}
| {
action: "signWithInstallationKey";
id: string;
result: Uint8Array;
data: {
signatureText: string;
};
}
| {
action: "verifySignedWithInstallationKey";
id: string;
result: boolean;
data: {
signatureText: string;
signatureBytes: Uint8Array;
};
}
| {
action: "verifySignedWithPublicKey";
id: string;
result: boolean;
data: {
signatureText: string;
signatureBytes: Uint8Array;
publicKey: Uint8Array;
};
}
/**
* Conversations actions
*/
Expand Down Expand Up @@ -231,6 +259,12 @@ export type ClientEvents =
result: undefined;
data: undefined;
}
| {
action: "syncAllConversations";
id: string;
result: undefined;
data: undefined;
}
/**
* Group actions
*/
Expand Down
44 changes: 44 additions & 0 deletions sdks/browser-sdk/src/workers/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
result: {
inboxId: client.inboxId,
installationId: client.installationId,
installationIdBytes: client.installationIdBytes,
},
});
break;
Expand Down Expand Up @@ -216,6 +217,40 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
});
break;
}
case "signWithInstallationKey": {
const result = client.signWithInstallationKey(data.signatureText);
postMessage({
id,
action,
result,
});
break;
}
case "verifySignedWithInstallationKey": {
const result = client.verifySignedWithInstallationKey(
data.signatureText,
data.signatureBytes,
);
postMessage({
id,
action,
result,
});
break;
}
case "verifySignedWithPublicKey": {
const result = client.verifySignedWithPublicKey(
data.signatureText,
data.signatureBytes,
data.publicKey,
);
postMessage({
id,
action,
result,
});
break;
}
/**
* Conversations actions
*/
Expand Down Expand Up @@ -286,6 +321,15 @@ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
});
break;
}
case "syncAllConversations": {
await client.conversations.syncAll();
postMessage({
id,
action,
result: undefined,
});
break;
}
case "getConversationById": {
const conversation = client.conversations.getConversationById(data.id);
postMessage({
Expand Down
2 changes: 1 addition & 1 deletion sdks/browser-sdk/test/Conversations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe.concurrent("Conversations", () => {
updateGroupPinnedFrameUrlPolicy: 0,
});
expect(group.addedByInboxId).toBe(client1.inboxId);
expect((await group.messages()).length).toBe(1);
expect((await group.messages()).length).toBe(0);
const members = await group.members();
expect(members.length).toBe(2);
const memberInboxIds = members.map((member) => member.inboxId);
Expand Down
5 changes: 3 additions & 2 deletions sdks/node-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@
"@xmtp/content-type-group-updated": "^1.0.1",
"@xmtp/content-type-primitives": "^1.0.3",
"@xmtp/content-type-text": "^1.0.1",
"@xmtp/node-bindings": "^0.0.20",
"@xmtp/proto": "^3.72.0"
"@xmtp/node-bindings": "^0.0.22",
"@xmtp/proto": "^3.72.3"
},
"devDependencies": {
"@rollup/plugin-json": "^6.1.0",
Expand All @@ -69,6 +69,7 @@
"rollup-plugin-filesize": "^10.0.0",
"rollup-plugin-tsconfig-paths": "^1.5.2",
"typescript": "^5.6.3",
"uint8array-extras": "^1.4.0",
"uuid": "^11.0.3",
"viem": "^2.13.6",
"vite": "^5.4.11",
Expand Down
Loading
Loading