Skip to content

Commit

Permalink
Refactor signature verification
Browse files Browse the repository at this point in the history
  • Loading branch information
rygine committed Nov 23, 2024
1 parent 29a1da2 commit 534ec58
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 10 deletions.
1 change: 1 addition & 0 deletions sdks/node-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
31 changes: 21 additions & 10 deletions sdks/node-sdk/src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
GroupMessageKind,
LogLevel,
SignatureRequestType,
verifySignedWithPublicKey as verifySignedWithPublicKeyBinding,
type Consent,
type ConsentEntityType,
type LogOptions,
Expand Down Expand Up @@ -410,21 +411,31 @@ export class Client {
signatureText: string,
signatureBytes: Uint8Array,
) {
this.#innerClient.verifySignedWithInstallationKey(
signatureText,
signatureBytes,
);
try {
this.#innerClient.verifySignedWithInstallationKey(
signatureText,
signatureBytes,
);
return true;
} catch {
return false;
}
}

verifySignedWithPublicKey(
static verifySignedWithPublicKey(
signatureText: string,
signatureBytes: Uint8Array,
publicKey: Uint8Array,
) {
this.#innerClient.verifySignedWithPublicKey(
signatureText,
signatureBytes,
publicKey,
);
try {
verifySignedWithPublicKeyBinding(
signatureText,
signatureBytes,
publicKey,
);
return true;
} catch {
return false;
}
}
}
36 changes: 36 additions & 0 deletions sdks/node-sdk/test/Client.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ConsentEntityType, ConsentState } from "@xmtp/node-bindings";
import { uint8ArrayToHex } from "uint8array-extras";
import { v4 } from "uuid";
import { describe, expect, it } from "vitest";
import { Client } from "@/Client";
Expand Down Expand Up @@ -195,4 +196,39 @@ describe("Client", () => {
await client2.getConsentState(ConsentEntityType.GroupId, group2!.id),
).toBe(ConsentState.Denied);
});

it("should verify signatures", async () => {
const user = createUser();
const client = await createRegisteredClient(user);
const signatureText = "gm1";
const signature = client.signWithInstallationKey(signatureText);
const verified = client.verifySignedWithInstallationKey(
signatureText,
signature,
);
expect(verified).toBe(true);
const verified2 = Client.verifySignedWithPublicKey(
signatureText,
signature,
client.installationIdBytes,
);
expect(verified2).toBe(true);

const signatureText2 = new Uint8Array(32).fill(1);
const signature2 = client.signWithInstallationKey(
uint8ArrayToHex(signatureText2),
);
const verified3 = Client.verifySignedWithPublicKey(
uint8ArrayToHex(signatureText2),
signature2,
client.installationIdBytes,
);
expect(verified3).toBe(true);
const verified4 = Client.verifySignedWithPublicKey(
uint8ArrayToHex(signatureText2),
signature,
client.installationIdBytes,
);
expect(verified4).toBe(false);
});
});

0 comments on commit 534ec58

Please sign in to comment.