-
Notifications
You must be signed in to change notification settings - Fork 14
fix: use Connect encryption key to encrypt app identities #275
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
427d174
fix: inboxes and app keys
pcarranzav 11e2fac
fix: use the app identity keys everywhere
pcarranzav c090340
fix: update pnpm lock
pcarranzav bd75bae
fix: use Connect encryption key to encrypt app identities
pcarranzav 4ad4015
Merge branch 'main' into pcv/change-app-identity-encryption
nikgraf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
29 changes: 29 additions & 0 deletions
29
apps/server/prisma/migrations/20250627185421_remove_app_identity_nonce/migration.sql
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /* | ||
| Warnings: | ||
|
|
||
| - You are about to drop the column `nonce` on the `AppIdentity` table. All the data in the column will be lost. | ||
|
|
||
| */ | ||
| -- RedefineTables | ||
| PRAGMA defer_foreign_keys=ON; | ||
| PRAGMA foreign_keys=OFF; | ||
| CREATE TABLE "new_AppIdentity" ( | ||
| "address" TEXT NOT NULL PRIMARY KEY, | ||
| "ciphertext" TEXT NOT NULL, | ||
| "signaturePublicKey" TEXT NOT NULL, | ||
| "encryptionPublicKey" TEXT NOT NULL, | ||
| "accountProof" TEXT NOT NULL, | ||
| "keyProof" TEXT NOT NULL, | ||
| "accountAddress" TEXT NOT NULL, | ||
| "appId" TEXT NOT NULL, | ||
| "sessionToken" TEXT NOT NULL, | ||
| "sessionTokenExpires" DATETIME NOT NULL, | ||
| CONSTRAINT "AppIdentity_accountAddress_fkey" FOREIGN KEY ("accountAddress") REFERENCES "Account" ("address") ON DELETE RESTRICT ON UPDATE CASCADE | ||
| ); | ||
| INSERT INTO "new_AppIdentity" ("accountAddress", "accountProof", "address", "appId", "ciphertext", "encryptionPublicKey", "keyProof", "sessionToken", "sessionTokenExpires", "signaturePublicKey") SELECT "accountAddress", "accountProof", "address", "appId", "ciphertext", "encryptionPublicKey", "keyProof", "sessionToken", "sessionTokenExpires", "signaturePublicKey" FROM "AppIdentity"; | ||
| DROP TABLE "AppIdentity"; | ||
| ALTER TABLE "new_AppIdentity" RENAME TO "AppIdentity"; | ||
| CREATE INDEX "AppIdentity_sessionToken_idx" ON "AppIdentity"("sessionToken"); | ||
| CREATE UNIQUE INDEX "AppIdentity_accountAddress_appId_key" ON "AppIdentity"("accountAddress", "appId"); | ||
| PRAGMA foreign_keys=ON; | ||
| PRAGMA defer_foreign_keys=OFF; |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,9 +5,15 @@ import { sha256 } from '@noble/hashes/sha256'; | |||||||||||||||||||||||||||||||
| import type { Hex } from 'viem'; | ||||||||||||||||||||||||||||||||
| import { verifyMessage } from 'viem'; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| import { cryptoBoxSeal, cryptoBoxSealOpen } from '@serenity-kit/noble-sodium'; | ||||||||||||||||||||||||||||||||
| import { bytesToHex, canonicalize, hexToBytes } from '../utils/index.js'; | ||||||||||||||||||||||||||||||||
| import type { IdentityKeys, PrivateAppIdentity, Signer } from './types.js'; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| export type AppIdentityForEncryption = Omit< | ||||||||||||||||||||||||||||||||
| PrivateAppIdentity, | ||||||||||||||||||||||||||||||||
| 'sessionToken' | 'sessionTokenExpires' | 'accountAddress' | ||||||||||||||||||||||||||||||||
| >; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // Adapted from the XMTP approach to encrypt keys | ||||||||||||||||||||||||||||||||
| // See: https://github.com/xmtp/xmtp-js/blob/8d6e5a65813902926baac8150a648587acbaad92/sdks/js-sdk/src/keystore/providers/NetworkKeyManager.ts#L79-L116 | ||||||||||||||||||||||||||||||||
| // (We reimplement their encrypt/decrypt functions using noble). | ||||||||||||||||||||||||||||||||
|
|
@@ -134,95 +140,54 @@ export const decryptIdentity = async (signer: Signer, ciphertext: string, nonce: | |||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| export const encryptAppIdentity = async ( | ||||||||||||||||||||||||||||||||
| signer: Signer, | ||||||||||||||||||||||||||||||||
| appIdentityAddress: string, | ||||||||||||||||||||||||||||||||
| appIdentityAddressPrivateKey: string, | ||||||||||||||||||||||||||||||||
| permissionId: string, | ||||||||||||||||||||||||||||||||
| appIdentity: AppIdentityForEncryption, | ||||||||||||||||||||||||||||||||
| keys: IdentityKeys, | ||||||||||||||||||||||||||||||||
| ): Promise<{ ciphertext: string; nonce: string }> => { | ||||||||||||||||||||||||||||||||
| const nonce = randomBytes(32); | ||||||||||||||||||||||||||||||||
| const message = signatureMessage(nonce); | ||||||||||||||||||||||||||||||||
| const signature = (await signer.signMessage(message)) as Hex; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // Check that the signature is valid | ||||||||||||||||||||||||||||||||
| const valid = await verifyMessage({ | ||||||||||||||||||||||||||||||||
| address: (await signer.getAddress()) as Hex, | ||||||||||||||||||||||||||||||||
| message, | ||||||||||||||||||||||||||||||||
| signature, | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
| if (!valid) { | ||||||||||||||||||||||||||||||||
| throw new Error('Invalid signature'); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| const secretKey = hexToBytes(signature); | ||||||||||||||||||||||||||||||||
| ): Promise<{ ciphertext: string }> => { | ||||||||||||||||||||||||||||||||
| // We use a simple plaintext encoding: | ||||||||||||||||||||||||||||||||
| // Hex keys separated by newlines | ||||||||||||||||||||||||||||||||
| const keysTxt = [ | ||||||||||||||||||||||||||||||||
| keys.encryptionPublicKey, | ||||||||||||||||||||||||||||||||
| keys.encryptionPrivateKey, | ||||||||||||||||||||||||||||||||
| keys.signaturePublicKey, | ||||||||||||||||||||||||||||||||
| keys.signaturePrivateKey, | ||||||||||||||||||||||||||||||||
| appIdentityAddress, | ||||||||||||||||||||||||||||||||
| appIdentityAddressPrivateKey, | ||||||||||||||||||||||||||||||||
| permissionId, | ||||||||||||||||||||||||||||||||
| appIdentity.encryptionPublicKey, | ||||||||||||||||||||||||||||||||
| appIdentity.encryptionPrivateKey, | ||||||||||||||||||||||||||||||||
| appIdentity.signaturePublicKey, | ||||||||||||||||||||||||||||||||
| appIdentity.signaturePrivateKey, | ||||||||||||||||||||||||||||||||
| appIdentity.address, | ||||||||||||||||||||||||||||||||
| appIdentity.addressPrivateKey, | ||||||||||||||||||||||||||||||||
| appIdentity.permissionId, | ||||||||||||||||||||||||||||||||
| ].join('\n'); | ||||||||||||||||||||||||||||||||
| const keysMsg = new TextEncoder().encode(keysTxt); | ||||||||||||||||||||||||||||||||
| const ciphertext = encrypt(keysMsg, secretKey); | ||||||||||||||||||||||||||||||||
| return { ciphertext, nonce: bytesToHex(nonce) }; | ||||||||||||||||||||||||||||||||
| const ciphertext = bytesToHex( | ||||||||||||||||||||||||||||||||
| cryptoBoxSeal({ | ||||||||||||||||||||||||||||||||
| message: keysMsg, | ||||||||||||||||||||||||||||||||
| publicKey: hexToBytes(keys.encryptionPublicKey), | ||||||||||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||
| return { ciphertext }; | ||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| export const decryptAppIdentity = async ( | ||||||||||||||||||||||||||||||||
| signer: Signer, | ||||||||||||||||||||||||||||||||
| ciphertext: string, | ||||||||||||||||||||||||||||||||
| nonce: string, | ||||||||||||||||||||||||||||||||
| ): Promise<Omit<PrivateAppIdentity, 'sessionToken' | 'sessionTokenExpires' | 'accountAddress'>> => { | ||||||||||||||||||||||||||||||||
| const message = signatureMessage(hexToBytes(nonce)); | ||||||||||||||||||||||||||||||||
| const signature = (await signer.signMessage(message)) as Hex; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // Check that the signature is valid | ||||||||||||||||||||||||||||||||
| const valid = await verifyMessage({ | ||||||||||||||||||||||||||||||||
| address: (await signer.getAddress()) as Hex, | ||||||||||||||||||||||||||||||||
| message, | ||||||||||||||||||||||||||||||||
| signature, | ||||||||||||||||||||||||||||||||
| export const decryptAppIdentity = async (ciphertext: string, keys: IdentityKeys): Promise<AppIdentityForEncryption> => { | ||||||||||||||||||||||||||||||||
| const ciphertextBytes = hexToBytes(ciphertext); | ||||||||||||||||||||||||||||||||
| const keysMsg = cryptoBoxSealOpen({ | ||||||||||||||||||||||||||||||||
| ciphertext: ciphertextBytes, | ||||||||||||||||||||||||||||||||
| privateKey: hexToBytes(keys.encryptionPrivateKey), | ||||||||||||||||||||||||||||||||
| publicKey: hexToBytes(keys.encryptionPublicKey), | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
Comment on lines
+169
to
173
|
||||||||||||||||||||||||||||||||
| const keysMsg = cryptoBoxSealOpen({ | |
| ciphertext: ciphertextBytes, | |
| privateKey: hexToBytes(keys.encryptionPrivateKey), | |
| publicKey: hexToBytes(keys.encryptionPublicKey), | |
| }); | |
| let keysMsg; | |
| try { | |
| keysMsg = cryptoBoxSealOpen({ | |
| ciphertext: ciphertextBytes, | |
| privateKey: hexToBytes(keys.encryptionPrivateKey), | |
| publicKey: hexToBytes(keys.encryptionPublicKey), | |
| }); | |
| } catch (error) { | |
| throw new Error("Failed to decrypt app identity. Please check the provided keys and ciphertext."); | |
| } |
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function should handle potential decryption failures and provide a meaningful error message. The cryptoBoxSealOpen function can throw if decryption fails, but there's no error handling or context about what went wrong.