@orangecheck/me-client 0.8.1 → 0.11.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.
- package/dist/index.d.mts +81 -4
- package/dist/index.d.ts +81 -4
- package/dist/index.js +173 -20
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +173 -21
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -109,7 +109,7 @@ async function invalidate(sessionId) {
|
|
|
109
109
|
}
|
|
110
110
|
var session = { create, refresh, invalidate };
|
|
111
111
|
function OcSignInButton({
|
|
112
|
-
scope,
|
|
112
|
+
scope: scope2,
|
|
113
113
|
sessionPolicy,
|
|
114
114
|
returnTo,
|
|
115
115
|
onSignin,
|
|
@@ -130,7 +130,7 @@ function OcSignInButton({
|
|
|
130
130
|
if (event2.defaultPrevented) return;
|
|
131
131
|
setPending(true);
|
|
132
132
|
try {
|
|
133
|
-
const result = await session.create({ scope, sessionPolicy, returnTo });
|
|
133
|
+
const result = await session.create({ scope: scope2, sessionPolicy, returnTo });
|
|
134
134
|
onSignin?.(result);
|
|
135
135
|
} catch (err) {
|
|
136
136
|
onSignInError?.(err instanceof Error ? err : new Error(String(err)));
|
|
@@ -190,21 +190,6 @@ async function authorize(opts) {
|
|
|
190
190
|
}
|
|
191
191
|
var payment = { authorize };
|
|
192
192
|
|
|
193
|
-
// src/event.ts
|
|
194
|
-
async function fire(options) {
|
|
195
|
-
if (!options.project_key) {
|
|
196
|
-
throw new Error("event.fire requires project_key");
|
|
197
|
-
}
|
|
198
|
-
if (!options.subtype) {
|
|
199
|
-
throw new Error("event.fire requires subtype");
|
|
200
|
-
}
|
|
201
|
-
return api("/api/integrator/event", {
|
|
202
|
-
method: "POST",
|
|
203
|
-
body: options
|
|
204
|
-
});
|
|
205
|
-
}
|
|
206
|
-
var event = { fire };
|
|
207
|
-
|
|
208
193
|
// src/types.ts
|
|
209
194
|
var PLATFORM_FEE_POLICY = {
|
|
210
195
|
/** Fraction of every gross_fee that OC retains. */
|
|
@@ -274,6 +259,122 @@ function validateIntegratorConfig(cfg) {
|
|
|
274
259
|
return { ok: errors.length === 0, errors };
|
|
275
260
|
}
|
|
276
261
|
|
|
262
|
+
// src/event.ts
|
|
263
|
+
async function fire(options) {
|
|
264
|
+
if (!options.project_key) {
|
|
265
|
+
throw new Error("event.fire requires project_key");
|
|
266
|
+
}
|
|
267
|
+
if (!options.subtype) {
|
|
268
|
+
throw new Error("event.fire requires subtype");
|
|
269
|
+
}
|
|
270
|
+
return api("/api/integrator/event", {
|
|
271
|
+
method: "POST",
|
|
272
|
+
body: options
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
async function fireBatch(options) {
|
|
276
|
+
if (!options.project_key) {
|
|
277
|
+
throw new Error("event.fireBatch requires project_key");
|
|
278
|
+
}
|
|
279
|
+
if (!Array.isArray(options.events) || options.events.length === 0) {
|
|
280
|
+
throw new Error("event.fireBatch requires at least one event");
|
|
281
|
+
}
|
|
282
|
+
if (options.events.length > 100) {
|
|
283
|
+
throw new Error(
|
|
284
|
+
`event.fireBatch \xB7 max 100 events per call (got ${options.events.length}). Split client-side.`
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
return api("/api/integrator/event/batch", {
|
|
288
|
+
method: "POST",
|
|
289
|
+
body: options
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
function verify(envelope, config2, payment_amount_sats) {
|
|
293
|
+
const issues = [];
|
|
294
|
+
const eventCfg = config2.events[envelope.subtype];
|
|
295
|
+
if (!eventCfg) {
|
|
296
|
+
return {
|
|
297
|
+
ok: false,
|
|
298
|
+
expected: null,
|
|
299
|
+
issues: [
|
|
300
|
+
{
|
|
301
|
+
field: "subtype_unconfigured",
|
|
302
|
+
expected: null,
|
|
303
|
+
actual: null,
|
|
304
|
+
message: `subtype ${envelope.subtype} is not configured on integrator ${config2.project_key}`
|
|
305
|
+
}
|
|
306
|
+
]
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
if (!eventCfg.enabled) {
|
|
310
|
+
return {
|
|
311
|
+
ok: false,
|
|
312
|
+
expected: null,
|
|
313
|
+
issues: [
|
|
314
|
+
{
|
|
315
|
+
field: "subtype_disabled",
|
|
316
|
+
expected: null,
|
|
317
|
+
actual: null,
|
|
318
|
+
message: `subtype ${envelope.subtype} is disabled on integrator ${config2.project_key} but the envelope was billed`
|
|
319
|
+
}
|
|
320
|
+
]
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
if (eventCfg.site_pays.kind === "percent_of_amount" && payment_amount_sats == null) {
|
|
324
|
+
return {
|
|
325
|
+
ok: false,
|
|
326
|
+
expected: null,
|
|
327
|
+
issues: [
|
|
328
|
+
{
|
|
329
|
+
field: "percent_amount_missing",
|
|
330
|
+
expected: null,
|
|
331
|
+
actual: null,
|
|
332
|
+
message: `subtype ${envelope.subtype} is percent_of_amount-priced; payment_amount_sats is required to verify`
|
|
333
|
+
}
|
|
334
|
+
]
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
const expected = computeFees(eventCfg, payment_amount_sats);
|
|
338
|
+
if (envelope.gross_fee_sats !== expected.gross_fee_sats) {
|
|
339
|
+
issues.push({
|
|
340
|
+
field: "gross_fee_sats",
|
|
341
|
+
expected: expected.gross_fee_sats,
|
|
342
|
+
actual: envelope.gross_fee_sats,
|
|
343
|
+
message: `gross_fee_sats mismatch \xB7 expected ${expected.gross_fee_sats}, recorded ${envelope.gross_fee_sats}`
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
if (envelope.platform_fee_sats !== expected.platform_fee_sats) {
|
|
347
|
+
issues.push({
|
|
348
|
+
field: "platform_fee_sats",
|
|
349
|
+
expected: expected.platform_fee_sats,
|
|
350
|
+
actual: envelope.platform_fee_sats,
|
|
351
|
+
message: `platform_fee_sats mismatch \xB7 expected ${expected.platform_fee_sats}, recorded ${envelope.platform_fee_sats}`
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
if (envelope.user_earned_sats !== expected.user_earned_sats) {
|
|
355
|
+
issues.push({
|
|
356
|
+
field: "user_earned_sats",
|
|
357
|
+
expected: expected.user_earned_sats,
|
|
358
|
+
actual: envelope.user_earned_sats,
|
|
359
|
+
message: `user_earned_sats mismatch \xB7 expected ${expected.user_earned_sats}, recorded ${envelope.user_earned_sats}`
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
if (envelope.site_rebate_sats !== expected.site_rebate_sats) {
|
|
363
|
+
issues.push({
|
|
364
|
+
field: "site_rebate_sats",
|
|
365
|
+
expected: expected.site_rebate_sats,
|
|
366
|
+
actual: envelope.site_rebate_sats,
|
|
367
|
+
message: `site_rebate_sats mismatch \xB7 expected ${expected.site_rebate_sats}, recorded ${envelope.site_rebate_sats}`
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
return {
|
|
371
|
+
ok: issues.length === 0,
|
|
372
|
+
expected,
|
|
373
|
+
issues
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
var event = { fire, fireBatch, verify };
|
|
377
|
+
|
|
277
378
|
// src/config.ts
|
|
278
379
|
function validate(cfg) {
|
|
279
380
|
return validateIntegratorConfig(cfg);
|
|
@@ -578,7 +679,7 @@ async function verifyEd25519(key, sig, msg) {
|
|
|
578
679
|
return false;
|
|
579
680
|
}
|
|
580
681
|
}
|
|
581
|
-
async function
|
|
682
|
+
async function verify2(rawBody, sigHex, kid, options = {}) {
|
|
582
683
|
if (!sigHex) return { ok: false, reason: "missing signature" };
|
|
583
684
|
if (!kid) return { ok: false, reason: "missing kid" };
|
|
584
685
|
const sig = hexToBytes(sigHex);
|
|
@@ -617,7 +718,7 @@ async function fetchJwksForce(issuer = DEFAULT_ISSUER) {
|
|
|
617
718
|
jwksCache.delete(issuer);
|
|
618
719
|
await fetchJwks(issuer, DEFAULT_TTL_MS);
|
|
619
720
|
}
|
|
620
|
-
var webhook = { verify, fetchJwks: fetchJwksForce };
|
|
721
|
+
var webhook = { verify: verify2, fetchJwks: fetchJwksForce };
|
|
621
722
|
|
|
622
723
|
// src/federations.ts
|
|
623
724
|
async function list() {
|
|
@@ -638,9 +739,60 @@ async function defaultLive() {
|
|
|
638
739
|
}
|
|
639
740
|
var federations = { list, live, get, defaultLive };
|
|
640
741
|
|
|
742
|
+
// src/scope.ts
|
|
743
|
+
var VALID_SCOPES = [
|
|
744
|
+
"bitcoin_address",
|
|
745
|
+
"email",
|
|
746
|
+
"attest_tier",
|
|
747
|
+
"display_name"
|
|
748
|
+
];
|
|
749
|
+
async function granted(options) {
|
|
750
|
+
if (!options.project_key) {
|
|
751
|
+
throw new Error("oc.scope.granted: project_key is required");
|
|
752
|
+
}
|
|
753
|
+
const r = await api(
|
|
754
|
+
`/api/integrator/scopes/${encodeURIComponent(options.project_key)}`,
|
|
755
|
+
{ method: "GET", signal: options.signal }
|
|
756
|
+
);
|
|
757
|
+
return {
|
|
758
|
+
sub: r.sub,
|
|
759
|
+
scopes_granted: r.scopes_granted,
|
|
760
|
+
scopes: r.scopes
|
|
761
|
+
};
|
|
762
|
+
}
|
|
763
|
+
function request(scopes, options) {
|
|
764
|
+
if (!options.project_key) {
|
|
765
|
+
throw new Error("oc.scope.request: project_key is required");
|
|
766
|
+
}
|
|
767
|
+
if (!options.return_to) {
|
|
768
|
+
throw new Error("oc.scope.request: return_to is required");
|
|
769
|
+
}
|
|
770
|
+
if (!Array.isArray(scopes) || scopes.length === 0) {
|
|
771
|
+
throw new Error("oc.scope.request: scopes must be a non-empty array");
|
|
772
|
+
}
|
|
773
|
+
for (const s of scopes) {
|
|
774
|
+
if (!VALID_SCOPES.includes(s)) {
|
|
775
|
+
throw new Error(
|
|
776
|
+
`oc.scope.request: unknown scope "${s}" \xB7 valid: ${VALID_SCOPES.join(", ")}`
|
|
777
|
+
);
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
if (typeof window === "undefined") {
|
|
781
|
+
throw new Error("oc.scope.request: must be called in a browser environment");
|
|
782
|
+
}
|
|
783
|
+
const origin2 = options.origin ?? getOrigin();
|
|
784
|
+
const url = new URL("/me/scope-grant", origin2);
|
|
785
|
+
url.searchParams.set("project_key", options.project_key);
|
|
786
|
+
url.searchParams.set("scopes", scopes.join(","));
|
|
787
|
+
url.searchParams.set("return_to", options.return_to);
|
|
788
|
+
window.location.assign(url.toString());
|
|
789
|
+
throw new Error("oc.scope.request: redirected to consent prompt");
|
|
790
|
+
}
|
|
791
|
+
var scope = { granted, request };
|
|
792
|
+
|
|
641
793
|
// src/index.ts
|
|
642
|
-
var oc = { session, payment, event, config, webhook, federations };
|
|
794
|
+
var oc = { session, payment, event, config, webhook, federations, scope };
|
|
643
795
|
|
|
644
|
-
export { MIN_INTEGRATOR_PRICE_SATS, MeClientError, OcSignInButton, PLATFORM_FEE_POLICY, clearBearerToken, computeFees, config, event, federations, getBearerToken, getOrigin, oc, onTelemetry, payment, session, setBearerToken, setOrigin, validateIntegratorConfig, webhook };
|
|
796
|
+
export { MIN_INTEGRATOR_PRICE_SATS, MeClientError, OcSignInButton, PLATFORM_FEE_POLICY, clearBearerToken, computeFees, config, event, federations, getBearerToken, getOrigin, oc, onTelemetry, payment, scope, session, setBearerToken, setOrigin, validateIntegratorConfig, webhook };
|
|
645
797
|
//# sourceMappingURL=index.mjs.map
|
|
646
798
|
//# sourceMappingURL=index.mjs.map
|