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

Fix encryption dependency #685

Merged
merged 7 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/sweet-coins-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@xmtp/content-type-remote-attachment": patch
"@xmtp/xmtp-js": patch
---

Fix encryption dependency
74 changes: 0 additions & 74 deletions .github/workflows/encryption.yml

This file was deleted.

2 changes: 0 additions & 2 deletions content-types/content-type-remote-attachment/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,9 @@
"dependencies": {
"@noble/secp256k1": "^1.7.1",
"@xmtp/content-type-primitives": "^1.0.2",
"@xmtp/encryption": "workspace:*",
"@xmtp/proto": "^3.61.1"
},
"devDependencies": {
"@rollup/plugin-node-resolve": "^15.3.0",
"@rollup/plugin-terser": "^0.4.4",
"@rollup/plugin-typescript": "^12.1.1",
"@types/node": "^20.16.11",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { nodeResolve } from "@rollup/plugin-node-resolve";
import terser from "@rollup/plugin-terser";
import typescript from "@rollup/plugin-typescript";
import { resolveExtensions } from "@xmtp/rollup-plugin-resolve-extensions";
Expand All @@ -14,9 +13,6 @@ const plugins = [
filesize({
showMinifiedSize: false,
}),
nodeResolve({
resolveOnly: ["@xmtp/encryption"],
}),
];

const external = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
type ContentCodec,
type EncodedContent,
} from "@xmtp/content-type-primitives";
import { Ciphertext, crypto, decrypt, encrypt } from "@xmtp/encryption";
import { content as proto } from "@xmtp/proto";
import { Ciphertext, crypto, decrypt, encrypt } from "./encryption";

export const ContentTypeRemoteAttachment = new ContentTypeId({
authorityId: "xmtp.org",
Expand Down Expand Up @@ -49,7 +49,7 @@
{
static async load<T>(
remoteAttachment: RemoteAttachment,
codecRegistry: CodecRegistry<T>,

Check warning on line 52 in content-types/content-type-remote-attachment/src/RemoteAttachment.ts

View workflow job for this annotation

GitHub Actions / Lint

`CodecRegistry` is deprecated
): Promise<T> {
const response = await fetch(remoteAttachment.url);
const payload = new Uint8Array(await response.arrayBuffer());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ciphertext } from "@xmtp/proto";
import Ciphertext, { AESGCMNonceSize, KDFSaltSize } from "@/Ciphertext";
import crypto from "@/crypto";
import Ciphertext, { AESGCMNonceSize, KDFSaltSize } from "./Ciphertext";
import crypto from "./crypto";

const hkdfNoInfo = new Uint8Array().buffer;
const hkdfNoSalt = new Uint8Array().buffer;
Expand Down
2 changes: 1 addition & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export default tseslint.config(
{
selector: "ImportDeclaration[source.value=/^(node:)?crypto$/]",
message:
"Do not import directly from `crypto`, use `@xmtp/encryption` instead.",
"Do not import directly from `crypto`, use `@/encryption` instead.",
},
],
},
Expand Down
2 changes: 1 addition & 1 deletion sdks/js-sdk/bench/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { crypto } from "@xmtp/encryption";
import type Benchmark from "benchmark";
import { cycle, save, suite } from "benny";
import type { Config } from "benny/lib/internal/common-types";
import { PrivateKeyBundleV1 } from "@/crypto/PrivateKeyBundle";
import { crypto } from "@/encryption";
import { newWallet } from "@test/helpers";

const MAX_RANDOM_BYTES_SIZE = 65536;
Expand Down
2 changes: 0 additions & 2 deletions sdks/js-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@
"@xmtp/consent-proof-signature": "^0.1.3",
"@xmtp/content-type-primitives": "^1.0.1",
"@xmtp/content-type-text": "^1.0.0",
"@xmtp/encryption": "workspace:*",
"@xmtp/proto": "^3.68.0",
"@xmtp/user-preferences-bindings-wasm": "^0.3.6",
"async-mutex": "^0.5.0",
Expand All @@ -108,7 +107,6 @@
"devDependencies": {
"@metamask/providers": "^17.1.1",
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^15.3.0",
"@rollup/plugin-terser": "^0.4.4",
"@rollup/plugin-typescript": "^12.1.1",
"@types/benchmark": "^2.1.5",
Expand Down
4 changes: 0 additions & 4 deletions sdks/js-sdk/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json from "@rollup/plugin-json";
import { nodeResolve } from "@rollup/plugin-node-resolve";
import terser from "@rollup/plugin-terser";
import typescript from "@rollup/plugin-typescript";
import { resolveExtensions } from "@xmtp/rollup-plugin-resolve-extensions";
Expand Down Expand Up @@ -36,9 +35,6 @@ const plugins = [
json({
preferConst: true,
}),
nodeResolve({
resolveOnly: ["@xmtp/encryption"],
}),
];

export default defineConfig([
Expand Down
2 changes: 1 addition & 1 deletion sdks/js-sdk/src/Invitation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Ciphertext, crypto, decrypt, encrypt } from "@xmtp/encryption";
import { invitation, type messageApi } from "@xmtp/proto";
import Long from "long";
import { Ciphertext, crypto, decrypt, encrypt } from "@/encryption";
import { dateToNs } from "@/utils/date";
import { buildDirectMessageTopicV2 } from "@/utils/topic";
import type { PrivateKeyBundleV2 } from "./crypto/PrivateKeyBundle";
Expand Down
2 changes: 1 addition & 1 deletion sdks/js-sdk/src/Message.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { ContentTypeId } from "@xmtp/content-type-primitives";
import { Ciphertext, sha256 } from "@xmtp/encryption";
import { message as proto, type conversationReference } from "@xmtp/proto";
import Long from "long";
import { PublicKey } from "@/crypto/PublicKey";
import { PublicKeyBundle } from "@/crypto/PublicKeyBundle";
import { Ciphertext, sha256 } from "@/encryption";
import type Client from "./Client";
import {
ConversationV1,
Expand Down
2 changes: 1 addition & 1 deletion sdks/js-sdk/src/PreparedMessage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { sha256 } from "@xmtp/encryption";
import type { Envelope } from "@xmtp/proto/ts/dist/types/message_api/v1/message_api.pb";
import { sha256 } from "@/encryption";
import { bytesToHex } from "./crypto/utils";
import type { DecodedMessage } from "./Message";

Expand Down
2 changes: 1 addition & 1 deletion sdks/js-sdk/src/conversations/Conversation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ContentTypeText } from "@xmtp/content-type-text";
import { sha256 } from "@xmtp/encryption";
import {
message,
content as proto,
Expand All @@ -22,6 +21,7 @@ import {
SignedPublicKeyBundle,
} from "@/crypto/PublicKeyBundle";
import Signature from "@/crypto/Signature";
import { sha256 } from "@/encryption";
import type { InvitationContext } from "@/Invitation";
import { DecodedMessage, MessageV1, MessageV2 } from "@/Message";
import { PreparedMessage } from "@/PreparedMessage";
Expand Down
2 changes: 1 addition & 1 deletion sdks/js-sdk/src/crypto/PrivateKey.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as secp from "@noble/secp256k1";
import { decrypt, encrypt, sha256, type Ciphertext } from "@xmtp/encryption";
import { privateKey } from "@xmtp/proto";
import Long from "long";
import { decrypt, encrypt, sha256, type Ciphertext } from "@/encryption";
import { PublicKey, SignedPublicKey, UnsignedPublicKey } from "./PublicKey";
import Signature, {
ecdsaSignerKey,
Expand Down
2 changes: 1 addition & 1 deletion sdks/js-sdk/src/crypto/PublicKey.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as secp from "@noble/secp256k1";
import { sha256 } from "@xmtp/encryption";
import { publicKey } from "@xmtp/proto";
import Long from "long";
import { hashMessage, hexToBytes, type Hex } from "viem";
import { sha256 } from "@/encryption";
import type { Signer } from "@/types/Signer";
import Signature, { WalletSigner } from "./Signature";
import { computeAddress, equalBytes, splitSignature } from "./utils";
Expand Down
2 changes: 1 addition & 1 deletion sdks/js-sdk/src/crypto/SignedEciesCiphertext.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { sha256 } from "@xmtp/encryption";
import { ciphertext } from "@xmtp/proto";
import { sha256 } from "@/encryption";
import type { PrivateKey, SignedPrivateKey } from "./PrivateKey";
import type { PublicKey, SignedPublicKey } from "./PublicKey";
import Signature from "./Signature";
Expand Down
2 changes: 1 addition & 1 deletion sdks/js-sdk/src/crypto/ecies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* `elliptic` is a CommonJS module and has issues with named imports
* DO NOT CHANGE THIS TO A NAMED IMPORT
*/
import { crypto } from "@xmtp/encryption";
import elliptic from "elliptic";
import { crypto } from "@/encryption";

const EC = elliptic.ec;
const ec = new EC("secp256k1");
Expand Down Expand Up @@ -233,7 +233,7 @@
const ephemeralPublicKey = getPublic(ephemPrivateKey);

const hash = await sha512(await derive(ephemPrivateKey, publicKeyTo));
const encryptionKey = hash.slice(0, 32);

Check warning on line 236 in sdks/js-sdk/src/crypto/ecies.ts

View workflow job for this annotation

GitHub Actions / Lint

`slice` is deprecated. Use `subarray` instead
const macKey = hash.slice(32);
const ciphertext = await aesCbcEncrypt(iv, encryptionKey, msg);

Expand Down
43 changes: 43 additions & 0 deletions sdks/js-sdk/src/encryption/Ciphertext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { ciphertext } from "@xmtp/proto";

export const AESKeySize = 32; // bytes
export const KDFSaltSize = 32; // bytes
// AES-GCM defaults from https://developer.mozilla.org/en-US/docs/Web/API/AesGcmParams
export const AESGCMNonceSize = 12; // property iv
export const AESGCMTagLength = 16; // property tagLength

// Ciphertext packages the encrypted ciphertext with the salt and nonce used to produce it.
// salt and nonce are not secret, and should be transmitted/stored along with the encrypted ciphertext.
export default class Ciphertext implements ciphertext.Ciphertext {
aes256GcmHkdfSha256: ciphertext.Ciphertext_Aes256gcmHkdfsha256 | undefined;

constructor(obj: ciphertext.Ciphertext) {
if (!obj.aes256GcmHkdfSha256) {
throw new Error("invalid ciphertext");
}
if (obj.aes256GcmHkdfSha256.payload.length < AESGCMTagLength) {
throw new Error(
`invalid ciphertext ciphertext length: ${obj.aes256GcmHkdfSha256.payload.length}`,
);
}
if (obj.aes256GcmHkdfSha256.hkdfSalt.length !== KDFSaltSize) {
throw new Error(
`invalid ciphertext salt length: ${obj.aes256GcmHkdfSha256.hkdfSalt.length}`,
);
}
if (obj.aes256GcmHkdfSha256.gcmNonce.length !== AESGCMNonceSize) {
throw new Error(
`invalid ciphertext nonce length: ${obj.aes256GcmHkdfSha256.gcmNonce.length}`,
);
}
this.aes256GcmHkdfSha256 = obj.aes256GcmHkdfSha256;
}

toBytes(): Uint8Array {
return ciphertext.Ciphertext.encode(this).finish();
}

static fromBytes(bytes: Uint8Array): Ciphertext {
return new Ciphertext(ciphertext.Ciphertext.decode(bytes));
}
}
5 changes: 5 additions & 0 deletions sdks/js-sdk/src/encryption/crypto.browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/***********************************************************************************************
* DO NOT IMPORT THIS FILE DIRECTLY
***********************************************************************************************/
const crypto = window.crypto;
export default crypto;
4 changes: 4 additions & 0 deletions sdks/js-sdk/src/encryption/crypto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { webcrypto } from "node:crypto";

const crypto = webcrypto;
export default crypto;
Loading
Loading