Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 🚨 Fix some typescript errors #223

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions src/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down
18 changes: 9 additions & 9 deletions src/keychain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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) {
Expand All @@ -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
};
}
Expand Down
Loading