@framers/agentos-ext-wallet 0.2.0 → 0.2.1
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.
- package/manifest.json +2 -2
- package/package.json +12 -9
- package/src/AgentWalletManager.ts +0 -324
- package/src/SpendingPolicyEnforcer.ts +0 -169
- package/src/cards/CardManager.ts +0 -274
- package/src/cards/LithicCardAdapter.ts +0 -159
- package/src/cards/MccCategoryMap.ts +0 -138
- package/src/cards/types.ts +0 -169
- package/src/chains/EvmWalletAdapter.ts +0 -170
- package/src/chains/SolanaWalletAdapter.ts +0 -184
- package/src/index.ts +0 -315
- package/src/tools/CardSpendingSummaryTool.ts +0 -52
- package/src/tools/CardStatusTool.ts +0 -68
- package/src/tools/CheckBalanceTool.ts +0 -145
- package/src/tools/FreezeCardTool.ts +0 -52
- package/src/tools/IssueCardTool.ts +0 -76
- package/src/tools/PayWithCardTool.ts +0 -109
- package/src/tools/SendCryptoTool.ts +0 -185
- package/src/tools/UnfreezeCardTool.ts +0 -52
- package/src/tools/WalletHistoryTool.ts +0 -113
- package/src/types.ts +0 -230
- package/test/AgentWalletManager.spec.ts +0 -301
- package/test/CardManager.spec.ts +0 -428
- package/test/LithicCardAdapter.spec.ts +0 -224
- package/test/MccCategoryMap.spec.ts +0 -153
- package/test/SpendingPolicyEnforcer.spec.ts +0 -254
- package/test/card-integration.spec.ts +0 -242
- package/test/card-tools.spec.ts +0 -285
- package/test/integration.spec.ts +0 -171
- package/test/wallet-tools.spec.ts +0 -275
- package/tsconfig.json +0 -22
package/src/cards/CardManager.ts
DELETED
|
@@ -1,274 +0,0 @@
|
|
|
1
|
-
// @ts-nocheck
|
|
2
|
-
/**
|
|
3
|
-
* @fileoverview High-level card lifecycle manager.
|
|
4
|
-
*
|
|
5
|
-
* Mirrors AgentWalletManager's pattern: one card per agent, spending policy
|
|
6
|
-
* enforcement via the shared SpendingPolicyEnforcer, and in-memory/DB store
|
|
7
|
-
* abstraction via ICardStore.
|
|
8
|
-
*
|
|
9
|
-
* @module wallet/cards/CardManager
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
import { v4 as uuidv4 } from 'uuid';
|
|
13
|
-
import type { SpendingPolicyEnforcer } from '../SpendingPolicyEnforcer.js';
|
|
14
|
-
import type { SpendCategory } from '../types.js';
|
|
15
|
-
import type { MccCategoryMap } from './MccCategoryMap.js';
|
|
16
|
-
import type {
|
|
17
|
-
AgentCardRecord,
|
|
18
|
-
CardConfig,
|
|
19
|
-
CardTransactionRecord,
|
|
20
|
-
ICardAdapter,
|
|
21
|
-
ICardStore,
|
|
22
|
-
LithicTransaction,
|
|
23
|
-
SpendingSummary,
|
|
24
|
-
} from './types.js';
|
|
25
|
-
|
|
26
|
-
// ---------------------------------------------------------------------------
|
|
27
|
-
// CardManager
|
|
28
|
-
// ---------------------------------------------------------------------------
|
|
29
|
-
|
|
30
|
-
export interface CardManagerOptions {
|
|
31
|
-
adapter: ICardAdapter;
|
|
32
|
-
store: ICardStore;
|
|
33
|
-
policyEnforcer: SpendingPolicyEnforcer;
|
|
34
|
-
mccMap: MccCategoryMap;
|
|
35
|
-
config: CardConfig;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export class CardManager {
|
|
39
|
-
private readonly adapter: ICardAdapter;
|
|
40
|
-
private readonly store: ICardStore;
|
|
41
|
-
private readonly policy: SpendingPolicyEnforcer;
|
|
42
|
-
private readonly mccMap: MccCategoryMap;
|
|
43
|
-
private readonly config: CardConfig;
|
|
44
|
-
|
|
45
|
-
constructor(opts: CardManagerOptions) {
|
|
46
|
-
this.adapter = opts.adapter;
|
|
47
|
-
this.store = opts.store;
|
|
48
|
-
this.policy = opts.policyEnforcer;
|
|
49
|
-
this.mccMap = opts.mccMap;
|
|
50
|
-
this.config = opts.config;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// -----------------------------------------------------------------------
|
|
54
|
-
// Card lifecycle
|
|
55
|
-
// -----------------------------------------------------------------------
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Issue a new virtual card for the agent.
|
|
59
|
-
* One card per agent — throws if card already exists.
|
|
60
|
-
*/
|
|
61
|
-
async issueCard(
|
|
62
|
-
agentId: string,
|
|
63
|
-
opts?: { memo?: string; spendLimitUsd?: number },
|
|
64
|
-
): Promise<AgentCardRecord> {
|
|
65
|
-
const existing = await this.store.getCard(agentId);
|
|
66
|
-
if (existing) {
|
|
67
|
-
throw new Error(`Agent ${agentId} already has a card (last4: ${existing.last4})`);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const spendLimit = opts?.spendLimitUsd ?? this.config.defaultSpendLimitUsd;
|
|
71
|
-
|
|
72
|
-
const lithicCard = await this.adapter.createCard({
|
|
73
|
-
type: 'VIRTUAL',
|
|
74
|
-
memo: opts?.memo || `Agent ${agentId} virtual card`,
|
|
75
|
-
spend_limit: Math.round(spendLimit * 100), // Lithic uses cents
|
|
76
|
-
spend_limit_duration: 'MONTHLY',
|
|
77
|
-
state: 'OPEN',
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
// Set auth rules for blocked categories
|
|
81
|
-
const blockedMccs = this.getBlockedMccs();
|
|
82
|
-
if (blockedMccs.length > 0) {
|
|
83
|
-
await this.adapter.createAuthRule(lithicCard.token, {
|
|
84
|
-
blocked_mcc: blockedMccs,
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const record: AgentCardRecord = {
|
|
89
|
-
id: uuidv4(),
|
|
90
|
-
agentId,
|
|
91
|
-
lithicCardToken: lithicCard.token,
|
|
92
|
-
last4: lithicCard.last_four,
|
|
93
|
-
cardType: lithicCard.type,
|
|
94
|
-
state: lithicCard.state,
|
|
95
|
-
spendLimitUsd: spendLimit,
|
|
96
|
-
spendLimitDuration: 'MONTHLY',
|
|
97
|
-
network: 'VISA',
|
|
98
|
-
memo: opts?.memo,
|
|
99
|
-
createdAt: Date.now(),
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
await this.store.saveCard(record);
|
|
103
|
-
return record;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/** Get the agent's card record, or null. */
|
|
107
|
-
async getCard(agentId: string): Promise<AgentCardRecord | null> {
|
|
108
|
-
return this.store.getCard(agentId);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Get sensitive card details (PAN, CVV, expiry) for online payments.
|
|
113
|
-
* Enforces spending policy before revealing details.
|
|
114
|
-
*/
|
|
115
|
-
async getCardDetails(
|
|
116
|
-
agentId: string,
|
|
117
|
-
estimatedAmountUsd?: number,
|
|
118
|
-
category?: SpendCategory,
|
|
119
|
-
): Promise<{ last4: string; expMonth: string; expYear: string; cvv: string; pan: string }> {
|
|
120
|
-
const card = await this.requireCard(agentId);
|
|
121
|
-
|
|
122
|
-
// If an estimated amount is provided, check spending policy first
|
|
123
|
-
if (estimatedAmountUsd !== undefined) {
|
|
124
|
-
const check = await this.policy.canSpend(agentId, estimatedAmountUsd, category || 'shopping');
|
|
125
|
-
if (!check.allowed) {
|
|
126
|
-
throw new CardSpendingBlockedError(check.reason || 'Spending policy violation');
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
const lithicCard = await this.adapter.getCard(card.lithicCardToken);
|
|
131
|
-
|
|
132
|
-
return {
|
|
133
|
-
last4: lithicCard.last_four,
|
|
134
|
-
expMonth: lithicCard.exp_month || '',
|
|
135
|
-
expYear: lithicCard.exp_year || '',
|
|
136
|
-
cvv: lithicCard.cvv || '',
|
|
137
|
-
pan: lithicCard.pan || '',
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/** Freeze (pause) the agent's card. */
|
|
142
|
-
async freezeCard(agentId: string): Promise<void> {
|
|
143
|
-
const card = await this.requireCard(agentId);
|
|
144
|
-
if (card.state === 'PAUSED') return;
|
|
145
|
-
if (card.state === 'CLOSED') throw new Error('Cannot freeze a closed card');
|
|
146
|
-
|
|
147
|
-
await this.adapter.pauseCard(card.lithicCardToken);
|
|
148
|
-
await this.store.updateCardState(agentId, 'PAUSED');
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/** Unfreeze (resume) the agent's card. */
|
|
152
|
-
async unfreezeCard(agentId: string): Promise<void> {
|
|
153
|
-
const card = await this.requireCard(agentId);
|
|
154
|
-
if (card.state === 'OPEN') return;
|
|
155
|
-
if (card.state === 'CLOSED') throw new Error('Cannot unfreeze a closed card');
|
|
156
|
-
|
|
157
|
-
await this.adapter.resumeCard(card.lithicCardToken);
|
|
158
|
-
await this.store.updateCardState(agentId, 'OPEN');
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/** Permanently close the agent's card. */
|
|
162
|
-
async closeCard(agentId: string): Promise<void> {
|
|
163
|
-
const card = await this.requireCard(agentId);
|
|
164
|
-
if (card.state === 'CLOSED') return;
|
|
165
|
-
|
|
166
|
-
await this.adapter.closeCard(card.lithicCardToken);
|
|
167
|
-
await this.store.updateCardState(agentId, 'CLOSED');
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
// -----------------------------------------------------------------------
|
|
171
|
-
// Spending summary
|
|
172
|
-
// -----------------------------------------------------------------------
|
|
173
|
-
|
|
174
|
-
async getSpendingSummary(agentId: string, period: 'day' | 'month' = 'month'): Promise<SpendingSummary> {
|
|
175
|
-
const card = await this.requireCard(agentId);
|
|
176
|
-
|
|
177
|
-
const now = new Date();
|
|
178
|
-
let since: number;
|
|
179
|
-
if (period === 'day') {
|
|
180
|
-
since = new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime();
|
|
181
|
-
} else {
|
|
182
|
-
since = new Date(now.getFullYear(), now.getMonth(), 1).getTime();
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
const txs = await this.store.getTransactionsByPeriod(card.id, since);
|
|
186
|
-
const settled = txs.filter(t => t.status === 'SETTLED' || t.status === 'PENDING');
|
|
187
|
-
|
|
188
|
-
const byCategory = new Map<SpendCategory, number>();
|
|
189
|
-
let totalUsd = 0;
|
|
190
|
-
|
|
191
|
-
for (const tx of settled) {
|
|
192
|
-
totalUsd += tx.amountUsd;
|
|
193
|
-
byCategory.set(tx.category, (byCategory.get(tx.category) || 0) + tx.amountUsd);
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
return {
|
|
197
|
-
totalUsd,
|
|
198
|
-
period,
|
|
199
|
-
byCategory: [...byCategory.entries()].map(([category, amountUsd]) => ({ category, amountUsd })),
|
|
200
|
-
transactionCount: settled.length,
|
|
201
|
-
};
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
// -----------------------------------------------------------------------
|
|
205
|
-
// Webhook processing
|
|
206
|
-
// -----------------------------------------------------------------------
|
|
207
|
-
|
|
208
|
-
/**
|
|
209
|
-
* Process a Lithic transaction webhook event.
|
|
210
|
-
* Resolves MCC → category and records the spend in the shared ledger.
|
|
211
|
-
*/
|
|
212
|
-
async processWebhookTransaction(agentId: string, lithicTx: LithicTransaction): Promise<void> {
|
|
213
|
-
const card = await this.store.getCard(agentId);
|
|
214
|
-
if (!card) return; // Unknown card — ignore
|
|
215
|
-
|
|
216
|
-
const category = this.mccMap.mccToCategory(lithicTx.merchant.mcc);
|
|
217
|
-
const amountUsd = Math.abs(lithicTx.amount) / 100; // Lithic amounts are in cents
|
|
218
|
-
|
|
219
|
-
const txRecord: CardTransactionRecord = {
|
|
220
|
-
id: uuidv4(),
|
|
221
|
-
cardId: card.id,
|
|
222
|
-
lithicTxToken: lithicTx.token,
|
|
223
|
-
merchantName: lithicTx.merchant.descriptor,
|
|
224
|
-
merchantMcc: lithicTx.merchant.mcc,
|
|
225
|
-
category,
|
|
226
|
-
amountUsd,
|
|
227
|
-
status: lithicTx.status,
|
|
228
|
-
createdAt: Date.now(),
|
|
229
|
-
};
|
|
230
|
-
|
|
231
|
-
await this.store.insertTransaction(txRecord);
|
|
232
|
-
|
|
233
|
-
// Record in shared spending ledger (same as crypto sends)
|
|
234
|
-
if (lithicTx.status === 'SETTLED' || lithicTx.status === 'PENDING') {
|
|
235
|
-
await this.policy.recordSpend(agentId, amountUsd, category);
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
// -----------------------------------------------------------------------
|
|
240
|
-
// Internal helpers
|
|
241
|
-
// -----------------------------------------------------------------------
|
|
242
|
-
|
|
243
|
-
private async requireCard(agentId: string): Promise<AgentCardRecord> {
|
|
244
|
-
const card = await this.store.getCard(agentId);
|
|
245
|
-
if (!card) {
|
|
246
|
-
throw new Error(`No card found for agent ${agentId}. Issue one first.`);
|
|
247
|
-
}
|
|
248
|
-
return card;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
/**
|
|
252
|
-
* Build a flat list of blocked MCC codes from the spending policy's
|
|
253
|
-
* blockedCategories, expanded via the MCC map.
|
|
254
|
-
*/
|
|
255
|
-
private getBlockedMccs(): string[] {
|
|
256
|
-
// Access the policy's config to get blocked categories
|
|
257
|
-
// The SpendingPolicyEnforcer doesn't expose config directly,
|
|
258
|
-
// so we pass blocked categories through the card config or
|
|
259
|
-
// use the MCC map to expand known blocked categories.
|
|
260
|
-
// For now, this is handled at the extension pack level.
|
|
261
|
-
return [];
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
// ---------------------------------------------------------------------------
|
|
266
|
-
// Error classes
|
|
267
|
-
// ---------------------------------------------------------------------------
|
|
268
|
-
|
|
269
|
-
export class CardSpendingBlockedError extends Error {
|
|
270
|
-
constructor(reason: string) {
|
|
271
|
-
super(reason);
|
|
272
|
-
this.name = 'CardSpendingBlockedError';
|
|
273
|
-
}
|
|
274
|
-
}
|
|
@@ -1,159 +0,0 @@
|
|
|
1
|
-
// @ts-nocheck
|
|
2
|
-
/**
|
|
3
|
-
* @fileoverview Pure REST client for the Lithic card issuance API.
|
|
4
|
-
*
|
|
5
|
-
* No SDK dependency — uses fetch() directly. Supports sandbox and production
|
|
6
|
-
* environments via configurable base URL.
|
|
7
|
-
*
|
|
8
|
-
* @see https://docs.lithic.com/reference
|
|
9
|
-
* @module wallet/cards/LithicCardAdapter
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
import type {
|
|
13
|
-
AuthRuleParams,
|
|
14
|
-
CreateCardParams,
|
|
15
|
-
ICardAdapter,
|
|
16
|
-
LithicCardResponse,
|
|
17
|
-
LithicTransaction,
|
|
18
|
-
ListTxOpts,
|
|
19
|
-
UpdateCardParams,
|
|
20
|
-
} from './types.js';
|
|
21
|
-
|
|
22
|
-
// ---------------------------------------------------------------------------
|
|
23
|
-
// Config
|
|
24
|
-
// ---------------------------------------------------------------------------
|
|
25
|
-
|
|
26
|
-
export interface LithicCardAdapterOptions {
|
|
27
|
-
apiKey: string;
|
|
28
|
-
/** Use sandbox environment. Defaults to true. */
|
|
29
|
-
sandbox?: boolean;
|
|
30
|
-
/** Override base URL (for testing). */
|
|
31
|
-
baseUrl?: string;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const SANDBOX_URL = 'https://sandbox.lithic.com/v1';
|
|
35
|
-
const PRODUCTION_URL = 'https://api.lithic.com/v1';
|
|
36
|
-
|
|
37
|
-
// ---------------------------------------------------------------------------
|
|
38
|
-
// Adapter
|
|
39
|
-
// ---------------------------------------------------------------------------
|
|
40
|
-
|
|
41
|
-
export class LithicCardAdapter implements ICardAdapter {
|
|
42
|
-
private readonly apiKey: string;
|
|
43
|
-
private readonly baseUrl: string;
|
|
44
|
-
|
|
45
|
-
constructor(opts: LithicCardAdapterOptions) {
|
|
46
|
-
this.apiKey = opts.apiKey;
|
|
47
|
-
this.baseUrl = opts.baseUrl || (opts.sandbox !== false ? SANDBOX_URL : PRODUCTION_URL);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// -----------------------------------------------------------------------
|
|
51
|
-
// Card CRUD
|
|
52
|
-
// -----------------------------------------------------------------------
|
|
53
|
-
|
|
54
|
-
async createCard(params: CreateCardParams): Promise<LithicCardResponse> {
|
|
55
|
-
return this.post<LithicCardResponse>('/cards', params);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
async getCard(cardToken: string): Promise<LithicCardResponse> {
|
|
59
|
-
return this.get<LithicCardResponse>(`/cards/${cardToken}`);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
async updateCard(cardToken: string, updates: UpdateCardParams): Promise<LithicCardResponse> {
|
|
63
|
-
return this.patch<LithicCardResponse>(`/cards/${cardToken}`, updates);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
async pauseCard(cardToken: string): Promise<void> {
|
|
67
|
-
await this.patch(`/cards/${cardToken}`, { state: 'PAUSED' });
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
async resumeCard(cardToken: string): Promise<void> {
|
|
71
|
-
await this.patch(`/cards/${cardToken}`, { state: 'OPEN' });
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
async closeCard(cardToken: string): Promise<void> {
|
|
75
|
-
await this.patch(`/cards/${cardToken}`, { state: 'CLOSED' });
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// -----------------------------------------------------------------------
|
|
79
|
-
// Transactions
|
|
80
|
-
// -----------------------------------------------------------------------
|
|
81
|
-
|
|
82
|
-
async listTransactions(cardToken: string, opts?: ListTxOpts): Promise<LithicTransaction[]> {
|
|
83
|
-
const params = new URLSearchParams();
|
|
84
|
-
params.set('card_token', cardToken);
|
|
85
|
-
if (opts?.begin) params.set('begin', opts.begin);
|
|
86
|
-
if (opts?.end) params.set('end', opts.end);
|
|
87
|
-
if (opts?.page_size) params.set('page_size', String(opts.page_size));
|
|
88
|
-
|
|
89
|
-
const result = await this.get<{ data: LithicTransaction[] }>(`/transactions?${params.toString()}`);
|
|
90
|
-
return result.data;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
// -----------------------------------------------------------------------
|
|
94
|
-
// Auth rules (MCC-based spending controls)
|
|
95
|
-
// -----------------------------------------------------------------------
|
|
96
|
-
|
|
97
|
-
async createAuthRule(cardToken: string, rule: AuthRuleParams): Promise<void> {
|
|
98
|
-
await this.post('/auth_rules', {
|
|
99
|
-
card_tokens: [cardToken],
|
|
100
|
-
...rule,
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
// -----------------------------------------------------------------------
|
|
105
|
-
// HTTP helpers
|
|
106
|
-
// -----------------------------------------------------------------------
|
|
107
|
-
|
|
108
|
-
private async get<T>(path: string): Promise<T> {
|
|
109
|
-
return this.request<T>('GET', path);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
private async post<T>(path: string, body: unknown): Promise<T> {
|
|
113
|
-
return this.request<T>('POST', path, body);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
private async patch<T>(path: string, body: unknown): Promise<T> {
|
|
117
|
-
return this.request<T>('PATCH', path, body);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
private async request<T>(method: string, path: string, body?: unknown): Promise<T> {
|
|
121
|
-
const url = `${this.baseUrl}${path}`;
|
|
122
|
-
const headers: Record<string, string> = {
|
|
123
|
-
'Authorization': this.apiKey,
|
|
124
|
-
'Accept': 'application/json',
|
|
125
|
-
};
|
|
126
|
-
if (body) headers['Content-Type'] = 'application/json';
|
|
127
|
-
|
|
128
|
-
const resp = await fetch(url, {
|
|
129
|
-
method,
|
|
130
|
-
headers,
|
|
131
|
-
body: body ? JSON.stringify(body) : undefined,
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
if (!resp.ok) {
|
|
135
|
-
const text = await resp.text().catch(() => '');
|
|
136
|
-
throw new LithicApiError(resp.status, `Lithic API error ${resp.status}: ${text}`, path);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
// Some endpoints return no body (204)
|
|
140
|
-
if (resp.status === 204) return undefined as T;
|
|
141
|
-
|
|
142
|
-
return resp.json() as Promise<T>;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
// ---------------------------------------------------------------------------
|
|
147
|
-
// Error
|
|
148
|
-
// ---------------------------------------------------------------------------
|
|
149
|
-
|
|
150
|
-
export class LithicApiError extends Error {
|
|
151
|
-
constructor(
|
|
152
|
-
public readonly statusCode: number,
|
|
153
|
-
message: string,
|
|
154
|
-
public readonly path: string,
|
|
155
|
-
) {
|
|
156
|
-
super(message);
|
|
157
|
-
this.name = 'LithicApiError';
|
|
158
|
-
}
|
|
159
|
-
}
|
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
// @ts-nocheck
|
|
2
|
-
/**
|
|
3
|
-
* @fileoverview Merchant Category Code (MCC) → SpendCategory mapping.
|
|
4
|
-
*
|
|
5
|
-
* Maps 4-digit MCC codes used by card networks to the wallet extension's
|
|
6
|
-
* SpendCategory enum. Uses range-based matching for efficient lookup.
|
|
7
|
-
*
|
|
8
|
-
* @module wallet/cards/MccCategoryMap
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import type { SpendCategory } from '../types.js';
|
|
12
|
-
|
|
13
|
-
// ---------------------------------------------------------------------------
|
|
14
|
-
// MCC range definitions
|
|
15
|
-
// ---------------------------------------------------------------------------
|
|
16
|
-
|
|
17
|
-
interface MccRange {
|
|
18
|
-
min: number;
|
|
19
|
-
max: number;
|
|
20
|
-
category: SpendCategory;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Ordered list of MCC ranges → SpendCategory mappings.
|
|
25
|
-
* Ranges are inclusive on both ends.
|
|
26
|
-
*/
|
|
27
|
-
const MCC_RANGES: MccRange[] = [
|
|
28
|
-
// Travel: airlines, hotels, rental cars, travel agencies
|
|
29
|
-
{ min: 3000, max: 3999, category: 'travel' },
|
|
30
|
-
{ min: 4511, max: 4511, category: 'travel' }, // airlines
|
|
31
|
-
{ min: 4722, max: 4722, category: 'travel' }, // travel agencies
|
|
32
|
-
{ min: 7011, max: 7012, category: 'travel' }, // hotels, timeshares
|
|
33
|
-
{ min: 7033, max: 7033, category: 'travel' }, // trailer parks
|
|
34
|
-
{ min: 7512, max: 7519, category: 'travel' }, // car rental
|
|
35
|
-
|
|
36
|
-
// Utilities: telecom, electric, gas, water
|
|
37
|
-
{ min: 4812, max: 4816, category: 'utilities' }, // telecom
|
|
38
|
-
{ min: 4899, max: 4900, category: 'utilities' }, // cable, electric/gas/water
|
|
39
|
-
|
|
40
|
-
// Shopping: general retail
|
|
41
|
-
{ min: 5200, max: 5499, category: 'shopping' }, // home supply, dept stores
|
|
42
|
-
{ min: 5511, max: 5599, category: 'shopping' }, // auto dealers, auto parts
|
|
43
|
-
{ min: 5600, max: 5699, category: 'shopping' }, // apparel
|
|
44
|
-
{ min: 5700, max: 5799, category: 'shopping' }, // furniture, electronics
|
|
45
|
-
{ min: 5900, max: 5999, category: 'shopping' }, // retail stores, misc
|
|
46
|
-
|
|
47
|
-
// Dining: restaurants, bars, fast food
|
|
48
|
-
{ min: 5811, max: 5811, category: 'dining' }, // caterers
|
|
49
|
-
{ min: 5812, max: 5812, category: 'dining' }, // eating places/restaurants
|
|
50
|
-
{ min: 5813, max: 5813, category: 'dining' }, // bars/taverns
|
|
51
|
-
{ min: 5814, max: 5814, category: 'dining' }, // fast food
|
|
52
|
-
|
|
53
|
-
// Subscriptions: digital goods/services
|
|
54
|
-
{ min: 5815, max: 5818, category: 'subscriptions' }, // digital goods, games, apps, streaming
|
|
55
|
-
|
|
56
|
-
// Entertainment: movies, theaters, events, recreation
|
|
57
|
-
{ min: 7832, max: 7833, category: 'entertainment' }, // movie theaters
|
|
58
|
-
{ min: 7841, max: 7841, category: 'entertainment' }, // video tape rental
|
|
59
|
-
{ min: 7911, max: 7911, category: 'entertainment' }, // dance halls/studios
|
|
60
|
-
{ min: 7922, max: 7922, category: 'entertainment' }, // theatrical producers
|
|
61
|
-
{ min: 7929, max: 7929, category: 'entertainment' }, // bands/orchestras
|
|
62
|
-
{ min: 7932, max: 7933, category: 'entertainment' }, // billiards, bowling
|
|
63
|
-
{ min: 7941, max: 7941, category: 'entertainment' }, // sports clubs/fields
|
|
64
|
-
{ min: 7991, max: 7999, category: 'entertainment' }, // tourist attractions, recreation
|
|
65
|
-
|
|
66
|
-
// Web services: computer programming, data processing, IT
|
|
67
|
-
{ min: 7372, max: 7379, category: 'web_services' }, // computer services
|
|
68
|
-
{ min: 4816, max: 4816, category: 'web_services' }, // computer network/info services (re-mapped from utilities for precision)
|
|
69
|
-
|
|
70
|
-
// API costs: SaaS / cloud compute (commonly billed under computer services)
|
|
71
|
-
// Note: most SaaS bills under 7372 (web_services). If needed, specific
|
|
72
|
-
// merchant descriptors can override at the CardManager level.
|
|
73
|
-
];
|
|
74
|
-
|
|
75
|
-
// ---------------------------------------------------------------------------
|
|
76
|
-
// Lookup class
|
|
77
|
-
// ---------------------------------------------------------------------------
|
|
78
|
-
|
|
79
|
-
export class MccCategoryMap {
|
|
80
|
-
private readonly ranges: MccRange[];
|
|
81
|
-
|
|
82
|
-
constructor() {
|
|
83
|
-
// Sort by min for binary-search-friendly access
|
|
84
|
-
this.ranges = [...MCC_RANGES].sort((a, b) => a.min - b.min);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Resolve a 4-digit MCC string to a SpendCategory.
|
|
89
|
-
* Returns 'other' if no matching range is found.
|
|
90
|
-
*/
|
|
91
|
-
mccToCategory(mcc: string): SpendCategory {
|
|
92
|
-
const code = parseInt(mcc, 10);
|
|
93
|
-
if (isNaN(code)) return 'other';
|
|
94
|
-
|
|
95
|
-
for (const range of this.ranges) {
|
|
96
|
-
if (code >= range.min && code <= range.max) {
|
|
97
|
-
return range.category;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
return 'other';
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Get all MCC ranges that map to a given SpendCategory.
|
|
105
|
-
* Useful for building Lithic auth rules.
|
|
106
|
-
*/
|
|
107
|
-
categoryToMccRanges(category: SpendCategory): Array<{ min: number; max: number }> {
|
|
108
|
-
return this.ranges
|
|
109
|
-
.filter(r => r.category === category)
|
|
110
|
-
.map(({ min, max }) => ({ min, max }));
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Get all individual MCC codes for a category (expands ranges).
|
|
115
|
-
* Useful for Lithic's blocked_mcc list which expects individual codes.
|
|
116
|
-
*/
|
|
117
|
-
categoryToMccCodes(category: SpendCategory): string[] {
|
|
118
|
-
const codes: string[] = [];
|
|
119
|
-
for (const range of this.ranges) {
|
|
120
|
-
if (range.category !== category) continue;
|
|
121
|
-
for (let code = range.min; code <= range.max; code++) {
|
|
122
|
-
codes.push(code.toString().padStart(4, '0'));
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
return codes;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Get all categories that have MCC mappings.
|
|
130
|
-
*/
|
|
131
|
-
getMappedCategories(): SpendCategory[] {
|
|
132
|
-
const categories = new Set<SpendCategory>();
|
|
133
|
-
for (const range of this.ranges) {
|
|
134
|
-
categories.add(range.category);
|
|
135
|
-
}
|
|
136
|
-
return [...categories];
|
|
137
|
-
}
|
|
138
|
-
}
|