-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathutil.js
58 lines (50 loc) · 1.54 KB
/
util.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
"use strict";
var cl = require("chloride");
var SSBURI = require("ssb-uri2");
exports.hash = function (data, enc) {
data =
"string" === typeof data && enc == null
? Buffer.from(data, "binary")
: Buffer.from(data, enc);
return cl.crypto_hash_sha256(data).toString("base64") + ".sha256";
};
exports.hasSigil = function hasSigil(s) {
return /^(@|%|&)/.test(s);
};
function tag(key, tag) {
if (!tag) throw new Error("no tag for:" + key.toString("base64"));
return key.toString("base64") + "." + tag.replace(/^\./, "");
}
exports.keysToJSON = function keysToJSON(keys, curve, feedFormat) {
curve = keys.curve || curve;
feedFormat = feedFormat || "classic";
var pub = tag(keys.public, curve);
let id = "@" + pub;
if (
feedFormat === "bendybutt-v1" ||
feedFormat === "buttwoo-v1" ||
feedFormat === "indexed-v1" ||
feedFormat === "gabbygrove-v1"
) {
const classicUri = SSBURI.fromFeedSigil(id);
const { type, data } = SSBURI.decompose(classicUri);
id = SSBURI.compose({ type, format: feedFormat, data });
}
return {
curve: curve,
public: pub,
private: keys.private ? tag(keys.private, curve) : undefined,
id,
};
};
exports.getTag = function getTag(string) {
var i = string.indexOf(".");
return string.substring(i + 1);
};
exports.toBuffer = function (buf) {
if (buf == null) return buf;
if (Buffer.isBuffer(buf)) return buf;
var i = buf.indexOf(".");
var start = exports.hasSigil(buf) ? 1 : 0;
return Buffer.from(buf.substring(start, ~i ? i : buf.length), "base64");
};