@decentrys/sentinel-sdk 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,253 @@
1
+ /**
2
+ * Audit findings → monitoring rules.
3
+ *
4
+ * The single most valuable thing the platform does, and the reason Audit and
5
+ * Sentinel belong in one product.
6
+ *
7
+ * An audit ends with a report, and a report is a document. Two months later
8
+ * the upgrade path a reviewer spent a day understanding is a paragraph nobody
9
+ * has reopened, and the person who reads it next is reading it during an
10
+ * incident. This turns what the audit *learned* into what the system
11
+ * *watches*: the privileged functions a reviewer identified become the
12
+ * functions that page someone when they are called.
13
+ *
14
+ * Two constraints shape the output:
15
+ *
16
+ * - **A capability is not a finding.** A contract being upgradeable is not a
17
+ * vulnerability, and the generated rule does not say it is. It watches for
18
+ * the upgrade *happening*, which is an event worth knowing about however
19
+ * legitimate the protocol.
20
+ * - **Every rule must be actionable.** A rule that fires on something nobody
21
+ * can respond to trains a team to close alerts unread, and then the real
22
+ * one is closed unread too. Nothing here generates a rule for an event
23
+ * with no plausible response.
24
+ */
25
+
26
+ import type { AlertSeverity, RuleAction } from './model';
27
+ import type { CreateRuleInput } from './client';
28
+
29
+ /** What an audit observed about a contract, in the shape Protect reports it. */
30
+ export interface AuditCapability {
31
+ /** e.g. UPGRADEABLE, MINT_AUTHORITY, PAUSABLE, BLACKLIST, FEE_CONTROL. */
32
+ type: string;
33
+ statement?: string;
34
+ /** The function, role or slot that grants it, when known. */
35
+ grantedBy?: string;
36
+ }
37
+
38
+ export interface AuditHandoverInput {
39
+ projectId: string;
40
+ /** Capabilities the audit confirmed the deployed code holds. */
41
+ capabilities: AuditCapability[];
42
+ /** Where alerts go. At minimum an in-platform alert. */
43
+ actions?: RuleAction[];
44
+ /**
45
+ * Treasury alert threshold in USD. Omitted means no treasury rule is
46
+ * generated: a threshold guessed on a customer's behalf is either so low it
47
+ * pages constantly or so high it never fires, and both teach people to
48
+ * ignore it.
49
+ */
50
+ treasuryThresholdUsd?: number;
51
+ }
52
+
53
+ interface Generated {
54
+ capability: string;
55
+ rule: CreateRuleInput;
56
+ }
57
+
58
+ const DEFAULT_ACTIONS: RuleAction[] = [{ type: 'ALERT', config: {} }];
59
+
60
+ /**
61
+ * Capability → the event worth watching, and why.
62
+ *
63
+ * Severity reflects how hard the change is to reverse, not how suspicious it
64
+ * is. A logic replacement is CRITICAL because every assumption in the audit
65
+ * stops holding the moment it lands — not because upgrading is wrong.
66
+ */
67
+ const RULES: Record<string, {
68
+ name: string;
69
+ description: string;
70
+ severity: AlertSeverity;
71
+ triggerType: string;
72
+ conditions: Record<string, unknown>;
73
+ }> = {
74
+ UPGRADEABLE: {
75
+ name: 'Contract logic replaced',
76
+ description:
77
+ 'The proxy now points at different code. Every finding in the audit describes the previous '
78
+ + 'implementation and stops applying the moment this fires.',
79
+ severity: 'CRITICAL',
80
+ triggerType: 'contract.upgraded',
81
+ conditions: { op: 'eq', field: 'upgrade.changed', value: true },
82
+ },
83
+ ADMIN_CONTROL: {
84
+ name: 'Admin address changed',
85
+ description: 'Whoever can upgrade or configure this contract is now a different account.',
86
+ severity: 'CRITICAL',
87
+ triggerType: 'contract.admin_changed',
88
+ conditions: { op: 'eq', field: 'admin.changed', value: true },
89
+ },
90
+ OWNERSHIP: {
91
+ name: 'Ownership transferred',
92
+ description: 'Owner-only functions identified during the audit are now controlled by someone else.',
93
+ severity: 'HIGH',
94
+ triggerType: 'contract.ownership_transferred',
95
+ conditions: { op: 'eq', field: 'ownership.changed', value: true },
96
+ },
97
+ MINT_AUTHORITY: {
98
+ name: 'Supply increased',
99
+ description:
100
+ 'The mint authority the audit identified was used. Legitimate for many designs — this reports it '
101
+ + 'so holders are not the last to know.',
102
+ severity: 'HIGH',
103
+ triggerType: 'token.supply_changed',
104
+ conditions: {
105
+ op: 'AND',
106
+ conditions: [
107
+ { op: 'eq', field: 'token.supplyIncreased', value: true },
108
+ // A percentage rather than an absolute: a fixed figure is meaningless
109
+ // across tokens with different supplies and decimals.
110
+ { op: 'gt', field: 'token.supplyChangePercent', value: 1 },
111
+ ],
112
+ },
113
+ },
114
+ PAUSABLE: {
115
+ name: 'Pause state changed',
116
+ description:
117
+ 'The contract was paused or unpaused. Pausing is usually a defence, and knowing it happened is '
118
+ + 'how you find out an incident started without you.',
119
+ severity: 'HIGH',
120
+ triggerType: 'contract.paused',
121
+ conditions: { op: 'eq', field: 'contract.pauseChanged', value: true },
122
+ },
123
+ BLACKLIST: {
124
+ name: 'Transfer restriction applied',
125
+ description: 'An address was restricted from transferring. Reported because it is invisible on-chain otherwise.',
126
+ severity: 'MEDIUM',
127
+ triggerType: 'contract.function_called',
128
+ conditions: { op: 'contains', field: 'event.types', value: 'BLACKLIST' },
129
+ },
130
+ FORCED_BALANCE_CHANGE: {
131
+ name: 'Balance moved without the holder acting',
132
+ description:
133
+ 'A permanent delegate or equivalent authority moved tokens from an account that did not sign for it. '
134
+ + 'Legitimate for some regulated designs, and always worth knowing about.',
135
+ severity: 'CRITICAL',
136
+ triggerType: 'contract.function_called',
137
+ conditions: { op: 'contains', field: 'event.types', value: 'FORCED_TRANSFER' },
138
+ },
139
+ GOVERNANCE: {
140
+ name: 'Governance proposal executed',
141
+ description: 'An executed proposal can change anything the audit assumed was fixed.',
142
+ severity: 'HIGH',
143
+ triggerType: 'governance.executed',
144
+ conditions: { op: 'eq', field: 'governance.executed', value: true },
145
+ },
146
+ };
147
+
148
+ /** Capability names normalised, so an audit may say MINT or MINT_AUTHORITY. */
149
+ const ALIASES: Record<string, string> = {
150
+ // The canonical names the Protect layer emits. Chain-specific spellings —
151
+ // Sui's TreasuryCap, Solana's mintAuthority, an EIP-1967 slot — are already
152
+ // normalised to these before an audit hands anything over, so mapping raw
153
+ // chain vocabulary here would be mapping names that never arrive.
154
+ UPGRADEABLE: 'UPGRADEABLE',
155
+ DELEGATED_EXECUTION: 'UPGRADEABLE',
156
+ MINT_AUTHORITY: 'MINT_AUTHORITY',
157
+ PAUSABLE: 'PAUSABLE',
158
+ ACCOUNT_FREEZE: 'BLACKLIST',
159
+ FORCED_BALANCE_CHANGE: 'FORCED_BALANCE_CHANGE',
160
+ GOVERNANCE: 'GOVERNANCE',
161
+ // Accepted because an audit report may use them in prose, and rejecting a
162
+ // reviewer's own wording would silently drop a rule they expected.
163
+ PROXY: 'UPGRADEABLE',
164
+ UPGRADE_AUTHORITY: 'UPGRADEABLE',
165
+ ADMIN: 'ADMIN_CONTROL',
166
+ ADMIN_CONTROL: 'ADMIN_CONTROL',
167
+ OWNER: 'OWNERSHIP',
168
+ OWNERSHIP: 'OWNERSHIP',
169
+ MINT: 'MINT_AUTHORITY',
170
+ PAUSE: 'PAUSABLE',
171
+ FREEZE_AUTHORITY: 'BLACKLIST',
172
+ BLACKLIST: 'BLACKLIST',
173
+ };
174
+
175
+ /**
176
+ * Turn confirmed capabilities into rules.
177
+ *
178
+ * Deduplicated: a contract with both a proxy and an upgrade authority holds
179
+ * one capability described twice, and generating two identical rules would
180
+ * page someone twice for one event.
181
+ */
182
+ export function rulesFromAudit(input: AuditHandoverInput): Generated[] {
183
+ const actions = input.actions?.length ? input.actions : DEFAULT_ACTIONS;
184
+ const seen = new Set<string>();
185
+ const generated: Generated[] = [];
186
+
187
+ for (const capability of input.capabilities) {
188
+ const key = ALIASES[capability.type.toUpperCase()];
189
+ if (!key || seen.has(key)) continue;
190
+ seen.add(key);
191
+
192
+ const template = RULES[key];
193
+ if (!template) continue;
194
+
195
+ generated.push({
196
+ capability: capability.type,
197
+ rule: {
198
+ projectId: input.projectId,
199
+ name: template.name,
200
+ description: capability.grantedBy
201
+ ? `${template.description} Granted by ${capability.grantedBy}, identified during the audit.`
202
+ : `${template.description} Identified during the audit.`,
203
+ severity: template.severity,
204
+ triggerType: template.triggerType,
205
+ conditions: template.conditions,
206
+ actions,
207
+ // Five minutes. Long enough that one event in a loop does not page a
208
+ // team repeatedly, short enough that a second, genuinely separate
209
+ // occurrence still gets through.
210
+ cooldownSeconds: 300,
211
+ },
212
+ });
213
+ }
214
+
215
+ if (typeof input.treasuryThresholdUsd === 'number' && input.treasuryThresholdUsd > 0) {
216
+ generated.push({
217
+ capability: 'TREASURY',
218
+ rule: {
219
+ projectId: input.projectId,
220
+ name: 'Large treasury outflow',
221
+ description:
222
+ `An outbound movement above $${input.treasuryThresholdUsd.toLocaleString()}, the threshold agreed `
223
+ + 'during the audit.',
224
+ severity: 'HIGH',
225
+ triggerType: 'treasury.transfer',
226
+ conditions: {
227
+ op: 'AND',
228
+ conditions: [
229
+ { op: 'eq', field: 'transfer.direction', value: 'OUT' },
230
+ { op: 'gt', field: 'transfer.valueUsd', value: input.treasuryThresholdUsd },
231
+ ],
232
+ },
233
+ actions,
234
+ cooldownSeconds: 300,
235
+ },
236
+ });
237
+ }
238
+
239
+ return generated;
240
+ }
241
+
242
+ /**
243
+ * Which capabilities produced no rule.
244
+ *
245
+ * Returned rather than silently dropped: a customer handed six rules for nine
246
+ * capabilities should be told which three are unwatched, so the gap is a
247
+ * decision rather than an assumption.
248
+ */
249
+ export function unmappedCapabilities(capabilities: AuditCapability[]): string[] {
250
+ return capabilities
251
+ .filter((capability) => !ALIASES[capability.type.toUpperCase()])
252
+ .map((capability) => capability.type);
253
+ }
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './model';
2
+ export * from './client';
3
+ export * from './from-audit';
package/src/model.ts ADDED
@@ -0,0 +1,85 @@
1
+ /**
2
+ * The Sentinel model.
3
+ *
4
+ * Sentinel answers a different question from Protect. Protect asks "should I
5
+ * sign this?" about something a user is about to do; Sentinel asks "did
6
+ * something happen to what I own?" about systems already deployed. That
7
+ * changes what matters:
8
+ *
9
+ * - **The subject is yours.** You register your own contracts, wallets and
10
+ * treasuries. Nothing here classifies a third party, so none of Protect's
11
+ * machinery for avoiding accusations applies — and neither does its
12
+ * restraint about severity.
13
+ * - **A rule is a statement about facts, not a score.** A rule fires or it
14
+ * does not, and the alert says which fact made it fire. There is no
15
+ * threshold model to argue with.
16
+ * - **An alert must be actionable.** An alert nobody can act on trains
17
+ * people to close alerts, which is worse than having none.
18
+ */
19
+
20
+ export const ALERT_SEVERITIES = ['INFO', 'LOW', 'MEDIUM', 'HIGH', 'CRITICAL'] as const;
21
+ export type AlertSeverity = (typeof ALERT_SEVERITIES)[number];
22
+
23
+ /** What a monitored thing is. Determines which rules can apply to it. */
24
+ export const TARGET_TYPES = [
25
+ 'CONTRACT', 'WALLET', 'TREASURY', 'MULTISIG', 'LP_POOL', 'ORACLE', 'BRIDGE', 'GOVERNANCE',
26
+ ] as const;
27
+ export type TargetType = (typeof TARGET_TYPES)[number];
28
+
29
+ export interface MonitoringTarget {
30
+ id: string;
31
+ projectId: string;
32
+ chainKey: string;
33
+ address: string;
34
+ targetType: TargetType;
35
+ label: string | null;
36
+ enabled: boolean;
37
+ createdAt: string;
38
+ }
39
+
40
+ export interface RuleAction {
41
+ type: string;
42
+ config: Record<string, unknown>;
43
+ }
44
+
45
+ export interface MonitoringRule {
46
+ id: string;
47
+ projectId: string;
48
+ name: string;
49
+ description: string | null;
50
+ severity: AlertSeverity;
51
+ triggerType: string;
52
+ conditions: Record<string, unknown>;
53
+ actions: RuleAction[];
54
+ /**
55
+ * Suppression window. Without one, a contract emitting the same event in a
56
+ * loop pages someone a thousand times for one incident, and the thousandth
57
+ * page is less useful than the first.
58
+ */
59
+ cooldownSeconds: number;
60
+ enabled: boolean;
61
+ createdAt: string;
62
+ }
63
+
64
+ export interface Alert {
65
+ id: string;
66
+ ruleId: string | null;
67
+ ruleName: string | null;
68
+ severity: AlertSeverity;
69
+ title: string;
70
+ description: string | null;
71
+ chainKey: string | null;
72
+ address: string | null;
73
+ txHash: string | null;
74
+ /** The facts that made the rule fire. An alert without these is unactionable. */
75
+ facts: Record<string, unknown> | null;
76
+ status: string;
77
+ acknowledgedAt: string | null;
78
+ createdAt: string;
79
+ }
80
+
81
+ export interface RuleTestResult {
82
+ matched: boolean;
83
+ /** Which conditions passed and which did not, so a rule can be debugged. */
84
+ explanation: string[];
85
+ }