Skip to content

Commit

Permalink
chore: refactor environment sensing
Browse files Browse the repository at this point in the history
  • Loading branch information
wemeetagain committed Jul 15, 2024
1 parent 301566e commit 3a3ea23
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/checksum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { sha256 } from "ethereum-cryptography/sha256";
import { concatBytes, equalsBytes, hexToBytes } from "ethereum-cryptography/utils";

import { IChecksumModule } from "./types";
import { hasWebCrypto } from "./env";

// default checksum configuration

Expand All @@ -27,7 +28,7 @@ export function checksum(mod: IChecksumModule, key: Uint8Array, ciphertext: Uint

export async function verifyChecksum(mod: IChecksumModule, key: Uint8Array, ciphertext: Uint8Array): Promise<boolean> {
if (mod.function === "sha256") {
if (globalThis?.crypto?.subtle) {
if (hasWebCrypto) {
return verifyChecksumWebCrypto(mod, key, ciphertext);
}
return equalsBytes(hexToBytes(mod.message), sha256(checksumData(key, ciphertext)));
Expand Down
5 changes: 3 additions & 2 deletions src/cipher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { encrypt as aesEncrypt, decrypt as aesDecrypt } from "ethereum-cryptogra

import { ICipherModule } from "./types";
import { bytesToHex, hexToBytes } from "ethereum-cryptography/utils";
import { hasWebCrypto } from "./env";

export function defaultAes128CtrModule(): Pick<ICipherModule, "function" | "params"> {
return {
Expand All @@ -16,7 +17,7 @@ export function defaultAes128CtrModule(): Pick<ICipherModule, "function" | "para
export async function cipherEncrypt(mod: ICipherModule, key: Uint8Array, data: Uint8Array): Promise<Uint8Array> {
if (mod.function === "aes-128-ctr") {
try {
if (globalThis?.crypto?.subtle) {
if (hasWebCrypto) {
return await cipherEncryptWebCrypto(mod, key, data);
}
return await aesEncrypt(
Expand Down Expand Up @@ -52,7 +53,7 @@ async function cipherEncryptWebCrypto(
export async function cipherDecrypt(mod: ICipherModule, key: Uint8Array): Promise<Uint8Array> {
if (mod.function === "aes-128-ctr") {
try {
if (globalThis?.crypto?.subtle) {
if (hasWebCrypto) {
return await cipherDecryptWebCrypto(mod, key);
}
return await aesDecrypt(
Expand Down
6 changes: 6 additions & 0 deletions src/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const isNode =
typeof process !== "undefined" &&
process.versions != null &&
process.versions.node != null;

export const hasWebCrypto = globalThis?.crypto?.subtle != null;
8 changes: 2 additions & 6 deletions src/kdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { scrypt } from "ethereum-cryptography/scrypt";
import { bytesToHex, hexToBytes } from "ethereum-cryptography/utils"

import { IKdfModule, IPbkdf2KdfModule, IScryptKdfModule } from "./types";
import { hasWebCrypto, isNode } from "./env";

// default kdf configurations

Expand Down Expand Up @@ -34,11 +35,6 @@ export function defaultScryptModule(): Pick<IScryptKdfModule, "function" | "para

// kdf operations

const isNode =
typeof process !== "undefined" &&
process.versions != null &&
process.versions.node != null;


export async function kdf(mod: IKdfModule, password: Uint8Array): Promise<Uint8Array> {
if (mod.function === "pbkdf2") {
Expand All @@ -53,7 +49,7 @@ async function doPbkdf2(params: IPbkdf2KdfModule["params"], password: Uint8Array
if (isNode) {
return await doPbkdf2Node(params, password);
}
if (globalThis?.crypto?.subtle) {
if (hasWebCrypto) {
return await doPbkdf2WebCrypto(params, password);
}
return pbkdf2(
Expand Down

0 comments on commit 3a3ea23

Please sign in to comment.