@ab-org/sdk-core 0.1.0 → 0.1.2-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.
- package/README.md +29 -0
- package/dist/chunk-5HURLKIK.js +1005 -0
- package/dist/chunk-MVZIAM4H.js +363 -0
- package/dist/cubeSignerAuth-DrPc9FeB.d.ts +426 -0
- package/dist/index.d.ts +237 -16
- package/dist/index.js +1119 -16
- package/dist/social-auth.d.ts +11 -0
- package/dist/social-auth.js +1 -0
- package/dist/social-provider-Bq58TBRt.d.ts +88 -0
- package/dist/social-provider.d.ts +2 -0
- package/dist/social-provider.js +2 -0
- package/dist/social.d.ts +3 -0
- package/dist/social.js +2 -0
- package/package.json +24 -6
- package/dist/core/capabilities.d.ts +0 -32
- package/dist/core/capabilities.js +0 -88
- package/dist/core/chains.d.ts +0 -23
- package/dist/core/chains.js +0 -83
- package/dist/core/errors.d.ts +0 -9
- package/dist/core/errors.js +0 -51
- package/dist/core/sessionStore.d.ts +0 -26
- package/dist/core/sessionStore.js +0 -129
- package/dist/core/types.d.ts +0 -75
- package/dist/core/types.js +0 -1
- package/dist/core/walletConnector.d.ts +0 -29
- package/dist/core/walletConnector.js +0 -153
- package/dist/core/walletExecution.d.ts +0 -22
- package/dist/core/walletExecution.js +0 -89
- package/dist/hooks/useAccount.d.ts +0 -11
- package/dist/hooks/useAccount.js +0 -32
- package/dist/hooks/useWalletConnect.d.ts +0 -16
- package/dist/hooks/useWalletConnect.js +0 -24
- package/dist/providers/base.d.ts +0 -15
- package/dist/providers/base.js +0 -30
- package/dist/providers/plugin/injectedEvmProvider.d.ts +0 -112
- package/dist/providers/plugin/injectedEvmProvider.js +0 -352
- package/dist/providers/plugin/injectedWalletRegistry.d.ts +0 -9
- package/dist/providers/plugin/injectedWalletRegistry.js +0 -34
- package/dist/providers/plugin/metamaskProvider.d.ts +0 -1
- package/dist/providers/plugin/metamaskProvider.js +0 -1
- package/dist/providers/social/baseSocialProvider.d.ts +0 -21
- package/dist/providers/social/baseSocialProvider.js +0 -11
- package/dist/providers/social/cubeSignerAuth.d.ts +0 -89
- package/dist/providers/social/cubeSignerAuth.js +0 -128
- package/dist/providers/social/cubistEvmWalletProvider.d.ts +0 -9
- package/dist/providers/social/cubistEvmWalletProvider.js +0 -175
- package/dist/providers/social/cubistProvider.d.ts +0 -52
- package/dist/providers/social/cubistProvider.js +0 -160
|
@@ -0,0 +1,1005 @@
|
|
|
1
|
+
var __typeError = (msg) => {
|
|
2
|
+
throw TypeError(msg);
|
|
3
|
+
};
|
|
4
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
5
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
6
|
+
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
7
|
+
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), member.set(obj, value), value);
|
|
8
|
+
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
|
|
9
|
+
|
|
10
|
+
// src/providers/social/cubistRuntime.ts
|
|
11
|
+
var DEFAULT_EXPIRATION_BUFFER_SECS = 30;
|
|
12
|
+
var DEFAULT_RETRY_ATTEMPTS = 3;
|
|
13
|
+
var DEV_STACK_NAME = "Dev-CubeSignerStack";
|
|
14
|
+
var EVM_KEY_TYPE = "SecpEthAddr";
|
|
15
|
+
var EMPTY_ORG_EVENTS_TOPIC_ARN = "";
|
|
16
|
+
var CUBESIGNER_TOKEN_PREFIX = "3d6fd7397:";
|
|
17
|
+
var envs = {
|
|
18
|
+
prod: {
|
|
19
|
+
SignerApiRoot: "https://prod.signer.cubist.dev",
|
|
20
|
+
OrgEventsTopicArn: EMPTY_ORG_EVENTS_TOPIC_ARN
|
|
21
|
+
},
|
|
22
|
+
gamma: {
|
|
23
|
+
SignerApiRoot: "https://gamma.signer.cubist.dev",
|
|
24
|
+
OrgEventsTopicArn: EMPTY_ORG_EVENTS_TOPIC_ARN
|
|
25
|
+
},
|
|
26
|
+
beta: {
|
|
27
|
+
SignerApiRoot: "https://beta.signer.cubist.dev",
|
|
28
|
+
OrgEventsTopicArn: EMPTY_ORG_EVENTS_TOPIC_ARN
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
function delay(ms) {
|
|
32
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
33
|
+
}
|
|
34
|
+
function normalizeBaseUrl(baseUrl) {
|
|
35
|
+
return baseUrl.replace(/\/+$/, "");
|
|
36
|
+
}
|
|
37
|
+
function base64Decode(value) {
|
|
38
|
+
if (typeof globalThis.atob === "function") {
|
|
39
|
+
return globalThis.atob(value);
|
|
40
|
+
}
|
|
41
|
+
const maybeBuffer = globalThis.Buffer;
|
|
42
|
+
if (maybeBuffer) {
|
|
43
|
+
return maybeBuffer.from(value, "base64").toString("utf8");
|
|
44
|
+
}
|
|
45
|
+
throw new Error("No base64 decoder available in this runtime");
|
|
46
|
+
}
|
|
47
|
+
function base64UrlEncode(bytes) {
|
|
48
|
+
let encoded;
|
|
49
|
+
if (typeof globalThis.btoa === "function") {
|
|
50
|
+
let binary = "";
|
|
51
|
+
for (const byte of bytes) {
|
|
52
|
+
binary += String.fromCharCode(byte);
|
|
53
|
+
}
|
|
54
|
+
encoded = globalThis.btoa(binary);
|
|
55
|
+
} else {
|
|
56
|
+
const maybeBuffer = globalThis.Buffer;
|
|
57
|
+
if (!maybeBuffer) {
|
|
58
|
+
throw new Error("No base64 encoder available in this runtime");
|
|
59
|
+
}
|
|
60
|
+
encoded = maybeBuffer.from(bytes).toString("base64");
|
|
61
|
+
}
|
|
62
|
+
return encoded.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/g, "");
|
|
63
|
+
}
|
|
64
|
+
function isAcceptedResponse(value) {
|
|
65
|
+
return typeof value === "object" && value !== null && "accepted" in value;
|
|
66
|
+
}
|
|
67
|
+
function isManyMfaReceipts(value) {
|
|
68
|
+
if (!value || typeof value !== "object") return false;
|
|
69
|
+
return "orgId" in value && "receipts" in value;
|
|
70
|
+
}
|
|
71
|
+
function getMfaHeaders(mfaReceipt) {
|
|
72
|
+
if (!mfaReceipt) return void 0;
|
|
73
|
+
const normalized = isManyMfaReceipts(mfaReceipt) ? mfaReceipt : {
|
|
74
|
+
orgId: mfaReceipt.mfaOrgId,
|
|
75
|
+
receipts: [
|
|
76
|
+
{
|
|
77
|
+
id: mfaReceipt.mfaId,
|
|
78
|
+
confirmation: mfaReceipt.mfaConf
|
|
79
|
+
}
|
|
80
|
+
]
|
|
81
|
+
};
|
|
82
|
+
if (normalized.receipts.length === 0) return void 0;
|
|
83
|
+
const payload = new TextEncoder().encode(JSON.stringify(normalized.receipts));
|
|
84
|
+
return {
|
|
85
|
+
"x-cubist-mfa-org-id": normalized.orgId,
|
|
86
|
+
"x-cubist-mfa-receipts": base64UrlEncode(payload)
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
function isStale(session) {
|
|
90
|
+
return session.session_info.auth_token_exp < Date.now() / 1e3 + DEFAULT_EXPIRATION_BUFFER_SECS;
|
|
91
|
+
}
|
|
92
|
+
function parseSessionLike(session) {
|
|
93
|
+
if (typeof session !== "string") {
|
|
94
|
+
return session;
|
|
95
|
+
}
|
|
96
|
+
const trimmed = session.trim();
|
|
97
|
+
if (trimmed.startsWith("{")) {
|
|
98
|
+
return JSON.parse(trimmed);
|
|
99
|
+
}
|
|
100
|
+
return JSON.parse(base64Decode(trimmed));
|
|
101
|
+
}
|
|
102
|
+
function getSessionMetadata(session) {
|
|
103
|
+
return {
|
|
104
|
+
env: session.env,
|
|
105
|
+
org_id: session.org_id,
|
|
106
|
+
role_id: session.role_id,
|
|
107
|
+
purpose: session.purpose,
|
|
108
|
+
session_exp: session.session_exp,
|
|
109
|
+
session_id: session.session_info.session_id,
|
|
110
|
+
epoch: session.session_info.epoch
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
function getPrimarySessionEnv(session) {
|
|
114
|
+
return session.env[DEV_STACK_NAME] ?? Object.values(session.env)[0];
|
|
115
|
+
}
|
|
116
|
+
function readSessionInfoRefreshToken(session) {
|
|
117
|
+
const token = session.session_info.refresh_token;
|
|
118
|
+
return typeof token === "string" && token.trim() ? token.trim() : void 0;
|
|
119
|
+
}
|
|
120
|
+
function decodeSerializedRefreshToken(session) {
|
|
121
|
+
const serialized = session.refresh_token?.trim();
|
|
122
|
+
if (!serialized) {
|
|
123
|
+
return void 0;
|
|
124
|
+
}
|
|
125
|
+
if (!serialized.startsWith(CUBESIGNER_TOKEN_PREFIX)) {
|
|
126
|
+
return serialized;
|
|
127
|
+
}
|
|
128
|
+
const parts = serialized.slice(CUBESIGNER_TOKEN_PREFIX.length).split(".");
|
|
129
|
+
if (parts.length !== 3 || parts.some((part) => part.length === 0)) {
|
|
130
|
+
throw new Error("CubeSigner refresh token has an unexpected format");
|
|
131
|
+
}
|
|
132
|
+
const [encodedSessionId, encodedAuthData, rawRefreshToken] = parts;
|
|
133
|
+
const sessionId = base64Decode(encodedSessionId);
|
|
134
|
+
if (sessionId !== session.session_info.session_id) {
|
|
135
|
+
throw new Error("CubeSigner refresh token does not match the active session");
|
|
136
|
+
}
|
|
137
|
+
let authData;
|
|
138
|
+
try {
|
|
139
|
+
authData = JSON.parse(base64Decode(encodedAuthData));
|
|
140
|
+
} catch {
|
|
141
|
+
throw new Error("CubeSigner refresh token auth payload is invalid");
|
|
142
|
+
}
|
|
143
|
+
if (authData.epoch_num !== session.session_info.epoch || authData.epoch_token !== session.session_info.epoch_token) {
|
|
144
|
+
throw new Error("CubeSigner refresh token is out of sync with the active session");
|
|
145
|
+
}
|
|
146
|
+
return rawRefreshToken;
|
|
147
|
+
}
|
|
148
|
+
function resolveRefreshOtherToken(session) {
|
|
149
|
+
const nestedToken = readSessionInfoRefreshToken(session);
|
|
150
|
+
if (nestedToken) {
|
|
151
|
+
return nestedToken;
|
|
152
|
+
}
|
|
153
|
+
const fallbackToken = decodeSerializedRefreshToken(session);
|
|
154
|
+
if (fallbackToken) {
|
|
155
|
+
return fallbackToken;
|
|
156
|
+
}
|
|
157
|
+
throw new Error("CubeSigner session is missing refresh token data");
|
|
158
|
+
}
|
|
159
|
+
function assertSessionManager(session) {
|
|
160
|
+
if (!session || typeof session !== "object" || typeof session.metadata !== "function" || typeof session.token !== "function") {
|
|
161
|
+
throw new Error("Invalid CubeSigner session input");
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
var NetworkError = class extends Error {
|
|
165
|
+
constructor(message) {
|
|
166
|
+
super(message);
|
|
167
|
+
this.name = "NetworkError";
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
var HttpError = class extends Error {
|
|
171
|
+
constructor(status, message, body) {
|
|
172
|
+
super(message);
|
|
173
|
+
this.name = "HttpError";
|
|
174
|
+
this.status = status;
|
|
175
|
+
this.body = body;
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
async function requestJson(baseUrl, pathname, init) {
|
|
179
|
+
let lastError = null;
|
|
180
|
+
for (let attempt = 0; attempt < DEFAULT_RETRY_ATTEMPTS; attempt += 1) {
|
|
181
|
+
const response = await fetch(`${normalizeBaseUrl(baseUrl)}${pathname}`, init);
|
|
182
|
+
if (response.status >= 500 && response.status < 600 && attempt < DEFAULT_RETRY_ATTEMPTS - 1) {
|
|
183
|
+
await delay(200 * (attempt + 1));
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
if (!response.ok) {
|
|
187
|
+
const detail = await response.text().catch(() => "");
|
|
188
|
+
lastError = new HttpError(
|
|
189
|
+
response.status,
|
|
190
|
+
`${init.method ?? "GET"} ${pathname} failed (${response.status}): ${detail || response.statusText}`,
|
|
191
|
+
detail || void 0
|
|
192
|
+
);
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
195
|
+
if (response.status === 204) {
|
|
196
|
+
return void 0;
|
|
197
|
+
}
|
|
198
|
+
const rawText = await response.text();
|
|
199
|
+
if (!rawText) {
|
|
200
|
+
return void 0;
|
|
201
|
+
}
|
|
202
|
+
return JSON.parse(rawText);
|
|
203
|
+
}
|
|
204
|
+
if (!lastError) throw new NetworkError(`${init.method ?? "GET"} ${pathname} failed`);
|
|
205
|
+
throw lastError;
|
|
206
|
+
}
|
|
207
|
+
var _data, _refreshing;
|
|
208
|
+
var MemorySessionManager = class {
|
|
209
|
+
constructor(data) {
|
|
210
|
+
__privateAdd(this, _data);
|
|
211
|
+
__privateAdd(this, _refreshing);
|
|
212
|
+
__privateSet(this, _data, data);
|
|
213
|
+
}
|
|
214
|
+
async retrieve() {
|
|
215
|
+
return __privateGet(this, _data);
|
|
216
|
+
}
|
|
217
|
+
async store(data) {
|
|
218
|
+
__privateSet(this, _data, data);
|
|
219
|
+
}
|
|
220
|
+
async metadata() {
|
|
221
|
+
return getSessionMetadata(__privateGet(this, _data));
|
|
222
|
+
}
|
|
223
|
+
async token() {
|
|
224
|
+
if (!isStale(__privateGet(this, _data))) {
|
|
225
|
+
return __privateGet(this, _data).token;
|
|
226
|
+
}
|
|
227
|
+
__privateGet(this, _refreshing) ?? __privateSet(this, _refreshing, refreshSession(__privateGet(this, _data)).then(async (refreshed2) => {
|
|
228
|
+
await this.store(refreshed2);
|
|
229
|
+
return refreshed2;
|
|
230
|
+
}).catch((error) => {
|
|
231
|
+
if (error instanceof Error && /\(401\)|\(403\)/.test(error.message) && typeof this.onInvalidToken === "function") {
|
|
232
|
+
this.onInvalidToken();
|
|
233
|
+
}
|
|
234
|
+
throw error;
|
|
235
|
+
}).finally(() => {
|
|
236
|
+
__privateSet(this, _refreshing, void 0);
|
|
237
|
+
}));
|
|
238
|
+
const refreshed = await __privateGet(this, _refreshing);
|
|
239
|
+
return refreshed.token;
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
_data = new WeakMap();
|
|
243
|
+
_refreshing = new WeakMap();
|
|
244
|
+
async function refreshSession(session1) {
|
|
245
|
+
const secretB64Token = btoa(JSON.stringify(session1)).replace(/\s/g, "");
|
|
246
|
+
const session = JSON.parse(atob(secretB64Token));
|
|
247
|
+
const env = getPrimarySessionEnv(session);
|
|
248
|
+
const payload = {
|
|
249
|
+
epoch_num: session.session_info.epoch,
|
|
250
|
+
epoch_token: session.session_info.epoch_token,
|
|
251
|
+
other_token: resolveRefreshOtherToken(session)
|
|
252
|
+
};
|
|
253
|
+
const refreshed = await requestJson(env.SignerApiRoot, `/v1/org/${encodeURIComponent(session.org_id)}/token/refresh`, {
|
|
254
|
+
method: "PATCH",
|
|
255
|
+
headers: {
|
|
256
|
+
Authorization: session.token,
|
|
257
|
+
"Content-Type": "application/json"
|
|
258
|
+
},
|
|
259
|
+
body: JSON.stringify(payload)
|
|
260
|
+
});
|
|
261
|
+
return {
|
|
262
|
+
...session,
|
|
263
|
+
...refreshed,
|
|
264
|
+
org_id: session.org_id
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
var _sessionManager, _sessionMeta, _targetOrgId, _CubistApiClient_instances, request_fn;
|
|
268
|
+
var _CubistApiClient = class _CubistApiClient {
|
|
269
|
+
constructor(sessionManager, sessionMeta, targetOrgId) {
|
|
270
|
+
__privateAdd(this, _CubistApiClient_instances);
|
|
271
|
+
__privateAdd(this, _sessionManager);
|
|
272
|
+
__privateAdd(this, _sessionMeta);
|
|
273
|
+
__privateAdd(this, _targetOrgId);
|
|
274
|
+
__privateSet(this, _sessionManager, sessionManager);
|
|
275
|
+
__privateSet(this, _sessionMeta, sessionMeta);
|
|
276
|
+
__privateSet(this, _targetOrgId, targetOrgId ?? sessionMeta.org_id);
|
|
277
|
+
}
|
|
278
|
+
get env() {
|
|
279
|
+
return __privateGet(this, _sessionMeta).env[DEV_STACK_NAME] ?? Object.values(__privateGet(this, _sessionMeta).env)[0];
|
|
280
|
+
}
|
|
281
|
+
get orgId() {
|
|
282
|
+
return __privateGet(this, _targetOrgId);
|
|
283
|
+
}
|
|
284
|
+
get sessionMeta() {
|
|
285
|
+
return __privateGet(this, _sessionMeta);
|
|
286
|
+
}
|
|
287
|
+
withOrg(orgId) {
|
|
288
|
+
return new _CubistApiClient(__privateGet(this, _sessionManager), __privateGet(this, _sessionMeta), orgId);
|
|
289
|
+
}
|
|
290
|
+
async sessionKeysList() {
|
|
291
|
+
const data = await __privateMethod(this, _CubistApiClient_instances, request_fn).call(this, "/v0/org/{org_id}/token/keys", {
|
|
292
|
+
method: "GET"
|
|
293
|
+
});
|
|
294
|
+
return data.keys;
|
|
295
|
+
}
|
|
296
|
+
async keyGetByMaterialId(keyType, materialId) {
|
|
297
|
+
return __privateMethod(this, _CubistApiClient_instances, request_fn).call(this, `/v0/org/{org_id}/keys/${encodeURIComponent(keyType)}/${encodeURIComponent(materialId)}`, {
|
|
298
|
+
method: "GET"
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
async sessionRevoke(sessionId) {
|
|
302
|
+
await __privateMethod(this, _CubistApiClient_instances, request_fn).call(this, `/v0/org/{org_id}/session/${encodeURIComponent(sessionId ?? "self")}`, {
|
|
303
|
+
method: "DELETE"
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
async mfaGet(mfaId) {
|
|
307
|
+
return __privateMethod(this, _CubistApiClient_instances, request_fn).call(this, `/v0/org/{org_id}/mfa/${encodeURIComponent(mfaId)}`, {
|
|
308
|
+
method: "GET"
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
async mfaVoteCs(mfaId, mfaVote) {
|
|
312
|
+
return __privateMethod(this, _CubistApiClient_instances, request_fn).call(this, `/v0/org/{org_id}/mfa/${encodeURIComponent(mfaId)}?mfa_vote=${encodeURIComponent(mfaVote)}`, {
|
|
313
|
+
method: "PATCH"
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
async signEvm(key, req, mfaReceipt) {
|
|
317
|
+
const pubkey = typeof key === "string" ? key : key.materialId;
|
|
318
|
+
const requestFn = async (headers) => __privateMethod(this, _CubistApiClient_instances, request_fn).call(this, `/v1/org/{org_id}/eth1/sign/${encodeURIComponent(pubkey)}`, {
|
|
319
|
+
method: "POST",
|
|
320
|
+
body: req,
|
|
321
|
+
headers
|
|
322
|
+
});
|
|
323
|
+
return CubeSignerResponse.create(this.env, requestFn, mfaReceipt);
|
|
324
|
+
}
|
|
325
|
+
async signEip191(key, req, mfaReceipt) {
|
|
326
|
+
const pubkey = typeof key === "string" ? key : key.materialId;
|
|
327
|
+
const requestFn = async (headers) => __privateMethod(this, _CubistApiClient_instances, request_fn).call(this, `/v0/org/{org_id}/evm/eip191/sign/${encodeURIComponent(pubkey)}`, {
|
|
328
|
+
method: "POST",
|
|
329
|
+
body: req,
|
|
330
|
+
headers
|
|
331
|
+
});
|
|
332
|
+
return CubeSignerResponse.create(this.env, requestFn, mfaReceipt);
|
|
333
|
+
}
|
|
334
|
+
async signEip712(key, req, mfaReceipt) {
|
|
335
|
+
const pubkey = typeof key === "string" ? key : key.materialId;
|
|
336
|
+
const requestFn = async (headers) => __privateMethod(this, _CubistApiClient_instances, request_fn).call(this, `/v0/org/{org_id}/evm/eip712/sign/${encodeURIComponent(pubkey)}`, {
|
|
337
|
+
method: "POST",
|
|
338
|
+
body: req,
|
|
339
|
+
headers
|
|
340
|
+
});
|
|
341
|
+
return CubeSignerResponse.create(this.env, requestFn, mfaReceipt);
|
|
342
|
+
}
|
|
343
|
+
};
|
|
344
|
+
_sessionManager = new WeakMap();
|
|
345
|
+
_sessionMeta = new WeakMap();
|
|
346
|
+
_targetOrgId = new WeakMap();
|
|
347
|
+
_CubistApiClient_instances = new WeakSet();
|
|
348
|
+
request_fn = async function(pathTemplate, options) {
|
|
349
|
+
let token;
|
|
350
|
+
try {
|
|
351
|
+
token = await __privateGet(this, _sessionManager).token();
|
|
352
|
+
} catch (error) {
|
|
353
|
+
if (error instanceof Error && /\(401\)|\(403\)/.test(error.message) && typeof __privateGet(this, _sessionManager).onInvalidToken === "function") {
|
|
354
|
+
__privateGet(this, _sessionManager).onInvalidToken();
|
|
355
|
+
}
|
|
356
|
+
throw error;
|
|
357
|
+
}
|
|
358
|
+
const headers = new Headers(options.headers);
|
|
359
|
+
headers.set("Authorization", token);
|
|
360
|
+
if (options.body !== void 0) {
|
|
361
|
+
headers.set("Content-Type", "application/json");
|
|
362
|
+
}
|
|
363
|
+
try {
|
|
364
|
+
return await requestJson(
|
|
365
|
+
this.env.SignerApiRoot,
|
|
366
|
+
pathTemplate.replace("{org_id}", encodeURIComponent(__privateGet(this, _targetOrgId))),
|
|
367
|
+
{
|
|
368
|
+
method: options.method,
|
|
369
|
+
headers,
|
|
370
|
+
body: options.body === void 0 ? void 0 : JSON.stringify(options.body)
|
|
371
|
+
}
|
|
372
|
+
);
|
|
373
|
+
} catch (error) {
|
|
374
|
+
if (error instanceof Error && /\(401\)|\(403\)/.test(error.message) && typeof __privateGet(this, _sessionManager).onInvalidToken === "function") {
|
|
375
|
+
__privateGet(this, _sessionManager).onInvalidToken();
|
|
376
|
+
}
|
|
377
|
+
throw error;
|
|
378
|
+
}
|
|
379
|
+
};
|
|
380
|
+
var CubistApiClient = _CubistApiClient;
|
|
381
|
+
var _apiClient;
|
|
382
|
+
var Key = class {
|
|
383
|
+
constructor(client, data) {
|
|
384
|
+
__privateAdd(this, _apiClient);
|
|
385
|
+
__privateSet(this, _apiClient, client instanceof CubeSignerClient ? client.apiClient : client);
|
|
386
|
+
this.id = data.key_id;
|
|
387
|
+
this.materialId = data.material_id;
|
|
388
|
+
this.publicKey = data.public_key;
|
|
389
|
+
this.cached = data;
|
|
390
|
+
}
|
|
391
|
+
async signEvm(req, mfaReceipt) {
|
|
392
|
+
return __privateGet(this, _apiClient).signEvm(this, req, mfaReceipt);
|
|
393
|
+
}
|
|
394
|
+
async signEip191(req, mfaReceipt) {
|
|
395
|
+
return __privateGet(this, _apiClient).signEip191(this, req, mfaReceipt);
|
|
396
|
+
}
|
|
397
|
+
async signEip712(req, mfaReceipt) {
|
|
398
|
+
return __privateGet(this, _apiClient).signEip712(this, req, mfaReceipt);
|
|
399
|
+
}
|
|
400
|
+
};
|
|
401
|
+
_apiClient = new WeakMap();
|
|
402
|
+
var _apiClient2, _id, _data2;
|
|
403
|
+
var _MfaRequest = class _MfaRequest {
|
|
404
|
+
constructor(apiClient, data) {
|
|
405
|
+
__privateAdd(this, _apiClient2);
|
|
406
|
+
__privateAdd(this, _id);
|
|
407
|
+
__privateAdd(this, _data2);
|
|
408
|
+
__privateSet(this, _apiClient2, apiClient);
|
|
409
|
+
if (typeof data === "string") {
|
|
410
|
+
__privateSet(this, _id, data);
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
__privateSet(this, _id, data.id);
|
|
414
|
+
__privateSet(this, _data2, data);
|
|
415
|
+
}
|
|
416
|
+
get id() {
|
|
417
|
+
return __privateGet(this, _id);
|
|
418
|
+
}
|
|
419
|
+
async fetch() {
|
|
420
|
+
__privateSet(this, _data2, await __privateGet(this, _apiClient2).mfaGet(__privateGet(this, _id)));
|
|
421
|
+
return __privateGet(this, _data2);
|
|
422
|
+
}
|
|
423
|
+
async receipt() {
|
|
424
|
+
const data = __privateGet(this, _data2) ?? await this.fetch();
|
|
425
|
+
const confirmation = data.receipt?.confirmation;
|
|
426
|
+
if (!confirmation) return void 0;
|
|
427
|
+
return {
|
|
428
|
+
mfaId: __privateGet(this, _id),
|
|
429
|
+
mfaOrgId: __privateGet(this, _apiClient2).orgId,
|
|
430
|
+
mfaConf: confirmation
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
async approve() {
|
|
434
|
+
const data = await __privateGet(this, _apiClient2).mfaVoteCs(__privateGet(this, _id), "approve");
|
|
435
|
+
return new _MfaRequest(__privateGet(this, _apiClient2), data);
|
|
436
|
+
}
|
|
437
|
+
};
|
|
438
|
+
_apiClient2 = new WeakMap();
|
|
439
|
+
_id = new WeakMap();
|
|
440
|
+
_data2 = new WeakMap();
|
|
441
|
+
var MfaRequest = _MfaRequest;
|
|
442
|
+
var _apiClient3;
|
|
443
|
+
var OrgClient = class {
|
|
444
|
+
constructor(apiClient) {
|
|
445
|
+
__privateAdd(this, _apiClient3);
|
|
446
|
+
__privateSet(this, _apiClient3, apiClient);
|
|
447
|
+
}
|
|
448
|
+
get id() {
|
|
449
|
+
return __privateGet(this, _apiClient3).orgId;
|
|
450
|
+
}
|
|
451
|
+
getMfaRequest(mfaId) {
|
|
452
|
+
return new MfaRequest(__privateGet(this, _apiClient3), mfaId);
|
|
453
|
+
}
|
|
454
|
+
};
|
|
455
|
+
_apiClient3 = new WeakMap();
|
|
456
|
+
var _env, _requestFn, _resp, _mfaRequired;
|
|
457
|
+
var _CubeSignerResponse = class _CubeSignerResponse {
|
|
458
|
+
constructor(env, requestFn, resp) {
|
|
459
|
+
__privateAdd(this, _env);
|
|
460
|
+
__privateAdd(this, _requestFn);
|
|
461
|
+
__privateAdd(this, _resp);
|
|
462
|
+
__privateAdd(this, _mfaRequired);
|
|
463
|
+
__privateSet(this, _env, env);
|
|
464
|
+
__privateSet(this, _requestFn, requestFn);
|
|
465
|
+
__privateSet(this, _resp, resp);
|
|
466
|
+
__privateSet(this, _mfaRequired, isAcceptedResponse(resp) ? resp.accepted?.MfaRequired : void 0);
|
|
467
|
+
}
|
|
468
|
+
static async create(env, requestFn, mfaReceipt) {
|
|
469
|
+
const seed = await requestFn(getMfaHeaders(mfaReceipt));
|
|
470
|
+
return new _CubeSignerResponse(env, requestFn, seed);
|
|
471
|
+
}
|
|
472
|
+
mfaId() {
|
|
473
|
+
return __privateGet(this, _mfaRequired)?.id ?? __privateGet(this, _mfaRequired)?.ids?.[0];
|
|
474
|
+
}
|
|
475
|
+
mfaIds() {
|
|
476
|
+
if (!__privateGet(this, _mfaRequired)) return [];
|
|
477
|
+
if (__privateGet(this, _mfaRequired).ids?.length) return __privateGet(this, _mfaRequired).ids;
|
|
478
|
+
return __privateGet(this, _mfaRequired).id ? [__privateGet(this, _mfaRequired).id] : [];
|
|
479
|
+
}
|
|
480
|
+
requiresMfa() {
|
|
481
|
+
return __privateGet(this, _mfaRequired) !== void 0;
|
|
482
|
+
}
|
|
483
|
+
async mfaClient() {
|
|
484
|
+
const session = __privateGet(this, _mfaRequired)?.session;
|
|
485
|
+
if (!session) return void 0;
|
|
486
|
+
return CubeSignerClient.create({
|
|
487
|
+
env: {
|
|
488
|
+
[DEV_STACK_NAME]: __privateGet(this, _env)
|
|
489
|
+
},
|
|
490
|
+
org_id: __privateGet(this, _mfaRequired)?.org_id ?? "",
|
|
491
|
+
token: session.token,
|
|
492
|
+
refresh_token: session.refresh_token,
|
|
493
|
+
session_exp: session.expiration,
|
|
494
|
+
session_info: session.session_info
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
data() {
|
|
498
|
+
if (this.requiresMfa()) {
|
|
499
|
+
throw new Error("Cannot call `data()` while MFA is required");
|
|
500
|
+
}
|
|
501
|
+
return __privateGet(this, _resp);
|
|
502
|
+
}
|
|
503
|
+
async approve(client) {
|
|
504
|
+
const mfaId = this.mfaId();
|
|
505
|
+
if (!mfaId || !__privateGet(this, _mfaRequired)) return this;
|
|
506
|
+
const approval = await client.apiClient.mfaVoteCs(mfaId, "approve");
|
|
507
|
+
const confirmation = approval.receipt?.confirmation;
|
|
508
|
+
if (!confirmation) return this;
|
|
509
|
+
return this.execWithMfaApproval({
|
|
510
|
+
mfaId,
|
|
511
|
+
mfaOrgId: __privateGet(this, _mfaRequired).org_id,
|
|
512
|
+
mfaConf: confirmation
|
|
513
|
+
});
|
|
514
|
+
}
|
|
515
|
+
async execWithMfaApproval(mfaReceipt) {
|
|
516
|
+
return new _CubeSignerResponse(
|
|
517
|
+
__privateGet(this, _env),
|
|
518
|
+
__privateGet(this, _requestFn),
|
|
519
|
+
await __privateGet(this, _requestFn).call(this, getMfaHeaders(mfaReceipt))
|
|
520
|
+
);
|
|
521
|
+
}
|
|
522
|
+
};
|
|
523
|
+
_env = new WeakMap();
|
|
524
|
+
_requestFn = new WeakMap();
|
|
525
|
+
_resp = new WeakMap();
|
|
526
|
+
_mfaRequired = new WeakMap();
|
|
527
|
+
var CubeSignerResponse = _CubeSignerResponse;
|
|
528
|
+
var _apiClient4;
|
|
529
|
+
var _CubeSignerClient = class _CubeSignerClient {
|
|
530
|
+
constructor(apiClient) {
|
|
531
|
+
__privateAdd(this, _apiClient4);
|
|
532
|
+
__privateSet(this, _apiClient4, apiClient);
|
|
533
|
+
}
|
|
534
|
+
get apiClient() {
|
|
535
|
+
return __privateGet(this, _apiClient4);
|
|
536
|
+
}
|
|
537
|
+
get env() {
|
|
538
|
+
return __privateGet(this, _apiClient4).env;
|
|
539
|
+
}
|
|
540
|
+
get orgId() {
|
|
541
|
+
return __privateGet(this, _apiClient4).orgId;
|
|
542
|
+
}
|
|
543
|
+
static async create(session, targetOrgId) {
|
|
544
|
+
const created = typeof session === "string" || isSessionData(session) ? new MemorySessionManager(parseSessionLike(session)) : session;
|
|
545
|
+
if (typeof session === "object" && session !== null && session.onInvalidToken && created instanceof MemorySessionManager) {
|
|
546
|
+
created.onInvalidToken = session.onInvalidToken;
|
|
547
|
+
}
|
|
548
|
+
assertSessionManager(created);
|
|
549
|
+
const metadata = await created.metadata();
|
|
550
|
+
return new _CubeSignerClient(new CubistApiClient(created, metadata, targetOrgId));
|
|
551
|
+
}
|
|
552
|
+
static async createOidcSession(env, orgId, token, scopes, lifetimes, mfaReceipt, purpose) {
|
|
553
|
+
const requestFn = async (headers) => {
|
|
554
|
+
const response = await requestJson(
|
|
555
|
+
env.SignerApiRoot,
|
|
556
|
+
`/v0/org/${encodeURIComponent(orgId)}/oidc`,
|
|
557
|
+
{
|
|
558
|
+
method: "POST",
|
|
559
|
+
headers: {
|
|
560
|
+
Authorization: token,
|
|
561
|
+
"Content-Type": "application/json",
|
|
562
|
+
...headers ?? {}
|
|
563
|
+
},
|
|
564
|
+
body: JSON.stringify({
|
|
565
|
+
scopes,
|
|
566
|
+
purpose,
|
|
567
|
+
tokens: lifetimes ? {
|
|
568
|
+
auth_lifetime: lifetimes.auth,
|
|
569
|
+
refresh_lifetime: lifetimes.refresh,
|
|
570
|
+
session_lifetime: lifetimes.session,
|
|
571
|
+
grace_lifetime: lifetimes.grace
|
|
572
|
+
} : void 0
|
|
573
|
+
})
|
|
574
|
+
}
|
|
575
|
+
);
|
|
576
|
+
if (isAcceptedResponse(response)) {
|
|
577
|
+
return response;
|
|
578
|
+
}
|
|
579
|
+
return {
|
|
580
|
+
env: {
|
|
581
|
+
[DEV_STACK_NAME]: env
|
|
582
|
+
},
|
|
583
|
+
org_id: orgId,
|
|
584
|
+
token: response.token,
|
|
585
|
+
refresh_token: response.refresh_token,
|
|
586
|
+
session_exp: response.expiration,
|
|
587
|
+
purpose: "sign in via oidc",
|
|
588
|
+
session_info: response.session_info
|
|
589
|
+
};
|
|
590
|
+
};
|
|
591
|
+
return CubeSignerResponse.create(env, requestFn, mfaReceipt);
|
|
592
|
+
}
|
|
593
|
+
static async proveOidcIdentity(env, orgId, token) {
|
|
594
|
+
return requestJson(
|
|
595
|
+
env.SignerApiRoot,
|
|
596
|
+
`/v0/org/${encodeURIComponent(orgId)}/identity/prove/oidc`,
|
|
597
|
+
{
|
|
598
|
+
method: "POST",
|
|
599
|
+
headers: {
|
|
600
|
+
Authorization: token
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
);
|
|
604
|
+
}
|
|
605
|
+
org() {
|
|
606
|
+
return new OrgClient(__privateGet(this, _apiClient4));
|
|
607
|
+
}
|
|
608
|
+
getOrg(orgId) {
|
|
609
|
+
return new OrgClient(__privateGet(this, _apiClient4).withOrg(orgId));
|
|
610
|
+
}
|
|
611
|
+
async sessionKeys() {
|
|
612
|
+
const keys = await __privateGet(this, _apiClient4).sessionKeysList();
|
|
613
|
+
return keys.map((keyInfo) => new Key(this, keyInfo));
|
|
614
|
+
}
|
|
615
|
+
async getKeyByMaterialId(keyType, materialId) {
|
|
616
|
+
const keyInfo = await __privateGet(this, _apiClient4).keyGetByMaterialId(keyType, materialId);
|
|
617
|
+
return new Key(this, keyInfo);
|
|
618
|
+
}
|
|
619
|
+
async revokeSession() {
|
|
620
|
+
await __privateGet(this, _apiClient4).sessionRevoke();
|
|
621
|
+
}
|
|
622
|
+
};
|
|
623
|
+
_apiClient4 = new WeakMap();
|
|
624
|
+
var CubeSignerClient = _CubeSignerClient;
|
|
625
|
+
function isSessionData(value) {
|
|
626
|
+
return typeof value === "object" && value !== null && "token" in value && "refresh_token" in value;
|
|
627
|
+
}
|
|
628
|
+
var _address, _key, _client, _options;
|
|
629
|
+
var EvmSigner = class {
|
|
630
|
+
constructor(address, client, options) {
|
|
631
|
+
__privateAdd(this, _address);
|
|
632
|
+
__privateAdd(this, _key);
|
|
633
|
+
__privateAdd(this, _client);
|
|
634
|
+
__privateAdd(this, _options);
|
|
635
|
+
if (typeof address === "string") {
|
|
636
|
+
__privateSet(this, _address, address);
|
|
637
|
+
} else {
|
|
638
|
+
__privateSet(this, _address, address.materialId);
|
|
639
|
+
__privateSet(this, _key, address);
|
|
640
|
+
}
|
|
641
|
+
__privateSet(this, _client, client);
|
|
642
|
+
__privateSet(this, _options, {
|
|
643
|
+
onMfaPoll: options?.onMfaPoll,
|
|
644
|
+
mfaPollIntervalMs: options?.mfaPollIntervalMs ?? 1e3
|
|
645
|
+
});
|
|
646
|
+
}
|
|
647
|
+
async signTransaction(req) {
|
|
648
|
+
const res = await this.signEvm(req);
|
|
649
|
+
return (await this.handleMfa(res)).rlp_signed_tx;
|
|
650
|
+
}
|
|
651
|
+
async signEvm(req) {
|
|
652
|
+
const key = await this.key();
|
|
653
|
+
return key.signEvm(req);
|
|
654
|
+
}
|
|
655
|
+
async signEip712(req) {
|
|
656
|
+
const key = await this.key();
|
|
657
|
+
return (await this.handleMfa(await key.signEip712(req))).signature;
|
|
658
|
+
}
|
|
659
|
+
async signEip191(req) {
|
|
660
|
+
const key = await this.key();
|
|
661
|
+
return (await this.handleMfa(await key.signEip191(req))).signature;
|
|
662
|
+
}
|
|
663
|
+
async key() {
|
|
664
|
+
if (!__privateGet(this, _key)) {
|
|
665
|
+
__privateSet(this, _key, await __privateGet(this, _client).getKeyByMaterialId(EVM_KEY_TYPE, __privateGet(this, _address).toLowerCase()));
|
|
666
|
+
}
|
|
667
|
+
return __privateGet(this, _key);
|
|
668
|
+
}
|
|
669
|
+
async handleMfa(res) {
|
|
670
|
+
let pending = res;
|
|
671
|
+
let mfaId = pending.mfaId();
|
|
672
|
+
while (mfaId) {
|
|
673
|
+
await delay(__privateGet(this, _options).mfaPollIntervalMs ?? 1e3);
|
|
674
|
+
const mfaInfo = await __privateGet(this, _client).org().getMfaRequest(mfaId).fetch();
|
|
675
|
+
__privateGet(this, _options).onMfaPoll?.(mfaInfo);
|
|
676
|
+
const confirmation = mfaInfo.receipt?.confirmation;
|
|
677
|
+
if (confirmation) {
|
|
678
|
+
pending = await pending.execWithMfaApproval({
|
|
679
|
+
mfaId,
|
|
680
|
+
mfaOrgId: __privateGet(this, _client).orgId,
|
|
681
|
+
mfaConf: confirmation
|
|
682
|
+
});
|
|
683
|
+
}
|
|
684
|
+
mfaId = pending.mfaId();
|
|
685
|
+
}
|
|
686
|
+
return pending.data();
|
|
687
|
+
}
|
|
688
|
+
};
|
|
689
|
+
_address = new WeakMap();
|
|
690
|
+
_key = new WeakMap();
|
|
691
|
+
_client = new WeakMap();
|
|
692
|
+
_options = new WeakMap();
|
|
693
|
+
|
|
694
|
+
// src/providers/social/cubeSignerAuth.ts
|
|
695
|
+
function resolveEnv(env) {
|
|
696
|
+
return typeof env === "string" ? envs[env] : env;
|
|
697
|
+
}
|
|
698
|
+
var CubeSignerAuth = class {
|
|
699
|
+
constructor(config) {
|
|
700
|
+
this._client = null;
|
|
701
|
+
this._sessionData = null;
|
|
702
|
+
this.env = resolveEnv(config.env);
|
|
703
|
+
this.orgId = config.orgId;
|
|
704
|
+
this.scopes = config.scopes ?? ["sign:*", "manage:*"];
|
|
705
|
+
this.defaultSessionPolicy = config.defaultSessionPolicy;
|
|
706
|
+
this.oidcLoginHooks = config.oidcLoginHooks;
|
|
707
|
+
}
|
|
708
|
+
/** Currently authenticated client (null if not logged in). */
|
|
709
|
+
get client() {
|
|
710
|
+
return this._client;
|
|
711
|
+
}
|
|
712
|
+
get currentSession() {
|
|
713
|
+
if (!this._client || !this._sessionData) return null;
|
|
714
|
+
return {
|
|
715
|
+
client: this._client,
|
|
716
|
+
sessionData: this._sessionData
|
|
717
|
+
};
|
|
718
|
+
}
|
|
719
|
+
/* ── Google ───────────────────────────────── */
|
|
720
|
+
/**
|
|
721
|
+
* Login with a Google ID token (JWT).
|
|
722
|
+
* The token is used directly as the OIDC credential.
|
|
723
|
+
*/
|
|
724
|
+
async loginWithGoogle(idToken) {
|
|
725
|
+
return this.loginWithOidcToken(idToken);
|
|
726
|
+
}
|
|
727
|
+
/**
|
|
728
|
+
* Login with a generic OIDC ID token.
|
|
729
|
+
* Useful when the caller already finished the upstream provider flow.
|
|
730
|
+
*/
|
|
731
|
+
async loginWithOidcToken(idToken) {
|
|
732
|
+
return this.authenticateWithOidcToken(idToken);
|
|
733
|
+
}
|
|
734
|
+
/* ── Twitter / X ──────────────────────────── */
|
|
735
|
+
/**
|
|
736
|
+
* Login with a Twitter OAuth 2.0 authorization code.
|
|
737
|
+
*
|
|
738
|
+
* 1. Exchange the code for an OIDC `id_token` via CubeSigner's
|
|
739
|
+
* `/v0/org/{org_id}/oauth2/twitter` endpoint.
|
|
740
|
+
* 2. Create a CubeSigner session with that token.
|
|
741
|
+
*/
|
|
742
|
+
async loginWithTwitter(params) {
|
|
743
|
+
const idToken = await this.exchangeTwitterCode(params);
|
|
744
|
+
return this.loginWithOidcToken(idToken);
|
|
745
|
+
}
|
|
746
|
+
/* ── Sign-out ─────────────────────────────── */
|
|
747
|
+
async signOut() {
|
|
748
|
+
if (this._client) {
|
|
749
|
+
try {
|
|
750
|
+
await this._client.revokeSession();
|
|
751
|
+
} catch {
|
|
752
|
+
}
|
|
753
|
+
this._client = null;
|
|
754
|
+
this._sessionData = null;
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
/* ── Session restore ────────────────────── */
|
|
758
|
+
/**
|
|
759
|
+
* Restore a CubeSigner session from previously persisted SessionData.
|
|
760
|
+
* Useful for reconnecting after page reload without re-authentication.
|
|
761
|
+
*/
|
|
762
|
+
async restoreSession(sessionData) {
|
|
763
|
+
if (this._client) {
|
|
764
|
+
await this.signOut();
|
|
765
|
+
}
|
|
766
|
+
const client = await CubeSignerClient.create(sessionData);
|
|
767
|
+
this._client = client;
|
|
768
|
+
this._sessionData = sessionData;
|
|
769
|
+
return { client, sessionData };
|
|
770
|
+
}
|
|
771
|
+
/* ── Private ──────────────────────────────── */
|
|
772
|
+
async authenticateWithOidcToken(oidcToken) {
|
|
773
|
+
if (this._client) {
|
|
774
|
+
await this.signOut();
|
|
775
|
+
}
|
|
776
|
+
const hooks = this.oidcLoginHooks;
|
|
777
|
+
if (hooks) {
|
|
778
|
+
const proof = await CubeSignerClient.proveOidcIdentity(
|
|
779
|
+
this.env,
|
|
780
|
+
this.orgId,
|
|
781
|
+
oidcToken
|
|
782
|
+
);
|
|
783
|
+
if (!proof.user_info?.initialized) {
|
|
784
|
+
await hooks.registerUser(oidcToken);
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
return this.createOidcSession(oidcToken);
|
|
788
|
+
}
|
|
789
|
+
/**
|
|
790
|
+
* Exchange a Twitter authorization code for an OIDC id_token
|
|
791
|
+
* via CubeSigner's dedicated endpoint.
|
|
792
|
+
*
|
|
793
|
+
* `POST {SignerApiRoot}/v0/org/{org_id}/oauth2/twitter`
|
|
794
|
+
*/
|
|
795
|
+
async exchangeTwitterCode(params) {
|
|
796
|
+
const url = `${this.env.SignerApiRoot}/v0/org/${this.orgId}/oauth2/twitter`;
|
|
797
|
+
const res = await fetch(url, {
|
|
798
|
+
method: "POST",
|
|
799
|
+
headers: { "Content-Type": "application/json" },
|
|
800
|
+
body: JSON.stringify({
|
|
801
|
+
code: params.code,
|
|
802
|
+
code_verifier: params.codeVerifier,
|
|
803
|
+
redirect_uri: params.redirectUri
|
|
804
|
+
})
|
|
805
|
+
});
|
|
806
|
+
if (!res.ok) {
|
|
807
|
+
const text = await res.text().catch(() => "");
|
|
808
|
+
throw new Error(
|
|
809
|
+
`Twitter code exchange failed (${res.status}): ${text}`
|
|
810
|
+
);
|
|
811
|
+
}
|
|
812
|
+
const data = await res.json();
|
|
813
|
+
if (!data.id_token) {
|
|
814
|
+
throw new Error("CubeSigner twitter exchange did not return id_token");
|
|
815
|
+
}
|
|
816
|
+
return data.id_token;
|
|
817
|
+
}
|
|
818
|
+
/**
|
|
819
|
+
* Create a CubeSigner OIDC session from a generic OIDC id_token.
|
|
820
|
+
*/
|
|
821
|
+
async createOidcSession(oidcToken) {
|
|
822
|
+
const resp = await CubeSignerClient.createOidcSession(
|
|
823
|
+
this.env,
|
|
824
|
+
this.orgId,
|
|
825
|
+
oidcToken,
|
|
826
|
+
this.scopes
|
|
827
|
+
);
|
|
828
|
+
const sessionData = resp.data();
|
|
829
|
+
const client = await CubeSignerClient.create(sessionData);
|
|
830
|
+
this._client = client;
|
|
831
|
+
this._sessionData = sessionData;
|
|
832
|
+
return { client, sessionData };
|
|
833
|
+
}
|
|
834
|
+
};
|
|
835
|
+
|
|
836
|
+
// src/providers/social/cubistEvmWalletProvider.ts
|
|
837
|
+
var evmChainIdMap = {
|
|
838
|
+
ETH: 1,
|
|
839
|
+
BSC: 56,
|
|
840
|
+
ETH_TENDERLY: 3030,
|
|
841
|
+
BSC_TENDERLY: 3131
|
|
842
|
+
};
|
|
843
|
+
var isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
844
|
+
var toBigIntQuantity = (value) => {
|
|
845
|
+
if (typeof value === "bigint") return value;
|
|
846
|
+
if (typeof value === "number") {
|
|
847
|
+
if (!Number.isInteger(value) || value < 0) {
|
|
848
|
+
throw new Error(`Invalid EVM quantity number: ${value}`);
|
|
849
|
+
}
|
|
850
|
+
return BigInt(value);
|
|
851
|
+
}
|
|
852
|
+
const trimmed = value.trim();
|
|
853
|
+
if (!trimmed) {
|
|
854
|
+
throw new Error("EVM quantity cannot be empty");
|
|
855
|
+
}
|
|
856
|
+
if (/^0x[0-9a-fA-F]+$/.test(trimmed) || /^\d+$/.test(trimmed)) {
|
|
857
|
+
return BigInt(trimmed);
|
|
858
|
+
}
|
|
859
|
+
throw new Error(`Invalid EVM quantity string: ${value}`);
|
|
860
|
+
};
|
|
861
|
+
var toHexQuantity = (value) => {
|
|
862
|
+
if (value === void 0) return void 0;
|
|
863
|
+
return `0x${toBigIntQuantity(value).toString(16)}`;
|
|
864
|
+
};
|
|
865
|
+
var normalizeAccessList = (accessList) => {
|
|
866
|
+
if (!accessList) return void 0;
|
|
867
|
+
return accessList.map((item) => ({
|
|
868
|
+
address: item.address,
|
|
869
|
+
storageKeys: item.storageKeys
|
|
870
|
+
}));
|
|
871
|
+
};
|
|
872
|
+
var textEncoder = new TextEncoder();
|
|
873
|
+
var resolveTransactionType = (transaction) => {
|
|
874
|
+
if (transaction.type !== void 0) {
|
|
875
|
+
const normalized = toHexQuantity(transaction.type);
|
|
876
|
+
if (normalized === "0x0" || normalized === "0x1" || normalized === "0x2") {
|
|
877
|
+
return normalized;
|
|
878
|
+
}
|
|
879
|
+
throw new Error(`Unsupported EVM transaction type for Cubist: ${normalized}`);
|
|
880
|
+
}
|
|
881
|
+
if (transaction.maxFeePerGas !== void 0 || transaction.maxPriorityFeePerGas !== void 0) {
|
|
882
|
+
return "0x2";
|
|
883
|
+
}
|
|
884
|
+
if (transaction.accessList?.length) {
|
|
885
|
+
return "0x1";
|
|
886
|
+
}
|
|
887
|
+
return "0x0";
|
|
888
|
+
};
|
|
889
|
+
var toCubistSignRequest = (address, chain, transaction) => {
|
|
890
|
+
const resolvedChainId = (transaction.chainId !== void 0 ? Number(toBigIntQuantity(transaction.chainId)) : void 0) ?? evmChainIdMap[chain];
|
|
891
|
+
if (!resolvedChainId) {
|
|
892
|
+
throw new Error("Cubist signing requires an EVM chainId");
|
|
893
|
+
}
|
|
894
|
+
const type = resolveTransactionType(transaction);
|
|
895
|
+
const commonFields = {
|
|
896
|
+
from: transaction.from ?? address,
|
|
897
|
+
to: transaction.to,
|
|
898
|
+
data: transaction.data,
|
|
899
|
+
gas: toHexQuantity(transaction.gas),
|
|
900
|
+
nonce: toHexQuantity(transaction.nonce),
|
|
901
|
+
value: toHexQuantity(transaction.value)
|
|
902
|
+
};
|
|
903
|
+
const accessList = normalizeAccessList(transaction.accessList);
|
|
904
|
+
const tx = type === "0x2" ? {
|
|
905
|
+
...commonFields,
|
|
906
|
+
type,
|
|
907
|
+
...accessList ? { accessList } : {},
|
|
908
|
+
maxFeePerGas: toHexQuantity(transaction.maxFeePerGas),
|
|
909
|
+
maxPriorityFeePerGas: toHexQuantity(transaction.maxPriorityFeePerGas)
|
|
910
|
+
} : type === "0x1" ? {
|
|
911
|
+
...commonFields,
|
|
912
|
+
type,
|
|
913
|
+
...accessList ? { accessList } : {},
|
|
914
|
+
gasPrice: toHexQuantity(transaction.gasPrice)
|
|
915
|
+
} : {
|
|
916
|
+
...commonFields,
|
|
917
|
+
type,
|
|
918
|
+
gasPrice: toHexQuantity(transaction.gasPrice)
|
|
919
|
+
};
|
|
920
|
+
return {
|
|
921
|
+
chain_id: resolvedChainId,
|
|
922
|
+
tx
|
|
923
|
+
};
|
|
924
|
+
};
|
|
925
|
+
var getTransactionParam = (params) => {
|
|
926
|
+
const candidate = params?.[0];
|
|
927
|
+
if (!isRecord(candidate)) {
|
|
928
|
+
throw new Error("eth_signTransaction requires a transaction object");
|
|
929
|
+
}
|
|
930
|
+
return candidate;
|
|
931
|
+
};
|
|
932
|
+
var toHexBytes = (value) => {
|
|
933
|
+
if (/^0x[0-9a-fA-F]*$/.test(value)) return value;
|
|
934
|
+
return `0x${Array.from(textEncoder.encode(value)).map((byte) => byte.toString(16).padStart(2, "0")).join("")}`;
|
|
935
|
+
};
|
|
936
|
+
var getMessageParam = (address, params) => {
|
|
937
|
+
const [first, second] = params ?? [];
|
|
938
|
+
if (typeof second === "string" && second.toLowerCase() === address.toLowerCase()) {
|
|
939
|
+
return String(first ?? "");
|
|
940
|
+
}
|
|
941
|
+
if (typeof first === "string" && first.toLowerCase() === address.toLowerCase()) {
|
|
942
|
+
return String(second ?? "");
|
|
943
|
+
}
|
|
944
|
+
return String(first ?? "");
|
|
945
|
+
};
|
|
946
|
+
var getTypedDataParam = (address, params) => {
|
|
947
|
+
const [first, second] = params ?? [];
|
|
948
|
+
if (typeof first === "string" && first.toLowerCase() === address.toLowerCase()) {
|
|
949
|
+
return typeof second === "string" ? JSON.parse(second) : second ?? {};
|
|
950
|
+
}
|
|
951
|
+
return typeof second === "string" ? JSON.parse(second) : second ?? first ?? {};
|
|
952
|
+
};
|
|
953
|
+
var createCubistEvmWalletProvider = ({
|
|
954
|
+
session,
|
|
955
|
+
address,
|
|
956
|
+
chain
|
|
957
|
+
}) => {
|
|
958
|
+
const signer = new EvmSigner(address, session.client);
|
|
959
|
+
return {
|
|
960
|
+
async request(payload) {
|
|
961
|
+
switch (payload.method) {
|
|
962
|
+
case "eth_accounts":
|
|
963
|
+
case "eth_requestAccounts":
|
|
964
|
+
return [address];
|
|
965
|
+
case "eth_chainId": {
|
|
966
|
+
const chainId = evmChainIdMap[chain];
|
|
967
|
+
if (!chainId) {
|
|
968
|
+
throw new Error(`CubistProvider: chain ${chain} does not expose an EVM chainId`);
|
|
969
|
+
}
|
|
970
|
+
return `0x${chainId.toString(16)}`;
|
|
971
|
+
}
|
|
972
|
+
case "eth_signTransaction": {
|
|
973
|
+
const transaction = getTransactionParam(payload.params);
|
|
974
|
+
const signRequest = toCubistSignRequest(address, chain, transaction);
|
|
975
|
+
return await signer.signTransaction(signRequest);
|
|
976
|
+
}
|
|
977
|
+
case "personal_sign": {
|
|
978
|
+
const message = getMessageParam(address, payload.params);
|
|
979
|
+
return await signer.signEip191({
|
|
980
|
+
data: toHexBytes(message)
|
|
981
|
+
});
|
|
982
|
+
}
|
|
983
|
+
case "eth_signTypedData_v4": {
|
|
984
|
+
const typedData = getTypedDataParam(address, payload.params);
|
|
985
|
+
const domain = typedData.domain;
|
|
986
|
+
const chainId = domain?.chainId !== void 0 ? Number(toBigIntQuantity(domain.chainId)) : evmChainIdMap[chain];
|
|
987
|
+
if (!chainId) {
|
|
988
|
+
throw new Error("CubistProvider: typed data signing requires an EVM chainId");
|
|
989
|
+
}
|
|
990
|
+
return await signer.signEip712({
|
|
991
|
+
chain_id: chainId,
|
|
992
|
+
typed_data: typedData
|
|
993
|
+
});
|
|
994
|
+
}
|
|
995
|
+
default:
|
|
996
|
+
throw new Error(`CubistProvider: unsupported RPC method "${payload.method}"`);
|
|
997
|
+
}
|
|
998
|
+
},
|
|
999
|
+
async disconnect() {
|
|
1000
|
+
await session.client.revokeSession();
|
|
1001
|
+
}
|
|
1002
|
+
};
|
|
1003
|
+
};
|
|
1004
|
+
|
|
1005
|
+
export { CubeSignerAuth, createCubistEvmWalletProvider };
|