-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
286 lines (260 loc) · 8.74 KB
/
Copy pathindex.js
File metadata and controls
286 lines (260 loc) · 8.74 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/**
* agentshield-js
* Official SDK for the AgentShield security protocol
* https://agentshield.win
*/
const DEFAULT_BASE_URL = "https://agentshield.win";
class AgentShield {
constructor(options = {}) {
this.baseUrl = options.baseUrl || DEFAULT_BASE_URL;
this.apiKey = options.apiKey || null;
}
async _request(path, options = {}) {
const headers = {
"Content-Type": "application/json",
"Accept": "application/json",
...options.headers,
};
if (this.apiKey) headers["X-API-Key"] = this.apiKey;
const res = await fetch(`${this.baseUrl}${path}`, { ...options, headers });
if (!res.ok) throw new Error(`AgentShield error ${res.status}: ${await res.text()}`);
return res.json();
}
/**
* Verify a smart contract before interacting.
* @param {string} contract - Contract address (0x...)
* @param {string|number} chain - Chain ID (default: 1 = Ethereum)
* @param {boolean} force - Skip cache and force fresh verification
* @returns {Promise<Object>}
*/
async verify(contract, chain = 1, force = false) {
const params = new URLSearchParams({ contract, chain: String(chain) });
if (force) params.set("force", "true");
return this._request(`/verify?${params}`);
}
/**
* Deep forensic analysis: ownership, permissions, exploit patterns, bytecode, risk breakdown.
* @param {string} contract - Contract address (0x...)
* @param {string|number} chain - Chain ID (default: 1 = Ethereum)
* @returns {Promise<Object>}
*/
async deepScan(contract, chain = 1) {
const params = new URLSearchParams({ contract, chain: String(chain) });
return this._request(`/deep-scan?${params}`);
}
/**
* Full threat detection scan: honeypots, rug pulls, mint/freeze authority, proxy backdoors,
* tax manipulation, blacklist functions, liquidity analysis, holder concentration. 14+ checks.
* @param {string} contract - Token/contract address
* @param {string} chain - Chain name: solana, ethereum, base, bsc, polygon, arbitrum, optimism
* @returns {Promise<Object>}
*/
async scan(contract, chain = "solana") {
const params = new URLSearchParams({ contract, chain });
return this._request(`/scan?${params}`);
}
/**
* Monitor a wallet for drain threats.
* @param {Object} opts
* @param {string} opts.wallet - Wallet address to monitor (0x...)
* @param {string} opts.callback_url - HTTPS URL to POST alerts to
* @param {number} [opts.threshold_pct=20] - Balance drop % to trigger alert
* @param {string} [opts.agent_id] - Agent identifier for grouping
* @param {string[]} [opts.chains=['ethereum','base']] - Chains to monitor
* @returns {Promise<Object>}
*/
async monitor({ wallet, callback_url, threshold_pct = 20, agent_id, chains = ["ethereum", "base"] }) {
return this._request("/monitor", {
method: "POST",
body: JSON.stringify({ wallet, callback_url, threshold_pct, agent_id, chains }),
});
}
/**
* Emergency freeze all wallets for a wallet address or agent.
* @param {Object} opts
* @param {string} [opts.wallet] - Wallet address to freeze
* @param {string} [opts.agent_id] - Agent ID to freeze all wallets for
* @param {string} [opts.reason] - Reason for freeze
* @returns {Promise<Object>}
*/
async freeze({ wallet, agent_id, reason = "Threat detected" } = {}) {
const headers = {};
if (this.apiKey) headers["X-Agent-Key"] = this.apiKey;
return this._request("/freeze", {
method: "POST",
headers,
body: JSON.stringify({ wallet, agent_id, reason }),
});
}
/**
* Get recent alerts for a wallet.
* @param {string} wallet - Wallet address
* @param {number} [limit=20] - Max alerts to return (max 100)
* @returns {Promise<Object>}
*/
async alerts(wallet, limit = 20) {
const params = new URLSearchParams({ wallet, limit: String(limit) });
return this._request(`/alerts?${params}`);
}
/**
* Get live protocol statistics.
* @returns {Promise<Object>}
*/
async stats() {
return this._request("/stats");
}
/**
* Safe interaction helper — verify before acting.
* Returns result if safe, throws if dangerous.
* @param {string} contract
* @param {string|number} chain
*/
async safeInteract(contract, chain = 1) {
const result = await this.verify(contract, chain);
if (result.risk_level === "CRITICAL" || result.is_honeypot) {
throw new Error(`[AgentShield] BLOCKED: ${result.message} — ${result.recommendation}`);
}
if (result.risk_level === "HIGH") {
console.warn(`[AgentShield] WARNING: ${result.message}`);
}
return result;
}
/**
* Safe scan helper — scan before trading any token.
* Returns result if safe, throws if dangerous.
* @param {string} contract
* @param {string} chain
*/
async safeScan(contract, chain = "solana") {
const result = await this.scan(contract, chain);
if (result.verdict === "DANGEROUS" || result.is_honeypot) {
throw new Error(`[AgentShield] BLOCKED: ${result.threat_level} — ${JSON.stringify(result.risks || [])}`);
}
return result;
}
/**
* Batch verify multiple contracts at once (paid feature).
* @param {string[]} contracts - Array of contract addresses
* @param {string|number} chain - Chain ID
* @returns {Promise<Object>}
*/
async verifyBatch(contracts, chain = 1) {
return this._request("/verify-batch", {
method: "POST",
body: JSON.stringify({ contracts, chain: String(chain) }),
});
}
/**
* Register webhook for contract change alerts (Pro+ only).
* @param {Object} opts
* @param {string} opts.contract - Contract to watch
* @param {string} opts.callback_url - URL to POST alerts to
* @param {string} [opts.chain='ethereum']
* @param {string[]} [opts.events] - Event types to watch
* @returns {Promise<Object>}
*/
async webhook({ contract, callback_url, chain = "ethereum", events }) {
return this._request("/webhook", {
method: "POST",
body: JSON.stringify({ contract, callback_url, chain, events }),
});
}
/**
* Get usage dashboard for your API key.
* @returns {Promise<Object>}
*/
async dashboard() {
return this._request("/dashboard");
}
/**
* Create or redeem a referral code.
* @param {'create'|'redeem'} action
* @param {string} [code] - Required for redeem
* @returns {Promise<Object>}
*/
async referral(action, code) {
return this._request("/referral", {
method: "POST",
body: JSON.stringify({ action, code }),
});
}
/**
* Report a malicious contract to the community.
* @param {Object} opts
* @param {string} opts.contract - Contract address
* @param {string} opts.reason - Why it's malicious
* @param {string} [opts.evidence] - Supporting evidence
* @param {string} [opts.chain='ethereum']
* @returns {Promise<Object>}
*/
async report({ contract, reason, evidence, chain = "ethereum" }) {
return this._request("/report", {
method: "POST",
body: JSON.stringify({ contract, reason, evidence, chain }),
});
}
/**
* Get agent reputation score.
* @param {string} [agent] - Agent key prefix (defaults to your key)
* @returns {Promise<Object>}
*/
async reputation(agent) {
const params = agent ? `?agent=${agent}` : "";
return this._request(`/reputation${params}`);
}
/**
* Check for proxy/ownership changes on a contract (Pro+ only).
* @param {string} contract
* @param {string} [chain='ethereum']
* @returns {Promise<Object>}
*/
async contractDiff(contract, chain = "ethereum") {
const params = new URLSearchParams({ contract, chain });
return this._request(`/contract-diff?${params}`);
}
/**
* Get community leaderboard.
* @returns {Promise<Object>}
*/
async leaderboard() {
return this._request("/leaderboard");
}
/**
* Get integration guides.
* @returns {Promise<Object>}
*/
async integrations() {
return this._request("/integrations");
}
/**
* View the active giveaway and past winners.
* @returns {Promise<Object>}
*/
async giveaway() {
return this._request("/giveaway");
}
/**
* Enter the active giveaway (paid subscribers only).
* @param {string} [agentName] - Display name for leaderboard
* @returns {Promise<Object>}
*/
async giveawayEnter(agentName) {
return this._request("/giveaway/enter", {
method: "POST",
body: JSON.stringify({ agent_name: agentName }),
});
}
/**
* Set your agent display name on the leaderboard.
* @param {string} name - Display name (max 32 chars)
* @returns {Promise<Object>}
*/
async setAgentName(name) {
return this._request("/agent-name", {
method: "POST",
body: JSON.stringify({ name }),
});
}
}
module.exports = AgentShield;
module.exports.default = AgentShield;