{"version":3,"sources":["../plugin.schema.json","../src/index.ts","../src/agent/AnomalyDetection.ts"],"sourcesContent":["{\n  \"IAnomalyDetection\": {\n    \"components\": {\n      \"schemas\": {\n        \"AnomalyDetectionLookupLocationArgs\": {\n          \"$ref\": \"#/components/schemas/PartialBy<GeolocationStoreArgs,(\\\"storeId\\\"|\\\"namespace\\\")>\"\n        },\n        \"PartialBy<GeolocationStoreArgs,(\\\"storeId\\\"|\\\"namespace\\\")>\": {\n          \"type\": \"object\",\n          \"additionalProperties\": false,\n          \"properties\": {\n            \"storeId\": {\n              \"type\": \"string\"\n            },\n            \"namespace\": {\n              \"type\": \"string\"\n            },\n            \"ipOrHostname\": {\n              \"type\": \"string\"\n            }\n          },\n          \"required\": [\"ipOrHostname\"]\n        },\n        \"AnomalyDetectionLookupLocationResult\": {\n          \"type\": \"object\",\n          \"properties\": {\n            \"continent\": {\n              \"type\": \"string\"\n            },\n            \"country\": {\n              \"type\": \"string\"\n            }\n          },\n          \"additionalProperties\": false\n        }\n      },\n      \"methods\": {\n        \"anomalyDetectionLookupLocation\": {\n          \"description\": \"\",\n          \"arguments\": {\n            \"$ref\": \"#/components/schemas/AnomalyDetectionLookupLocationArgs\"\n          },\n          \"returnType\": {\n            \"$ref\": \"#/components/schemas/AnomalyDetectionLookupLocationResult\"\n          }\n        }\n      }\n    }\n  }\n}\n","/**\n * @public\n */\nconst schema = require('../plugin.schema.json')\nexport { schema }\nexport { AnomalyDetection, anomalyDetectionMethods } from './agent/AnomalyDetection'\nexport * from './types/IAnomalyDetection'\n","import { contextHasPlugin } from '@sphereon/ssi-sdk.agent-config'\nimport { IAgentPlugin } from '@veramo/core'\nimport { CountryResponse, Reader } from 'mmdb-lib'\nimport { AnomalyDetectionLookupLocationArgs, AnomalyDetectionLookupLocationResult, IAnomalyDetection, IRequiredContext, schema } from '../index'\n\ntype DnsLookupFn = (hostname: string) => Promise<string>\n\nexport const anomalyDetectionMethods: Array<string> = ['lookupLocation']\n\n/**\n * {@inheritDoc IAnomalyDetection}\n */\nexport class AnomalyDetection implements IAgentPlugin {\n  readonly schema = schema.IAnomalyDetection\n  private readonly db: Uint8Array\n  private readonly dnsLookup?: DnsLookupFn\n  readonly methods: IAnomalyDetection = {\n    anomalyDetectionLookupLocation: this.anomalyDetectionLookupLocation.bind(this),\n  }\n\n  constructor(args: { geoIpDB: Uint8Array; dnsLookupCallback?: DnsLookupFn }) {\n    const { geoIpDB, dnsLookupCallback } = { ...args }\n    if (geoIpDB === undefined || geoIpDB === null) {\n      throw new Error('The geoIpDB argument is required')\n    }\n    this.db = geoIpDB\n    this.dnsLookup = dnsLookupCallback\n  }\n\n  private async anomalyDetectionLookupLocation(\n    args: AnomalyDetectionLookupLocationArgs,\n    context: IRequiredContext,\n  ): Promise<AnomalyDetectionLookupLocationResult> {\n    const { ipOrHostname, storeId, namespace } = { ...args }\n    const reader = new Reader<CountryResponse>(Buffer.from(this.db))\n    const ipv4Reg = '(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\\\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'\n    const ipv6Reg =\n      '(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))'\n    let result: CountryResponse | null\n\n    if (!new RegExp(ipv4Reg).test(ipOrHostname) && !new RegExp(ipv6Reg).test(ipOrHostname)) {\n      const ip = await this.resolveDns(ipOrHostname)\n      result = reader.get(ip)\n    } else {\n      result = reader.get(ipOrHostname)\n    }\n\n    const lookupResult = {\n      continent: result?.continent?.code,\n      country: result?.country?.iso_code,\n    }\n\n    if (contextHasPlugin(context, 'geolocationStorePersistLocation'))\n      await context.agent.geolocationStorePersistLocation({\n        namespace,\n        storeId,\n        ipOrHostname,\n        locationArgs: lookupResult,\n      })\n    return Promise.resolve(lookupResult)\n  }\n\n  private async resolveDns(hostname: string): Promise<string> {\n    if (this.dnsLookup) {\n      return this.dnsLookup(hostname)\n    }\n\n    // Fallback to Node dns\n    try {\n      const dns = await import('dns')\n      return new Promise((resolve, reject) => {\n        dns.lookup(hostname, (error: NodeJS.ErrnoException | null, address: string, family: number) => {\n          if (error) {\n            reject(error)\n            return\n          }\n          resolve(address)\n        })\n      })\n    } catch (e) {\n      console.error(e)\n      throw new Error(\n        `DNS resolution not available on this platform, use the dnsLookupCallback in the AnomalyDetection constructor to implement DNS resolution for your platform.\\r\\n${e.message}`,\n      )\n    }\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA,gCAAAA,SAAA;AAAA,IAAAA,QAAA;AAAA,MACE,mBAAqB;AAAA,QACnB,YAAc;AAAA,UACZ,SAAW;AAAA,YACT,oCAAsC;AAAA,cACpC,MAAQ;AAAA,YACV;AAAA,YACA,2DAA+D;AAAA,cAC7D,MAAQ;AAAA,cACR,sB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B,EAAE,CAAA;AAC9D,UAAMkB,UAAU;AAChB,UAAMC,UACJ;AACF,QAAIC;AAEJ,QAAI,CAAC,IAAIC,OAAOH,OAAAA,EAASI,KAAKX,YAAAA,KAAiB,CAAC,IAAIU,OAAOF,OAAAA,EAASG,KAAKX,YAAAA,GAAe;AACtF,YAAMY,KAAK,MAAM,KAAKC,WAAWb,YAAAA;AACjCS,eAASN,OAAOW,IAAIF,EAAAA;IACtB,OAAO;AACLH,eAASN,OAAOW,IAAId,YAAAA;IACtB;AAEA,UAAMe,eAAe;MACnBC,WAAWP,QAAQO,WAAWC;MAC9BC,SAAST,QAAQS,SAASC;IAC5B;AAEA,YAAIC,iCAAiBrB,SAAS,iCAAA,EAC5B,OAAMA,QAAQsB,MAAMC,gCAAgC;MAClDpB;MACAD;MACAD;MACAuB,cAAcR;IAChB,CAAA;AACF,WAAOS,QAAQC,QAAQV,YAAAA;EACzB;EAEA,MAAcF,WAAWa,UAAmC;AAC1D,QAAI,KAAKpC,WAAW;AAClB,aAAO,KAAKA,UAAUoC,QAAAA;IACxB;AAGA,QAAI;AACF,YAAMC,MAAM,MAAM,OAAO,KAAA;AACzB,aAAO,IAAIH,QAAQ,CAACC,SAASG,WAAAA;AAC3BD,YAAIE,OAAOH,UAAU,CAACI,OAAqCC,SAAiBC,WAAAA;AAC1E,cAAIF,OAAO;AACTF,mBAAOE,KAAAA;AACP;UACF;AACAL,kBAAQM,OAAAA;QACV,CAAA;MACF,CAAA;IACF,SAASE,GAAG;AACVC,cAAQJ,MAAMG,CAAAA;AACd,YAAM,IAAInC,MACR;EAAkKmC,EAAEE,OAAO,EAAE;IAEjL;EACF;AACF;;;ADnFA,IAAMC,SAASC;","names":["module","anomalyDetectionMethods","AnomalyDetection","schema","IAnomalyDetection","db","dnsLookup","methods","anomalyDetectionLookupLocation","bind","args","geoIpDB","dnsLookupCallback","undefined","Error","context","ipOrHostname","storeId","namespace","reader","Reader","Buffer","from","ipv4Reg","ipv6Reg","result","RegExp","test","ip","resolveDns","get","lookupResult","continent","code","country","iso_code","contextHasPlugin","agent","geolocationStorePersistLocation","locationArgs","Promise","resolve","hostname","dns","reject","lookup","error","address","family","e","console","message","schema","require"]}