-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-client.ts
More file actions
237 lines (214 loc) · 7.79 KB
/
Copy pathapi-client.ts
File metadata and controls
237 lines (214 loc) · 7.79 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
import {
PaymentSessionSchema,
CreateSessionResponseSchema,
QuoteResponseSchema,
SignedPaymentConfigEnvelopeSchema,
Web3SettleApiError,
type PaymentConfig,
type PaymentSession,
type CreateSessionResponse,
type QuoteResponse,
} from './types';
import { verifyPaymentConfig } from './payment-config-verifier';
import { SUPPORTED_ABI_VERSIONS, KNOWN_CONTRACT_ADDRESSES } from './config';
interface RequestOptions {
method?: 'GET' | 'POST' | 'PUT' | 'DELETE';
body?: unknown;
headers?: Record<string, string>;
signal?: AbortSignal;
}
const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
function assertValidStorefrontId(id: string): void {
if (!UUID_REGEX.test(id)) {
throw new Error(`Invalid storefrontId: must be a UUID, got "${id}"`);
}
}
function assertValidSessionId(id: string): void {
if (!UUID_REGEX.test(id)) {
throw new Error(`Invalid sessionId: must be a UUID`);
}
}
export class Web3SettleApiClient {
private readonly baseUrl: URL;
private readonly storefrontId: string;
constructor(baseUrl: string, storefrontId: string) {
assertValidStorefrontId(storefrontId);
this.baseUrl = new URL(baseUrl);
this.storefrontId = storefrontId;
}
async fetchPaymentConfig(signal?: AbortSignal): Promise<PaymentConfig> {
const raw = await this.request(
`api/storefronts/${this.storefrontId}/payment-config`,
{ signal },
);
// Step 1: shape-validate the envelope (data + signedAt + signature).
const envelope = this.parse(raw, SignedPaymentConfigEnvelopeSchema, 'payment config envelope');
// Step 2: cryptographic provenance — the signature must verify against a
// baked-in Ed25519 public key over `signed_at + canonical_json(data)`. A
// poisoned-DNS / CDN-edge MITM that substitutes contract addresses cannot
// forge this without the private key in Vault.
const verify = verifyPaymentConfig({
data: envelope.data,
signed_at: envelope.signedAt,
signature: envelope.signature,
});
if (!verify.ok) {
throw new Web3SettleApiError(
`Refusing payment-config response: signature verification failed (${verify.reason}${verify.detail ? `: ${verify.detail}` : ''}). The SDK will not build a transaction against an unverified contract address.`,
0,
{ reason: verify.reason },
);
}
// Step 3: ABI-version handshake (Phase 7). Fail closed when the backend
// has rolled forward to a revision the SDK can't speak — calldata would
// revert silently otherwise.
if (!SUPPORTED_ABI_VERSIONS.has(envelope.data.contractAbiVersion)) {
throw new Web3SettleApiError(
`Unsupported contractAbiVersion "${envelope.data.contractAbiVersion}" — upgrade @web3settle/merchant-sdk to a release that supports this version.`,
0,
{ abiVersion: envelope.data.contractAbiVersion },
);
}
// Step 4: contract-address allowlist (Phase 3). The SDK refuses to build
// calldata for a contract address that is neither in the baked-in
// KNOWN_CONTRACT_ADDRESSES nor explicitly elevated by the signed payload.
// Backend compromise alone cannot redirect funds — a fresh address still
// requires an SDK release.
for (const chain of envelope.data.chains) {
const baked = KNOWN_CONTRACT_ADDRESSES[chain.chainId] ?? new Set<string>();
const elevated = new Set(
(envelope.data.allowedContractAddresses[String(chain.chainId)] ?? []).map((a) =>
a.toLowerCase(),
),
);
const addrLower = chain.contractAddress.toLowerCase();
if (!baked.has(addrLower) && !elevated.has(addrLower)) {
throw new Web3SettleApiError(
`Refusing chain ${chain.chainId}: contractAddress "${chain.contractAddress}" is not in the SDK allowlist and was not explicitly elevated by the signed payload.`,
0,
{ chainId: chain.chainId, address: addrLower },
);
}
}
return envelope.data;
}
async createTopUpSession(
userId: string,
amount: number,
idempotencyKey: string,
signal?: AbortSignal,
): Promise<CreateSessionResponse> {
const raw = await this.request(
`api/storefronts/${this.storefrontId}/sessions`,
{
method: 'POST',
body: { userId, amount, idempotencyKey },
signal,
},
);
return this.parse(raw, CreateSessionResponseSchema, 'session');
}
/**
* Server-side USD → token quote backed by Chainlink. Use the returned `amountToken` (atomic,
* decimal string) as the `value` / `amount` arg when building the on-chain tx so the user
* signs exactly what they were quoted.
*
* `token` is either `"native"` for the chain's gas token or a 0x-prefixed ERC20 address. The
* server rejects tokens not enabled on the storefront's active contract.
*/
async fetchQuote(
network: string,
token: string,
amountUsd: number,
signal?: AbortSignal,
): Promise<QuoteResponse> {
const qs = new URLSearchParams({
network,
token,
amountUsd: amountUsd.toString(),
});
const raw = await this.request(
`api/storefronts/${this.storefrontId}/quote?${qs.toString()}`,
{ signal },
);
return this.parse(raw, QuoteResponseSchema, 'quote');
}
async getSessionStatus(sessionId: string, signal?: AbortSignal): Promise<PaymentSession> {
assertValidSessionId(sessionId);
const raw = await this.request(
`api/storefronts/${this.storefrontId}/sessions/${sessionId}`,
{ signal },
);
return this.parse(raw, PaymentSessionSchema, 'session status');
}
private parse<T>(
raw: unknown,
schema: { safeParse: (v: unknown) => { success: true; data: T } | { success: false; error: { message: string } } },
kind: string,
): T {
const result = schema.safeParse(raw);
if (!result.success) {
throw new Web3SettleApiError(
`Invalid ${kind} response: ${result.error.message}`,
0,
raw,
);
}
return result.data;
}
private buildUrl(path: string): string {
const base = this.baseUrl.toString().replace(/\/+$/, '');
const suffix = path.replace(/^\/+/, '');
return `${base}/${suffix}`;
}
private async request(path: string, options: RequestOptions = {}): Promise<unknown> {
const { method = 'GET', body, headers = {}, signal } = options;
const url = this.buildUrl(path);
const fetchHeaders: Record<string, string> = {
Accept: 'application/json',
...headers,
};
if (body !== undefined) {
fetchHeaders['Content-Type'] = 'application/json';
}
let response: Response;
try {
response = await fetch(url, {
method,
headers: fetchHeaders,
body: body !== undefined ? JSON.stringify(body) : undefined,
signal,
});
} catch (err) {
if (err instanceof DOMException && err.name === 'AbortError') {
throw err;
}
throw new Web3SettleApiError(
`Network error: ${err instanceof Error ? err.message : 'Unknown error'}`,
0,
);
}
const contentType = response.headers.get('content-type') ?? '';
let responseBody: unknown;
if (contentType.includes('application/json')) {
try {
responseBody = await response.json();
} catch {
throw new Web3SettleApiError('Failed to parse JSON response', response.status);
}
} else {
responseBody = await response.text();
}
if (!response.ok) {
const message =
typeof responseBody === 'object' &&
responseBody !== null &&
'message' in responseBody &&
typeof (responseBody as { message: unknown }).message === 'string'
? (responseBody as { message: string }).message
: `HTTP ${response.status}`;
throw new Web3SettleApiError(message, response.status, responseBody);
}
return responseBody;
}
}