prividium 0.6.0 → 0.7.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.
|
@@ -2,3 +2,10 @@
|
|
|
2
2
|
* Checks if a Response contains a Prividium unauthorized/forbidden error.
|
|
3
3
|
*/
|
|
4
4
|
export declare function hasPrividiumUnauthorizedError(response: Response): Promise<boolean>;
|
|
5
|
+
/**
|
|
6
|
+
* Extracts a human-readable error string from a failed HTTP response.
|
|
7
|
+
* Attempts to parse the server's `{ error: { code, message } }` JSON structure,
|
|
8
|
+
* falls back to plain text, and ultimately to status + statusText.
|
|
9
|
+
* Never throws — always returns a usable string.
|
|
10
|
+
*/
|
|
11
|
+
export declare function extractResponseError(response: Response): Promise<string>;
|
package/dist/sdk/error-utils.js
CHANGED
|
@@ -7,6 +7,12 @@ const jsonRpcErrorSchema = z.object({
|
|
|
7
7
|
data: z.unknown().optional()
|
|
8
8
|
})
|
|
9
9
|
});
|
|
10
|
+
const apiErrorSchema = z.object({
|
|
11
|
+
error: z.object({
|
|
12
|
+
code: z.string(),
|
|
13
|
+
message: z.string()
|
|
14
|
+
})
|
|
15
|
+
});
|
|
10
16
|
/**
|
|
11
17
|
* Checks if a Response contains a Prividium unauthorized/forbidden error.
|
|
12
18
|
*/
|
|
@@ -19,3 +25,35 @@ export async function hasPrividiumUnauthorizedError(response) {
|
|
|
19
25
|
}
|
|
20
26
|
return false;
|
|
21
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* Extracts a human-readable error string from a failed HTTP response.
|
|
30
|
+
* Attempts to parse the server's `{ error: { code, message } }` JSON structure,
|
|
31
|
+
* falls back to plain text, and ultimately to status + statusText.
|
|
32
|
+
* Never throws — always returns a usable string.
|
|
33
|
+
*/
|
|
34
|
+
export async function extractResponseError(response) {
|
|
35
|
+
const base = `${response.status} ${response.statusText}`;
|
|
36
|
+
try {
|
|
37
|
+
const cloned = response.clone();
|
|
38
|
+
const text = await cloned.text();
|
|
39
|
+
if (!text) {
|
|
40
|
+
return base;
|
|
41
|
+
}
|
|
42
|
+
try {
|
|
43
|
+
const json = JSON.parse(text);
|
|
44
|
+
const parsed = apiErrorSchema.safeParse(json);
|
|
45
|
+
if (parsed.success) {
|
|
46
|
+
const { code, message } = parsed.data.error;
|
|
47
|
+
return `${base}: ${message} (${code})`;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
// Not JSON
|
|
52
|
+
}
|
|
53
|
+
return `${base}: ${text}`;
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
// Body unreadable
|
|
57
|
+
}
|
|
58
|
+
return base;
|
|
59
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { http } from 'viem';
|
|
2
2
|
import { LocalStorage, TokenManager } from './storage.js';
|
|
3
3
|
import { PopupAuth } from './popup-auth.js';
|
|
4
|
-
import { hasPrividiumUnauthorizedError } from './error-utils.js';
|
|
4
|
+
import { extractResponseError, hasPrividiumUnauthorizedError } from './error-utils.js';
|
|
5
5
|
import { PrividiumSessionError } from './errors.js';
|
|
6
6
|
import { buildChainObject, createApiMethods, rpcUrl } from './chain-core.js';
|
|
7
7
|
export function createPrividiumChain(config) {
|
|
@@ -44,7 +44,8 @@ export function createPrividiumChain(config) {
|
|
|
44
44
|
throw new PrividiumSessionError();
|
|
45
45
|
}
|
|
46
46
|
if (!response.ok) {
|
|
47
|
-
|
|
47
|
+
const detail = await extractResponseError(response);
|
|
48
|
+
throw new Error(`Error calling ${url}: ${detail}`);
|
|
48
49
|
}
|
|
49
50
|
const parsed = schema.safeParse(await response.json());
|
|
50
51
|
if (!parsed.success) {
|
package/dist/sdk/siwe-auth.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
|
+
import { extractResponseError } from './error-utils.js';
|
|
2
3
|
const siweMessageResponseSchema = z.object({
|
|
3
4
|
msg: z.string()
|
|
4
5
|
});
|
|
@@ -19,8 +20,7 @@ export class SiweAuth {
|
|
|
19
20
|
}
|
|
20
21
|
async authorize() {
|
|
21
22
|
// Step 1: Request SIWE message
|
|
22
|
-
const
|
|
23
|
-
const siweResponse = await fetch(siweMessageUrl, {
|
|
23
|
+
const siweResponse = await fetch(new URL('/api/siwe-messages', this.config.prividiumApiBaseUrl), {
|
|
24
24
|
method: 'POST',
|
|
25
25
|
headers: { 'Content-Type': 'application/json' },
|
|
26
26
|
body: JSON.stringify({
|
|
@@ -29,20 +29,21 @@ export class SiweAuth {
|
|
|
29
29
|
})
|
|
30
30
|
});
|
|
31
31
|
if (!siweResponse.ok) {
|
|
32
|
-
|
|
32
|
+
const detail = await extractResponseError(siweResponse);
|
|
33
|
+
throw new Error(`Failed to get SIWE message: ${detail}`);
|
|
33
34
|
}
|
|
34
35
|
const siweData = siweMessageResponseSchema.parse(await siweResponse.json());
|
|
35
36
|
// Step 2: Sign the message
|
|
36
37
|
const signature = await this.config.account.signMessage({ message: siweData.msg });
|
|
37
38
|
// Step 3: Login
|
|
38
|
-
const
|
|
39
|
-
const loginResponse = await fetch(loginUrl, {
|
|
39
|
+
const loginResponse = await fetch(new URL('/api/auth/login/crypto-native', this.config.prividiumApiBaseUrl), {
|
|
40
40
|
method: 'POST',
|
|
41
41
|
headers: { 'Content-Type': 'application/json' },
|
|
42
42
|
body: JSON.stringify({ message: siweData.msg, signature })
|
|
43
43
|
});
|
|
44
44
|
if (!loginResponse.ok) {
|
|
45
|
-
|
|
45
|
+
const detail = await extractResponseError(loginResponse);
|
|
46
|
+
throw new Error(`SIWE login failed: ${detail}`);
|
|
46
47
|
}
|
|
47
48
|
const loginJson = await loginResponse.json();
|
|
48
49
|
// Check for MFA requirement (admin users with passkeys)
|
package/dist/sdk/siwe-chain.js
CHANGED
|
@@ -2,7 +2,7 @@ import { http } from 'viem';
|
|
|
2
2
|
import { TokenManager } from './storage.js';
|
|
3
3
|
import { MemoryStorage } from './memory-storage.js';
|
|
4
4
|
import { SiweAuth } from './siwe-auth.js';
|
|
5
|
-
import { hasPrividiumUnauthorizedError } from './error-utils.js';
|
|
5
|
+
import { extractResponseError, hasPrividiumUnauthorizedError } from './error-utils.js';
|
|
6
6
|
import { PrividiumSessionError } from './errors.js';
|
|
7
7
|
import { buildChainObject, createApiMethods, rpcUrl } from './chain-core.js';
|
|
8
8
|
export function createPrividiumSiweChain(config) {
|
|
@@ -102,7 +102,8 @@ export function createPrividiumSiweChain(config) {
|
|
|
102
102
|
throw new PrividiumSessionError();
|
|
103
103
|
}
|
|
104
104
|
if (!response.ok) {
|
|
105
|
-
|
|
105
|
+
const detail = await extractResponseError(response);
|
|
106
|
+
throw new Error(`Error calling ${url}: ${detail}`);
|
|
106
107
|
}
|
|
107
108
|
const parsed = schema.safeParse(await response.json());
|
|
108
109
|
if (!parsed.success) {
|