@opensecret/react 1.0.0-beta.4 → 1.0.1

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 CHANGED
@@ -91,7 +91,7 @@ The `useOpenSecret` hook provides access to the OpenSecret API. It returns an ob
91
91
  #### Account Management Methods
92
92
  - `refetchUser(): Promise<void>`: Refreshes the user's authentication state.
93
93
  - `changePassword(currentPassword: string, newPassword: string): Promise<void>`: Changes the user's password.
94
- - `generateThirdPartyToken(audience: string): Promise<{ token: string }>`: Generates a JWT token for use with pre-authorized third-party services (e.g. "https://api.devservice.com"). Developers must register this URL in advance (coming soon).
94
+ - `generateThirdPartyToken(audience?: string): Promise<{ token: string }>`: Generates a JWT token for use with third-party services. If an audience is provided, it can be any valid URL. If omitted, a token with no audience restriction will be generated.
95
95
 
96
96
  #### Cryptographic Methods
97
97
  - `getPrivateKey(): Promise<{ mnemonic: string }>`: Retrieves the user's private key mnemonic phrase. This is used for cryptographic operations and should be kept secure.
@@ -129,6 +129,36 @@ The `useOpenSecret` hook provides access to the OpenSecret API. It returns an ob
129
129
  const messageBytes = new Uint8Array(Buffer.from("deadbeef", "hex"));
130
130
  ```
131
131
 
132
+ - `encryptData(data: string, derivationPath?: string): Promise<{ encrypted_data: string }>`: Encrypts arbitrary string data using the user's private key.
133
+ - Uses AES-256-GCM encryption with a random nonce for each operation
134
+ - If derivation_path is provided, encryption uses the derived key at that path
135
+ - If derivation_path is omitted, encryption uses the master key
136
+ - Returns base64-encoded encrypted data containing the nonce, ciphertext, and authentication tag
137
+
138
+ Example:
139
+ ```typescript
140
+ // Encrypt with master key
141
+ const { encrypted_data } = await os.encryptData("Secret message");
142
+
143
+ // Encrypt with derived key
144
+ const { encrypted_data } = await os.encryptData("Secret message", "m/44'/0'/0'/0/0");
145
+ ```
146
+
147
+ - `decryptData(encryptedData: string, derivationPath?: string): Promise<string>`: Decrypts data that was previously encrypted with the user's key.
148
+ - Uses AES-256-GCM decryption with authentication tag verification
149
+ - If derivation_path is provided, decryption uses the derived key at that path
150
+ - If derivation_path is omitted, decryption uses the master key
151
+ - The encrypted_data must be in the exact format returned by the encrypt endpoint
152
+
153
+ Example:
154
+ ```typescript
155
+ // Decrypt with master key
156
+ const decrypted = await os.decryptData(encrypted_data);
157
+
158
+ // Decrypt with derived key (must use same path as encryption)
159
+ const decrypted = await os.decryptData(encrypted_data, "m/44'/0'/0'/0/0");
160
+ ```
161
+
132
162
  ### AI Integration
133
163
 
134
164
  To get encrypted-to-the-gpu AI chat we provide a special version of `fetch` (`os.aiCustomFetch`) that handles all the encryption. Because we require the user to be logged in, and do the encryption client-side, this is safe to call from the client.
@@ -212,3 +242,4 @@ NPM_CONFIG_TOKEN=$NPM_CONFIG_TOKEN bun publish --access public
212
242
  ## License
213
243
 
214
244
  This project is licensed under the MIT License.
245
+
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { default as default_2 } from 'react';
2
- import { JSX as JSX_2 } from 'react/jsx-runtime';
2
+ import { JSX } from 'react/jsx-runtime';
3
3
  import { z } from 'zod';
4
4
 
5
5
  declare function acceptInvite(code: string): Promise<{
@@ -38,6 +38,8 @@ declare namespace api {
38
38
  fetchPublicKey,
39
39
  convertGuestToEmailAccount,
40
40
  generateThirdPartyToken,
41
+ encryptData,
42
+ decryptData,
41
43
  LoginResponse,
42
44
  UserResponse,
43
45
  KVListItem,
@@ -49,7 +51,10 @@ declare namespace api {
49
51
  SignMessageRequest,
50
52
  PublicKeyResponse,
51
53
  ThirdPartyTokenRequest,
52
- ThirdPartyTokenResponse
54
+ ThirdPartyTokenResponse,
55
+ EncryptDataRequest,
56
+ EncryptDataResponse,
57
+ DecryptDataRequest
53
58
  }
54
59
  }
55
60
 
@@ -193,6 +198,31 @@ declare function createProject(orgId: string, name: string, description?: string
193
198
 
194
199
  declare function createProjectSecret(orgId: string, projectId: string, keyName: string, secret: string): Promise<ProjectSecret>;
195
200
 
201
+ /**
202
+ * Decrypts data that was previously encrypted with the user's key
203
+ * @param encryptedData - Base64-encoded encrypted data string
204
+ * @param derivationPath - Optional BIP32 derivation path
205
+ * @returns A promise resolving to the decrypted string
206
+ * @throws {Error} If:
207
+ * - The encrypted data is malformed
208
+ * - The derivation path is invalid
209
+ * - Authentication fails
210
+ * - Server-side decryption error occurs
211
+ *
212
+ * @description
213
+ * - Uses AES-256-GCM decryption with authentication tag verification
214
+ * - Extracts the nonce from the first 12 bytes of the encrypted data
215
+ * - If derivation_path is provided, decryption uses the derived key at that path
216
+ * - If derivation_path is omitted, decryption uses the master key
217
+ * - The encrypted_data must be in the exact format returned by the encrypt endpoint
218
+ */
219
+ declare function decryptData(encryptedData: string, derivationPath?: string): Promise<string>;
220
+
221
+ declare type DecryptDataRequest = {
222
+ encrypted_data: string;
223
+ derivation_path?: string;
224
+ };
225
+
196
226
  declare function deleteOrganization(orgId: string): Promise<void>;
197
227
 
198
228
  declare function deleteOrganizationInvite(orgId: string, inviteCode: string): Promise<{
@@ -215,6 +245,37 @@ declare type EmailSettings = {
215
245
  email_verification_url: string;
216
246
  };
217
247
 
248
+ /**
249
+ * Encrypts arbitrary string data using the user's private key
250
+ * @param data - String content to be encrypted
251
+ * @param derivationPath - Optional BIP32 derivation path
252
+ * @returns A promise resolving to the encrypted data response
253
+ * @throws {Error} If:
254
+ * - The derivation path is invalid
255
+ * - Authentication fails
256
+ * - Server-side encryption error occurs
257
+ *
258
+ * @description
259
+ * - Encrypts data with AES-256-GCM
260
+ * - A random nonce is generated for each encryption operation (included in the result)
261
+ * - If derivation_path is provided, encryption uses the derived key at that path
262
+ * - If derivation_path is omitted, encryption uses the master key
263
+ * - The encrypted_data format:
264
+ * - First 12 bytes: Nonce used for encryption (prepended to ciphertext)
265
+ * - Remaining bytes: AES-256-GCM encrypted ciphertext + authentication tag
266
+ * - The entire payload is base64-encoded for safe transport
267
+ */
268
+ declare function encryptData(data: string, derivationPath?: string): Promise<EncryptDataResponse>;
269
+
270
+ declare type EncryptDataRequest = {
271
+ data: string;
272
+ derivation_path?: string;
273
+ };
274
+
275
+ declare type EncryptDataResponse = {
276
+ encrypted_data: string;
277
+ };
278
+
218
279
  declare const EXPECTED_ROOT_CERT_HASH = "641a0321a3e244efe456463195d606317ed7cdcc3c1756e09893f3c68f79bb5b";
219
280
 
220
281
  declare function fetchAttestationDocument(nonce: string, explicitApiUrl?: string): Promise<string>;
@@ -274,7 +335,16 @@ declare function fetchUser(): Promise<UserResponse>;
274
335
 
275
336
  export declare function generateSecureSecret(): string;
276
337
 
277
- declare function generateThirdPartyToken(audience: string): Promise<ThirdPartyTokenResponse>;
338
+ /**
339
+ * Generates a JWT token for use with third-party services
340
+ * @param audience - Optional URL of the service
341
+ * @returns A promise resolving to the token response containing the JWT
342
+ *
343
+ * @description
344
+ * - If audience is provided, it can be any valid URL
345
+ * - If audience is omitted, a token with no audience restriction will be generated
346
+ */
347
+ declare function generateThirdPartyToken(audience?: string): Promise<ThirdPartyTokenResponse>;
278
348
 
279
349
  declare function getApiUrl(): string;
280
350
 
@@ -632,21 +702,61 @@ export declare type OpenSecretContextType = {
632
702
  */
633
703
  getAttestationDocument: () => Promise<ParsedAttestationView>;
634
704
  /**
635
- * Generates a JWT token for use with authorized third-party services
636
- * @param audience - The URL of the authorized service (e.g. "https://billing.opensecret.cloud")
705
+ * Generates a JWT token for use with third-party services
706
+ * @param audience - Optional URL of the service (e.g. "https://billing.opensecret.cloud")
637
707
  * @returns A promise resolving to the token response
638
708
  * @throws {Error} If:
639
709
  * - The user is not authenticated
640
- * - The audience URL is invalid
641
- * - The audience URL is not authorized
710
+ * - The audience URL is invalid (if provided)
642
711
  *
643
712
  * @description
644
- * - Generates a signed JWT token for use with specific authorized third-party services
645
- * - The audience must be an pre-authorized URL registered by the developer (e.g. api.devservice.com)
713
+ * - Generates a signed JWT token for use with third-party services
714
+ * - If audience is provided, it can be any valid URL
715
+ * - If audience is omitted, a token with no audience restriction will be generated
646
716
  * - Requires an active authentication session
647
717
  * - Token can be used to authenticate with the specified service
648
718
  */
649
- generateThirdPartyToken: (audience: string) => Promise<ThirdPartyTokenResponse>;
719
+ generateThirdPartyToken: (audience?: string) => Promise<ThirdPartyTokenResponse>;
720
+ /**
721
+ * Encrypts arbitrary string data using the user's private key
722
+ * @param data - String content to be encrypted
723
+ * @param derivationPath? - Optional BIP32 derivation path (e.g., 'm/44'/0'/0'/0/0')
724
+ * @returns A promise resolving to the encrypted data response
725
+ * @throws {Error} If:
726
+ * - The derivation path is invalid
727
+ * - Authentication fails
728
+ * - Server-side encryption error occurs
729
+ *
730
+ * @description
731
+ * - Encrypts data with AES-256-GCM
732
+ * - A random nonce is generated for each encryption operation (included in the result)
733
+ * - If derivation_path is provided, encryption uses the derived key at that path
734
+ * - If derivation_path is omitted, encryption uses the master key
735
+ * - The encrypted_data format:
736
+ * - First 12 bytes: Nonce used for encryption (prepended to ciphertext)
737
+ * - Remaining bytes: AES-256-GCM encrypted ciphertext + authentication tag
738
+ * - The entire payload is base64-encoded for safe transport
739
+ */
740
+ encryptData: typeof api.encryptData;
741
+ /**
742
+ * Decrypts data that was previously encrypted with the user's key
743
+ * @param encryptedData - Base64-encoded encrypted data string
744
+ * @param derivationPath? - Optional BIP32 derivation path (e.g., 'm/44'/0'/0'/0/0')
745
+ * @returns A promise resolving to the decrypted string
746
+ * @throws {Error} If:
747
+ * - The encrypted data is malformed
748
+ * - The derivation path is invalid
749
+ * - Authentication fails
750
+ * - Server-side decryption error occurs
751
+ *
752
+ * @description
753
+ * - Uses AES-256-GCM decryption with authentication tag verification
754
+ * - Extracts the nonce from the first 12 bytes of the encrypted data
755
+ * - If derivation_path is provided, decryption uses the derived key at that path
756
+ * - If derivation_path is omitted, decryption uses the master key
757
+ * - The encrypted_data must be in the exact format returned by the encrypt endpoint
758
+ */
759
+ decryptData: typeof api.decryptData;
650
760
  };
651
761
 
652
762
  /**
@@ -670,7 +780,7 @@ export declare function OpenSecretDeveloper({ children, apiUrl, pcrConfig }: {
670
780
  children: default_2.ReactNode;
671
781
  apiUrl: string;
672
782
  pcrConfig?: PcrConfig;
673
- }): JSX_2.Element;
783
+ }): JSX.Element;
674
784
 
675
785
  export declare type OpenSecretDeveloperAuthState = {
676
786
  loading: boolean;
@@ -1034,7 +1144,7 @@ export declare function OpenSecretProvider({ children, apiUrl, clientId, pcrConf
1034
1144
  apiUrl: string;
1035
1145
  clientId: string;
1036
1146
  pcrConfig?: PcrConfig;
1037
- }): JSX_2.Element;
1147
+ }): JSX.Element;
1038
1148
 
1039
1149
  declare type Organization = {
1040
1150
  id: string;
@@ -1327,7 +1437,7 @@ declare type SignMessageResponse = {
1327
1437
  };
1328
1438
 
1329
1439
  declare type ThirdPartyTokenRequest = {
1330
- audience: string;
1440
+ audience?: string;
1331
1441
  };
1332
1442
 
1333
1443
  declare type ThirdPartyTokenResponse = {