bitcoincash-oauth-client 0.1.1 → 0.2.2
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/index.browser.min.js +1 -1
- package/dist/index.cjs +632 -101
- package/dist/index.d.ts +145 -15
- package/dist/index.mjs +627 -102
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -13,9 +13,9 @@ export interface Keypair {
|
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
export interface SecureStorage {
|
|
16
|
-
getItem(key: string): string | null
|
|
17
|
-
setItem(key: string, value: string): void
|
|
18
|
-
removeItem(key: string): void
|
|
16
|
+
getItem(key: string): string | null | Promise<string | null>;
|
|
17
|
+
setItem(key: string, value: string): void | Promise<void>;
|
|
18
|
+
removeItem(key: string): void | Promise<void>;
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
export interface OAuthClientOptions {
|
|
@@ -27,17 +27,29 @@ export interface OAuthClientOptions {
|
|
|
27
27
|
secureStorage?: SecureStorage;
|
|
28
28
|
/** Custom fetch implementation (optional) */
|
|
29
29
|
fetch?: typeof fetch;
|
|
30
|
+
/** Key for storing access token (default: "oauth_token") */
|
|
31
|
+
tokenKey?: string;
|
|
32
|
+
/** Key for storing refresh token (default: "oauth_refresh_token") */
|
|
33
|
+
refreshTokenKey?: string;
|
|
34
|
+
/** Enable automatic token refresh (default: true) */
|
|
35
|
+
autoRefresh?: boolean;
|
|
36
|
+
/** Seconds before expiry to trigger refresh (default: 300 = 5 minutes) */
|
|
37
|
+
refreshThreshold?: number;
|
|
38
|
+
/** Enable debug logging (default: false) */
|
|
39
|
+
debug?: boolean;
|
|
30
40
|
}
|
|
31
41
|
|
|
32
42
|
export interface AuthenticationResult {
|
|
33
43
|
/** JWT access token */
|
|
34
44
|
access_token: string;
|
|
35
45
|
/** Refresh token */
|
|
36
|
-
refresh_token
|
|
46
|
+
refresh_token?: string;
|
|
37
47
|
/** Token expiration in seconds */
|
|
38
48
|
expires_in: number;
|
|
39
49
|
/** Token type (e.g., "bearer") */
|
|
40
50
|
token_type: string;
|
|
51
|
+
/** Granted scopes */
|
|
52
|
+
scopes?: string[];
|
|
41
53
|
}
|
|
42
54
|
|
|
43
55
|
export interface RegistrationResult {
|
|
@@ -47,7 +59,71 @@ export interface RegistrationResult {
|
|
|
47
59
|
message?: string;
|
|
48
60
|
}
|
|
49
61
|
|
|
62
|
+
export interface AuthParams {
|
|
63
|
+
/** User ID */
|
|
64
|
+
userId: string;
|
|
65
|
+
/** Private key in hex format */
|
|
66
|
+
privateKeyHex: string;
|
|
67
|
+
/** Public key in hex format */
|
|
68
|
+
publicKeyHex: string;
|
|
69
|
+
/** Domain for authentication */
|
|
70
|
+
domain?: string;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Error Classes
|
|
74
|
+
|
|
75
|
+
export class OAuthError extends Error {
|
|
76
|
+
/** Error code */
|
|
77
|
+
code: string;
|
|
78
|
+
/** HTTP status code (if applicable) */
|
|
79
|
+
statusCode: number | null;
|
|
80
|
+
|
|
81
|
+
constructor(message: string, code: string, statusCode?: number | null);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export class NetworkError extends OAuthError {
|
|
85
|
+
/** Original error that caused this */
|
|
86
|
+
originalError: Error | null;
|
|
87
|
+
|
|
88
|
+
constructor(message: string, originalError?: Error | null);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export class AuthenticationError extends OAuthError {
|
|
92
|
+
constructor(message: string, statusCode: number, code?: string);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export class TokenExpiredError extends AuthenticationError {
|
|
96
|
+
constructor(message?: string);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export class UserNotFoundError extends AuthenticationError {
|
|
100
|
+
constructor(message?: string);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export class InvalidTokenError extends AuthenticationError {
|
|
104
|
+
constructor(message?: string);
|
|
105
|
+
}
|
|
106
|
+
|
|
50
107
|
export class BitcoinCashOAuthClient {
|
|
108
|
+
/** OAuth server URL */
|
|
109
|
+
serverUrl: string;
|
|
110
|
+
/** Network type */
|
|
111
|
+
network: string;
|
|
112
|
+
/** Storage interface for tokens */
|
|
113
|
+
secureStorage: SecureStorage | null;
|
|
114
|
+
/** Fetch implementation */
|
|
115
|
+
fetchImpl: typeof fetch;
|
|
116
|
+
/** Key for storing access token */
|
|
117
|
+
tokenKey: string;
|
|
118
|
+
/** Key for storing refresh token */
|
|
119
|
+
refreshTokenKey: string;
|
|
120
|
+
/** Enable automatic token refresh */
|
|
121
|
+
autoRefresh: boolean;
|
|
122
|
+
/** Seconds before expiry to trigger refresh */
|
|
123
|
+
refreshThreshold: number;
|
|
124
|
+
/** Enable debug logging */
|
|
125
|
+
debug: boolean;
|
|
126
|
+
|
|
51
127
|
constructor(options?: OAuthClientOptions);
|
|
52
128
|
|
|
53
129
|
/** Initialize the client by instantiating secp256k1 */
|
|
@@ -62,37 +138,91 @@ export class BitcoinCashOAuthClient {
|
|
|
62
138
|
/** Register a new user with the server */
|
|
63
139
|
register(address: string, userId?: string | null): Promise<RegistrationResult>;
|
|
64
140
|
|
|
65
|
-
/** Create authentication message (userId
|
|
66
|
-
createAuthMessage(userId: string, timestamp?: number | null): string;
|
|
141
|
+
/** Create authentication message (protocol|domain|userId|timestamp) */
|
|
142
|
+
createAuthMessage(userId: string, timestamp?: number | null, domain?: string | null): string;
|
|
67
143
|
|
|
68
144
|
/** Sign authentication message with private key */
|
|
69
145
|
signAuthMessage(message: string, privateKeyHex: string): Promise<string>;
|
|
70
146
|
|
|
71
|
-
/**
|
|
147
|
+
/**
|
|
148
|
+
* Authenticate with the server
|
|
149
|
+
* @param userId - User ID
|
|
150
|
+
* @param privateKeyHex - Private key in hex format
|
|
151
|
+
* @param publicKeyHex - Public key in hex format
|
|
152
|
+
* @param timestamp - Optional Unix timestamp (defaults to now)
|
|
153
|
+
* @param domain - Optional domain for message binding
|
|
154
|
+
*/
|
|
72
155
|
authenticate(
|
|
73
156
|
userId: string,
|
|
74
157
|
privateKeyHex: string,
|
|
75
158
|
publicKeyHex: string,
|
|
76
|
-
timestamp?: number | null
|
|
159
|
+
timestamp?: number | null,
|
|
160
|
+
domain?: string | null
|
|
77
161
|
): Promise<AuthenticationResult>;
|
|
78
162
|
|
|
79
|
-
/** Get stored token */
|
|
163
|
+
/** Get stored access token */
|
|
80
164
|
getToken(): string | null;
|
|
81
165
|
|
|
82
|
-
/**
|
|
83
|
-
|
|
166
|
+
/** Get stored refresh token */
|
|
167
|
+
getRefreshToken(): string | null;
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Validate if the current token is still valid
|
|
171
|
+
* @param serverCheck - If true, validates with the server; if false, only checks local expiry
|
|
172
|
+
*/
|
|
173
|
+
isTokenValid(serverCheck?: boolean): Promise<boolean>;
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Make authenticated request with automatic retry on token expiration
|
|
177
|
+
* @param endpoint - API endpoint (relative to serverUrl)
|
|
178
|
+
* @param options - Fetch options
|
|
179
|
+
* @param authParams - Parameters to re-authenticate if token expires (optional)
|
|
180
|
+
*/
|
|
181
|
+
authenticatedRequest(
|
|
182
|
+
endpoint: string,
|
|
183
|
+
options?: RequestInit,
|
|
184
|
+
authParams?: AuthParams | null
|
|
185
|
+
): Promise<Response>;
|
|
84
186
|
|
|
85
|
-
/**
|
|
187
|
+
/**
|
|
188
|
+
* Refresh access token using refresh token
|
|
189
|
+
* @param refreshToken - The refresh token
|
|
190
|
+
*/
|
|
86
191
|
refreshToken(refreshToken: string): Promise<AuthenticationResult>;
|
|
87
192
|
|
|
193
|
+
/**
|
|
194
|
+
* Refresh access token with automatic retry on expiration
|
|
195
|
+
* @param userId - User ID
|
|
196
|
+
* @param privateKeyHex - Private key in hex format
|
|
197
|
+
* @param publicKeyHex - Public key in hex format
|
|
198
|
+
* @param domain - Optional domain for authentication
|
|
199
|
+
*/
|
|
200
|
+
refreshAccessToken(
|
|
201
|
+
userId: string,
|
|
202
|
+
privateKeyHex: string,
|
|
203
|
+
publicKeyHex: string,
|
|
204
|
+
domain?: string | null
|
|
205
|
+
): Promise<AuthenticationResult>;
|
|
206
|
+
|
|
88
207
|
/** Revoke token */
|
|
89
|
-
revokeToken(token: string): Promise<{
|
|
208
|
+
revokeToken(token: string): Promise<{ success: boolean; message?: string }>;
|
|
209
|
+
|
|
210
|
+
/** Clean up resources (clear timers, etc.) */
|
|
211
|
+
destroy(): void;
|
|
90
212
|
|
|
91
213
|
// Utility methods
|
|
92
214
|
bytesToHex(bytes: Uint8Array): string;
|
|
93
215
|
hexToBytes(hex: string): Uint8Array;
|
|
94
|
-
sha256(data: Uint8Array): Promise<Uint8Array>;
|
|
95
|
-
ripemd160(data: Uint8Array): Promise<Uint8Array>;
|
|
96
216
|
}
|
|
97
217
|
|
|
218
|
+
// Utility functions
|
|
219
|
+
export function isBrowser(): boolean;
|
|
220
|
+
export function isNode(): boolean;
|
|
221
|
+
export function isCapacitor(): boolean;
|
|
222
|
+
export function isHybridApp(): boolean;
|
|
223
|
+
export function getFetch(userProvidedFetch?: typeof fetch | null): typeof fetch;
|
|
224
|
+
export function getRandomBytes(length: number): Promise<Uint8Array>;
|
|
225
|
+
export function sha256(data: Uint8Array): Promise<Uint8Array>;
|
|
226
|
+
export function ripemd160(data: Uint8Array): Promise<Uint8Array>;
|
|
227
|
+
|
|
98
228
|
export default BitcoinCashOAuthClient;
|