@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.
Files changed (229) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +996 -0
  3. package/dist/commonjs/classes/FountainDecoder.d.ts +125 -0
  4. package/dist/commonjs/classes/FountainDecoder.js +453 -0
  5. package/dist/commonjs/classes/FountainDecoder.js.map +1 -0
  6. package/dist/commonjs/classes/FountainEncoder.d.ts +63 -0
  7. package/dist/commonjs/classes/FountainEncoder.js +168 -0
  8. package/dist/commonjs/classes/FountainEncoder.js.map +1 -0
  9. package/dist/commonjs/classes/RegistryItem.d.ts +104 -0
  10. package/dist/commonjs/classes/RegistryItem.js +172 -0
  11. package/dist/commonjs/classes/RegistryItem.js.map +1 -0
  12. package/dist/commonjs/classes/UR.d.ts +89 -0
  13. package/dist/commonjs/classes/UR.js +243 -0
  14. package/dist/commonjs/classes/UR.js.map +1 -0
  15. package/dist/commonjs/classes/UrFountainDecoder.d.ts +15 -0
  16. package/dist/commonjs/classes/UrFountainDecoder.js +127 -0
  17. package/dist/commonjs/classes/UrFountainDecoder.js.map +1 -0
  18. package/dist/commonjs/classes/UrFountainEncoder.d.ts +42 -0
  19. package/dist/commonjs/classes/UrFountainEncoder.js +92 -0
  20. package/dist/commonjs/classes/UrFountainEncoder.js.map +1 -0
  21. package/dist/commonjs/classes/key.helper.d.ts +27 -0
  22. package/dist/commonjs/classes/key.helper.js +70 -0
  23. package/dist/commonjs/classes/key.helper.js.map +1 -0
  24. package/dist/commonjs/encodingMethods/BytewordEncoding.d.ts +11 -0
  25. package/dist/commonjs/encodingMethods/BytewordEncoding.js +23 -0
  26. package/dist/commonjs/encodingMethods/BytewordEncoding.js.map +1 -0
  27. package/dist/commonjs/encodingMethods/CborEncoding.d.ts +44 -0
  28. package/dist/commonjs/encodingMethods/CborEncoding.js +151 -0
  29. package/dist/commonjs/encodingMethods/CborEncoding.js.map +1 -0
  30. package/dist/commonjs/encodingMethods/HexEncoding.d.ts +8 -0
  31. package/dist/commonjs/encodingMethods/HexEncoding.js +24 -0
  32. package/dist/commonjs/encodingMethods/HexEncoding.js.map +1 -0
  33. package/dist/commonjs/encodingMethods/UrEncoding.d.ts +10 -0
  34. package/dist/commonjs/encodingMethods/UrEncoding.js +19 -0
  35. package/dist/commonjs/encodingMethods/UrEncoding.js.map +1 -0
  36. package/dist/commonjs/encodingMethods/bytewords.d.ts +27 -0
  37. package/dist/commonjs/encodingMethods/bytewords.js +152 -0
  38. package/dist/commonjs/encodingMethods/bytewords.js.map +1 -0
  39. package/dist/commonjs/encodingMethods/index.d.ts +19 -0
  40. package/dist/commonjs/encodingMethods/index.js +27 -0
  41. package/dist/commonjs/encodingMethods/index.js.map +1 -0
  42. package/dist/commonjs/encodingMethods/pipeline.d.ts +19 -0
  43. package/dist/commonjs/encodingMethods/pipeline.js +80 -0
  44. package/dist/commonjs/encodingMethods/pipeline.js.map +1 -0
  45. package/dist/commonjs/enums/EncodingMethodName.d.ts +10 -0
  46. package/dist/commonjs/enums/EncodingMethodName.js +15 -0
  47. package/dist/commonjs/enums/EncodingMethodName.js.map +1 -0
  48. package/dist/commonjs/errors.d.ts +15 -0
  49. package/dist/commonjs/errors.js +39 -0
  50. package/dist/commonjs/errors.js.map +1 -0
  51. package/dist/commonjs/helpers/aliasSampling.d.ts +6 -0
  52. package/dist/commonjs/helpers/aliasSampling.js +73 -0
  53. package/dist/commonjs/helpers/aliasSampling.js.map +1 -0
  54. package/dist/commonjs/helpers/crc32.d.ts +1 -0
  55. package/dist/commonjs/helpers/crc32.js +19 -0
  56. package/dist/commonjs/helpers/crc32.js.map +1 -0
  57. package/dist/commonjs/helpers/fountainUtils.d.ts +40 -0
  58. package/dist/commonjs/helpers/fountainUtils.js +124 -0
  59. package/dist/commonjs/helpers/fountainUtils.js.map +1 -0
  60. package/dist/commonjs/helpers/sha256.d.ts +1 -0
  61. package/dist/commonjs/helpers/sha256.js +78 -0
  62. package/dist/commonjs/helpers/sha256.js.map +1 -0
  63. package/dist/commonjs/helpers/type.helper.d.ts +3 -0
  64. package/dist/commonjs/helpers/type.helper.js +3 -0
  65. package/dist/commonjs/helpers/type.helper.js.map +1 -0
  66. package/dist/commonjs/helpers/uintArrayHelper.d.ts +287 -0
  67. package/dist/commonjs/helpers/uintArrayHelper.js +545 -0
  68. package/dist/commonjs/helpers/uintArrayHelper.js.map +1 -0
  69. package/dist/commonjs/helpers/utils.d.ts +55 -0
  70. package/dist/commonjs/helpers/utils.js +123 -0
  71. package/dist/commonjs/helpers/utils.js.map +1 -0
  72. package/dist/commonjs/index-react-native.d.ts +9 -0
  73. package/dist/commonjs/index-react-native.js +15 -0
  74. package/dist/commonjs/index-react-native.js.map +1 -0
  75. package/dist/commonjs/index.d.ts +21 -0
  76. package/dist/commonjs/index.js +34 -0
  77. package/dist/commonjs/index.js.map +1 -0
  78. package/dist/commonjs/interfaces/IEncodingMethod.d.ts +9 -0
  79. package/dist/commonjs/interfaces/IEncodingMethod.js +3 -0
  80. package/dist/commonjs/interfaces/IEncodingMethod.js.map +1 -0
  81. package/dist/commonjs/package.json +3 -0
  82. package/dist/commonjs/registry.d.ts +26 -0
  83. package/dist/commonjs/registry.js +118 -0
  84. package/dist/commonjs/registry.js.map +1 -0
  85. package/dist/commonjs/test.utils.d.ts +31 -0
  86. package/dist/commonjs/test.utils.js +88 -0
  87. package/dist/commonjs/test.utils.js.map +1 -0
  88. package/dist/commonjs/wrappers/cbor2-cjs.cjs.map +1 -0
  89. package/dist/commonjs/wrappers/cbor2.d.ts +2 -0
  90. package/dist/commonjs/wrappers/cbor2.js +14 -0
  91. package/dist/commonjs/wrappers/cbor2Wrapper.d.ts +14 -0
  92. package/dist/commonjs/wrappers/cbor2Wrapper.js +49 -0
  93. package/dist/commonjs/wrappers/cbor2Wrapper.js.map +1 -0
  94. package/dist/commonjs/xoshiro.d.ts +12 -0
  95. package/dist/commonjs/xoshiro.js +52 -0
  96. package/dist/commonjs/xoshiro.js.map +1 -0
  97. package/dist/esm/classes/FountainDecoder.d.ts +125 -0
  98. package/dist/esm/classes/FountainDecoder.js +447 -0
  99. package/dist/esm/classes/FountainDecoder.js.map +1 -0
  100. package/dist/esm/classes/FountainEncoder.d.ts +63 -0
  101. package/dist/esm/classes/FountainEncoder.js +164 -0
  102. package/dist/esm/classes/FountainEncoder.js.map +1 -0
  103. package/dist/esm/classes/RegistryItem.d.ts +104 -0
  104. package/dist/esm/classes/RegistryItem.js +166 -0
  105. package/dist/esm/classes/RegistryItem.js.map +1 -0
  106. package/dist/esm/classes/UR.d.ts +89 -0
  107. package/dist/esm/classes/UR.js +239 -0
  108. package/dist/esm/classes/UR.js.map +1 -0
  109. package/dist/esm/classes/UrFountainDecoder.d.ts +15 -0
  110. package/dist/esm/classes/UrFountainDecoder.js +123 -0
  111. package/dist/esm/classes/UrFountainDecoder.js.map +1 -0
  112. package/dist/esm/classes/UrFountainEncoder.d.ts +42 -0
  113. package/dist/esm/classes/UrFountainEncoder.js +88 -0
  114. package/dist/esm/classes/UrFountainEncoder.js.map +1 -0
  115. package/dist/esm/classes/key.helper.d.ts +27 -0
  116. package/dist/esm/classes/key.helper.js +66 -0
  117. package/dist/esm/classes/key.helper.js.map +1 -0
  118. package/dist/esm/encodingMethods/BytewordEncoding.d.ts +11 -0
  119. package/dist/esm/encodingMethods/BytewordEncoding.js +19 -0
  120. package/dist/esm/encodingMethods/BytewordEncoding.js.map +1 -0
  121. package/dist/esm/encodingMethods/CborEncoding.d.ts +44 -0
  122. package/dist/esm/encodingMethods/CborEncoding.js +147 -0
  123. package/dist/esm/encodingMethods/CborEncoding.js.map +1 -0
  124. package/dist/esm/encodingMethods/HexEncoding.d.ts +8 -0
  125. package/dist/esm/encodingMethods/HexEncoding.js +20 -0
  126. package/dist/esm/encodingMethods/HexEncoding.js.map +1 -0
  127. package/dist/esm/encodingMethods/UrEncoding.d.ts +10 -0
  128. package/dist/esm/encodingMethods/UrEncoding.js +15 -0
  129. package/dist/esm/encodingMethods/UrEncoding.js.map +1 -0
  130. package/dist/esm/encodingMethods/bytewords.d.ts +27 -0
  131. package/dist/esm/encodingMethods/bytewords.js +147 -0
  132. package/dist/esm/encodingMethods/bytewords.js.map +1 -0
  133. package/dist/esm/encodingMethods/index.d.ts +19 -0
  134. package/dist/esm/encodingMethods/index.js +24 -0
  135. package/dist/esm/encodingMethods/index.js.map +1 -0
  136. package/dist/esm/encodingMethods/pipeline.d.ts +19 -0
  137. package/dist/esm/encodingMethods/pipeline.js +76 -0
  138. package/dist/esm/encodingMethods/pipeline.js.map +1 -0
  139. package/dist/esm/enums/EncodingMethodName.d.ts +10 -0
  140. package/dist/esm/enums/EncodingMethodName.js +12 -0
  141. package/dist/esm/enums/EncodingMethodName.js.map +1 -0
  142. package/dist/esm/errors.d.ts +15 -0
  143. package/dist/esm/errors.js +31 -0
  144. package/dist/esm/errors.js.map +1 -0
  145. package/dist/esm/helpers/aliasSampling.d.ts +6 -0
  146. package/dist/esm/helpers/aliasSampling.js +70 -0
  147. package/dist/esm/helpers/aliasSampling.js.map +1 -0
  148. package/dist/esm/helpers/crc32.d.ts +1 -0
  149. package/dist/esm/helpers/crc32.js +16 -0
  150. package/dist/esm/helpers/crc32.js.map +1 -0
  151. package/dist/esm/helpers/fountainUtils.d.ts +40 -0
  152. package/dist/esm/helpers/fountainUtils.js +114 -0
  153. package/dist/esm/helpers/fountainUtils.js.map +1 -0
  154. package/dist/esm/helpers/sha256.d.ts +1 -0
  155. package/dist/esm/helpers/sha256.js +75 -0
  156. package/dist/esm/helpers/sha256.js.map +1 -0
  157. package/dist/esm/helpers/type.helper.d.ts +3 -0
  158. package/dist/esm/helpers/type.helper.js +2 -0
  159. package/dist/esm/helpers/type.helper.js.map +1 -0
  160. package/dist/esm/helpers/uintArrayHelper.d.ts +287 -0
  161. package/dist/esm/helpers/uintArrayHelper.js +526 -0
  162. package/dist/esm/helpers/uintArrayHelper.js.map +1 -0
  163. package/dist/esm/helpers/utils.d.ts +55 -0
  164. package/dist/esm/helpers/utils.js +102 -0
  165. package/dist/esm/helpers/utils.js.map +1 -0
  166. package/dist/esm/index-react-native.d.ts +9 -0
  167. package/dist/esm/index-react-native.js +12 -0
  168. package/dist/esm/index-react-native.js.map +1 -0
  169. package/dist/esm/index.d.ts +21 -0
  170. package/dist/esm/index.js +16 -0
  171. package/dist/esm/index.js.map +1 -0
  172. package/dist/esm/interfaces/IEncodingMethod.d.ts +9 -0
  173. package/dist/esm/interfaces/IEncodingMethod.js +2 -0
  174. package/dist/esm/interfaces/IEncodingMethod.js.map +1 -0
  175. package/dist/esm/package.json +3 -0
  176. package/dist/esm/registry.d.ts +26 -0
  177. package/dist/esm/registry.js +114 -0
  178. package/dist/esm/registry.js.map +1 -0
  179. package/dist/esm/test.utils.d.ts +31 -0
  180. package/dist/esm/test.utils.js +83 -0
  181. package/dist/esm/test.utils.js.map +1 -0
  182. package/dist/esm/wrappers/cbor2-deno.d.mts +2 -0
  183. package/dist/esm/wrappers/cbor2-deno.mjs +5 -0
  184. package/dist/esm/wrappers/cbor2-deno.mjs.map +1 -0
  185. package/dist/esm/wrappers/cbor2.d.ts +2 -0
  186. package/dist/esm/wrappers/cbor2.js +5 -0
  187. package/dist/esm/wrappers/cbor2.js.map +1 -0
  188. package/dist/esm/wrappers/cbor2Wrapper.d.ts +5 -0
  189. package/dist/esm/wrappers/cbor2Wrapper.js +6 -0
  190. package/dist/esm/wrappers/cbor2Wrapper.js.map +1 -0
  191. package/dist/esm/xoshiro.d.ts +12 -0
  192. package/dist/esm/xoshiro.js +47 -0
  193. package/dist/esm/xoshiro.js.map +1 -0
  194. package/dist/web/bytewords.js +335 -0
  195. package/index.html +98 -0
  196. package/package.json +94 -0
  197. package/src/classes/FountainDecoder.ts +539 -0
  198. package/src/classes/FountainEncoder.ts +211 -0
  199. package/src/classes/RegistryItem.ts +240 -0
  200. package/src/classes/UR.ts +308 -0
  201. package/src/classes/UrFountainDecoder.ts +142 -0
  202. package/src/classes/UrFountainEncoder.ts +103 -0
  203. package/src/classes/key.helper.ts +85 -0
  204. package/src/encodingMethods/BytewordEncoding.ts +23 -0
  205. package/src/encodingMethods/CborEncoding.ts +196 -0
  206. package/src/encodingMethods/HexEncoding.ts +23 -0
  207. package/src/encodingMethods/UrEncoding.ts +19 -0
  208. package/src/encodingMethods/bytewords.ts +215 -0
  209. package/src/encodingMethods/index.ts +26 -0
  210. package/src/encodingMethods/pipeline.ts +103 -0
  211. package/src/enums/EncodingMethodName.ts +10 -0
  212. package/src/errors.ts +34 -0
  213. package/src/helpers/aliasSampling.ts +87 -0
  214. package/src/helpers/crc32.ts +19 -0
  215. package/src/helpers/fountainUtils.ts +157 -0
  216. package/src/helpers/sha256.ts +88 -0
  217. package/src/helpers/type.helper.ts +1 -0
  218. package/src/helpers/uintArrayHelper.ts +611 -0
  219. package/src/helpers/utils.ts +135 -0
  220. package/src/index-react-native.ts +12 -0
  221. package/src/index.ts +44 -0
  222. package/src/interfaces/IEncodingMethod.ts +10 -0
  223. package/src/registry.ts +146 -0
  224. package/src/test.utils.ts +105 -0
  225. package/src/wrappers/cbor2-cjs.cts +6 -0
  226. package/src/wrappers/cbor2-deno.mts +6 -0
  227. package/src/wrappers/cbor2.ts +7 -0
  228. package/src/wrappers/cbor2Wrapper.ts +14 -0
  229. package/src/xoshiro.ts +66 -0
@@ -0,0 +1,125 @@
1
+ export type MultipartPayload = {
2
+ seqNum: number;
3
+ seqLength: number;
4
+ messageLength: number;
5
+ checksum: number;
6
+ fragment: Uint8Array;
7
+ };
8
+ declare class FountainBlock {
9
+ protected _indexes: number[];
10
+ protected _fragment: Uint8Array;
11
+ constructor(_indexes: number[], _fragment: Uint8Array);
12
+ /** What blocks is mixed in this block */
13
+ get indexes(): number[];
14
+ /** Data */
15
+ get fragment(): Uint8Array<ArrayBufferLike>;
16
+ isSimple(): boolean;
17
+ isSubsetOf(other: FountainBlock): boolean;
18
+ isSupersetOf(other: FountainBlock): boolean;
19
+ reduceBy(other: FountainBlock): FountainBlock;
20
+ }
21
+ export declare class FountainDecoder {
22
+ /** Did we received any parts and started decoding */
23
+ started: boolean;
24
+ done: boolean;
25
+ /** Stores the error if decoding fauls */
26
+ protected error: Error | undefined;
27
+ /** Stores the decoded data*/
28
+ protected resultRaw: Uint8Array | undefined;
29
+ /** Stores the expected length of the final message */
30
+ protected expectedMessageLength: number;
31
+ /** Stores the expected checksum of the final message */
32
+ protected expectedChecksum: number;
33
+ /** Stores the expected message length of each fragment */
34
+ protected expectedFragmentLength: number;
35
+ /** Total number of simple fragments */
36
+ protected expectedPartCount: number;
37
+ /** Mixed Parts that we cannot reduce to simple parts yet */
38
+ protected mixedBlocks: FountainBlock[];
39
+ /** Non-mixed single parts */
40
+ protected simpleBlocks: FountainBlock[];
41
+ /** Queue of parts that may take part in reduction */
42
+ protected queuedBlocks: FountainBlock[];
43
+ /** Bitmap array of seen block */
44
+ seenBlocks: number[];
45
+ /** Bitmap array of decoded blocks */
46
+ decodedBlocks: number[];
47
+ /** Keeps track of the how many parts have been processed */
48
+ protected processedPartsCount: number;
49
+ get result(): Uint8Array | Error | undefined;
50
+ isSuccessful(): boolean;
51
+ isComplete(): boolean;
52
+ constructor(parts?: Uint8Array[]);
53
+ reset(): void;
54
+ protected setExpectedValues(decodedPart: MultipartPayload): void;
55
+ /**
56
+ * Set the expected values on the initial run the current decoder.
57
+ * And check if the next multipart ur is a 'member' of the originally scanned ur with the current decoder.
58
+ * @param decodedPart received multipart ur
59
+ * @returns boolean indicating if the multipart ur is a 'member' of the originally scanned ur with the current decoder.
60
+ */
61
+ protected validatePart(decodedPart: MultipartPayload): boolean;
62
+ finalize(): void;
63
+ parseInput(input: Uint8Array | MultipartPayload): MultipartPayload;
64
+ receivePart(encodedPart: Uint8Array | MultipartPayload): boolean;
65
+ protected processQueue(): void;
66
+ /**
67
+ * Process a "pure" fragment. this is a original fragment that is not mixed with any other fragments.
68
+ * @param block object with the indexes and the fragment payload buffer.
69
+ * @returns
70
+ */
71
+ protected processSimpleBlock(block: FountainBlock): void;
72
+ /**
73
+ * Process the mixed parts
74
+ * @param newPart
75
+ * @returns
76
+ */
77
+ protected processMixedBlock(newPart: FountainBlock): void;
78
+ /**
79
+ * Process all the mixed blocks by the given block
80
+ * If the mixed part can be reduced to a simple part, add it to the queue
81
+ * If a mixed part is reduced to simpler part add it to the mixed
82
+ * @param block
83
+ */
84
+ protected reduceAllMixedBlocksBy(block: FountainBlock): void;
85
+ /**
86
+ *
87
+ *
88
+ * The next part it receives is 3: A ⊕ B ⊕ C ⊕ D.
89
+ * Each time a part is received, the decoder checks to see whether the set of fragments it contains
90
+ * is a proper subset or superset of the set of fragments in any part is has received.
91
+ * If so, it can reduce the superset part by the subset part. In this case,
92
+ * it discovers it can reduce the incoming part 3: A ⊕ B ⊕ C ⊕ D by XORing it with part 1: A ⊕ B ⊕ C,
93
+ * yielding the simple part 3: D.
94
+ */
95
+ /**
96
+ *
97
+ * Try the reduce mixed part A by the part B
98
+ * If B is a subset of A then we can reduce A by B
99
+ * Otherwise return A
100
+ *
101
+ * @param a existing mixedpart
102
+ * @param b newly received mixedpart
103
+ * @returns
104
+ */
105
+ private reducePartByPart_;
106
+ private reducePartByPart;
107
+ estimatedPercentComplete(): number;
108
+ getProgress(): number;
109
+ getDecodedData(): any;
110
+ getError(): Error | undefined;
111
+ }
112
+ export type IMultipartUrPayload = [number, number, number, number, Uint8Array];
113
+ /**
114
+ * Parse CBOR encoded Multipart Payload
115
+ * @param encodeded
116
+ * @returns
117
+ */
118
+ export declare function parseMultipartCbor(encodeded: Uint8Array): MultipartPayload;
119
+ /**
120
+ * Validate and convert the decoded multipart payload to MultipartPayload object
121
+ * @param decoded
122
+ * @returns
123
+ */
124
+ export declare function validateDecodedMultipart(decoded: IMultipartUrPayload): MultipartPayload;
125
+ export {};
@@ -0,0 +1,453 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FountainDecoder = void 0;
4
+ exports.parseMultipartCbor = parseMultipartCbor;
5
+ exports.validateDecodedMultipart = validateDecodedMultipart;
6
+ const utils_js_1 = require("../helpers/utils.js");
7
+ const uintArrayHelper_js_1 = require("../helpers/uintArrayHelper.js");
8
+ const errors_js_1 = require("../errors.js");
9
+ const fountainUtils_js_1 = require("../helpers/fountainUtils.js");
10
+ const utils_js_2 = require("../helpers/utils.js");
11
+ const CborEncoding_js_1 = require("../encodingMethods/CborEncoding.js");
12
+ const cborEncoder = new CborEncoding_js_1.CborEncoding();
13
+ class FountainBlock {
14
+ _indexes;
15
+ _fragment;
16
+ constructor(_indexes, _fragment) {
17
+ this._indexes = _indexes;
18
+ this._fragment = _fragment;
19
+ }
20
+ /** What blocks is mixed in this block */
21
+ get indexes() {
22
+ return this._indexes;
23
+ }
24
+ /** Data */
25
+ get fragment() {
26
+ return this._fragment;
27
+ }
28
+ isSimple() {
29
+ return this.indexes.length === 1;
30
+ }
31
+ isSubsetOf(other) {
32
+ // Dont use set use array
33
+ // return isSubset(new Set(this.indexes), new Set(other.indexes));
34
+ return (0, utils_js_2.arrayContains)(other.indexes, this.indexes);
35
+ }
36
+ isSupersetOf(other) {
37
+ // return isSubset(new Set(other.indexes), new Set(this.indexes));
38
+ return (0, utils_js_2.arrayContains)(this.indexes, other.indexes);
39
+ }
40
+ reduceBy(other) {
41
+ // Check if the other block is a subset of this block
42
+ if (!this.isSupersetOf(other)) {
43
+ return this;
44
+ }
45
+ // Otherwise reduce the other block from this block
46
+ // const newIndexes = difference(new Set(this.indexes), new Set(other.indexes));
47
+ const newIndexes = (0, utils_js_2.setDifference)(this.indexes, other.indexes);
48
+ const newFragment = (0, utils_js_2.bufferXOR)(this.fragment, other.fragment);
49
+ return new FountainBlock([...newIndexes], newFragment);
50
+ }
51
+ }
52
+ class FountainDecoder {
53
+ /** Did we received any parts and started decoding */
54
+ started = false;
55
+ done = false;
56
+ /** Stores the error if decoding fauls */
57
+ error;
58
+ /** Stores the decoded data*/
59
+ resultRaw = undefined;
60
+ /** Stores the expected length of the final message */
61
+ expectedMessageLength = 0;
62
+ /** Stores the expected checksum of the final message */
63
+ expectedChecksum = 0;
64
+ /** Stores the expected message length of each fragment */
65
+ expectedFragmentLength = 0;
66
+ /** Total number of simple fragments */
67
+ expectedPartCount = 0;
68
+ /** Mixed Parts that we cannot reduce to simple parts yet */
69
+ mixedBlocks = [];
70
+ /** Non-mixed single parts */
71
+ simpleBlocks = [];
72
+ /** Queue of parts that may take part in reduction */
73
+ queuedBlocks = [];
74
+ // For tracking the progress of decoding we can keep seen indexes and decoded indexes
75
+ /** Bitmap array of seen block */
76
+ seenBlocks = [];
77
+ /** Bitmap array of decoded blocks */
78
+ decodedBlocks = [];
79
+ /** Keeps track of the how many parts have been processed */
80
+ processedPartsCount = 0;
81
+ get result() {
82
+ return this.resultRaw || this.error;
83
+ }
84
+ isSuccessful() {
85
+ return this.done && !this.error;
86
+ }
87
+ isComplete() {
88
+ return this.done;
89
+ }
90
+ constructor(parts = []) {
91
+ parts.forEach((part) => this.receivePart(part));
92
+ }
93
+ reset() {
94
+ this.error = undefined;
95
+ this.resultRaw = undefined;
96
+ this.expectedMessageLength = 0;
97
+ this.expectedChecksum = 0;
98
+ this.expectedFragmentLength = 0;
99
+ this.mixedBlocks = [];
100
+ this.simpleBlocks = [];
101
+ this.queuedBlocks = [];
102
+ this.seenBlocks = [];
103
+ this.decodedBlocks = [];
104
+ this.processedPartsCount = 0;
105
+ }
106
+ setExpectedValues(decodedPart) {
107
+ this.expectedPartCount = decodedPart.seqLength;
108
+ this.expectedMessageLength = decodedPart.messageLength;
109
+ this.expectedChecksum = decodedPart.checksum;
110
+ this.expectedFragmentLength = decodedPart.fragment.length;
111
+ this.seenBlocks = new Array(this.expectedPartCount).fill(0);
112
+ this.decodedBlocks = new Array(this.expectedPartCount).fill(0);
113
+ // Set started so we know we have expected values
114
+ this.started = true;
115
+ this.done = false;
116
+ }
117
+ /**
118
+ * Set the expected values on the initial run the current decoder.
119
+ * And check if the next multipart ur is a 'member' of the originally scanned ur with the current decoder.
120
+ * @param decodedPart received multipart ur
121
+ * @returns boolean indicating if the multipart ur is a 'member' of the originally scanned ur with the current decoder.
122
+ */
123
+ validatePart(decodedPart) {
124
+ // If this part's values don't match the first part's values, throw away the part
125
+ if (this.expectedPartCount !== decodedPart.seqLength) {
126
+ // TODO: display proper error message
127
+ return false;
128
+ }
129
+ if (this.expectedMessageLength !== decodedPart.messageLength) {
130
+ return false;
131
+ }
132
+ if (this.expectedChecksum !== decodedPart.checksum) {
133
+ return false;
134
+ }
135
+ if (this.expectedFragmentLength !== decodedPart.fragment.length) {
136
+ return false;
137
+ }
138
+ // This part should be processed
139
+ return true;
140
+ }
141
+ finalize() {
142
+ if (this.simpleBlocks.length !== this.expectedPartCount) {
143
+ console.warn("Not all parts have been received");
144
+ return;
145
+ }
146
+ // Sort the simple blocks by their index
147
+ const sortedParts = [...this.simpleBlocks].sort((a, b) => a.indexes[0] - b.indexes[0]);
148
+ // Get fragments in order and combine them to message
149
+ const message = joinFragments(sortedParts.map((p) => p.fragment), this.expectedMessageLength);
150
+ // Get the final checksum
151
+ const checksum = (0, utils_js_2.getCRC)(message);
152
+ if (checksum === this.expectedChecksum) {
153
+ this.resultRaw = message;
154
+ }
155
+ else {
156
+ this.error = new errors_js_1.InvalidChecksumError();
157
+ }
158
+ this.done = true;
159
+ }
160
+ parseInput(input) {
161
+ // Check the type of input payload
162
+ if (input instanceof Uint8Array) {
163
+ return parseMultipartCbor(input);
164
+ }
165
+ else if (typeof input === "object") {
166
+ // Check its correct type
167
+ if (!validateMultipartPayload(input)) {
168
+ throw new Error("Invalid multipart payload");
169
+ }
170
+ return input;
171
+ }
172
+ throw new Error("Invalid input type");
173
+ }
174
+ receivePart(encodedPart) {
175
+ // If we already have a result, we're done
176
+ if (this.done) {
177
+ return false;
178
+ }
179
+ let decodedPart;
180
+ try {
181
+ decodedPart = this.parseInput(encodedPart);
182
+ }
183
+ catch (error) {
184
+ // Skip receiving invalid parts
185
+ console.warn("Cannot parse part", error);
186
+ return false;
187
+ }
188
+ try {
189
+ // If this is the first part we've seen then set expected values
190
+ if (!this.started)
191
+ this.setExpectedValues(decodedPart);
192
+ // Validate part
193
+ if (!this.validatePart(decodedPart)) {
194
+ return false;
195
+ }
196
+ // Now we can start processing the part
197
+ // Find which original data fragments are in this fragment what is mixed in this
198
+ let indexes = (0, fountainUtils_js_1.chooseFragments)(decodedPart.seqNum, decodedPart.seqLength, decodedPart.checksum);
199
+ // order the indexes
200
+ indexes = indexes.sort((a, b) => a - b);
201
+ // Get the data
202
+ const fragment = decodedPart.fragment;
203
+ const block = new FountainBlock(indexes, fragment);
204
+ this.queuedBlocks.push(block);
205
+ this.processQueue();
206
+ this.processedPartsCount += 1;
207
+ }
208
+ catch (error) {
209
+ // Skip receiving invalid parts
210
+ console.warn("Error receiving part", error);
211
+ return false;
212
+ }
213
+ return true;
214
+ }
215
+ // http://blog.notdot.net/2012/01/Damn-Cool-Algorithms-Fountain-Codes
216
+ processQueue() {
217
+ // Process the queued blocks until queue is empty or we're done
218
+ while (!this.done && this.queuedBlocks.length > 0) {
219
+ const block = this.queuedBlocks.shift();
220
+ // Add indexes to seen indexes
221
+ block.indexes.forEach((index) => {
222
+ this.seenBlocks[index] = 1;
223
+ });
224
+ if (block.isSimple()) {
225
+ this.processSimpleBlock(block);
226
+ }
227
+ else {
228
+ this.processMixedBlock(block);
229
+ }
230
+ }
231
+ }
232
+ /**
233
+ * Process a "pure" fragment. this is a original fragment that is not mixed with any other fragments.
234
+ * @param block object with the indexes and the fragment payload buffer.
235
+ * @returns
236
+ */
237
+ processSimpleBlock(block) {
238
+ if (!block.isSimple()) {
239
+ throw new Error("Part is not simple");
240
+ }
241
+ // Don't process duplicate blocks
242
+ // if (this.simpleBlocks.has(block.indexes)) return;
243
+ if (this.simpleBlocks.some((b) => (0, utils_js_2.arraysEqual)(b.indexes, block.indexes)))
244
+ return;
245
+ // Add our block to simple blocks
246
+ this.simpleBlocks.push(block);
247
+ // Keep track of the decoded blocks
248
+ this.decodedBlocks[block.indexes[0]] = 1;
249
+ // If we've received all the parts
250
+ if (this.simpleBlocks.length == this.expectedPartCount) {
251
+ this.finalize();
252
+ }
253
+ else {
254
+ // Otherwise try to reduce the all the mixed parts by this simple part
255
+ this.reduceAllMixedBlocksBy(block);
256
+ }
257
+ }
258
+ /**
259
+ * Process the mixed parts
260
+ * @param newPart
261
+ * @returns
262
+ */
263
+ processMixedBlock(newPart) {
264
+ // Check if already have this block, if so pass
265
+ // if (this.mixedBlocks.has(newPart.indexes)) {
266
+ // return;
267
+ // }
268
+ if (this.mixedBlocks.some((b) => (0, utils_js_2.arraysEqual)(b.indexes, newPart.indexes))) {
269
+ return;
270
+ }
271
+ // Get all simple blocks that we have that makes up this part and reduce current block by whatever we have
272
+ let reducedBlock = this.simpleBlocks.reduce((proccessed, currentSimple) => proccessed.reduceBy(currentSimple), newPart);
273
+ // Now check if we have a simple part if so add it to the queue
274
+ if (reducedBlock.isSimple()) {
275
+ this.queuedBlocks.push(reducedBlock);
276
+ return;
277
+ }
278
+ // We still have have a mixed block
279
+ // So we will check if there are any subsets of this block that we can reduce
280
+ // So if we have 1x2x3 try to find subparts (1x2, 2x3, 1x3) that will directly reduce this part to simple part
281
+ // For now we will go though all the parts and try to reduce them so if we have 1x2x3x4 XOR 1x2 we will get 3x4
282
+ reducedBlock = this.mixedBlocks.reduce((proccessed, currentMixed) => proccessed.reduceBy(currentMixed), reducedBlock);
283
+ // If after all the operations we have a simple part add it to the queue
284
+ if (reducedBlock.isSimple()) {
285
+ this.queuedBlocks.push(reducedBlock);
286
+ return;
287
+ }
288
+ // If we dont have a simple part we will try to reduce all the mixed parts by this part
289
+ this.reduceAllMixedBlocksBy(reducedBlock);
290
+ // Then add our part to the mixed parts
291
+ this.mixedBlocks.push(reducedBlock);
292
+ }
293
+ /**
294
+ * Process all the mixed blocks by the given block
295
+ * If the mixed part can be reduced to a simple part, add it to the queue
296
+ * If a mixed part is reduced to simpler part add it to the mixed
297
+ * @param block
298
+ */
299
+ reduceAllMixedBlocksBy(block) {
300
+ const newMixed = [];
301
+ // Try to reduce all the mixed parts by this simple part
302
+ this.mixedBlocks
303
+ .map((mixedPart) => mixedPart.reduceBy(block))
304
+ .forEach((reducedPart) => {
305
+ if (reducedPart.isSimple()) {
306
+ // Add to the queue if it is a simple part
307
+ this.queuedBlocks.push(reducedPart);
308
+ }
309
+ else {
310
+ // Otherwise add it to as a new mixed part
311
+ newMixed.push(reducedPart);
312
+ }
313
+ });
314
+ // Override the mixed parts with new reduces parts
315
+ this.mixedBlocks = newMixed;
316
+ }
317
+ /**
318
+ *
319
+ *
320
+ * The next part it receives is 3: A ⊕ B ⊕ C ⊕ D.
321
+ * Each time a part is received, the decoder checks to see whether the set of fragments it contains
322
+ * is a proper subset or superset of the set of fragments in any part is has received.
323
+ * If so, it can reduce the superset part by the subset part. In this case,
324
+ * it discovers it can reduce the incoming part 3: A ⊕ B ⊕ C ⊕ D by XORing it with part 1: A ⊕ B ⊕ C,
325
+ * yielding the simple part 3: D.
326
+ */
327
+ /**
328
+ *
329
+ * Try the reduce mixed part A by the part B
330
+ * If B is a subset of A then we can reduce A by B
331
+ * Otherwise return A
332
+ *
333
+ * @param a existing mixedpart
334
+ * @param b newly received mixedpart
335
+ * @returns
336
+ */
337
+ reducePartByPart_(a, b) {
338
+ // If the fragments mixed into `b` are a strict (proper) subset of those in `a`...
339
+ const aSet = new Set(a.indexes);
340
+ const bSet = new Set(b.indexes);
341
+ // If B is a subset of A then we can reduce A by B
342
+ if ((0, utils_js_1.isSubset)(bSet, aSet)) {
343
+ // A - B => new indexes in the mixed part
344
+ const newIndexes = (0, utils_js_1.difference)(aSet, bSet);
345
+ const newFragment = (0, utils_js_2.bufferXOR)(a.fragment, b.fragment);
346
+ return new FountainBlock([...newIndexes], newFragment);
347
+ }
348
+ // If A is a subset of B then we can reduce B by A
349
+ // else if (isSubset(aSet, bSet)) {
350
+ // // B - A => new indexes in the mixed part
351
+ // const newIndexes = difference(bSet, aSet);
352
+ // const newFragment = bufferXOR(b.fragment, a.fragment);
353
+ // return new FountainEncodedPart([...newIndexes], newFragment);
354
+ // }
355
+ else {
356
+ // If A is not reducable by B then return A
357
+ return a;
358
+ }
359
+ }
360
+ reducePartByPart(a, b) {
361
+ // If the fragments mixed into `b` are a strict (proper) subset of those in `a`...
362
+ if ((0, utils_js_2.arrayContains)(a.indexes, b.indexes)) {
363
+ const newIndexes = (0, utils_js_2.setDifference)(a.indexes, b.indexes);
364
+ const newFragment = (0, utils_js_2.bufferXOR)(a.fragment, b.fragment);
365
+ return new FountainBlock(newIndexes, newFragment);
366
+ }
367
+ else {
368
+ // `a` is not reducable by `b`, so return a
369
+ return a;
370
+ }
371
+ }
372
+ estimatedPercentComplete() {
373
+ if (this.done) {
374
+ return 1;
375
+ }
376
+ const expectedPartCount = this.expectedPartCount;
377
+ if (expectedPartCount === 0) {
378
+ return 0;
379
+ }
380
+ // We multiply the expectedPartCount by `1.75` as a way to compensate for the facet
381
+ // that `this.processedPartsCount` also tracks the duplicate parts that have been
382
+ // processeed.
383
+ return Math.min(0.99, this.processedPartsCount / (expectedPartCount * 1.75));
384
+ }
385
+ getProgress() {
386
+ if (this.done) {
387
+ return 1;
388
+ }
389
+ const expectedPartCount = this.expectedPartCount;
390
+ if (expectedPartCount === 0) {
391
+ return 0;
392
+ }
393
+ return this.simpleBlocks.length / expectedPartCount;
394
+ }
395
+ getDecodedData() {
396
+ if (!this.isSuccessful()) {
397
+ console.log('Fountain decoding was not successful');
398
+ return undefined;
399
+ }
400
+ return cborEncoder.decode(this.resultRaw);
401
+ }
402
+ getError() {
403
+ return this.error;
404
+ }
405
+ }
406
+ exports.FountainDecoder = FountainDecoder;
407
+ /**
408
+ * Parse CBOR encoded Multipart Payload
409
+ * @param encodeded
410
+ * @returns
411
+ */
412
+ function parseMultipartCbor(encodeded) {
413
+ const decoded = cborEncoder.decode(encodeded);
414
+ return validateDecodedMultipart(decoded);
415
+ }
416
+ /**
417
+ * Validate and convert the decoded multipart payload to MultipartPayload object
418
+ * @param decoded
419
+ * @returns
420
+ */
421
+ function validateDecodedMultipart(decoded) {
422
+ const [seqNum, seqLength, messageLength, checksum, fragment] = decoded;
423
+ if (!validateMultipartPayload({ seqNum, seqLength, messageLength, checksum, fragment })) {
424
+ throw new Error("Invalid multipart payload");
425
+ }
426
+ return { seqNum, seqLength, messageLength, checksum, fragment };
427
+ }
428
+ function validateMultipartPayload(decoded) {
429
+ if (typeof decoded.seqNum !== "number" ||
430
+ typeof decoded.seqLength !== "number" ||
431
+ typeof decoded.messageLength !== "number" ||
432
+ typeof decoded.checksum !== "number" ||
433
+ !(0, uintArrayHelper_js_1.isUint8Array)(decoded.fragment) ||
434
+ decoded.fragment.length === 0) {
435
+ return false;
436
+ }
437
+ return true;
438
+ }
439
+ /**
440
+ * Join the fragments together.
441
+ * @param fragments fragments to join
442
+ * @param messageLength length of the expected message, full if not provided.
443
+ * @returns the concatenated fragments with the expected length.
444
+ */
445
+ function joinFragments(fragments, messageLength) {
446
+ let result = (0, uintArrayHelper_js_1.concatUint8Arrays)(fragments);
447
+ if (messageLength) {
448
+ // with 'slice', we remove the additionally created buffer parts, needed to achieve the minimum fragment length.
449
+ result = result.slice(0, messageLength);
450
+ }
451
+ return result;
452
+ }
453
+ //# sourceMappingURL=FountainDecoder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FountainDecoder.js","sourceRoot":"","sources":["../../../src/classes/FountainDecoder.ts"],"names":[],"mappings":";;;AA2eA,gDAGC;AAOD,4DAOC;AA5fD,kDAA2D;AAC3D,sEAAgF;AAEhF,4CAAwE;AACxE,kEAA8D;AAC9D,kDAAmG;AAEnG,wEAAkE;AAElE,MAAM,WAAW,GAAG,IAAI,8BAAY,EAAE,CAAC;AAUvC,MAAM,aAAa;IACK;IAA8B;IAApD,YAAsB,QAAkB,EAAY,SAAqB;QAAnD,aAAQ,GAAR,QAAQ,CAAU;QAAY,cAAS,GAAT,SAAS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S,wBAAwB,CAAC,OAAyB;IACzD,IACE,OAAO,OAAO,CAAC,MAAM,KAAK,QAAQ;QAClC,OAAO,OAAO,CAAC,SAAS,KAAK,QAAQ;QACrC,OAAO,OAAO,CAAC,aAAa,KAAK,QAAQ;QACzC,OAAO,OAAO,CAAC,QAAQ,KAAK,QAAQ;QACpC,CAAC,IAAA,iCAAY,EAAC,OAAO,CAAC,QAAQ,CAAC;QAC/B,OAAO,CAAC,QAAQ,CAAC,MAAM,KAAK,CAAC,EAC7B,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;;GAKG;AACH,SAAS,aAAa,CAAC,SAAuB,EAAE,aAAsB;IACpE,IAAI,MAAM,GAAG,IAAA,sCAAiB,EAAC,SAAS,CAAC,CAAC;IAC1C,IAAI,aAAa,EAAE,CAAC;QAClB,gHAAgH;QAChH,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,aAAa,CAAC,CAAC;IAC1C,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC"}
@@ -0,0 +1,63 @@
1
+ import { findNominalFragmentLength } from "../helpers/fountainUtils.js";
2
+ /** [seqNum, seqLength, messageLength, checksum, fragment] */
3
+ export type IMultipartUrPayload = [number, number, number, number, Uint8Array];
4
+ /**
5
+ * Encode data on the fly. This encoder uses an internal state to keep generating ur fragments of the payload.
6
+ */
7
+ export declare class FountainEncoder {
8
+ fragmentLenghtFinder: typeof findNominalFragmentLength;
9
+ /** Total data size of the input message */
10
+ _messageLength: number;
11
+ /** Maximum data size in the fragment */
12
+ protected _maxFragmentLength: number;
13
+ /** Minumum data size in the fragment */
14
+ protected _minFragmentLength: number;
15
+ /** Calculated data size in the fragment */
16
+ _nominalFragmentLength: number;
17
+ /** Array of pure fragments (without any fountain encoded) */
18
+ _pureFragments: Uint8Array[];
19
+ /** Current index of the fragment start from 1 */
20
+ protected _seqNum: number;
21
+ /** Checksum of the original data */
22
+ protected _checksum: number;
23
+ /** Original Input data as UR */
24
+ protected _input: Uint8Array;
25
+ constructor(input: Uint8Array, maxFragmentLength?: number, minFragmentLength?: number, firstSeqNum?: number);
26
+ setFragmentLengthFinder(fn: typeof findNominalFragmentLength): void;
27
+ /**
28
+ * Return all the fragments based on the fountain ratio at once as an array of Uint8Arrays.
29
+ * @param fountainRatio The ratio of the fountain fragments to the pure fragments. Default is 0.
30
+ * @returns
31
+ */
32
+ getAllParts(fountainRatio?: number): Uint8Array[];
33
+ protected encodeFragment(seqNum: number, fragment: Uint8Array): Uint8Array;
34
+ /**
35
+ * Reset the state of the encoder to start generating fragments from the beginning.
36
+ */
37
+ reset(): void;
38
+ /**
39
+ * Checks if all the pure fragments (full payload data) for this ur is generated.
40
+ * @returns boolean indicating if generated fragments have included all the data.
41
+ */
42
+ isComplete(): boolean;
43
+ /**
44
+ * Checks if there is only one fragment generated for the ur.
45
+ * @returns boolean if the ur payload is contained in one fragment.
46
+ */
47
+ isSinglePart(): boolean;
48
+ /**
49
+ * Gets the count of the "pure" fragments. These are fragments where the data is not mixed.
50
+ * @returns The count of the "pure" fragments.
51
+ */
52
+ getPureFragmentCount(): number;
53
+ /**
54
+ * Get the pure fragments of the ur.
55
+ * @returns the pure fragments of the ur as an array of Uint8Arrays.
56
+ */
57
+ getPureFragments(): Uint8Array[];
58
+ /**
59
+ * Give the 'next' fragment for the ur for which the fountainEncoder was created.
60
+ * @returns the 'next' fragment, represented as a Ur multipart string.
61
+ */
62
+ nextPart(): Uint8Array;
63
+ }