1- class CryptoService {
2- constructor ( key ) {
3- this . key = key ;
4- }
5-
6- static async new ( seedPhrase ) {
7- const encoder = new TextEncoder ( ) ;
8- const data = encoder . encode ( seedPhrase ) ;
9- const hash = await crypto . subtle . digest ( 'SHA-256' , data ) ;
10- return new CryptoService ( new Uint8Array ( hash ) ) ;
11- }
12-
13- async encrypt ( data ) {
14- const iv = crypto . getRandomValues ( new Uint8Array ( 12 ) ) ;
15- const key = await crypto . subtle . importKey (
16- 'raw' ,
17- this . key ,
18- { name : 'AES-GCM' } ,
19- false ,
20- [ 'encrypt' ]
21- ) ;
22-
23- const encoded = new TextEncoder ( ) . encode ( JSON . stringify ( data ) ) ;
24- const encrypted = await crypto . subtle . encrypt (
25- { name : 'AES-GCM' , iv } ,
26- key ,
27- encoded
28- ) ;
29-
30- return {
31- iv : Array . from ( iv ) ,
32- data : Array . from ( new Uint8Array ( encrypted ) )
33- } ;
34- }
35-
36- async decrypt ( encrypted ) {
37- const key = await crypto . subtle . importKey (
38- 'raw' ,
39- this . key ,
40- { name : 'AES-GCM' } ,
41- false ,
42- [ 'decrypt' ]
43- ) ;
44-
45- const decrypted = await crypto . subtle . decrypt (
46- { name : 'AES-GCM' , iv : new Uint8Array ( encrypted . iv ) } ,
47- key ,
48- new Uint8Array ( encrypted . data )
49- ) ;
50-
51- return JSON . parse ( new TextDecoder ( ) . decode ( decrypted ) ) ;
52- }
53- }
1+ import { CryptoService } from './services/cryptoService.js' ;
542
553let cryptoService = null ;
564let webappTabCheckInterval = null ;
575let lastSyncTime = 0 ;
586const SYNC_COOLDOWN = 2000 ;
597
8+ // Initialize the crypto service when the extension starts
9+ async function initializeCrypto ( ) {
10+ try {
11+ // Get seed phrase from storage
12+ const result = await chrome . storage . local . get ( 'seedPhrase' ) ;
13+ if ( result . seedPhrase ) {
14+ cryptoService = await CryptoService . new ( result . seedPhrase ) ;
15+ console . log ( 'Crypto service initialized with PQ support:' , cryptoService . isPQAvailable ( ) ) ;
16+ } else {
17+ console . log ( 'No seed phrase found, crypto not initialized' ) ;
18+ }
19+ } catch ( error ) {
20+ console . error ( 'Failed to initialize crypto:' , error ) ;
21+ }
22+ }
23+
6024async function encryptAndStoreNotes ( notes ) {
6125 try {
6226 if ( ! cryptoService ) {
@@ -237,7 +201,64 @@ function startPeriodicCheck() {
237201
238202startPeriodicCheck ( ) ;
239203
204+ // Handle messages from content scripts or popup
240205chrome . runtime . onMessage . addListener ( ( message , sender , sendResponse ) => {
206+ if ( message . action === 'encrypt' ) {
207+ if ( ! cryptoService ) {
208+ sendResponse ( { error : 'Crypto service not initialized' } ) ;
209+ return ;
210+ }
211+
212+ // Encrypt data asynchronously
213+ ( async ( ) => {
214+ try {
215+ const encrypted = await cryptoService . encrypt ( message . data ) ;
216+ sendResponse ( { success : true , encrypted } ) ;
217+ } catch ( error ) {
218+ sendResponse ( { error : error . message } ) ;
219+ }
220+ } ) ( ) ;
221+
222+ return true ; // Indicate asynchronous response
223+ }
224+
225+ if ( message . action === 'decrypt' ) {
226+ if ( ! cryptoService ) {
227+ sendResponse ( { error : 'Crypto service not initialized' } ) ;
228+ return ;
229+ }
230+
231+ // Decrypt data asynchronously
232+ ( async ( ) => {
233+ try {
234+ const decrypted = await cryptoService . decrypt ( message . encrypted ) ;
235+ sendResponse ( { success : true , decrypted } ) ;
236+ } catch ( error ) {
237+ sendResponse ( { error : error . message } ) ;
238+ }
239+ } ) ( ) ;
240+
241+ return true ; // Indicate asynchronous response
242+ }
243+
244+ if ( message . action === 'setupCrypto' ) {
245+ // Initialize with new seed phrase
246+ ( async ( ) => {
247+ try {
248+ await chrome . storage . local . set ( { seedPhrase : message . seedPhrase } ) ;
249+ cryptoService = await CryptoService . new ( message . seedPhrase ) ;
250+ sendResponse ( {
251+ success : true ,
252+ pqAvailable : cryptoService . isPQAvailable ( )
253+ } ) ;
254+ } catch ( error ) {
255+ sendResponse ( { error : error . message } ) ;
256+ }
257+ } ) ( ) ;
258+
259+ return true ; // Indicate asynchronous response
260+ }
261+
241262 if ( message . type === 'NOTES_UPDATED' && message . notes ) {
242263 lastSyncTime = Date . now ( ) ;
243264 encryptAndStoreNotes ( message . notes ) ;
@@ -257,4 +278,7 @@ chrome.runtime.onConnect.addListener(function(port) {
257278 port . onDisconnect . addListener ( function ( ) {
258279 startPeriodicCheck ( ) ;
259280 } ) ;
260- } ) ;
281+ } ) ;
282+
283+ // Initialize when extension loads
284+ initializeCrypto ( ) ;
0 commit comments