-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate_public_key.mjs
43 lines (35 loc) · 1.21 KB
/
validate_public_key.mjs
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
// @ts-check
import base58_to_binary from "base58-js/base58_to_binary.mjs";
import ripemd160 from "ripemd160-js/ripemd160.mjs";
/**
* Validate Antelope public key.
* @name validate_public_key
* @kind function
* @param {String} wif_public_key wallet import format Antelope public key.
* @returns {Promise<Boolean>} Returns true if valid
*/
export default async function validate_public_key(wif_public_key) {
let key_type;
const key_code = [
[75, 49],
[82, 49],
[87, 65],
];
if (wif_public_key.startsWith("PUB_K1_")) key_type = 0;
else if (wif_public_key.startsWith("PUB_R1_")) key_type = 1;
else if (wif_public_key.startsWith("PUB_WA_")) key_type = 2;
else
throw new Error("Public keys need to start with PUB_K1, PUB_R1 or PUB_WA.");
let public_key = wif_public_key
.replace("PUB_K1_", "")
.replace("PUB_R1_", "")
.replace("PUB_WA_", "");
const base58_str = base58_to_binary(public_key);
const checksum_check = base58_str.slice(-4);
const checksum = await ripemd160(
Uint8Array.from([...base58_str.slice(0, -4), ...key_code[key_type]])
);
for (let i = 0; i < 4; i++)
if (checksum[i] !== checksum_check[i]) throw new Error("Invalid checksum");
return true;
}