prividium 0.1.3 → 0.1.5
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/cli/server/server.js +12 -3
- package/dist/sdk/error-utils.d.ts +4 -0
- package/dist/sdk/error-utils.js +21 -0
- package/dist/sdk/index.d.ts +1 -0
- package/dist/sdk/index.js +1 -0
- package/dist/sdk/prividium-chain.js +4 -3
- package/dist/sdk/rpc-error-codes.d.ts +24 -0
- package/dist/sdk/rpc-error-codes.js +24 -0
- package/dist/tsconfig.sdk.tsbuildinfo +1 -1
- package/package.json +1 -1
|
@@ -11,6 +11,8 @@ function fastifyApp() {
|
|
|
11
11
|
function randomStateString() {
|
|
12
12
|
return randomBytes(20).toString('hex');
|
|
13
13
|
}
|
|
14
|
+
const rpcCallSchema = z.object({ method: z.string() });
|
|
15
|
+
const rpcReqSchema = z.union([rpcCallSchema, rpcCallSchema.array()]);
|
|
14
16
|
export function buildServer(config) {
|
|
15
17
|
const app = fastifyApp();
|
|
16
18
|
app.setValidatorCompiler(validatorCompiler);
|
|
@@ -57,9 +59,16 @@ export function buildServer(config) {
|
|
|
57
59
|
rewritePrefix: '/rpc',
|
|
58
60
|
routes: ['/'],
|
|
59
61
|
preValidation: (request, _reply, done) => {
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
|
|
62
|
+
const method = rpcReqSchema.safeParse(request.body);
|
|
63
|
+
if (!method.success) {
|
|
64
|
+
config.onCall('unknown');
|
|
65
|
+
}
|
|
66
|
+
else if (Array.isArray(method.data)) {
|
|
67
|
+
config.onCall(method.data.map((m) => m.method).join(', '));
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
config.onCall(method.data.method);
|
|
71
|
+
}
|
|
63
72
|
done();
|
|
64
73
|
},
|
|
65
74
|
preHandler: (_req, reply, done) => {
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { UNAUTHORIZED_ERROR_CODE, FORBIDDEN_ERROR_CODE } from './rpc-error-codes.js';
|
|
3
|
+
const jsonRpcErrorSchema = z.object({
|
|
4
|
+
error: z.object({
|
|
5
|
+
code: z.number(),
|
|
6
|
+
message: z.string().optional(),
|
|
7
|
+
data: z.unknown().optional()
|
|
8
|
+
})
|
|
9
|
+
});
|
|
10
|
+
/**
|
|
11
|
+
* Checks if a Response contains a Prividium unauthorized/forbidden error.
|
|
12
|
+
*/
|
|
13
|
+
export async function hasPrividiumUnauthorizedError(response) {
|
|
14
|
+
const clonedResponse = response.clone();
|
|
15
|
+
const parsed = jsonRpcErrorSchema.safeParse(await clonedResponse.json());
|
|
16
|
+
// { success: false, error: {...} } | { success: true, data: {...} }
|
|
17
|
+
if (parsed.success) {
|
|
18
|
+
return [UNAUTHORIZED_ERROR_CODE, FORBIDDEN_ERROR_CODE].includes(parsed.data.error.code);
|
|
19
|
+
}
|
|
20
|
+
return false;
|
|
21
|
+
}
|
package/dist/sdk/index.d.ts
CHANGED
|
@@ -4,3 +4,4 @@ export { AUTH_ERRORS, STORAGE_KEYS } from './types.js';
|
|
|
4
4
|
export { LocalStorage, TokenManager } from './storage.js';
|
|
5
5
|
export { generateRandomState } from './token-utils.js';
|
|
6
6
|
export { PopupAuth, handleAuthCallback, type AuthCallbackMessage, type OauthScope } from './popup-auth.js';
|
|
7
|
+
export { UNAUTHORIZED_ERROR_CODE, FORBIDDEN_ERROR_CODE } from './rpc-error-codes.js';
|
package/dist/sdk/index.js
CHANGED
|
@@ -3,3 +3,4 @@ export { AUTH_ERRORS, STORAGE_KEYS } from './types.js';
|
|
|
3
3
|
export { LocalStorage, TokenManager } from './storage.js';
|
|
4
4
|
export { generateRandomState } from './token-utils.js';
|
|
5
5
|
export { PopupAuth, handleAuthCallback } from './popup-auth.js';
|
|
6
|
+
export { UNAUTHORIZED_ERROR_CODE, FORBIDDEN_ERROR_CODE } from './rpc-error-codes.js';
|
|
@@ -2,6 +2,7 @@ import { http } from 'viem';
|
|
|
2
2
|
import { mainnet } from 'viem/chains';
|
|
3
3
|
import { LocalStorage, TokenManager } from './storage.js';
|
|
4
4
|
import { PopupAuth } from './popup-auth.js';
|
|
5
|
+
import { hasPrividiumUnauthorizedError } from './error-utils.js';
|
|
5
6
|
export function createPrividiumChain(config) {
|
|
6
7
|
const storage = config.storage || new LocalStorage();
|
|
7
8
|
const tokenManager = new TokenManager(storage, config.chain.id);
|
|
@@ -33,9 +34,9 @@ export function createPrividiumChain(config) {
|
|
|
33
34
|
...getAuthHeaders()
|
|
34
35
|
};
|
|
35
36
|
},
|
|
36
|
-
onFetchResponse: (response) => {
|
|
37
|
-
// Handle
|
|
38
|
-
if (response
|
|
37
|
+
onFetchResponse: async (response) => {
|
|
38
|
+
// Handle JSON RPC 2.0 error responses with authorization error codes
|
|
39
|
+
if (await hasPrividiumUnauthorizedError(response)) {
|
|
39
40
|
tokenManager.clearToken();
|
|
40
41
|
config.onAuthExpiry?.();
|
|
41
42
|
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RPC error codes used in Prividium SDK.
|
|
3
|
+
* Standard codes follow JSON-RPC 2.0 spec, custom codes handle auth/authz.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Internal error (-32603). Standard JSON-RPC 2.0 code for server errors.
|
|
7
|
+
*/
|
|
8
|
+
export declare const INTERNAL_RPC_ERROR = -32603;
|
|
9
|
+
/**
|
|
10
|
+
* Invalid Request (-32600). Standard JSON-RPC 2.0 code for malformed requests.
|
|
11
|
+
*/
|
|
12
|
+
export declare const INVALID_REQUEST_ERROR_CODE = -32600;
|
|
13
|
+
/**
|
|
14
|
+
* Invalid params (-32602). Standard JSON-RPC 2.0 code for bad parameters.
|
|
15
|
+
*/
|
|
16
|
+
export declare const BAD_RPC_PARAMS_ERROR_CODE = -32602;
|
|
17
|
+
/**
|
|
18
|
+
* Unauthorized (-32090). Custom code for authentication failures (no/invalid token).
|
|
19
|
+
*/
|
|
20
|
+
export declare const UNAUTHORIZED_ERROR_CODE = -32090;
|
|
21
|
+
/**
|
|
22
|
+
* Forbidden (-32001). Custom code for authorization failures (valid token, insufficient permissions).
|
|
23
|
+
*/
|
|
24
|
+
export declare const FORBIDDEN_ERROR_CODE = -32001;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RPC error codes used in Prividium SDK.
|
|
3
|
+
* Standard codes follow JSON-RPC 2.0 spec, custom codes handle auth/authz.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Internal error (-32603). Standard JSON-RPC 2.0 code for server errors.
|
|
7
|
+
*/
|
|
8
|
+
export const INTERNAL_RPC_ERROR = -32603;
|
|
9
|
+
/**
|
|
10
|
+
* Invalid Request (-32600). Standard JSON-RPC 2.0 code for malformed requests.
|
|
11
|
+
*/
|
|
12
|
+
export const INVALID_REQUEST_ERROR_CODE = -32600;
|
|
13
|
+
/**
|
|
14
|
+
* Invalid params (-32602). Standard JSON-RPC 2.0 code for bad parameters.
|
|
15
|
+
*/
|
|
16
|
+
export const BAD_RPC_PARAMS_ERROR_CODE = -32602;
|
|
17
|
+
/**
|
|
18
|
+
* Unauthorized (-32090). Custom code for authentication failures (no/invalid token).
|
|
19
|
+
*/
|
|
20
|
+
export const UNAUTHORIZED_ERROR_CODE = -32090;
|
|
21
|
+
/**
|
|
22
|
+
* Forbidden (-32001). Custom code for authorization failures (valid token, insufficient permissions).
|
|
23
|
+
*/
|
|
24
|
+
export const FORBIDDEN_ERROR_CODE = -32001;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"root":["../src/index.ts","../src/popup-auth.ts","../src/prividium-chain.ts","../src/storage.ts","../src/token-utils.ts","../src/types.ts"],"version":"5.8.3"}
|
|
1
|
+
{"root":["../src/error-utils.ts","../src/index.ts","../src/popup-auth.ts","../src/prividium-chain.ts","../src/rpc-error-codes.ts","../src/storage.ts","../src/token-utils.ts","../src/types.ts"],"version":"5.8.3"}
|