This is an automated email from the ASF dual-hosted git repository.

apratim pushed a commit to branch main
in repository 
https://gitbox.apache.org/repos/asf/incubator-resilientdb-resvault.git


The following commit(s) were added to refs/heads/main by this push:
     new d35f81c  Added handler for SDK transactions
d35f81c is described below

commit d35f81cdb91b0d8ae868ca034e3de4523ff95d96
Author: Apratim Shukla <[email protected]>
AuthorDate: Sun Sep 29 23:33:32 2024 -0700

    Added handler for SDK transactions
---
 public/background.js | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++
 public/content.js    |   2 -
 2 files changed, 111 insertions(+), 2 deletions(-)

diff --git a/public/background.js b/public/background.js
index c0b775f..631d374 100644
--- a/public/background.js
+++ b/public/background.js
@@ -329,6 +329,117 @@ chrome.runtime.onMessage.addListener(function (request, 
sender, sendResponse) {
             });
         })();
 
+        return true; // Keep the message channel open for async sendResponse
+    } else if (request.action === 'submitTransaction') {
+        // Added the missing submitTransaction handler
+
+        (async function() {
+            console.log('Handling submitTransaction action');
+            console.log('Sender:', sender);
+            let senderUrl = null;
+            if (sender.tab && sender.tab.url) {
+                senderUrl = sender.tab.url;
+            } else if (sender.url) {
+                senderUrl = sender.url;
+            } else if (sender.origin) {
+                senderUrl = sender.origin;
+            } else {
+                console.error('Sender URL is undefined');
+                sendResponse({ success: false, error: 'Cannot determine sender 
URL' });
+                return;
+            }
+            console.log('Sender URL:', senderUrl);
+
+            const domain = getBaseDomain(senderUrl);
+            console.log('Domain:', domain);
+
+            chrome.storage.local.get(['keys', 'connectedNets'], async function 
(result) {
+                const keys = result.keys || {};
+                const connectedNets = result.connectedNets || {};
+                console.log('ConnectedNets:', connectedNets);
+                const net = connectedNets[domain];
+                console.log('Net for domain:', domain, 'is', net);
+
+                if (keys[domain] && keys[domain][net]) {
+                    const { publicKey, privateKey, url, exportedKey } = 
keys[domain][net];
+
+                    try {
+                        // Import the key material from JWK format
+                        const keyMaterial = await crypto.subtle.importKey(
+                            'jwk',
+                            exportedKey,
+                            { name: 'AES-GCM' },
+                            true,
+                            ['encrypt', 'decrypt']
+                        );
+
+                        const decryptedPublicKey = await 
decryptData(publicKey.ciphertext, publicKey.iv, keyMaterial);
+                        const decryptedPrivateKey = await 
decryptData(privateKey.ciphertext, privateKey.iv, keyMaterial);
+                        const decryptedUrl = await decryptData(url.ciphertext, 
url.iv, keyMaterial);
+
+                        // Check if required fields are defined
+                        if (!decryptedPublicKey || !decryptedPrivateKey || 
!request.recipient) {
+                            console.error('Missing required fields for 
transaction submission');
+                            sendResponse({ success: false, error: 'Missing 
required fields for transaction' });
+                            return;
+                        }
+
+                        // Prepare asset data as a JSON string
+                        const assetData = JSON.stringify({
+                            data: request.data || {}
+                        });
+
+                        // Construct the GraphQL mutation
+                        const mutation = `
+                            mutation {
+                                postTransaction(data: {
+                                    operation: "CREATE",
+                                    amount: ${parseInt(request.amount)},
+                                    signerPublicKey: 
"${escapeGraphQLString(decryptedPublicKey)}",
+                                    signerPrivateKey: 
"${escapeGraphQLString(decryptedPrivateKey)}",
+                                    recipientPublicKey: 
"${escapeGraphQLString(request.recipient)}",
+                                    asset: """${assetData}"""
+                                }) {
+                                    id
+                                }
+                            }
+                        `;
+
+                        // Log the mutation for debugging
+                        console.log('Mutation:', mutation);
+
+                        const response = await fetch(decryptedUrl, {
+                            method: 'POST',
+                            headers: {
+                                'Content-Type': 'application/json',
+                            },
+                            body: JSON.stringify({ query: mutation }),
+                        });
+
+                        if (!response.ok) {
+                            throw new Error(`Network response was not ok: 
${response.statusText}`);
+                        }
+
+                        const resultData = await response.json();
+                        if (resultData.errors) {
+                            console.error('GraphQL errors:', 
resultData.errors);
+                            sendResponse({ success: false, errors: 
resultData.errors });
+                        } else {
+                            console.log('Transaction submitted successfully:', 
resultData.data);
+                            sendResponse({ success: true, data: 
resultData.data });
+                        }
+                    } catch (error) {
+                        console.error('Error submitting transaction:', error);
+                        sendResponse({ success: false, error: error.message });
+                    }
+                } else {
+                    console.error('No keys found for domain:', domain, 'and 
net:', net);
+                    console.log('Available keys:', keys);
+                    sendResponse({ error: "No keys found for domain and net" 
});
+                }
+            });
+        })();
+
         return true; // Keep the message channel open for async sendResponse
     }
 });
\ No newline at end of file
diff --git a/public/content.js b/public/content.js
index 3e7221d..6345305 100644
--- a/public/content.js
+++ b/public/content.js
@@ -21,8 +21,6 @@ function sendMessageToPage(request) {
   window.postMessage({ type: 'FROM_CONTENT_SCRIPT', data: request }, '*');
 }
 
-// Removed detectFavicon() and its invocation since favicon handling is now in 
background.js
-
 // Add event listener to listen for messages from the web page
 window.addEventListener("message", (event) => {
   if (event.source === window) {

Reply via email to