@fedify/fedify 1.5.0-dev.709 → 1.5.0-dev.714
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGES.md +6 -0
- package/esm/deno.js +1 -2
- package/esm/runtime/key.js +1 -1
- package/esm/runtime/multibase/base.js +30 -0
- package/esm/runtime/multibase/constants.js +78 -0
- package/esm/runtime/multibase/index.js +59 -0
- package/esm/runtime/multibase/rfc4648.js +79 -0
- package/esm/runtime/multibase/util.js +13 -0
- package/esm/vocab/vocab.js +177 -177
- package/package.json +2 -2
- package/types/deno.d.ts +0 -1
- package/types/runtime/multibase/base.d.ts +15 -0
- package/types/runtime/multibase/base.d.ts.map +1 -0
- package/types/runtime/multibase/constants.d.ts +5 -0
- package/types/runtime/multibase/constants.d.ts.map +1 -0
- package/types/runtime/multibase/index.d.ts +24 -0
- package/types/runtime/multibase/index.d.ts.map +1 -0
- package/types/runtime/multibase/rfc4648.d.ts +6 -0
- package/types/runtime/multibase/rfc4648.d.ts.map +1 -0
- package/types/runtime/multibase/util.d.ts +4 -0
- package/types/runtime/multibase/util.d.ts.map +1 -0
package/CHANGES.md
CHANGED
@@ -37,8 +37,14 @@ To be released.
|
|
37
37
|
the page.
|
38
38
|
- You can easily copy the fediverse handle of the ephemeral actor.
|
39
39
|
|
40
|
+
- Internalized the [multibase] package, which is obsolete and no longer
|
41
|
+
maintained. [[#127], [#215] by Fróði Karlsson]
|
42
|
+
|
43
|
+
[#127]: https://github.com/fedify-dev/fedify/issues/127
|
40
44
|
[#209]: https://github.com/fedify-dev/fedify/issues/209
|
41
45
|
[#211]: https://github.com/fedify-dev/fedify/issues/211
|
46
|
+
[#215]: https://github.com/fedify-dev/fedify/pull/215
|
47
|
+
[multibase]: https://github.com/multiformats/js-multibase
|
42
48
|
|
43
49
|
|
44
50
|
Version 1.4.6
|
package/esm/deno.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
export default {
|
2
2
|
"name": "@fedify/fedify",
|
3
|
-
"version": "1.5.0-dev.
|
3
|
+
"version": "1.5.0-dev.714+56abdd05",
|
4
4
|
"license": "MIT",
|
5
5
|
"exports": {
|
6
6
|
".": "./mod.ts",
|
@@ -40,7 +40,6 @@ export default {
|
|
40
40
|
"json-canon": "npm:json-canon@^1.0.1",
|
41
41
|
"jsonld": "npm:jsonld@^8.3.2",
|
42
42
|
"mock_fetch": "jsr:@hongminhee/deno-mock-fetch@^0.3.2",
|
43
|
-
"multibase": "npm:multibase@^4.0.6",
|
44
43
|
"multicodec": "npm:multicodec@^3.2.1",
|
45
44
|
"pkijs": "npm:pkijs@^3.2.4",
|
46
45
|
"uri-template-router": "npm:uri-template-router@^0.0.16",
|
package/esm/runtime/key.js
CHANGED
@@ -4,7 +4,7 @@ import { decodeBase64, encodeBase64 } from "../deps/jsr.io/@std/encoding/1.0.7/b
|
|
4
4
|
import { decodeBase64Url } from "../deps/jsr.io/@std/encoding/1.0.7/base64url.js";
|
5
5
|
import { decodeHex } from "../deps/jsr.io/@std/encoding/1.0.7/hex.js";
|
6
6
|
import { Integer, Sequence } from "asn1js";
|
7
|
-
import { decode, encode } from "multibase";
|
7
|
+
import { decode, encode } from "./multibase/index.js";
|
8
8
|
import { addPrefix, getCodeFromData, rmPrefix } from "multicodec";
|
9
9
|
import { createPublicKey } from "node:crypto";
|
10
10
|
import { PublicKeyInfo } from "pkijs";
|
@@ -0,0 +1,30 @@
|
|
1
|
+
import { encodeText } from "./util.js";
|
2
|
+
/**
|
3
|
+
* Class to encode/decode in the supported Bases
|
4
|
+
*/
|
5
|
+
export class Base {
|
6
|
+
name;
|
7
|
+
code;
|
8
|
+
alphabet;
|
9
|
+
codeBuf;
|
10
|
+
codec;
|
11
|
+
constructor(name, code, factory, alphabet) {
|
12
|
+
this.name = name;
|
13
|
+
this.code = code;
|
14
|
+
this.alphabet = alphabet;
|
15
|
+
this.codeBuf = encodeText(this.code);
|
16
|
+
this.alphabet = alphabet;
|
17
|
+
this.codec = factory(alphabet);
|
18
|
+
}
|
19
|
+
encode(buf) {
|
20
|
+
return this.codec.encode(buf);
|
21
|
+
}
|
22
|
+
decode(string) {
|
23
|
+
for (const char of string) {
|
24
|
+
if (this.alphabet && this.alphabet.indexOf(char) < 0) {
|
25
|
+
throw new Error(`invalid character '${char}' in '${string}'`);
|
26
|
+
}
|
27
|
+
}
|
28
|
+
return this.codec.decode(string);
|
29
|
+
}
|
30
|
+
}
|
@@ -0,0 +1,78 @@
|
|
1
|
+
import baseX from "@multiformats/base-x";
|
2
|
+
import { Base } from "./base.js";
|
3
|
+
import { rfc4648 } from "./rfc4648.js";
|
4
|
+
import { decodeText, encodeText } from "./util.js";
|
5
|
+
const identity = () => {
|
6
|
+
return {
|
7
|
+
encode: decodeText,
|
8
|
+
decode: encodeText,
|
9
|
+
};
|
10
|
+
};
|
11
|
+
/**
|
12
|
+
* name, code, implementation, alphabet
|
13
|
+
*
|
14
|
+
* @type {Array<[BaseName, BaseCode, CodecFactory, string]>}
|
15
|
+
*/
|
16
|
+
const constants = [
|
17
|
+
["identity", "\x00", identity, ""],
|
18
|
+
["base2", "0", rfc4648(1), "01"],
|
19
|
+
["base8", "7", rfc4648(3), "01234567"],
|
20
|
+
["base10", "9", baseX, "0123456789"],
|
21
|
+
["base16", "f", rfc4648(4), "0123456789abcdef"],
|
22
|
+
["base16upper", "F", rfc4648(4), "0123456789ABCDEF"],
|
23
|
+
["base32hex", "v", rfc4648(5), "0123456789abcdefghijklmnopqrstuv"],
|
24
|
+
["base32hexupper", "V", rfc4648(5), "0123456789ABCDEFGHIJKLMNOPQRSTUV"],
|
25
|
+
["base32hexpad", "t", rfc4648(5), "0123456789abcdefghijklmnopqrstuv="],
|
26
|
+
["base32hexpadupper", "T", rfc4648(5), "0123456789ABCDEFGHIJKLMNOPQRSTUV="],
|
27
|
+
["base32", "b", rfc4648(5), "abcdefghijklmnopqrstuvwxyz234567"],
|
28
|
+
["base32upper", "B", rfc4648(5), "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"],
|
29
|
+
["base32pad", "c", rfc4648(5), "abcdefghijklmnopqrstuvwxyz234567="],
|
30
|
+
["base32padupper", "C", rfc4648(5), "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567="],
|
31
|
+
["base32z", "h", rfc4648(5), "ybndrfg8ejkmcpqxot1uwisza345h769"],
|
32
|
+
["base36", "k", baseX, "0123456789abcdefghijklmnopqrstuvwxyz"],
|
33
|
+
["base36upper", "K", baseX, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"],
|
34
|
+
[
|
35
|
+
"base58btc",
|
36
|
+
"z",
|
37
|
+
baseX,
|
38
|
+
"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz",
|
39
|
+
],
|
40
|
+
[
|
41
|
+
"base58flickr",
|
42
|
+
"Z",
|
43
|
+
baseX,
|
44
|
+
"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ",
|
45
|
+
],
|
46
|
+
[
|
47
|
+
"base64",
|
48
|
+
"m",
|
49
|
+
rfc4648(6),
|
50
|
+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
|
51
|
+
],
|
52
|
+
[
|
53
|
+
"base64pad",
|
54
|
+
"M",
|
55
|
+
rfc4648(6),
|
56
|
+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
|
57
|
+
],
|
58
|
+
[
|
59
|
+
"base64url",
|
60
|
+
"u",
|
61
|
+
rfc4648(6),
|
62
|
+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",
|
63
|
+
],
|
64
|
+
[
|
65
|
+
"base64urlpad",
|
66
|
+
"U",
|
67
|
+
rfc4648(6),
|
68
|
+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=",
|
69
|
+
],
|
70
|
+
];
|
71
|
+
export const names = constants.reduce((prev, tupple) => {
|
72
|
+
prev[tupple[0]] = new Base(tupple[0], tupple[1], tupple[2], tupple[3]);
|
73
|
+
return prev;
|
74
|
+
}, {});
|
75
|
+
export const codes = constants.reduce((prev, tupple) => {
|
76
|
+
prev[tupple[1]] = names[tupple[0]];
|
77
|
+
return prev;
|
78
|
+
}, {});
|
@@ -0,0 +1,59 @@
|
|
1
|
+
import * as constants from "./constants.js";
|
2
|
+
import { concat, decodeText, encodeText } from "./util.js";
|
3
|
+
/**
|
4
|
+
* Encode data with the specified base and add the multibase prefix.
|
5
|
+
*
|
6
|
+
* @throws {Error} Will throw if the encoding is not supported
|
7
|
+
*/
|
8
|
+
export function encode(nameOrCode, buf) {
|
9
|
+
const enc = encoding(nameOrCode);
|
10
|
+
const data = encodeText(enc.encode(buf));
|
11
|
+
return concat([enc.codeBuf, data], enc.codeBuf.length + data.length);
|
12
|
+
}
|
13
|
+
/**
|
14
|
+
* Takes a Uint8Array or string encoded with multibase header, decodes it and
|
15
|
+
* returns the decoded buffer
|
16
|
+
*
|
17
|
+
* @throws {Error} Will throw if the encoding is not supported
|
18
|
+
*/
|
19
|
+
export function decode(data) {
|
20
|
+
if (data instanceof Uint8Array) {
|
21
|
+
data = decodeText(data);
|
22
|
+
}
|
23
|
+
const prefix = data[0];
|
24
|
+
// Make all encodings case-insensitive except the ones that include upper and lower chars in the alphabet
|
25
|
+
if (["f", "F", "v", "V", "t", "T", "b", "B", "c", "C", "h", "k", "K"].includes(prefix)) {
|
26
|
+
data = data.toLowerCase();
|
27
|
+
}
|
28
|
+
const enc = encoding(data[0]);
|
29
|
+
return enc.decode(data.substring(1));
|
30
|
+
}
|
31
|
+
/**
|
32
|
+
* Get the encoding by name or code
|
33
|
+
* @throws {Error} Will throw if the encoding is not supported
|
34
|
+
*/
|
35
|
+
function encoding(nameOrCode) {
|
36
|
+
if (Object.prototype.hasOwnProperty.call(constants.names, nameOrCode)) {
|
37
|
+
return constants.names[nameOrCode];
|
38
|
+
}
|
39
|
+
else if (Object.prototype.hasOwnProperty.call(constants.codes,
|
40
|
+
/** @type {BaseCode} */ (nameOrCode))) {
|
41
|
+
return constants.codes[nameOrCode];
|
42
|
+
}
|
43
|
+
else {
|
44
|
+
throw new Error(`Unsupported encoding: ${nameOrCode}`);
|
45
|
+
}
|
46
|
+
}
|
47
|
+
/**
|
48
|
+
* Get encoding from data
|
49
|
+
*
|
50
|
+
* @param {string|Uint8Array} data
|
51
|
+
* @returns {Base}
|
52
|
+
* @throws {Error} Will throw if the encoding is not supported
|
53
|
+
*/
|
54
|
+
export function encodingFromData(data) {
|
55
|
+
if (data instanceof Uint8Array) {
|
56
|
+
data = decodeText(data);
|
57
|
+
}
|
58
|
+
return encoding(data[0]);
|
59
|
+
}
|
@@ -0,0 +1,79 @@
|
|
1
|
+
const decode = (string, alphabet, bitsPerChar) => {
|
2
|
+
// Build the character lookup table:
|
3
|
+
const codes = {};
|
4
|
+
for (let i = 0; i < alphabet.length; ++i) {
|
5
|
+
codes[alphabet[i]] = i;
|
6
|
+
}
|
7
|
+
// Count the padding bytes:
|
8
|
+
let end = string.length;
|
9
|
+
while (string[end - 1] === "=") {
|
10
|
+
--end;
|
11
|
+
}
|
12
|
+
// Allocate the output:
|
13
|
+
const out = new Uint8Array((end * bitsPerChar / 8) | 0);
|
14
|
+
// Parse the data:
|
15
|
+
let bits = 0; // Number of bits currently in the buffer
|
16
|
+
let buffer = 0; // Bits waiting to be written out, MSB first
|
17
|
+
let written = 0; // Next byte to write
|
18
|
+
for (let i = 0; i < end; ++i) {
|
19
|
+
// Read one character from the string:
|
20
|
+
const value = codes[string[i]];
|
21
|
+
if (value === undefined) {
|
22
|
+
throw new SyntaxError("Invalid character " + string[i]);
|
23
|
+
}
|
24
|
+
// Append the bits to the buffer:
|
25
|
+
buffer = (buffer << bitsPerChar) | value;
|
26
|
+
bits += bitsPerChar;
|
27
|
+
// Write out some bits if the buffer has a byte's worth:
|
28
|
+
if (bits >= 8) {
|
29
|
+
bits -= 8;
|
30
|
+
out[written++] = 0xff & (buffer >> bits);
|
31
|
+
}
|
32
|
+
}
|
33
|
+
// Verify that we have received just enough bits:
|
34
|
+
if (bits >= bitsPerChar || 0xff & (buffer << (8 - bits))) {
|
35
|
+
throw new SyntaxError("Unexpected end of data");
|
36
|
+
}
|
37
|
+
return out;
|
38
|
+
};
|
39
|
+
const encode = (data, alphabet, bitsPerChar) => {
|
40
|
+
const pad = alphabet[alphabet.length - 1] === "=";
|
41
|
+
const mask = (1 << bitsPerChar) - 1;
|
42
|
+
let out = "";
|
43
|
+
let bits = 0; // Number of bits currently in the buffer
|
44
|
+
let buffer = 0; // Bits waiting to be written out, MSB first
|
45
|
+
for (let i = 0; i < data.length; ++i) {
|
46
|
+
// Slurp data into the buffer:
|
47
|
+
buffer = (buffer << 8) | data[i];
|
48
|
+
bits += 8;
|
49
|
+
// Write out as much as we can:
|
50
|
+
while (bits > bitsPerChar) {
|
51
|
+
bits -= bitsPerChar;
|
52
|
+
out += alphabet[mask & (buffer >> bits)];
|
53
|
+
}
|
54
|
+
}
|
55
|
+
// Partial character:
|
56
|
+
if (bits) {
|
57
|
+
out += alphabet[mask & (buffer << (bitsPerChar - bits))];
|
58
|
+
}
|
59
|
+
// Add padding characters until we hit a byte boundary:
|
60
|
+
if (pad) {
|
61
|
+
while ((out.length * bitsPerChar) & 7) {
|
62
|
+
out += "=";
|
63
|
+
}
|
64
|
+
}
|
65
|
+
return out;
|
66
|
+
};
|
67
|
+
/**
|
68
|
+
* RFC4648 Factory
|
69
|
+
*/
|
70
|
+
export const rfc4648 = (bitsPerChar) => (alphabet) => {
|
71
|
+
return {
|
72
|
+
encode(input) {
|
73
|
+
return encode(input, alphabet, bitsPerChar);
|
74
|
+
},
|
75
|
+
decode(input) {
|
76
|
+
return decode(input, alphabet, bitsPerChar);
|
77
|
+
},
|
78
|
+
};
|
79
|
+
};
|
@@ -0,0 +1,13 @@
|
|
1
|
+
const textDecoder = new TextDecoder();
|
2
|
+
export const decodeText = (bytes) => textDecoder.decode(bytes);
|
3
|
+
const textEncoder = new TextEncoder();
|
4
|
+
export const encodeText = (text) => textEncoder.encode(text);
|
5
|
+
export function concat(arrs, length) {
|
6
|
+
const output = new Uint8Array(length);
|
7
|
+
let offset = 0;
|
8
|
+
for (const arr of arrs) {
|
9
|
+
output.set(arr, offset);
|
10
|
+
offset += arr.length;
|
11
|
+
}
|
12
|
+
return output;
|
13
|
+
}
|