From 8a1fa59f129dd0c7ee0e72e1981e6fe36c76ed98 Mon Sep 17 00:00:00 2001 From: Samuel Manzanera Date: Fri, 18 Oct 2024 14:51:30 +0200 Subject: [PATCH] refactor: :rotating_light: Fix some typescript errors --- src/crypto.ts | 24 ++++++++---------------- src/keychain.ts | 18 +++++++++--------- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/src/crypto.ts b/src/crypto.ts index fde9375..3ea5d31 100644 --- a/src/crypto.ts +++ b/src/crypto.ts @@ -82,6 +82,7 @@ export function hashAlgoToID(hashAlgo: HashAlgorithm): number { * @returns {Uint8Array} Hash digest */ export function getHashDigest(content: string | Uint8Array, algo: HashAlgorithm): Uint8Array { + content = maybeStringToUint8Array(content) switch (algo) { case HashAlgorithm.sha256: { const input = CryptoJS.lib.WordArray.create(content); @@ -184,11 +185,8 @@ export function derivePrivateKey(seed: string | Uint8Array, index: number = 0): throw new Error("Index must be a positive number"); } - //Convert seed to Uint8Array - seed = CryptoJS.lib.WordArray.create(maybeStringToUint8Array(seed)); - //Derive master keys - const hash = wordArrayToUint8Array(CryptoJS.SHA512(seed)); + const hash = wordArrayToUint8Array(CryptoJS.SHA512(CryptoJS.lib.WordArray.create(maybeStringToUint8Array(seed)))); const masterKey = hash.subarray(0, 32); const masterEntropy = hash.subarray(32, 64); @@ -292,8 +290,7 @@ function getKeypair(pvKey: string | Uint8Array, curve: Curve): { publicKey: Uint } // Uniform key's seed if (pvKey.length < 32) { - pvKey = CryptoJS.lib.WordArray.create(pvKey); - pvKey = wordArrayToUint8Array(CryptoJS.SHA256(pvKey)); + pvKey = wordArrayToUint8Array(CryptoJS.SHA256(CryptoJS.lib.WordArray.create(pvKey))); } if (pvKey.length > 32) { @@ -352,14 +349,12 @@ export function sign(data: string | Uint8Array, privateKey: string | Uint8Array) return nacl.sign.detached(data, secretKey); } case 1: { - data = CryptoJS.lib.WordArray.create(data); - const msgHash = wordArrayToUint8Array(CryptoJS.SHA256(data)); + const msgHash = wordArrayToUint8Array(CryptoJS.SHA256(CryptoJS.lib.WordArray.create(data))); const key = ec_P256.keyFromPrivate(pvBuf); return Uint8Array.from(key.sign(msgHash).toDER()); } case 2: { - data = CryptoJS.lib.WordArray.create(data); - const msgHash = wordArrayToUint8Array(CryptoJS.SHA256(data)); + const msgHash = wordArrayToUint8Array(CryptoJS.SHA256(CryptoJS.lib.WordArray.create(data))); const key = ec_secp256k1.keyFromPrivate(pvBuf); return Uint8Array.from(key.sign(msgHash).toDER()); } @@ -389,14 +384,12 @@ export function verify(sig: string | Uint8Array, data: string | Uint8Array, publ return nacl.sign.detached.verify(data, sig, pubBuf); } case 1: { - data = CryptoJS.lib.WordArray.create(data); - const msgHash = wordArrayToUint8Array(CryptoJS.SHA256(data)); + const msgHash = wordArrayToUint8Array(CryptoJS.SHA256(CryptoJS.lib.WordArray.create(data))); const key = ec_P256.keyFromPublic(pubBuf); return key.verify(msgHash, sig); } case 2: { - data = CryptoJS.lib.WordArray.create(data); - const msgHash = wordArrayToUint8Array(CryptoJS.SHA256(data)); + const msgHash = wordArrayToUint8Array(CryptoJS.SHA256(CryptoJS.lib.WordArray.create(data))); const key = ec_secp256k1.keyFromPublic(pubBuf); return key.verify(msgHash, sig); } @@ -557,8 +550,7 @@ export function aesDecrypt(cipherText: string | Uint8Array, aesKey: string | Uin * @returns {Object} {aesKey: Uint8Array, iv: Uint8Array} */ function deriveSecret(sharedKey: Uint8Array): { aesKey: Uint8Array; iv: Uint8Array } { - sharedKey = CryptoJS.lib.WordArray.create(sharedKey); - const pseudoRandomKey = CryptoJS.SHA256(sharedKey); + const pseudoRandomKey = CryptoJS.SHA256(CryptoJS.lib.WordArray.create(sharedKey)); const iv = wordArrayToUint8Array(CryptoJS.HmacSHA256("0", pseudoRandomKey)).subarray(0, 32); const aesKey = wordArrayToUint8Array(CryptoJS.HmacSHA256("1", CryptoJS.lib.WordArray.create(iv))).subarray(0, 32); diff --git a/src/keychain.ts b/src/keychain.ts index a278f95..0997189 100644 --- a/src/keychain.ts +++ b/src/keychain.ts @@ -330,9 +330,7 @@ function deriveArchethicKeypair( } function deriveServiceSeed(seed: string | Uint8Array, derivationPath: string, index: number, pathSuffix: string = "") { - seed = CryptoJS.lib.WordArray.create(maybeHexToUint8Array(seed)); - - let hashedPath = ""; + let hashedPath; if (isPathWithIndex(derivationPath)) { //Hash the derivation path hashedPath = CryptoJS.SHA256(replaceDerivationPathIndex(derivationPath, pathSuffix, index)); @@ -343,7 +341,9 @@ function deriveServiceSeed(seed: string | Uint8Array, derivationPath: string, in hashedPath = CryptoJS.SHA256(path.concat([serviceName]).join("/")); } - return wordArrayToUint8Array(CryptoJS.HmacSHA512(hashedPath, seed)).subarray(0, 32); + return wordArrayToUint8Array( + CryptoJS.HmacSHA512(hashedPath, CryptoJS.lib.WordArray.create(maybeHexToUint8Array(seed))) + ).subarray(0, 32); } function isPathWithIndex(path: string) { @@ -367,23 +367,23 @@ export function keyToJWK(publicKey: Uint8Array, keyID: string) { return { kty: "OKP", crv: "Ed25519", - x: base64url(key), + x: base64url(key.buffer), kid: keyID }; case 1: return { kty: "EC", crv: "P-256", - x: base64url(key.subarray(16)), - y: base64url(key.subarray(-16)), + x: base64url(key.subarray(16).buffer), + y: base64url(key.subarray(-16).buffer), kid: keyID }; case 2: return { kty: "EC", crv: "secp256k1", - x: base64url(key.subarray(16)), - y: base64url(key.subarray(-16)), + x: base64url(key.subarray(16).buffer), + y: base64url(key.subarray(-16).buffer), kid: keyID }; }