@neus/sdk 1.0.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/errors.js ADDED
@@ -0,0 +1,228 @@
1
+ /**
2
+ * NEUS SDK Error Classes
3
+ * @license Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * Base SDK Error Class
8
+ */
9
+ export class SDKError extends Error {
10
+ constructor(message, code = 'SDK_ERROR', details = {}) {
11
+ super(message);
12
+ this.name = 'SDKError';
13
+ this.code = code;
14
+ this.details = details;
15
+ this.timestamp = Date.now();
16
+
17
+ // Ensure proper prototype chain
18
+ if (Error.captureStackTrace) {
19
+ Error.captureStackTrace(this, SDKError);
20
+ }
21
+ }
22
+
23
+ toJSON() {
24
+ return {
25
+ name: this.name,
26
+ message: this.message,
27
+ code: this.code,
28
+ details: this.details,
29
+ timestamp: this.timestamp
30
+ };
31
+ }
32
+ }
33
+
34
+ /**
35
+ * API-related errors (server responses, HTTP issues)
36
+ */
37
+ export class ApiError extends SDKError {
38
+ constructor(message, statusCode = 500, code = 'API_ERROR', response = null) {
39
+ super(message, code);
40
+ this.name = 'ApiError';
41
+ this.statusCode = statusCode;
42
+ this.response = response;
43
+
44
+ // Additional classification
45
+ this.isClientError = statusCode >= 400 && statusCode < 500;
46
+ this.isServerError = statusCode >= 500;
47
+ this.isRetryable = this.isServerError || statusCode === 429; // Server errors or rate limit
48
+ }
49
+
50
+ static fromResponse(response, responseData) {
51
+ const statusCode = response.status;
52
+ const message =
53
+ responseData?.error?.message ||
54
+ responseData?.message ||
55
+ `API request failed with status ${statusCode}`;
56
+ const code = responseData?.error?.code || 'API_ERROR';
57
+
58
+ return new ApiError(message, statusCode, code, responseData);
59
+ }
60
+
61
+ toJSON() {
62
+ return {
63
+ ...super.toJSON(),
64
+ statusCode: this.statusCode,
65
+ isClientError: this.isClientError,
66
+ isServerError: this.isServerError,
67
+ isRetryable: this.isRetryable
68
+ };
69
+ }
70
+ }
71
+
72
+ /**
73
+ * Client-side validation errors (invalid parameters, missing required fields)
74
+ */
75
+ export class ValidationError extends SDKError {
76
+ constructor(message, field = null, value = null) {
77
+ super(message, 'VALIDATION_ERROR');
78
+ this.name = 'ValidationError';
79
+ this.field = field;
80
+ this.value = value;
81
+ this.isRetryable = false; // Validation errors are not retryable
82
+ }
83
+
84
+ toJSON() {
85
+ return {
86
+ ...super.toJSON(),
87
+ field: this.field,
88
+ value: this.value,
89
+ isRetryable: this.isRetryable
90
+ };
91
+ }
92
+ }
93
+
94
+ /**
95
+ * Network-related errors (connectivity, timeouts, DNS issues)
96
+ */
97
+ export class NetworkError extends SDKError {
98
+ constructor(message, code = 'NETWORK_ERROR', originalError = null) {
99
+ super(message, code);
100
+ this.name = 'NetworkError';
101
+ this.originalError = originalError;
102
+ this.isRetryable = true; // Network errors are typically retryable
103
+ }
104
+
105
+ static isNetworkError(error) {
106
+ return (
107
+ error instanceof NetworkError ||
108
+ (error.name === 'TypeError' && error.message.includes('fetch')) ||
109
+ error.name === 'AbortError' ||
110
+ error.code === 'ENOTFOUND' ||
111
+ error.code === 'ECONNREFUSED' ||
112
+ error.code === 'ETIMEDOUT'
113
+ );
114
+ }
115
+
116
+ toJSON() {
117
+ return {
118
+ ...super.toJSON(),
119
+ isRetryable: this.isRetryable,
120
+ originalError: this.originalError
121
+ ? {
122
+ name: this.originalError.name,
123
+ message: this.originalError.message,
124
+ code: this.originalError.code
125
+ }
126
+ : null
127
+ };
128
+ }
129
+ }
130
+
131
+ /**
132
+ * Configuration-related errors (missing configuration, invalid settings)
133
+ */
134
+ export class ConfigurationError extends SDKError {
135
+ constructor(message, configKey = null) {
136
+ super(message, 'CONFIGURATION_ERROR');
137
+ this.name = 'ConfigurationError';
138
+ this.configKey = configKey;
139
+ this.isRetryable = false; // Config errors require user intervention
140
+ }
141
+
142
+ toJSON() {
143
+ return {
144
+ ...super.toJSON(),
145
+ configKey: this.configKey,
146
+ isRetryable: this.isRetryable
147
+ };
148
+ }
149
+ }
150
+
151
+ /**
152
+ * Verification-specific errors (verifier failures, invalid proofs)
153
+ */
154
+ export class VerificationError extends SDKError {
155
+ constructor(message, verifierId = null, code = 'VERIFICATION_ERROR') {
156
+ super(message, code);
157
+ this.name = 'VerificationError';
158
+ this.verifierId = verifierId;
159
+ this.isRetryable = true; // Some verification errors might be retryable
160
+ }
161
+
162
+ toJSON() {
163
+ return {
164
+ ...super.toJSON(),
165
+ verifierId: this.verifierId,
166
+ isRetryable: this.isRetryable
167
+ };
168
+ }
169
+ }
170
+
171
+ /**
172
+ * Authentication-related errors (signature validation, wallet connection)
173
+ */
174
+ export class AuthenticationError extends SDKError {
175
+ constructor(message, code = 'AUTHENTICATION_ERROR') {
176
+ super(message, code);
177
+ this.name = 'AuthenticationError';
178
+ this.isRetryable = false; // Auth errors require user intervention
179
+ }
180
+
181
+ toJSON() {
182
+ return {
183
+ ...super.toJSON(),
184
+ isRetryable: this.isRetryable
185
+ };
186
+ }
187
+ }
188
+
189
+ /**
190
+ * Utility function to create appropriate error from generic error
191
+ */
192
+ export function createErrorFromGeneric(error, context = {}) {
193
+ if (error instanceof SDKError) {
194
+ return error;
195
+ }
196
+
197
+ // Network-related errors
198
+ if (NetworkError.isNetworkError(error)) {
199
+ return new NetworkError(
200
+ error.message || 'Network error occurred',
201
+ error.code || 'NETWORK_ERROR',
202
+ error
203
+ );
204
+ }
205
+
206
+ // Timeout errors
207
+ if (error.name === 'AbortError' || error.message.includes('timeout')) {
208
+ return new NetworkError('Request timeout', 'TIMEOUT', error);
209
+ }
210
+
211
+ // Generic error wrapper
212
+ return new SDKError(error.message || 'Unknown error occurred', error.code || 'UNKNOWN_ERROR', {
213
+ originalError: error,
214
+ context
215
+ });
216
+ }
217
+
218
+ // Export all error classes as default
219
+ export default {
220
+ SDKError,
221
+ ApiError,
222
+ ValidationError,
223
+ NetworkError,
224
+ ConfigurationError,
225
+ VerificationError,
226
+ AuthenticationError,
227
+ createErrorFromGeneric
228
+ };
package/index.js ADDED
@@ -0,0 +1,70 @@
1
+ /**
2
+ * NEUS SDK - Universal Verification Protocol
3
+ * Create and verify cryptographic proofs across applications
4
+ * @license Apache-2.0
5
+ */
6
+
7
+ // Core client
8
+ export { NeusClient } from './client.js';
9
+
10
+ // Essential utilities
11
+ export {
12
+ constructVerificationMessage,
13
+ validateWalletAddress,
14
+ validateTimestamp,
15
+ validateQHash,
16
+ normalizeAddress,
17
+ isTerminalStatus,
18
+ isSuccessStatus,
19
+ isFailureStatus,
20
+ formatVerificationStatus,
21
+ formatTimestamp,
22
+ isSupportedChain,
23
+ StatusPoller,
24
+ computeContentHash,
25
+ deriveDid,
26
+ validateVerifierPayload,
27
+ buildVerificationRequest,
28
+ createVerificationData,
29
+ validateSignatureComponents,
30
+ withRetry,
31
+ delay,
32
+ NEUS_CONSTANTS
33
+ } from './utils.js';
34
+
35
+ // IPFS helpers
36
+ export const IPFS_GATEWAY = 'https://ipfs.neus.network/ipfs/';
37
+ export const toIpfsUrl = cid => `${IPFS_GATEWAY}${cid.replace(/^ipfs:\/\//, '')}`;
38
+ export const resolveIpfsUrl = cid => toIpfsUrl(cid);
39
+
40
+ // Error classes
41
+ export {
42
+ SDKError,
43
+ ApiError,
44
+ ValidationError,
45
+ NetworkError,
46
+ ConfigurationError,
47
+ VerificationError,
48
+ AuthenticationError
49
+ } from './errors.js';
50
+
51
+ // Convenience functions
52
+ export const verifyProof = async qHash => {
53
+ const { NeusClient } = await import('./client.js');
54
+ const client = new NeusClient();
55
+ return client.getStatus(qHash);
56
+ };
57
+
58
+ export const checkProofStatus = async proofId => {
59
+ const { NeusClient } = await import('./client.js');
60
+ const client = new NeusClient();
61
+ return client.getStatus(proofId);
62
+ };
63
+
64
+ // Default export
65
+ export default {
66
+ NeusClient: () => import('./client.js').then(m => m.NeusClient),
67
+ verifyProof,
68
+ toIpfsUrl,
69
+ resolveIpfsUrl
70
+ };
package/package.json ADDED
@@ -0,0 +1,75 @@
1
+ {
2
+ "name": "@neus/sdk",
3
+ "version": "1.0.0",
4
+ "description": "NEUS SDK - Create and verify cryptographic proofs with a simple, clean API",
5
+ "main": "index.js",
6
+ "type": "module",
7
+ "types": "types.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./types.d.ts",
11
+ "import": "./index.js",
12
+ "require": "./index.js"
13
+ },
14
+ "./client": "./client.js",
15
+ "./utils": "./utils.js",
16
+ "./errors": "./errors.js"
17
+ },
18
+ "sideEffects": false,
19
+ "scripts": {
20
+ "test": "vitest run",
21
+ "test:watch": "vitest",
22
+ "test:coverage": "vitest --coverage",
23
+ "lint": "eslint *.js",
24
+ "format": "prettier --write \"**/*.js\"",
25
+ "prepublishOnly": "cross-env NEUS_SDK_LIVE_TESTS=false npm run lint && npm test"
26
+ },
27
+ "keywords": [
28
+ "verification",
29
+ "cryptographic-proofs",
30
+ "identity",
31
+ "authentication",
32
+ "blockchain",
33
+ "cross-chain",
34
+ "web3",
35
+ "passwordless",
36
+ "universal-protocol"
37
+ ],
38
+ "author": "NEUS Network",
39
+ "license": "Apache-2.0",
40
+ "publishConfig": { "access": "public" },
41
+ "repository": {
42
+ "type": "git",
43
+ "url": "git+https://github.com/neus/network.git",
44
+ "directory": "sdk"
45
+ },
46
+ "bugs": {
47
+ "url": "https://github.com/neus/network/issues"
48
+ },
49
+ "homepage": "https://neus.network",
50
+ "engines": {
51
+ "node": ">=18.0.0"
52
+ },
53
+ "dependencies": {},
54
+ "peerDependencies": {
55
+ "ethers": "^6.0.0"
56
+ },
57
+ "devDependencies": {
58
+ "vitest": "^1.2.0",
59
+ "@vitest/coverage-v8": "^1.2.0",
60
+ "eslint": "^8.56.0",
61
+ "prettier": "^3.2.0",
62
+ "cross-env": "^7.0.3",
63
+ "ethers": "^6.0.0"
64
+ },
65
+ "files": [
66
+ "index.js",
67
+ "client.js",
68
+ "utils.js",
69
+ "errors.js",
70
+ "types.d.ts",
71
+ "README.md",
72
+ "LICENSE",
73
+ "SECURITY.md"
74
+ ]
75
+ }