-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkey.js
62 lines (56 loc) · 1.82 KB
/
key.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* Represents a cryptographic key.
*/
export class Key {
#key = new Uint8Array(1);
/**
* Creates a new Key instance.
*
* @param {string} type - The type of key, either "public" or "private".
* @param {Object} details - Details about the key.
* @param {Uint8Array} [details.fromByteArray] - The key as a byte array.
* @param {string} [details.fromHexString] - The key as a hexadecimal string.
* @throws {TypeError} If the type is not "public" or "private", or if details are provided but do not contain a valid key representation.
*/
constructor(type, details) {
if (type.toLowerCase() !== "public" && type.toLowerCase() !== "private")
throw new TypeError(
"Required property 'type' may only take 'public' or 'private' as values"
);
else this.keyType = type;
if (!details || (!details.fromByteArray && !details.fromHexString))
throw new TypeError(
"Missing required property 'fromByteArray' or 'fromHexString' in parameter 'details'"
);
if (details.fromByteArray && details.fromHexString)
console.warn(
"Both 'fromByteArray' and 'fromHexString' present. Value of 'fromHexString' will be used."
);
if (details.fromByteArray) {
this.#key = details.fromByteArray;
} else if (details.fromHexString) {
this.#key = Uint8Array.from(
details.fromHexString.match(/.{1,2}/g).map((byte) => parseInt(byte, 16))
);
}
}
/**
* Gets the key as a hexadecimal string.
*
* @returns {string} The key as a hexadecimal string.
*/
get asHexString() {
return this.#key.reduce(
(str, byte) => str + byte.toString(16).padStart(2, "0"),
""
);
}
/**
* Gets the key as a byte array.
*
* @returns {Uint8Array} The key as a byte array.
*/
get asByteArray() {
return this.#key;
}
}