Extension Source Code

Explore the implementation details of our browser extension

Extension Source Code

This is the source code for our browser extension that provides AI-powered phishing protection for Solana users. The extension is built using standard WebExtension APIs and is compatible with Chrome, Firefox, and other Chromium-based browsers.

// Background service worker for Iteration 0001
import { loadModel, initThreatDatabase } from './lib/ai-engine.js';
import { checkDomainAgainstRegistry } from './lib/threat-registry.js';

// Initialize the AI model when the extension is installed
let model = null;

// Load the AI model on startup
async function initialize() {
  console.log('Initializing Iteration 0001 security extension...');
  
  // Load the optimized AI model
  model = await loadModel();
  console.log('AI model loaded successfully');
  
  // Initialize the threat database
  await initThreatDatabase();
  console.log('Threat database initialized');
}

// Initialize on install
chrome.runtime.onInstalled.addListener(() => {
  initialize();
});

// Listen for messages from content scripts
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
  if (message.type === 'ANALYZE_PAGE') {
    analyzePage(message.data, sender.tab.url)
      .then(results => sendResponse(results))
      .catch(error => sendResponse({ error: error.message }));
    return true; // Required for async response
  }
  
  if (message.type === 'CHECK_TRANSACTION') {
    analyzeTransaction(message.data)
      .then(results => sendResponse(results))
      .catch(error => sendResponse(results))
      .catch(error => sendResponse({ error: error.message }));
    return true; // Required for async response
  }
});

// Analyze webpage for phishing attempts
async function analyzePage(pageData, url) {
  try {
    // Check domain against known threat registry
    const domainCheck = await checkDomainAgainstRegistry(new URL(url).hostname);
    
    // If domain is known malicious, return immediately
    if (domainCheck.isThreatListed && domainCheck.confidence > 80) {
      return {
        threatDetected: true,
        confidence: domainCheck.confidence,
        reasons: ['Domain is known to be malicious'],
        threatType: domainCheck.threatType
      };
    }
    
    // Extract features from page data
    const features = extractFeatures(pageData);
    
    // Run AI model inference
    const prediction = await model.predict(features);
    
    // Process prediction results
    return processResults(prediction, domainCheck);
  } catch (error) {
    console.error('Error analyzing page:', error);
    throw error;
  }
}

// Analyze transaction for security risks
async function analyzeTransaction(transactionData) {
  try {
    // Decode transaction
    const decodedTx = decodeTransaction(transactionData);
    
    // Check for risky patterns
    const riskAssessment = assessTransactionRisk(decodedTx);
    
    return riskAssessment;
  } catch (error) {
    console.error('Error analyzing transaction:', error);
    throw error;
  }
}

// Helper functions
function extractFeatures(pageData) {
  // Extract relevant features from page data for AI analysis
  // This includes scripts, DOM structure, etc.
  return {
    scripts: pageData.scripts,
    links: pageData.links,
    forms: pageData.forms,
    domFeatures: extractDOMFeatures(pageData.dom)
  };
}

function processResults(prediction, domainCheck) {
  // Process the AI model prediction and combine with domain check
  const threatScore = prediction.threatScore;
  const detectedPatterns = prediction.detectedPatterns;
  
  return {
    threatDetected: threatScore > 0.7,
    confidence: Math.round(threatScore * 100),
    reasons: detectedPatterns,
    domainThreatLevel: domainCheck.isThreatListed ? 'high' : 'unknown'
  };
}

// Additional helper functions would be implemented here...
function extractDOMFeatures(dom) { /* ... */ }
function decodeTransaction(txData) { /* ... */ }
function assessTransactionRisk(decodedTx) { /* ... */ }

// Initialize the extension
initialize();