This repository has been archived by the owner on May 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
113 lines (104 loc) · 3.21 KB
/
index.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// SPDX-FileCopyrightText: 2021 Andrew 'glyph' Reid
//
// SPDX-License-Identifier: LGPL-3.0-only
const v = require("node-bindgen-loader")({
moduleName: "ssb-validate2-rsjs-node",
dir: __dirname,
});
const stringify = (msg) => JSON.stringify(msg, null, 2);
const verifySignatures = (hmacKey, msgs, cb) => {
if (!Array.isArray(msgs)) {
cb(new Error("input must be an array of message objects"));
return;
}
const jsonMsgs = msgs.map(stringify);
// convert `null` and `undefined` to a string ("none") for easier matching in rustland
if (!hmacKey) hmacKey = "none";
const [err, result] = v.verifySignatures(hmacKey, jsonMsgs);
if (err) {
cb(new Error(err));
return;
}
cb(err, result);
};
const validateSingle = (hmacKey, msg, previous, cb) => {
const jsonMsg = stringify(msg);
// convert `null` and `undefined` to a string ("none") for easier matching in rustland
if (!hmacKey) hmacKey = "none";
let err;
let result;
if (previous) {
const jsonPrevious = stringify(previous);
// `result` is a string of the hash (`key`) for the given `jsonMsg` value
[err, result] = v.validateSingle(hmacKey, jsonMsg, jsonPrevious);
} else {
[err, result] = v.validateSingle(hmacKey, jsonMsg);
}
if (err) {
cb(new Error(err));
return;
}
cb(err, result);
};
const validateBatch = (hmacKey, msgs, previous, cb) => {
if (!Array.isArray(msgs)) {
cb(new Error("input must be an array of message objects"));
return;
}
const jsonMsgs = msgs.map(stringify);
if (!hmacKey) hmacKey = "none";
let err;
let result;
if (previous) {
const jsonPrevious = stringify(previous);
// `result` is an array of strings (each string a `key`) for the given `jsonMsgs`
[err, result] = v.validateBatch(hmacKey, jsonMsgs, jsonPrevious);
} else {
[err, result] = v.validateBatch(hmacKey, jsonMsgs);
}
if (err) {
cb(new Error(err));
return;
}
cb(err, result);
};
const validateOOOBatch = (hmacKey, msgs, cb) => {
if (!Array.isArray(msgs)) {
cb(new Error("input must be an array of message objects"));
return;
}
const jsonMsgs = msgs.map(stringify);
if (!hmacKey) hmacKey = "none";
const [err, result] = v.validateOOOBatch(hmacKey, jsonMsgs);
if (err) {
cb(new Error(err));
return;
}
cb(err, result);
};
const validateMultiAuthorBatch = (hmacKey, msgs, cb) => {
if (!Array.isArray(msgs)) {
cb(new Error("input must be an array of message objects"));
return;
}
const jsonMsgs = msgs.map(stringify);
if (!hmacKey) hmacKey = "none";
const [err, result] = v.validateMultiAuthorBatch(hmacKey, jsonMsgs);
if (err) {
cb(new Error(err));
return;
}
cb(err, result);
};
// Mirrors the `ready` function for the `web` version of `ssb-validate2-rsjs`.
// The function initializes WASM and WebWorkers in `web`. We define it here with
// a callback so that both libraries can be safely called with the same code.
const ready = (cb) => {
cb();
};
module.exports.ready = ready;
module.exports.verifySignatures = verifySignatures;
module.exports.validateSingle = validateSingle;
module.exports.validateBatch = validateBatch;
module.exports.validateOOOBatch = validateOOOBatch;
module.exports.validateMultiAuthorBatch = validateMultiAuthorBatch;