Skip to content

Commit cf9bd0c

Browse files
committed
refactor: enhance encryption options with new algorithm types and maintain backward compatibility
1 parent e213054 commit cf9bd0c

File tree

1 file changed

+57
-9
lines changed

1 file changed

+57
-9
lines changed

src/index.ts

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,42 @@ export interface EcdsaParams extends Algorithm {
8787
hash: AlgorithmIdentifier
8888
}
8989

90-
export type EncryptDecryptOptions = { name: string } | { algorithm: string, hash: string, salt?: string }
90+
export type BufferSource = ArrayBufferView | ArrayBuffer
91+
92+
export interface RsaOaepParams {
93+
name: 'RSA-OAEP'
94+
label?: BufferSource
95+
}
96+
97+
export interface AesCtrParams {
98+
name: 'AES-CTR'
99+
counter: BufferSource
100+
length: number
101+
}
102+
103+
export interface AesCbcParams {
104+
name: 'AES-CBC'
105+
iv: BufferSource
106+
}
107+
108+
export interface AesGcmParams {
109+
name: 'AES-GCM'
110+
iv: BufferSource
111+
additionalData?: BufferSource
112+
tagLength?: number
113+
}
114+
115+
// New API format (current)
116+
export type EncryptDecryptAlgorithm = RsaOaepParams | AesCtrParams | AesCbcParams | AesGcmParams
117+
118+
// Old deprecated format (for backwards compatibility)
119+
export interface DeprecatedEncryptDecryptOptions {
120+
algorithm: string
121+
hash: string
122+
salt?: string
123+
}
124+
125+
export type EncryptDecryptOptions = EncryptDecryptAlgorithm | DeprecatedEncryptDecryptOptions
91126

92127
export interface TokenInfo {
93128
id?: string
@@ -481,15 +516,21 @@ export class NodeArweaveWallet {
481516
* Requires ENCRYPT permission.
482517
*
483518
* @param data - The data to encrypt (string or Uint8Array)
484-
* @param options - Encryption options
485-
* @param options.algorithm - The encryption algorithm (e.g., 'RSA-OAEP')
486-
* @param options.hash - The hash algorithm (e.g., 'SHA-256')
487-
* @param options.salt - Optional salt value
519+
* @param options - Encryption algorithm parameters (RsaOaepParams, AesCtrParams, AesCbcParams, or AesGcmParams)
488520
* @returns A promise that resolves to the encrypted data as a Uint8Array
489521
* @throws {Error} If permission is not granted or encryption fails
490522
*
523+
* @see https://docs.wander.app/api/encrypt
524+
*
491525
* @example
492526
* ```typescript
527+
* // New API format (recommended)
528+
* const encrypted = await wallet.encrypt(
529+
* new TextEncoder().encode('Secret message'),
530+
* { name: 'RSA-OAEP' }
531+
* )
532+
*
533+
* // Old deprecated format (still supported)
493534
* const encrypted = await wallet.encrypt('Secret message', {
494535
* algorithm: 'RSA-OAEP',
495536
* hash: 'SHA-256'
@@ -510,15 +551,22 @@ export class NodeArweaveWallet {
510551
* Requires DECRYPT permission.
511552
*
512553
* @param data - The encrypted data as a Uint8Array
513-
* @param options - Decryption options
514-
* @param options.algorithm - The decryption algorithm (e.g., 'RSA-OAEP')
515-
* @param options.hash - The hash algorithm (e.g., 'SHA-256')
516-
* @param options.salt - Optional salt value
554+
* @param options - Decryption algorithm parameters (RsaOaepParams, AesCtrParams, AesCbcParams, or AesGcmParams)
517555
* @returns A promise that resolves to the decrypted data as a Uint8Array
518556
* @throws {Error} If permission is not granted or decryption fails
519557
*
558+
* @see https://docs.wander.app/api/decrypt
559+
*
520560
* @example
521561
* ```typescript
562+
* // New API format (recommended)
563+
* const decrypted = await wallet.decrypt(
564+
* encryptedData,
565+
* { name: 'RSA-OAEP' }
566+
* )
567+
* const text = new TextDecoder().decode(decrypted)
568+
*
569+
* // Old deprecated format (still supported)
522570
* const decrypted = await wallet.decrypt(encryptedData, {
523571
* algorithm: 'RSA-OAEP',
524572
* hash: 'SHA-256'

0 commit comments

Comments
 (0)