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

Add Crypto.isValidAddress function #194

Merged
merged 1 commit into from
May 3, 2024
Merged
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
63 changes: 62 additions & 1 deletion src/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,17 @@ const hashAlgoMap = {
* @param {number} ID Hash algorithm's ID
*/
export function IDToHashAlgo(ID: number): HashAlgorithm {
const hashAlgo = Object.keys(hashAlgoMap).find((key) => hashAlgoMap[key as HashAlgorithm] === ID);
const hashAlgo = findHashAlgoById(ID);
if (hashAlgo === undefined) {
throw new Error("Hash algorithm not supported");
}
return hashAlgo as HashAlgorithm;
}

function findHashAlgoById(ID: number) {
return Object.keys(hashAlgoMap).find((key) => hashAlgoMap[key as HashAlgorithm] === ID);
}

/**
* Get the ID of a given hash algorithm
* @params {String} hashAlgo Hash algorithm
Expand Down Expand Up @@ -584,3 +588,60 @@ function aesAuthDecrypt(encrypted: Uint8Array, aesKey: Uint8Array, iv: Uint8Arra
}
return hexToUint8Array(sjcl.codec.hex.fromBits(decrypted));
}

/**
* Determines if an address is valid
* @param { string | UIntArray } address Address to verify
*/
export function isValidAddress(address: string | Uint8Array): boolean {
try {
const addressBinary = maybeHexToUint8Array(address);
const curveId = addressBinary[0];
if (!validCurveId(curveId)) {
return false;
}

const hashAlgoId = addressBinary[1];
if (!validHashAlgoId(hashAlgoId)) {
return false;
}

const digest = addressBinary.slice(2, addressBinary.length);
return validHash(findHashAlgoById(hashAlgoId) as HashAlgorithm, digest);
} catch (e) {
return false;
}
}

function validCurveId(id: number): boolean {
try {
IDToCurve(id);
return true;
} catch (e) {
return false;
}
}

function validHashAlgoId(id: number): boolean {
try {
IDToHashAlgo(id);
return true;
} catch (e) {
return false;
}
}

function validHash(hashAlgo: HashAlgorithm, digest: Uint8Array) {
switch (hashAlgo) {
case HashAlgorithm.sha256:
return digest.length == 32;
case HashAlgorithm.sha512:
return digest.length == 64;
case HashAlgorithm.sha3_256:
return digest.length == 32;
case HashAlgorithm.sha3_512:
return digest.length == 64;
case HashAlgorithm.blake2b:
return digest.length == 64;
}
}
59 changes: 58 additions & 1 deletion tests/crypto.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { deriveKeyPair, hash, sign, verify, ecDecrypt, ecEncrypt } from "../src/crypto";
import { deriveKeyPair, hash, sign, verify, ecDecrypt, ecEncrypt, isValidAddress } from "../src/crypto";
import { uint8ArrayToHex } from "../src/utils";
import { Curve, HashAlgorithm } from "../src/types";

Expand Down Expand Up @@ -115,4 +115,61 @@ describe("crypto", () => {
expect(ecDecrypt(ciphertext, keypair.privateKey)).toStrictEqual(blob);
});
});

describe("isValidAddress", () => {
it("should return false when the address is not hex", () => {
expect(isValidAddress("zzz")).toBeFalsy();
});

it("should return false when the curve id not valid", () => {
expect(isValidAddress("0500B54236A41380EC0D184FD3C643772243E2541F638F33C9AC5591C8E15B1D79B3")).toBeFalsy();
});

it("should return false when the hash algo not valid", () => {
expect(isValidAddress("0009B54236A41380EC0D184FD3C643772243E2541F638F33C9AC5591C8E15B1D79B3")).toBeFalsy();
});

it("should return false when the hash digest's length not valid", () => {
expect(isValidAddress("0000B54236A41380EC0D184FD3C643772243E2541F638F33C")).toBeFalsy();
expect(isValidAddress("0001B54236A41380EC0D184FD3C643772243E2541F638F33C")).toBeFalsy();
expect(isValidAddress("0002B54236A41380EC0D184FD3C643772243E2541F638F33C")).toBeFalsy();
expect(isValidAddress("0003B54236A41380EC0D184FD3C643772243E2541F638F33C")).toBeFalsy();
expect(isValidAddress("0004B54236A41380EC0D184FD3C643772243E2541F638F33C")).toBeFalsy();
});

it("should be valid with several curves", () => {
expect(isValidAddress("0000B54236A41380EC0D184FD3C643772243E2541F638F33C9AC5591C8E15B1D79B3")).toBeTruthy();
expect(isValidAddress("0100B54236A41380EC0D184FD3C643772243E2541F638F33C9AC5591C8E15B1D79B3")).toBeTruthy();
expect(isValidAddress("0200B54236A41380EC0D184FD3C643772243E2541F638F33C9AC5591C8E15B1D79B3")).toBeTruthy();
});

it("should be valid with several hash algorithms", () => {
//SHA256
expect(isValidAddress("0000B54236A41380EC0D184FD3C643772243E2541F638F33C9AC5591C8E15B1D79B3")).toBeTruthy();

//SHA512
expect(
isValidAddress(
"00015BE81A5EA91C1284D1D5EAC4F1CCB129DF4E6AE07D9A0EF2AF5E7907C0A990AD7779C848F52CF1617AC754E56EDF25C33539BEE56E44F46C216B9E6020BE391B"
)
).toBeTruthy();

//SHA3_256
expect(isValidAddress("0002B54236A41380EC0D184FD3C643772243E2541F638F33C9AC5591C8E15B1D79B3")).toBeTruthy();

//SHA3_512
expect(
isValidAddress(
"00035BE81A5EA91C1284D1D5EAC4F1CCB129DF4E6AE07D9A0EF2AF5E7907C0A990AD7779C848F52CF1617AC754E56EDF25C33539BEE56E44F46C216B9E6020BE391B"
)
).toBeTruthy();

//BLAKE2B
expect(
isValidAddress(
"00045BE81A5EA91C1284D1D5EAC4F1CCB129DF4E6AE07D9A0EF2AF5E7907C0A990AD7779C848F52CF1617AC754E56EDF25C33539BEE56E44F46C216B9E6020BE391B"
)
).toBeTruthy();
});
});
});
Loading