@qrkit/bc-ur 2.0.0-beta.9-qrkit.1
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/LICENSE +21 -0
- package/README.md +996 -0
- package/dist/commonjs/classes/FountainDecoder.d.ts +125 -0
- package/dist/commonjs/classes/FountainDecoder.js +453 -0
- package/dist/commonjs/classes/FountainDecoder.js.map +1 -0
- package/dist/commonjs/classes/FountainEncoder.d.ts +63 -0
- package/dist/commonjs/classes/FountainEncoder.js +168 -0
- package/dist/commonjs/classes/FountainEncoder.js.map +1 -0
- package/dist/commonjs/classes/RegistryItem.d.ts +104 -0
- package/dist/commonjs/classes/RegistryItem.js +172 -0
- package/dist/commonjs/classes/RegistryItem.js.map +1 -0
- package/dist/commonjs/classes/UR.d.ts +89 -0
- package/dist/commonjs/classes/UR.js +243 -0
- package/dist/commonjs/classes/UR.js.map +1 -0
- package/dist/commonjs/classes/UrFountainDecoder.d.ts +15 -0
- package/dist/commonjs/classes/UrFountainDecoder.js +127 -0
- package/dist/commonjs/classes/UrFountainDecoder.js.map +1 -0
- package/dist/commonjs/classes/UrFountainEncoder.d.ts +42 -0
- package/dist/commonjs/classes/UrFountainEncoder.js +92 -0
- package/dist/commonjs/classes/UrFountainEncoder.js.map +1 -0
- package/dist/commonjs/classes/key.helper.d.ts +27 -0
- package/dist/commonjs/classes/key.helper.js +70 -0
- package/dist/commonjs/classes/key.helper.js.map +1 -0
- package/dist/commonjs/encodingMethods/BytewordEncoding.d.ts +11 -0
- package/dist/commonjs/encodingMethods/BytewordEncoding.js +23 -0
- package/dist/commonjs/encodingMethods/BytewordEncoding.js.map +1 -0
- package/dist/commonjs/encodingMethods/CborEncoding.d.ts +44 -0
- package/dist/commonjs/encodingMethods/CborEncoding.js +151 -0
- package/dist/commonjs/encodingMethods/CborEncoding.js.map +1 -0
- package/dist/commonjs/encodingMethods/HexEncoding.d.ts +8 -0
- package/dist/commonjs/encodingMethods/HexEncoding.js +24 -0
- package/dist/commonjs/encodingMethods/HexEncoding.js.map +1 -0
- package/dist/commonjs/encodingMethods/UrEncoding.d.ts +10 -0
- package/dist/commonjs/encodingMethods/UrEncoding.js +19 -0
- package/dist/commonjs/encodingMethods/UrEncoding.js.map +1 -0
- package/dist/commonjs/encodingMethods/bytewords.d.ts +27 -0
- package/dist/commonjs/encodingMethods/bytewords.js +152 -0
- package/dist/commonjs/encodingMethods/bytewords.js.map +1 -0
- package/dist/commonjs/encodingMethods/index.d.ts +19 -0
- package/dist/commonjs/encodingMethods/index.js +27 -0
- package/dist/commonjs/encodingMethods/index.js.map +1 -0
- package/dist/commonjs/encodingMethods/pipeline.d.ts +19 -0
- package/dist/commonjs/encodingMethods/pipeline.js +80 -0
- package/dist/commonjs/encodingMethods/pipeline.js.map +1 -0
- package/dist/commonjs/enums/EncodingMethodName.d.ts +10 -0
- package/dist/commonjs/enums/EncodingMethodName.js +15 -0
- package/dist/commonjs/enums/EncodingMethodName.js.map +1 -0
- package/dist/commonjs/errors.d.ts +15 -0
- package/dist/commonjs/errors.js +39 -0
- package/dist/commonjs/errors.js.map +1 -0
- package/dist/commonjs/helpers/aliasSampling.d.ts +6 -0
- package/dist/commonjs/helpers/aliasSampling.js +73 -0
- package/dist/commonjs/helpers/aliasSampling.js.map +1 -0
- package/dist/commonjs/helpers/crc32.d.ts +1 -0
- package/dist/commonjs/helpers/crc32.js +19 -0
- package/dist/commonjs/helpers/crc32.js.map +1 -0
- package/dist/commonjs/helpers/fountainUtils.d.ts +40 -0
- package/dist/commonjs/helpers/fountainUtils.js +124 -0
- package/dist/commonjs/helpers/fountainUtils.js.map +1 -0
- package/dist/commonjs/helpers/sha256.d.ts +1 -0
- package/dist/commonjs/helpers/sha256.js +78 -0
- package/dist/commonjs/helpers/sha256.js.map +1 -0
- package/dist/commonjs/helpers/type.helper.d.ts +3 -0
- package/dist/commonjs/helpers/type.helper.js +3 -0
- package/dist/commonjs/helpers/type.helper.js.map +1 -0
- package/dist/commonjs/helpers/uintArrayHelper.d.ts +287 -0
- package/dist/commonjs/helpers/uintArrayHelper.js +545 -0
- package/dist/commonjs/helpers/uintArrayHelper.js.map +1 -0
- package/dist/commonjs/helpers/utils.d.ts +55 -0
- package/dist/commonjs/helpers/utils.js +123 -0
- package/dist/commonjs/helpers/utils.js.map +1 -0
- package/dist/commonjs/index-react-native.d.ts +9 -0
- package/dist/commonjs/index-react-native.js +15 -0
- package/dist/commonjs/index-react-native.js.map +1 -0
- package/dist/commonjs/index.d.ts +21 -0
- package/dist/commonjs/index.js +34 -0
- package/dist/commonjs/index.js.map +1 -0
- package/dist/commonjs/interfaces/IEncodingMethod.d.ts +9 -0
- package/dist/commonjs/interfaces/IEncodingMethod.js +3 -0
- package/dist/commonjs/interfaces/IEncodingMethod.js.map +1 -0
- package/dist/commonjs/package.json +3 -0
- package/dist/commonjs/registry.d.ts +26 -0
- package/dist/commonjs/registry.js +118 -0
- package/dist/commonjs/registry.js.map +1 -0
- package/dist/commonjs/test.utils.d.ts +31 -0
- package/dist/commonjs/test.utils.js +88 -0
- package/dist/commonjs/test.utils.js.map +1 -0
- package/dist/commonjs/wrappers/cbor2-cjs.cjs.map +1 -0
- package/dist/commonjs/wrappers/cbor2.d.ts +2 -0
- package/dist/commonjs/wrappers/cbor2.js +14 -0
- package/dist/commonjs/wrappers/cbor2Wrapper.d.ts +14 -0
- package/dist/commonjs/wrappers/cbor2Wrapper.js +49 -0
- package/dist/commonjs/wrappers/cbor2Wrapper.js.map +1 -0
- package/dist/commonjs/xoshiro.d.ts +12 -0
- package/dist/commonjs/xoshiro.js +52 -0
- package/dist/commonjs/xoshiro.js.map +1 -0
- package/dist/esm/classes/FountainDecoder.d.ts +125 -0
- package/dist/esm/classes/FountainDecoder.js +447 -0
- package/dist/esm/classes/FountainDecoder.js.map +1 -0
- package/dist/esm/classes/FountainEncoder.d.ts +63 -0
- package/dist/esm/classes/FountainEncoder.js +164 -0
- package/dist/esm/classes/FountainEncoder.js.map +1 -0
- package/dist/esm/classes/RegistryItem.d.ts +104 -0
- package/dist/esm/classes/RegistryItem.js +166 -0
- package/dist/esm/classes/RegistryItem.js.map +1 -0
- package/dist/esm/classes/UR.d.ts +89 -0
- package/dist/esm/classes/UR.js +239 -0
- package/dist/esm/classes/UR.js.map +1 -0
- package/dist/esm/classes/UrFountainDecoder.d.ts +15 -0
- package/dist/esm/classes/UrFountainDecoder.js +123 -0
- package/dist/esm/classes/UrFountainDecoder.js.map +1 -0
- package/dist/esm/classes/UrFountainEncoder.d.ts +42 -0
- package/dist/esm/classes/UrFountainEncoder.js +88 -0
- package/dist/esm/classes/UrFountainEncoder.js.map +1 -0
- package/dist/esm/classes/key.helper.d.ts +27 -0
- package/dist/esm/classes/key.helper.js +66 -0
- package/dist/esm/classes/key.helper.js.map +1 -0
- package/dist/esm/encodingMethods/BytewordEncoding.d.ts +11 -0
- package/dist/esm/encodingMethods/BytewordEncoding.js +19 -0
- package/dist/esm/encodingMethods/BytewordEncoding.js.map +1 -0
- package/dist/esm/encodingMethods/CborEncoding.d.ts +44 -0
- package/dist/esm/encodingMethods/CborEncoding.js +147 -0
- package/dist/esm/encodingMethods/CborEncoding.js.map +1 -0
- package/dist/esm/encodingMethods/HexEncoding.d.ts +8 -0
- package/dist/esm/encodingMethods/HexEncoding.js +20 -0
- package/dist/esm/encodingMethods/HexEncoding.js.map +1 -0
- package/dist/esm/encodingMethods/UrEncoding.d.ts +10 -0
- package/dist/esm/encodingMethods/UrEncoding.js +15 -0
- package/dist/esm/encodingMethods/UrEncoding.js.map +1 -0
- package/dist/esm/encodingMethods/bytewords.d.ts +27 -0
- package/dist/esm/encodingMethods/bytewords.js +147 -0
- package/dist/esm/encodingMethods/bytewords.js.map +1 -0
- package/dist/esm/encodingMethods/index.d.ts +19 -0
- package/dist/esm/encodingMethods/index.js +24 -0
- package/dist/esm/encodingMethods/index.js.map +1 -0
- package/dist/esm/encodingMethods/pipeline.d.ts +19 -0
- package/dist/esm/encodingMethods/pipeline.js +76 -0
- package/dist/esm/encodingMethods/pipeline.js.map +1 -0
- package/dist/esm/enums/EncodingMethodName.d.ts +10 -0
- package/dist/esm/enums/EncodingMethodName.js +12 -0
- package/dist/esm/enums/EncodingMethodName.js.map +1 -0
- package/dist/esm/errors.d.ts +15 -0
- package/dist/esm/errors.js +31 -0
- package/dist/esm/errors.js.map +1 -0
- package/dist/esm/helpers/aliasSampling.d.ts +6 -0
- package/dist/esm/helpers/aliasSampling.js +70 -0
- package/dist/esm/helpers/aliasSampling.js.map +1 -0
- package/dist/esm/helpers/crc32.d.ts +1 -0
- package/dist/esm/helpers/crc32.js +16 -0
- package/dist/esm/helpers/crc32.js.map +1 -0
- package/dist/esm/helpers/fountainUtils.d.ts +40 -0
- package/dist/esm/helpers/fountainUtils.js +114 -0
- package/dist/esm/helpers/fountainUtils.js.map +1 -0
- package/dist/esm/helpers/sha256.d.ts +1 -0
- package/dist/esm/helpers/sha256.js +75 -0
- package/dist/esm/helpers/sha256.js.map +1 -0
- package/dist/esm/helpers/type.helper.d.ts +3 -0
- package/dist/esm/helpers/type.helper.js +2 -0
- package/dist/esm/helpers/type.helper.js.map +1 -0
- package/dist/esm/helpers/uintArrayHelper.d.ts +287 -0
- package/dist/esm/helpers/uintArrayHelper.js +526 -0
- package/dist/esm/helpers/uintArrayHelper.js.map +1 -0
- package/dist/esm/helpers/utils.d.ts +55 -0
- package/dist/esm/helpers/utils.js +102 -0
- package/dist/esm/helpers/utils.js.map +1 -0
- package/dist/esm/index-react-native.d.ts +9 -0
- package/dist/esm/index-react-native.js +12 -0
- package/dist/esm/index-react-native.js.map +1 -0
- package/dist/esm/index.d.ts +21 -0
- package/dist/esm/index.js +16 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/interfaces/IEncodingMethod.d.ts +9 -0
- package/dist/esm/interfaces/IEncodingMethod.js +2 -0
- package/dist/esm/interfaces/IEncodingMethod.js.map +1 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/registry.d.ts +26 -0
- package/dist/esm/registry.js +114 -0
- package/dist/esm/registry.js.map +1 -0
- package/dist/esm/test.utils.d.ts +31 -0
- package/dist/esm/test.utils.js +83 -0
- package/dist/esm/test.utils.js.map +1 -0
- package/dist/esm/wrappers/cbor2-deno.d.mts +2 -0
- package/dist/esm/wrappers/cbor2-deno.mjs +5 -0
- package/dist/esm/wrappers/cbor2-deno.mjs.map +1 -0
- package/dist/esm/wrappers/cbor2.d.ts +2 -0
- package/dist/esm/wrappers/cbor2.js +5 -0
- package/dist/esm/wrappers/cbor2.js.map +1 -0
- package/dist/esm/wrappers/cbor2Wrapper.d.ts +5 -0
- package/dist/esm/wrappers/cbor2Wrapper.js +6 -0
- package/dist/esm/wrappers/cbor2Wrapper.js.map +1 -0
- package/dist/esm/xoshiro.d.ts +12 -0
- package/dist/esm/xoshiro.js +47 -0
- package/dist/esm/xoshiro.js.map +1 -0
- package/dist/web/bytewords.js +335 -0
- package/index.html +98 -0
- package/package.json +94 -0
- package/src/classes/FountainDecoder.ts +539 -0
- package/src/classes/FountainEncoder.ts +211 -0
- package/src/classes/RegistryItem.ts +240 -0
- package/src/classes/UR.ts +308 -0
- package/src/classes/UrFountainDecoder.ts +142 -0
- package/src/classes/UrFountainEncoder.ts +103 -0
- package/src/classes/key.helper.ts +85 -0
- package/src/encodingMethods/BytewordEncoding.ts +23 -0
- package/src/encodingMethods/CborEncoding.ts +196 -0
- package/src/encodingMethods/HexEncoding.ts +23 -0
- package/src/encodingMethods/UrEncoding.ts +19 -0
- package/src/encodingMethods/bytewords.ts +215 -0
- package/src/encodingMethods/index.ts +26 -0
- package/src/encodingMethods/pipeline.ts +103 -0
- package/src/enums/EncodingMethodName.ts +10 -0
- package/src/errors.ts +34 -0
- package/src/helpers/aliasSampling.ts +87 -0
- package/src/helpers/crc32.ts +19 -0
- package/src/helpers/fountainUtils.ts +157 -0
- package/src/helpers/sha256.ts +88 -0
- package/src/helpers/type.helper.ts +1 -0
- package/src/helpers/uintArrayHelper.ts +611 -0
- package/src/helpers/utils.ts +135 -0
- package/src/index-react-native.ts +12 -0
- package/src/index.ts +44 -0
- package/src/interfaces/IEncodingMethod.ts +10 -0
- package/src/registry.ts +146 -0
- package/src/test.utils.ts +105 -0
- package/src/wrappers/cbor2-cjs.cts +6 -0
- package/src/wrappers/cbor2-deno.mts +6 -0
- package/src/wrappers/cbor2.ts +7 -0
- package/src/wrappers/cbor2Wrapper.ts +14 -0
- package/src/xoshiro.ts +66 -0
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import Xoshiro from "../xoshiro.js";
|
|
2
|
+
import { crc32 } from "./crc32.js";
|
|
3
|
+
import { stringToUint8Array, toUint8Array } from "./uintArrayHelper.js";
|
|
4
|
+
|
|
5
|
+
export const partition = (s: string, n: number): string[] =>
|
|
6
|
+
s.match(new RegExp(".{1," + n + "}", "g")) || [s];
|
|
7
|
+
|
|
8
|
+
export const split = (
|
|
9
|
+
s: Uint8Array,
|
|
10
|
+
length: number
|
|
11
|
+
): [Uint8Array, Uint8Array] => [s.slice(0, -length), s.slice(-length)];
|
|
12
|
+
|
|
13
|
+
export const getCRC = (message: Uint8Array): number => crc32(message);
|
|
14
|
+
|
|
15
|
+
export const getCRCHex = (message: Uint8Array): string =>
|
|
16
|
+
crc32(message).toString(16).padStart(8, "0");
|
|
17
|
+
|
|
18
|
+
export const toUint32 = (number: number): number => number >>> 0;
|
|
19
|
+
|
|
20
|
+
export const intToBytes = (num: number): Uint8Array => {
|
|
21
|
+
const arr = new ArrayBuffer(4); // an Int32 takes 4 bytes
|
|
22
|
+
const view = new DataView(arr);
|
|
23
|
+
|
|
24
|
+
view.setUint32(0, num, false); // byteOffset = 0; litteEndian = false
|
|
25
|
+
|
|
26
|
+
return toUint8Array(arr);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export const arraysEqual = (ar1: number[], ar2: number[]): boolean => {
|
|
30
|
+
if (ar1.length !== ar2.length) {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return ar1.every((el) => ar2.includes(el));
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Checks if ar1 contains all elements of ar2
|
|
39
|
+
* @param ar1 the outer array
|
|
40
|
+
* @param ar2 the array to be contained in ar1
|
|
41
|
+
*/
|
|
42
|
+
export const arrayContains = (ar1: number[], ar2: number[]): boolean => {
|
|
43
|
+
return ar2.every((v) => ar1.includes(v));
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Returns the difference array of `ar1` - `ar2`
|
|
48
|
+
*/
|
|
49
|
+
export const setDifference = (ar1: number[], ar2: number[]): number[] => {
|
|
50
|
+
return ar1.filter((x) => ar2.indexOf(x) < 0);
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export const bufferXOR = (a: Uint8Array, b: Uint8Array): Uint8Array => {
|
|
54
|
+
const length = Math.max(a.length, b.length);
|
|
55
|
+
const uInt8Array = new Uint8Array(length);
|
|
56
|
+
|
|
57
|
+
for (let i = 0; i < length; ++i) {
|
|
58
|
+
uInt8Array[i] = a[i] ^ b[i];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return uInt8Array;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export const makeMessage = (
|
|
65
|
+
length: number,
|
|
66
|
+
seed: string = "Wolf"
|
|
67
|
+
): Uint8Array => {
|
|
68
|
+
const rng = new Xoshiro(stringToUint8Array(seed));
|
|
69
|
+
|
|
70
|
+
return new Uint8Array(rng.nextData(length));
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
/// Set operations
|
|
76
|
+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/difference#browser_compatibility
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Returns the union of two sets.
|
|
80
|
+
* @param set1 - The first set.
|
|
81
|
+
* @param set2 - The second set.
|
|
82
|
+
* @returns A new set containing all unique elements from both sets.
|
|
83
|
+
*/
|
|
84
|
+
export const union = <T>(set1: Set<T>, set2: Set<T>): Set<T> => {
|
|
85
|
+
return new Set([...set1, ...set2]);
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Returns the intersection of two sets.
|
|
90
|
+
* @param set1 - The first set.
|
|
91
|
+
* @param set2 - The second set.
|
|
92
|
+
* @returns A new set containing elements present in both sets.
|
|
93
|
+
*/
|
|
94
|
+
export const intersection = <T>(set1: Set<T>, set2: Set<T>): Set<T> => {
|
|
95
|
+
return new Set([...set1].filter((x) => set2.has(x)));
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Returns the difference of two sets (set1 - set2).
|
|
100
|
+
* @param set1 - The first set.
|
|
101
|
+
* @param set2 - The second set.
|
|
102
|
+
* @returns A new set containing elements in set1 that are not in set2.
|
|
103
|
+
*/
|
|
104
|
+
export const difference = <T>(set1: Set<T>, set2: Set<T>): Set<T> => {
|
|
105
|
+
return new Set([...set1].filter((x) => !set2.has(x)));
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Returns the symmetric difference of two sets.
|
|
110
|
+
* @param set1 - The first set.
|
|
111
|
+
* @param set2 - The second set.
|
|
112
|
+
* @returns A new set containing elements in either set1 or set2, but not in both.
|
|
113
|
+
*/
|
|
114
|
+
export const symmetricDifference = <T>(set1: Set<T>, set2: Set<T>): Set<T> => {
|
|
115
|
+
return new Set([
|
|
116
|
+
...[...set1].filter((x) => !set2.has(x)),
|
|
117
|
+
...[...set2].filter((x) => !set1.has(x)),
|
|
118
|
+
]);
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Checks if set1 is a subset of set2.
|
|
123
|
+
* @param sub - The potential subset.
|
|
124
|
+
* @param container - The set to check against.
|
|
125
|
+
* @returns True if set1 is a subset of set2, otherwise false.
|
|
126
|
+
*/
|
|
127
|
+
export const isSubset = <T>(sub: Set<T>, container: Set<T>): boolean => {
|
|
128
|
+
return [...sub].every((x) => container.has(x));
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
export function assert(condition: boolean, message?: string): void {
|
|
132
|
+
if (!condition) {
|
|
133
|
+
throw new Error(message || "Assertion failed");
|
|
134
|
+
}
|
|
135
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This is for react-native compatibility
|
|
3
|
+
* In Expo SDK 51 (React Native 74), Hermes supports TextEncoder natively but not TextDecoder.
|
|
4
|
+
* Latest version of Expo 52 has added TextDecoder support for Hermes. But it is still unsupported in React Native 77,
|
|
5
|
+
*
|
|
6
|
+
* So lets inject TextDecoder polyfill for React Native if it is not supported
|
|
7
|
+
*/
|
|
8
|
+
import "@bacons/text-decoder/install";
|
|
9
|
+
|
|
10
|
+
// Export everything from the main index file
|
|
11
|
+
//@ts-ignore
|
|
12
|
+
export * from "./index.js";
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { UR } from "./classes/UR.js"
|
|
2
|
+
import { UrRegistry } from "./registry.js"
|
|
3
|
+
import { registryItemFactory, RegistryItemBase, isRegistryItem } from "./classes/RegistryItem.js"
|
|
4
|
+
import { UrFountainEncoder } from "./classes/UrFountainEncoder.js"
|
|
5
|
+
import { UrFountainDecoder } from "./classes/UrFountainDecoder.js"
|
|
6
|
+
import { FountainEncoder } from "./classes/FountainEncoder.js"
|
|
7
|
+
import { FountainDecoder } from "./classes/FountainDecoder.js"
|
|
8
|
+
import { defaultEncoders, dataPipeline } from "./encodingMethods/index.js"
|
|
9
|
+
|
|
10
|
+
// Encodings
|
|
11
|
+
import { CborEncoding } from "./encodingMethods/CborEncoding.js"
|
|
12
|
+
import { BytewordEncoding } from "./encodingMethods/BytewordEncoding.js"
|
|
13
|
+
import { HexEncoding } from "./encodingMethods/HexEncoding.js"
|
|
14
|
+
|
|
15
|
+
// CBOR2 library
|
|
16
|
+
import * as cbor2 from "./wrappers/cbor2.js";
|
|
17
|
+
|
|
18
|
+
export {
|
|
19
|
+
UrRegistry,
|
|
20
|
+
registryItemFactory,
|
|
21
|
+
RegistryItemBase,
|
|
22
|
+
isRegistryItem,
|
|
23
|
+
UR,
|
|
24
|
+
UrFountainDecoder,
|
|
25
|
+
UrFountainEncoder,
|
|
26
|
+
FountainDecoder,
|
|
27
|
+
FountainEncoder,
|
|
28
|
+
CborEncoding,
|
|
29
|
+
BytewordEncoding,
|
|
30
|
+
HexEncoding,
|
|
31
|
+
defaultEncoders,
|
|
32
|
+
dataPipeline,
|
|
33
|
+
cbor2,
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Export types
|
|
37
|
+
import type { RegistryItem, RegistryItemClass } from "./classes/RegistryItem.js"
|
|
38
|
+
import type { DecodeOptions as DecodeOptionsType, EncodeOptions as EncodeOptionsType, CommentOptions as CommentOptionsType } from "./wrappers/cbor2.js";
|
|
39
|
+
export namespace cbor2Types {
|
|
40
|
+
export type DecodeOptions = DecodeOptionsType;
|
|
41
|
+
export type EncodeOptions = EncodeOptionsType;
|
|
42
|
+
export type CommentOptions = CommentOptionsType;
|
|
43
|
+
}
|
|
44
|
+
export type { RegistryItem, RegistryItemClass }
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { EncodingMethodName } from "../enums/EncodingMethodName.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Encoding method that defines the incoming type T and the outgoing type U
|
|
5
|
+
*/
|
|
6
|
+
export interface IEncodingMethod<T, U> {
|
|
7
|
+
name: EncodingMethodName | string;
|
|
8
|
+
encode(payload: T, config?: unknown): U;
|
|
9
|
+
decode(payload: U, config?: unknown): T;
|
|
10
|
+
}
|
package/src/registry.ts
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { RegistryItemClass } from "./classes/RegistryItem.js";
|
|
2
|
+
import {
|
|
3
|
+
Tag,
|
|
4
|
+
} from "./wrappers/cbor2.js";
|
|
5
|
+
|
|
6
|
+
type Registry = Map<string, RegistryItemClass>;
|
|
7
|
+
|
|
8
|
+
export class URRegistryClass {
|
|
9
|
+
private static instance: URRegistryClass;
|
|
10
|
+
private registry: Registry = new Map();
|
|
11
|
+
private tagMap: Map<number, string> = new Map();
|
|
12
|
+
private loggingEnabled: boolean;
|
|
13
|
+
|
|
14
|
+
private constructor(
|
|
15
|
+
items: RegistryItemClass[] = [],
|
|
16
|
+
loggingEnabled: boolean = true
|
|
17
|
+
) {
|
|
18
|
+
this.loggingEnabled = loggingEnabled;
|
|
19
|
+
this.addItems(items);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public static getInstance(
|
|
23
|
+
items: RegistryItemClass[] = [],
|
|
24
|
+
loggingEnabled: boolean = true
|
|
25
|
+
): URRegistryClass {
|
|
26
|
+
if (!URRegistryClass.instance) {
|
|
27
|
+
URRegistryClass.instance = new URRegistryClass(items, loggingEnabled);
|
|
28
|
+
}
|
|
29
|
+
return URRegistryClass.instance;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public setLoggingEnabled(enabled: boolean): void {
|
|
33
|
+
this.loggingEnabled = enabled;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
private log(message: string): void {
|
|
37
|
+
if (this.loggingEnabled) {
|
|
38
|
+
console.warn(message);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
public addItem(item: RegistryItemClass): void {
|
|
43
|
+
if (this.registry.has(item.URType)) {
|
|
44
|
+
this.log(
|
|
45
|
+
`Warning: Overwriting existing item with URType: ${item.URType}`
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
if (this.tagMap.has(item.tag)) {
|
|
49
|
+
this.log(`Warning: Tag collision detected for tag: ${item.tag}`);
|
|
50
|
+
}
|
|
51
|
+
this.registry.set(item.URType, item);
|
|
52
|
+
|
|
53
|
+
// Register to CBOR decoder only if it has a tag
|
|
54
|
+
if (!Number.isNaN(item.tag)) {
|
|
55
|
+
this.tagMap.set(item.tag, item.URType);
|
|
56
|
+
Tag.registerDecoder(item.tag, (tag: Tag, opts: any) => {
|
|
57
|
+
return item.fromCBORData.bind(item)(
|
|
58
|
+
tag.contents,
|
|
59
|
+
item.allowKeysNotInMap,
|
|
60
|
+
opts
|
|
61
|
+
);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
public addItemOnce(item: RegistryItemClass): void {
|
|
67
|
+
// If we already have the item in the registry, do nothing
|
|
68
|
+
if (this.registry.has(item.URType) || this.tagMap.has(item.tag)) {
|
|
69
|
+
// Check if its the same item
|
|
70
|
+
const existingItem = this.registry.get(item.URType);
|
|
71
|
+
if (existingItem !== item) {
|
|
72
|
+
this.log(
|
|
73
|
+
`Warning: Resgistry already has an item with URType: ${item.URType} but it seems to be a different instance`
|
|
74
|
+
);
|
|
75
|
+
this.log(`Existing item: ${existingItem}`);
|
|
76
|
+
this.log(`New item: ${item}`);
|
|
77
|
+
}
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Otherwise, add the item
|
|
82
|
+
this.addItem(item);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
public addItems(items: RegistryItemClass[]): void {
|
|
86
|
+
items.forEach((item) => this.addItem(item));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
public queryByTag(tag: number): RegistryItemClass | undefined {
|
|
90
|
+
const URType = this.tagMap.get(tag);
|
|
91
|
+
return URType ? this.registry.get(URType) : undefined;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
public queryByURType(URType: string): RegistryItemClass | undefined {
|
|
95
|
+
return this.registry.get(URType);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Removes an item from the registry based on its URType, tag, or item instance.
|
|
100
|
+
* @param findItem - The item to be removed. It can be an instance of RegistryItemClass, a URType string, or a tag number.
|
|
101
|
+
*/
|
|
102
|
+
public removeItem(findItem: RegistryItemClass | string | number): void {
|
|
103
|
+
let foundItem: RegistryItemClass | undefined;
|
|
104
|
+
if (typeof findItem === "string") {
|
|
105
|
+
foundItem = this.queryByURType(findItem);
|
|
106
|
+
if (!foundItem) {
|
|
107
|
+
this.log(`Warning: No item found with URType: ${findItem}`);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
} else if (typeof findItem === "number") {
|
|
111
|
+
foundItem = this.queryByTag(findItem);
|
|
112
|
+
if (!foundItem) {
|
|
113
|
+
this.log(`Warning: No item found with tag: ${findItem}`);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
} else {
|
|
117
|
+
// Check if the item is in the registry
|
|
118
|
+
const URType = findItem.URType;
|
|
119
|
+
if (this.registry.has(URType)) {
|
|
120
|
+
foundItem = findItem;
|
|
121
|
+
} else {
|
|
122
|
+
this.log(`Warning: Item not found in registry with type: ${URType}`);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Remove it from registry
|
|
128
|
+
this.registry.delete(foundItem.URType);
|
|
129
|
+
this.tagMap.delete(foundItem.tag);
|
|
130
|
+
Tag.clearDecoder(foundItem.tag);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
public clearRegistry(): void {
|
|
134
|
+
this.registry.forEach((item) => {
|
|
135
|
+
Tag.clearDecoder(item.tag);
|
|
136
|
+
});
|
|
137
|
+
this.registry.clear();
|
|
138
|
+
this.tagMap.clear();
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
public getRegistry(): Registry {
|
|
142
|
+
return this.registry;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export const UrRegistry = URRegistryClass.getInstance();
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { registryItemFactory } from "./classes/RegistryItem.js";
|
|
2
|
+
|
|
3
|
+
// Define a nested registry items
|
|
4
|
+
interface IUser {
|
|
5
|
+
id: number;
|
|
6
|
+
name: string;
|
|
7
|
+
email?: string;
|
|
8
|
+
phone?: string;
|
|
9
|
+
address?: string;
|
|
10
|
+
[key: string]: any;
|
|
11
|
+
}
|
|
12
|
+
export class User extends registryItemFactory({
|
|
13
|
+
tag: 111,
|
|
14
|
+
URType: "user",
|
|
15
|
+
CDDL: `
|
|
16
|
+
user = #6.111({
|
|
17
|
+
id: uint,
|
|
18
|
+
name: text,
|
|
19
|
+
? email: text,
|
|
20
|
+
? phone: text,
|
|
21
|
+
? address: text
|
|
22
|
+
})
|
|
23
|
+
`,
|
|
24
|
+
}) {
|
|
25
|
+
private user: IUser;
|
|
26
|
+
|
|
27
|
+
constructor(user: IUser) {
|
|
28
|
+
super(user);
|
|
29
|
+
this.user = user;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
verifyInput(input: any) {
|
|
33
|
+
let reasons: Error[] = [];
|
|
34
|
+
|
|
35
|
+
if (!input.id) {
|
|
36
|
+
reasons.push(new Error("ID is required"));
|
|
37
|
+
} else {
|
|
38
|
+
if (typeof input.id !== "number") {
|
|
39
|
+
reasons.push(new Error("ID should be a number"));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (!input.name) {
|
|
44
|
+
reasons.push(new Error("Name is required"));
|
|
45
|
+
} else {
|
|
46
|
+
if (typeof input.name !== "string") {
|
|
47
|
+
reasons.push(new Error("Name should be a string"));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const valid = reasons.length === 0;
|
|
52
|
+
return { valid, reasons };
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Parent class
|
|
57
|
+
interface IUserCollection {
|
|
58
|
+
name: string;
|
|
59
|
+
users: User[];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const UserCollectionType = {
|
|
63
|
+
tag: 112,
|
|
64
|
+
URType: "user-collection",
|
|
65
|
+
CDDL: `
|
|
66
|
+
user-collection = #6.112({
|
|
67
|
+
name: text,
|
|
68
|
+
users: [+ #6.112(user)]
|
|
69
|
+
})
|
|
70
|
+
`,
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
export class UserCollection extends registryItemFactory(UserCollectionType) {
|
|
74
|
+
constructor(private userCollection: IUserCollection) {
|
|
75
|
+
super(userCollection);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
verifyInput(input: any) {
|
|
79
|
+
let reasons: Error[] = [];
|
|
80
|
+
|
|
81
|
+
if (!input.name) {
|
|
82
|
+
reasons.push(new Error("Name is required"));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (typeof input.name !== "string") {
|
|
86
|
+
reasons.push(new Error("Name should be a string"));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (input.users) {
|
|
90
|
+
// Check its array
|
|
91
|
+
if (!Array.isArray(input.users)) {
|
|
92
|
+
reasons.push(new Error("Users should be an array"));
|
|
93
|
+
} else {
|
|
94
|
+
input.users.forEach((user: any) => {
|
|
95
|
+
if (!(user instanceof User)) {
|
|
96
|
+
reasons.push(new Error("Users should be an array of User"));
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const valid = reasons.length === 0;
|
|
103
|
+
return { valid, reasons };
|
|
104
|
+
}
|
|
105
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// This is the common ground for both esm and commonjs modules
|
|
2
|
+
// To overcome dual-package-hazard
|
|
3
|
+
//@ts-ignore
|
|
4
|
+
export { decode, encode, Tag, registerEncoder, comment, diagnose } from "./cbor2Wrapper.js";
|
|
5
|
+
//@ts-ignore
|
|
6
|
+
export type { DecodeOptions, EncodeOptions, CommentOptions } from "./cbor2Wrapper.js";
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// If you need a provide an ESM dialect that doesn't support CommonJS (eg, deno, browser, etc), then you can do this:
|
|
2
|
+
// No dual package hazard here
|
|
3
|
+
//@ts-ignore
|
|
4
|
+
export { decode, encode, Tag, registerEncoder, comment, diagnose } from "./cbor2Wrapper.js";
|
|
5
|
+
//@ts-ignore
|
|
6
|
+
export type { DecodeOptions, EncodeOptions, CommentOptions } from "./cbor2Wrapper.js";
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// ESM consumers should stay entirely within the ESM output tree.
|
|
2
|
+
// The CommonJS build gets its own wrapper entry via `cbor2-cjs.cts`.
|
|
3
|
+
|
|
4
|
+
//@ts-ignore
|
|
5
|
+
export { decode, encode, Tag, registerEncoder, comment, diagnose } from "./cbor2Wrapper.js";
|
|
6
|
+
//@ts-ignore
|
|
7
|
+
export type { DecodeOptions, EncodeOptions, CommentOptions } from "./cbor2Wrapper.js";
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
DecodeOptions,
|
|
3
|
+
EncodeOptions,
|
|
4
|
+
CommentOptions,
|
|
5
|
+
//@ts-ignore
|
|
6
|
+
} from 'cbor2' with { 'resolution-mode': 'import' };
|
|
7
|
+
|
|
8
|
+
//@ts-ignore
|
|
9
|
+
import { decode, encode, Tag, comment, diagnose } from 'cbor2';
|
|
10
|
+
//@ts-ignore
|
|
11
|
+
import { registerEncoder } from 'cbor2/encoder';
|
|
12
|
+
|
|
13
|
+
export { decode, encode, Tag, registerEncoder, comment, diagnose };
|
|
14
|
+
export type { DecodeOptions, EncodeOptions, CommentOptions };
|
package/src/xoshiro.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { sha256 } from "./helpers/sha256.js";
|
|
2
|
+
|
|
3
|
+
export const sha256Hash = (data) => {
|
|
4
|
+
return sha256(data);
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
const MAX_UINT64 = BigInt("0xffffffffffffffff");
|
|
8
|
+
|
|
9
|
+
const rotl = (x: bigint, k: number): bigint =>
|
|
10
|
+
(x << BigInt(k) | x >> BigInt(64 - k)) & MAX_UINT64;
|
|
11
|
+
|
|
12
|
+
export default class Xoshiro {
|
|
13
|
+
private s: bigint[];
|
|
14
|
+
|
|
15
|
+
constructor(seed: Uint8Array) {
|
|
16
|
+
const digest = sha256Hash(seed);
|
|
17
|
+
|
|
18
|
+
this.s = [0n, 0n, 0n, 0n];
|
|
19
|
+
this.setS(digest);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
private setS = (digest: Uint8Array) => {
|
|
23
|
+
for (let i = 0; i < 4; i++) {
|
|
24
|
+
let o = i * 8;
|
|
25
|
+
let v = 0n;
|
|
26
|
+
for (let n = 0; n < 8; n++) {
|
|
27
|
+
v = (v << 8n) | BigInt(digest[o + n]);
|
|
28
|
+
}
|
|
29
|
+
this.s[i] = v & MAX_UINT64;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
private roll = (): bigint => {
|
|
34
|
+
const result =
|
|
35
|
+
((rotl((this.s[1] * 5n) & MAX_UINT64, 7) * 9n) & MAX_UINT64);
|
|
36
|
+
|
|
37
|
+
const t = (this.s[1] << 17n) & MAX_UINT64;
|
|
38
|
+
|
|
39
|
+
this.s[2] ^= this.s[0];
|
|
40
|
+
this.s[3] ^= this.s[1];
|
|
41
|
+
this.s[1] ^= this.s[2];
|
|
42
|
+
this.s[0] ^= this.s[3];
|
|
43
|
+
|
|
44
|
+
this.s[2] ^= t;
|
|
45
|
+
this.s[3] = rotl(this.s[3], 45);
|
|
46
|
+
|
|
47
|
+
return result;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
next = (): bigint => {
|
|
51
|
+
return this.roll();
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
nextDouble = (): number => {
|
|
55
|
+
return Number(this.roll()) / Number(MAX_UINT64 + 1n);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
nextInt = (low: number, high: number): number => {
|
|
59
|
+
return Math.floor(this.nextDouble() * (high - low + 1) + low);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
nextByte = () => this.nextInt(0, 255);
|
|
63
|
+
|
|
64
|
+
nextData = (count: number) =>
|
|
65
|
+
[...new Array(count)].map(() => this.nextByte());
|
|
66
|
+
}
|