Skip to content

Commit

Permalink
refactor: add docs to encryption module
Browse files Browse the repository at this point in the history
  • Loading branch information
dawidsowardx committed Nov 22, 2024
1 parent 472e382 commit 979faa8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,14 @@ export const EncryptionModule = () => {
typedError,
)

const combineIVandCipherText = (iv: Buffer, ciphertext: Buffer): Buffer =>
Buffer.concat([iv, ciphertext])

/**
* Decrypts data using AES-GCM algorithm
*
* @param {Buffer} data - payload to be decrypted
* @param {Buffer} encryptionKey - key used for decryption
* @param {Buffer} iv - initialization vector used when data was encrypted
* @returns decrypted data wrapped inside ResultAsync
*/
const decrypt = (
data: Buffer,
encryptionKey: Buffer,
Expand All @@ -51,6 +56,14 @@ export const EncryptionModule = () => {
cryptoDecrypt(data, cryptoKey, iv),
)

/**
* Encrypts data using AES-GCM algorithm
*
* @param {Buffer} data - payload to be encrypted
* @param {Buffer} encryptionKey - key used for encryption
* @param {Buffer} iv - optional initialization vector
* @returns encrypted data wrapped inside ResultAsync
*/
const encrypt = (
data: Buffer,
encryptionKey: Buffer,
Expand All @@ -62,11 +75,15 @@ export const EncryptionModule = () => {
getKey(encryptionKey)
.andThen((cryptoKey) => cryptoEncrypt(data, cryptoKey, iv))
.map((ciphertext) => ({
combined: combineIVandCipherText(iv, ciphertext),
combined: Buffer.concat([iv, ciphertext]),
iv,
ciphertext,
}))

/**
* Creates 96 bits (12 bytes) initialization vector for AES-GCM encryption
* @returns {Buffer} 12 randomly generated bytes
*/
const createIV = () => Buffer.from(crypto.getRandomValues(new Uint8Array(12)))

return { encrypt, decrypt, createIV }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,13 @@ import { Result } from 'neverthrow'
import { readBuffer } from './buffer-reader'

export type SealedBoxProps = {
ciphertext: Buffer
iv: Buffer
authTag: Buffer
combined: Buffer
ciphertext: Buffer
ciphertextAndAuthTag: Buffer
}

const combineSealboxToBuffer = ({
iv,
ciphertext,
authTag,
}: Pick<SealedBoxProps, 'iv' | 'ciphertext' | 'authTag'>): Buffer =>
Buffer.concat([iv, ciphertext, authTag])

const combineCiphertextAndAuthtag = ({
ciphertext,
authTag,
}: Pick<SealedBoxProps, 'ciphertext' | 'authTag'>): Buffer =>
Buffer.concat([ciphertext, authTag])

export const transformBufferToSealbox = (
buffer: Buffer,
): Result<SealedBoxProps, Error> => {
Expand All @@ -39,10 +26,7 @@ export const transformBufferToSealbox = (
iv,
ciphertext,
authTag,
combined: combineSealboxToBuffer({ iv, ciphertext, authTag }),
ciphertextAndAuthTag: combineCiphertextAndAuthtag({
ciphertext,
authTag,
}),
combined: Buffer.concat([iv, ciphertext, authTag]),
ciphertextAndAuthTag: Buffer.concat([ciphertext, authTag]),
}))
}
6 changes: 0 additions & 6 deletions packages/dapp-toolkit/src/test-helpers/delay-async.ts

This file was deleted.

0 comments on commit 979faa8

Please sign in to comment.