-
Notifications
You must be signed in to change notification settings - Fork 0
/
signature.mjs
38 lines (32 loc) · 1.04 KB
/
signature.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
import base58_to_binary from "base58-js/base58_to_binary.mjs";
import ripemd160 from "ripemd160-js/ripemd160.mjs";
/**
* Serialises an EOS signature to WASM hex string.
* @name signature
* @kind function
* @param {string} sig_string EOS signature to be serilaised.
* @returns {string} Hex string.
* @ignore
*/
async function signature(sig_string) {
if (!sig_string.startsWith("SIG_K1_"))
throw new Error("Invalid signature format, must start with “SIG_K1_”");
const signature_as_bin = base58_to_binary(sig_string.replace("SIG_K1_", ""));
const K1 = [75, 49]; // K1 as ascii
const raw_sig = signature_as_bin.slice(0, -4);
const hash = await ripemd160(new Uint8Array([...raw_sig, ...K1]));
hash.slice(0, 4).forEach((x, i) => {
if (x != signature_as_bin.slice(-4)[i])
throw new Error("Invalid signature checksum.");
});
const key_type = {
k1: "00",
r1: "01",
wa: "02",
};
return raw_sig.reduce(
(acc, i) => (acc += i.toString(16).padStart(2, "00")),
key_type.k1
);
}
export default signature;