To use the ChainPatrol SDK, you’ll need an API key. You can get one by emailing [email protected].

The most common use-case for using the ChainPatrol SDK is to provide scam detection in a browser extension-based wallet (ex. Metamask).

The code snippet below shows how to use the SDK in the background script of a browser extension, demonstrating 4 core concepts:

  • Instantiating the ThreatDetector
  • Listening for navigation events
  • Using the ThreatDetector to check a URL, and redirecting the user if the URL is a scam
  • Handling events from the warning page using the Relay class to forward events
import { ThreatDetector, Relay, Events, Storage } from "@chainpatrol/sdk";

// Instantiate the detector
const detector = new ThreatDetector({
  mode: "cloud",
  apiKey: "YOUR_API_KEY",
  storage: Storage.extension(),
});

// Listen for navigation events
chrome.webNavigation.onBeforeNavigate.addListener(async ({ url, tabId }) => {
  const result = await detector.url(url);
  if (result.ok && result.status === "BLOCKED") {
    // Redirect the user to the warning page
    chrome.tabs.update(tabId, {
      url: result.redirectUrl,
    });
  }
});

// Listen for events from the warning page
Relay.on(Events.ContinueAtOwnRisk, async ({ domain }) => {
  await detector.ignore(domain);
});

These concepts are generally applicable to other environments as well. For example if you’re using the SDK in a messaging web application, instead of listening for navigation events using chrome.webNavigation, you would listen for navigation events for links clicked in the application, or you could instead wait until the user initiates a transaction and check the URL that initiated the transaction.

The next sections of the documentation will go into more detail about the different classes exported by the SDK, and how to use them.