@ab-org/predicate-market-sdk 2.0.0 → 2.0.1-beta.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,149 @@
1
+ import { getOptionalEnv, setMerchantBaseUrl } from './chunk-SHLNBZBY.js';
2
+ import { getSDKConfig, initSDK } from './chunk-WHTI52FI.js';
3
+ import axios from 'axios';
4
+
5
+ // src/auth/bundledConfig.ts
6
+ function getBundledAuthConfig() {
7
+ const relayOrigin = getOptionalEnv("RELAY_ORIGIN");
8
+ const cubeEnv = getOptionalEnv("CUBE_SIGNER_ENV");
9
+ const cubeOrgId = getOptionalEnv("CUBE_SIGNER_ORG_ID");
10
+ return {
11
+ googleClientId: getOptionalEnv("GOOGLE_CLIENT_ID"),
12
+ twitterClientId: getOptionalEnv("X_CLIENT_ID"),
13
+ twitterRedirectUri: relayOrigin ? `${relayOrigin}/auth/twitter-callback` : void 0,
14
+ cubeSigner: cubeEnv && cubeOrgId ? {
15
+ env: cubeEnv,
16
+ orgId: cubeOrgId
17
+ } : void 0
18
+ };
19
+ }
20
+ function resolveSignerApiRoot(env) {
21
+ if (!env) return "https://gamma.signer.cubist.dev";
22
+ if (/^https?:\/\//.test(env)) return env.replace(/\/$/, "");
23
+ if (env === "gamma" || env === "dev") return "https://gamma.signer.cubist.dev";
24
+ if (env === "prod") return "https://prod.signer.cubist.dev";
25
+ return "https://gamma.signer.cubist.dev";
26
+ }
27
+ async function sha256Hex(input) {
28
+ const g = globalThis;
29
+ try {
30
+ if (g.crypto?.subtle) {
31
+ const enc = new TextEncoder();
32
+ const buf = await g.crypto.subtle.digest("SHA-256", enc.encode(input));
33
+ const bytes = new Uint8Array(buf);
34
+ return Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join("");
35
+ }
36
+ } catch {
37
+ }
38
+ try {
39
+ const { createHash } = await import('crypto');
40
+ return createHash("sha256").update(input).digest("hex");
41
+ } catch {
42
+ throw new Error("SHA-256 not available in this runtime");
43
+ }
44
+ }
45
+ function pickStageSalt() {
46
+ const stage = getOptionalEnv("STAGE") ?? "dev";
47
+ if (stage === "prod" || stage === "pre") return "xnPWRJT5XG2WyevuydMjMpZq";
48
+ return "dev@tomo";
49
+ }
50
+ async function defaultRegisterUser(oidcToken) {
51
+ const env = getOptionalEnv("CUBE_SIGNER_ENV") ?? "gamma";
52
+ const orgId = getOptionalEnv("CUBE_SIGNER_ORG_ID") ?? "";
53
+ const registerApi = getOptionalEnv("CUBE_REG") ?? "";
54
+ if (!orgId) throw new Error("CUBE_SIGNER_ORG_ID is not set");
55
+ if (!registerApi) throw new Error("CUBE_REG is not set");
56
+ const root = resolveSignerApiRoot(env);
57
+ const proveUrl = `${root}/v0/org/${encodeURIComponent(orgId)}/identity/prove/oidc`;
58
+ const proveRes = await axios.post(proveUrl, void 0, {
59
+ headers: { Authorization: oidcToken },
60
+ timeout: 2e4
61
+ });
62
+ const proof = proveRes.data ?? {};
63
+ const identity_proof = {
64
+ id: proof?.user_info?.user_id,
65
+ aud: proof?.aud,
66
+ email: proof?.email,
67
+ username: proof?.preferred_username,
68
+ identity: { iss: proof?.identity?.iss, sub: proof?.identity?.sub },
69
+ exp_epoch: proof?.exp_epoch
70
+ };
71
+ const timestamp = Date.now().toString();
72
+ const sigPayload = JSON.stringify({ iss: identity_proof.identity.iss, sub: identity_proof.identity.sub, timestamp });
73
+ const signature = await sha256Hex(sigPayload + pickStageSalt());
74
+ const regRes = await axios.post(
75
+ registerApi,
76
+ { identity_proof, timestamp, signature },
77
+ { headers: { "Content-Type": "application/json" }, timeout: 2e4 }
78
+ );
79
+ const body = regRes.data ?? {};
80
+ if (typeof body.code !== "undefined" && body.code !== 0) {
81
+ throw new Error(`User register failed: ${body?.message || body?.error || body.code}`);
82
+ }
83
+ }
84
+
85
+ // src/auth/config.ts
86
+ function getFixedAuthConfig() {
87
+ const bundled = getBundledAuthConfig();
88
+ const envGoogle = getOptionalEnv("GOOGLE_CLIENT_ID");
89
+ const envTwitter = getOptionalEnv("X_CLIENT_ID");
90
+ const relayOrigin = getOptionalEnv("RELAY_ORIGIN");
91
+ const envRedirect = relayOrigin ? `${relayOrigin}/auth/twitter-callback` : void 0;
92
+ const cubeEnv = getOptionalEnv("CUBE_SIGNER_ENV");
93
+ const cubeOrgId = getOptionalEnv("CUBE_SIGNER_ORG_ID");
94
+ const cubeSignerFromEnv = cubeEnv && cubeOrgId ? { env: cubeEnv, orgId: cubeOrgId } : void 0;
95
+ return {
96
+ googleClientId: envGoogle || bundled.googleClientId,
97
+ twitterClientId: envTwitter || bundled.twitterClientId,
98
+ twitterRedirectUri: envRedirect || bundled.twitterRedirectUri,
99
+ cubeSigner: cubeSignerFromEnv ?? bundled.cubeSigner
100
+ };
101
+ }
102
+ function scheduleAutoReconnect() {
103
+ if (typeof window === "undefined") return;
104
+ void import('./autoReconnect-6YV7YSSL.js').then(({ tryAutoReconnect }) => tryAutoReconnect()).catch(() => {
105
+ });
106
+ }
107
+ function initSDK2(config = {}) {
108
+ const { merchantBaseUrl, registerUser, ...rest } = config;
109
+ const fixed = getFixedAuthConfig();
110
+ const prev = getSDKConfig();
111
+ const merged = {
112
+ ...prev,
113
+ ...fixed,
114
+ ...rest,
115
+ cubeSigner: rest.cubeSigner ?? fixed.cubeSigner ?? prev.cubeSigner
116
+ };
117
+ if (registerUser != null && merged.cubeSigner) {
118
+ merged.cubeSigner = {
119
+ ...merged.cubeSigner,
120
+ oidcLoginHooks: {
121
+ ...merged.cubeSigner.oidcLoginHooks,
122
+ registerUser
123
+ }
124
+ };
125
+ }
126
+ if (registerUser == null && merged.cubeSigner) {
127
+ const hasCubeReg = Boolean(getOptionalEnv("CUBE_REG"));
128
+ const alreadyHas = Boolean(merged.cubeSigner.oidcLoginHooks?.registerUser);
129
+ if (hasCubeReg && alreadyHas == false) {
130
+ merged.cubeSigner = {
131
+ ...merged.cubeSigner,
132
+ oidcLoginHooks: {
133
+ ...merged.cubeSigner.oidcLoginHooks,
134
+ registerUser: defaultRegisterUser
135
+ }
136
+ };
137
+ }
138
+ }
139
+ if (merchantBaseUrl) {
140
+ setMerchantBaseUrl(merchantBaseUrl);
141
+ }
142
+ initSDK(merged);
143
+ scheduleAutoReconnect();
144
+ }
145
+ function getSDKConfig2() {
146
+ return getSDKConfig();
147
+ }
148
+
149
+ export { getFixedAuthConfig, getSDKConfig2 as getSDKConfig, initSDK2 as initSDK };
@@ -0,0 +1,72 @@
1
+ // src/utils/env.ts
2
+ function pick(...candidates) {
3
+ for (const candidate of candidates) {
4
+ if (candidate != null && candidate !== "") return candidate;
5
+ }
6
+ return void 0;
7
+ }
8
+ function nextPublicProcessEnvKey(logicalKey) {
9
+ return `NEXT_PUBLIC_${logicalKey}`;
10
+ }
11
+ function readProcessEnvStatic(key) {
12
+ if (typeof process === "undefined" || !process.env) return void 0;
13
+ switch (key) {
14
+ case "STAGE":
15
+ return pick(process.env.NEXT_PUBLIC_STAGE, process.env.STAGE);
16
+ case "GOOGLE_CLIENT_ID":
17
+ return pick(process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID, process.env.GOOGLE_CLIENT_ID);
18
+ case "X_CLIENT_ID":
19
+ return pick(process.env.NEXT_PUBLIC_X_CLIENT_ID, process.env.X_CLIENT_ID);
20
+ case "MERCHANT_BASE_URL":
21
+ return pick(process.env.NEXT_PUBLIC_MERCHANT_BASE_URL, process.env.MERCHANT_BASE_URL);
22
+ case "CUBE_SIGNER_ENV":
23
+ return pick(process.env.NEXT_PUBLIC_CUBE_SIGNER_ENV, process.env.CUBE_SIGNER_ENV);
24
+ case "CUBE_SIGNER_ORG_ID":
25
+ return pick(process.env.NEXT_PUBLIC_CUBE_SIGNER_ORG_ID, process.env.CUBE_SIGNER_ORG_ID);
26
+ case "CUBE_REG":
27
+ return pick(process.env.NEXT_PUBLIC_CUBE_REG, process.env.CUBE_REG);
28
+ case "RELAY_ORIGIN":
29
+ return pick(process.env.NEXT_PUBLIC_RELAY_ORIGIN, process.env.RELAY_ORIGIN);
30
+ case "FUNDING_TOKEN_SYMBOL":
31
+ return pick(process.env.NEXT_PUBLIC_FUNDING_TOKEN_SYMBOL, process.env.FUNDING_TOKEN_SYMBOL) || "USDT";
32
+ default:
33
+ return void 0;
34
+ }
35
+ }
36
+ function readEnv(key) {
37
+ const staticVal = readProcessEnvStatic(key);
38
+ if (staticVal != null && staticVal !== "") return staticVal;
39
+ const globalEnv = globalThis.process?.env;
40
+ if (globalEnv) {
41
+ const pub = nextPublicProcessEnvKey(key);
42
+ const val = globalEnv[key] ?? globalEnv[pub];
43
+ if (val != null && val !== "") return val;
44
+ }
45
+ try {
46
+ const metaEnv = import.meta.env;
47
+ if (metaEnv) {
48
+ const pub = nextPublicProcessEnvKey(key);
49
+ const val = metaEnv[key] ?? metaEnv[pub];
50
+ if (val != null && val !== "") return val;
51
+ }
52
+ } catch {
53
+ }
54
+ return void 0;
55
+ }
56
+ function getOptionalEnv(key) {
57
+ return readEnv(key);
58
+ }
59
+ function getEnv(key) {
60
+ return readEnv(key) ?? "";
61
+ }
62
+
63
+ // src/modules/apiConfig.ts
64
+ var configuredMerchantBaseUrl;
65
+ function setMerchantBaseUrl(baseUrl) {
66
+ configuredMerchantBaseUrl = baseUrl && baseUrl.trim() !== "" ? baseUrl : void 0;
67
+ }
68
+ function getMerchantBaseUrl() {
69
+ return configuredMerchantBaseUrl ?? getOptionalEnv("MERCHANT_BASE_URL");
70
+ }
71
+
72
+ export { getEnv, getMerchantBaseUrl, getOptionalEnv, setMerchantBaseUrl };