-
Notifications
You must be signed in to change notification settings - Fork 9
/
bundle-polkadot-phishing.js
150 lines (142 loc) · 6.56 KB
/
bundle-polkadot-phishing.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@polkadot/util'), require('@polkadot/util-crypto')) :
typeof define === 'function' && define.amd ? define(['exports', '@polkadot/util', '@polkadot/util-crypto'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.polkadotPhishing = {}, global.polkadotUtil, global.polkadotUtilCrypto));
})(this, (function (exports, util, utilCrypto) { 'use strict';
const global = typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : window;
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
function evaluateThis(fn) {
return fn('return this');
}
const xglobal = (typeof globalThis !== 'undefined'
? globalThis
: typeof global !== 'undefined'
? global
: typeof self !== 'undefined'
? self
: typeof window !== 'undefined'
? window
: evaluateThis(Function));
const fetch = xglobal.fetch;
async function fetchWithTimeout(url, timeout = 2000) {
const controller = new AbortController();
let isAborted = false;
const id = setTimeout(() => {
console.log(`Timeout on ${url}`);
isAborted = true;
controller.abort();
}, timeout);
try {
const response = await fetch(url, { signal: controller.signal });
clearTimeout(id);
return response;
}
catch (error) {
if (!isAborted) {
clearTimeout(id);
}
throw error;
}
}
function fetchJson(url, timeout) {
return fetchWithTimeout(url, timeout).then((r) => r.json());
}
const packageInfo = { name: '@polkadot/phishing', path: (({ url: (typeof document === 'undefined' && typeof location === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : typeof document === 'undefined' ? location.href : (_documentCurrentScript && _documentCurrentScript.src || new URL('bundle-polkadot-phishing.js', document.baseURI).href)) }) && (typeof document === 'undefined' && typeof location === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : typeof document === 'undefined' ? location.href : (_documentCurrentScript && _documentCurrentScript.src || new URL('bundle-polkadot-phishing.js', document.baseURI).href))) ? new URL((typeof document === 'undefined' && typeof location === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : typeof document === 'undefined' ? location.href : (_documentCurrentScript && _documentCurrentScript.src || new URL('bundle-polkadot-phishing.js', document.baseURI).href))).pathname.substring(0, new URL((typeof document === 'undefined' && typeof location === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : typeof document === 'undefined' ? location.href : (_documentCurrentScript && _documentCurrentScript.src || new URL('bundle-polkadot-phishing.js', document.baseURI).href))).pathname.lastIndexOf('/') + 1) : 'auto', type: 'esm', version: '0.22.8' };
const PHISHING = 'https://polkadot.js.org/phishing';
const ADDRESS_JSON = `${PHISHING}/address.json`;
const CACHE_TIMEOUT = 45 * 60 * 1000;
const cacheAddr = {
end: 0,
list: {},
u8a: []
};
const cacheHost = {};
function splitHostParts(host) {
return host
.split('.')
.reverse();
}
function extractHostParts(host) {
return splitHostParts(host
.replace(/https:\/\/|http:\/\/|wss:\/\/|ws:\/\//, '')
.split('/')[0]);
}
async function retrieveAddrCache(allowCached = true) {
const now = Date.now();
if (allowCached && (now < cacheAddr.end)) {
return cacheAddr;
}
const list = await fetchJson(ADDRESS_JSON);
cacheAddr.end = now + CACHE_TIMEOUT;
cacheAddr.list = list;
cacheAddr.u8a = Object.entries(list).map(([key, addresses]) => [key, addresses.map((a) => utilCrypto.decodeAddress(a))]);
return cacheAddr;
}
async function retrieveHostCache(allowCached = true, root = '*') {
const now = Date.now();
if (allowCached && cacheHost[root] && (now < cacheHost[root].end)) {
return cacheHost[root];
}
let list;
try {
list = root === '*'
? await fetchJson(`${PHISHING}/all.json`)
: {
allow: [],
deny: await fetchJson(`${PHISHING}/all/${root}/all.json`)
};
}
catch {
list = { allow: [], deny: [] };
}
cacheHost[root] = {
end: now + CACHE_TIMEOUT,
list,
parts: list.deny.map((h) => splitHostParts(h))
};
return cacheHost[root];
}
function checkHostParts(items, hostParts) {
return items.some((parts) =>
(parts.length <= hostParts.length) &&
parts.every((part, index) => hostParts[index] === part));
}
async function retrieveAddrList(allowCached = true) {
const cache = await retrieveAddrCache(allowCached);
return cache.list;
}
async function retrieveHostList(allowCached = true, root = '*') {
const cache = await retrieveHostCache(allowCached, root);
return cache.list;
}
function checkHost(list, host) {
return checkHostParts(list.map((h) => splitHostParts(h)), extractHostParts(host));
}
async function checkAddress(address, allowCached = true) {
try {
const u8a = utilCrypto.decodeAddress(address);
const cache = await retrieveAddrCache(allowCached);
const entry = cache.u8a.find(([, u8as]) => u8as.some((a) => util.u8aEq(a, u8a)));
return entry?.[0] || null;
}
catch {
return null;
}
}
async function checkIfDenied(host, allowCached = true) {
try {
const hostParts = extractHostParts(host);
const cache = await retrieveHostCache(allowCached, hostParts[0]);
return checkHostParts(cache.parts, hostParts);
}
catch {
return false;
}
}
exports.checkAddress = checkAddress;
exports.checkHost = checkHost;
exports.checkIfDenied = checkIfDenied;
exports.packageInfo = packageInfo;
exports.retrieveAddrList = retrieveAddrList;
exports.retrieveHostList = retrieveHostList;
}));