@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,243 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UR = void 0;
4
+ const RegistryItem_js_1 = require("./RegistryItem.js");
5
+ const EncodingMethodName_js_1 = require("../enums/EncodingMethodName.js");
6
+ const errors_js_1 = require("../errors.js");
7
+ const index_js_1 = require("../encodingMethods/index.js");
8
+ /**
9
+ * https://github.com/BlockchainCommons/Research/blob/master/papers/bcr-2020-005-ur.md
10
+ * Class that represents the structure of the data we encode/decode in this package.
11
+ * e.g. 'ur:bytes/lpamcmcfatrdcyzcpldpgwhdhtiaiaecgyktgsflguhshthfghjtjngrhsfegtiafegaktgugui'
12
+ *
13
+ *
14
+ * Single part = ur:<type>/<message(payload)>
15
+ * Multi part = ur:<type>/<seqNum-seqLength>/<fragment(payload)>
16
+ *
17
+ * CBOR encoding should not include tag on top level when UR has a tag
18
+ *
19
+ * I want to be able to create UR from encoding normal js types
20
+ * Or from any type in the pipeline
21
+ *
22
+ * Should i do it on the encoder or ur class itself?
23
+ */
24
+ class UR {
25
+ type;
26
+ payload;
27
+ seqNum;
28
+ seqLength;
29
+ isFragment;
30
+ static pipeline = index_js_1.dataPipeline;
31
+ // If type is unknown it should be cbor, because default encoding will take any and convert to cbor without tagging
32
+ constructor(input) {
33
+ // Create from registry item
34
+ if (input instanceof RegistryItem_js_1.RegistryItemBase || (0, RegistryItem_js_1.isRegistryItem)(input)) {
35
+ const ur = UR.fromRegistryItem(input);
36
+ this.type = ur.type;
37
+ this.payload = ur.payload;
38
+ this.seqNum = ur.seqNum;
39
+ this.seqLength = ur.seqLength;
40
+ this.isFragment = ur.isFragment;
41
+ }
42
+ // Create from raw data
43
+ else if (typeof input == "object" && 'type' in input && 'payload' in input) {
44
+ const { type, payload, seqNum = 0, seqLength = 0 } = input;
45
+ this.payload = payload;
46
+ this.type = type;
47
+ this.seqNum = seqNum;
48
+ this.seqLength = seqLength;
49
+ this.isFragment = seqLength > 0;
50
+ }
51
+ else {
52
+ throw new errors_js_1.InvalidTypeError();
53
+ }
54
+ }
55
+ // Decode
56
+ decode(until) {
57
+ return UR.pipeline.decode(this.payload, { until, enforceType: this.isFragment ? undefined : this.type });
58
+ }
59
+ // Get string representation
60
+ toString() {
61
+ return UR.getUrString(this.type, this.payload, this.seqNum, this.seqLength);
62
+ }
63
+ // Get payload in bytewords
64
+ getPayloadBytewords() {
65
+ return this.payload;
66
+ }
67
+ // Get payload in hex
68
+ getPayloadHex() {
69
+ return UR.pipeline.decode(this.payload, { until: EncodingMethodName_js_1.EncodingMethodName.hex });
70
+ // TODO: add tag information
71
+ }
72
+ // Get Payload in cbor
73
+ getPayloadCbor() {
74
+ return UR.pipeline.decode(this.payload, { until: EncodingMethodName_js_1.EncodingMethodName.cbor });
75
+ // TODO: add tag information
76
+ }
77
+ toRegistryItem() {
78
+ if (this.isFragment) {
79
+ throw new Error("Cannot convert a multipart ur to registry item, it needs to be decoded first");
80
+ }
81
+ // Enforce type
82
+ // registrtqueryByURType
83
+ return UR.decode(this);
84
+ }
85
+ /// Static methods
86
+ static fromRegistryItem(item) {
87
+ // First convert Registry item to bytewords
88
+ // Only on the top level, we should not include the tag
89
+ const bytewords = UR.pipeline.encode(item, { ignoreTopLevelTag: true });
90
+ // Now create new UR
91
+ return new UR({
92
+ type: item.type.URType,
93
+ payload: bytewords,
94
+ });
95
+ }
96
+ /**
97
+ * Create UR from native javascript types by encoding them to bytewords
98
+ * @param input
99
+ * @returns
100
+ */
101
+ static fromData(input) {
102
+ const bytewords = UR.pipeline.encode(input.payload);
103
+ // Now create new UR
104
+ return new UR({
105
+ ...input,
106
+ payload: bytewords,
107
+ });
108
+ }
109
+ static fromCbor(input) {
110
+ // This means we already have cbor Uint8Array
111
+ // So we will start from the next method
112
+ const bytewords = UR.pipeline.encode(input.payload, { from: EncodingMethodName_js_1.EncodingMethodName.hex });
113
+ return new UR({
114
+ ...input,
115
+ payload: bytewords,
116
+ });
117
+ }
118
+ static fromHex(input) {
119
+ // This means we already have hex string
120
+ // So we will start from the next method
121
+ const bytewords = UR.pipeline.encode(input.payload, { from: EncodingMethodName_js_1.EncodingMethodName.bytewords });
122
+ return new UR({
123
+ ...input,
124
+ payload: bytewords,
125
+ });
126
+ }
127
+ static fromBytewords(input) {
128
+ return new UR({
129
+ ...input,
130
+ });
131
+ }
132
+ static from(input, type) {
133
+ switch (type) {
134
+ case EncodingMethodName_js_1.EncodingMethodName.bytewords:
135
+ return UR.fromBytewords(input);
136
+ case EncodingMethodName_js_1.EncodingMethodName.cbor:
137
+ return UR.fromCbor(input);
138
+ case EncodingMethodName_js_1.EncodingMethodName.hex:
139
+ return UR.fromHex(input);
140
+ case EncodingMethodName_js_1.EncodingMethodName.ur:
141
+ return UR.fromData(input);
142
+ default:
143
+ throw new Error("Invalid encoding method");
144
+ }
145
+ }
146
+ static fromString(ur) {
147
+ return new UR(UR.parseUr(ur));
148
+ }
149
+ static encode = this.fromRegistryItem;
150
+ static decode(ur) {
151
+ // Force cbor type
152
+ return UR.pipeline.decode(ur.payload);
153
+ }
154
+ /**
155
+ * Check if the given string is a valid UR
156
+ * For single part ur, it should be in the form of "ur:<type>/<payload>" which is "ur:<lowercase letters, numbers or dashes>/<bytewords>"
157
+ * For multi part ur, it should be in the form of "ur:<type>/<seqNum-seqLength>/<fragment>" which is "ur:<lowercase letters, numbers or dashes>/<number-number>/<bytewords>"
158
+ * ur uses minimal bytewords encoding style which is a-z and has checksome bytes at the end
159
+ *
160
+ * @param input
161
+ * @returns
162
+ */
163
+ static validate(input) {
164
+ // TODO: use bytewords encoding to validate the payload
165
+ const singlePartPattern = /^ur:[a-z0-9-]+\/[a-z]+$/;
166
+ const multiPartPattern = /^ur:[a-z0-9-]+\/[0-9]+-[0-9]+\/[a-z]+$/;
167
+ return singlePartPattern.test(input) || multiPartPattern.test(input);
168
+ }
169
+ /**
170
+ * Generates a UR string from the given type and payload.
171
+ * Single part = ur:<type>/<message(payload)> if seqNum and seqLength are 0
172
+ * Multi part = ur:<type>/<seqNum-seqLength>/<fragment(payload)>
173
+ */
174
+ static getUrString(type, payload, seqNum = 0, seqLen = 0) {
175
+ // Single UR
176
+ if (seqNum === 0 && seqLen === 0) {
177
+ return joinUri("ur", [type, payload]);
178
+ }
179
+ // Multi part UR
180
+ const seq = `${seqNum}-${seqLen}`;
181
+ return joinUri("ur", [type, seq, payload]);
182
+ }
183
+ /**
184
+ * Parses a UR and performs basic validation
185
+ * @param message e.g. "UR:BYTES/6-23/LPAMCHCFATTTCYCLEHGSDPHDHGEHFGHKKKDL..."
186
+ */
187
+ static parseUr(message) {
188
+ const lowercase = message.toLowerCase(); // e.g. "ur:bytes/6-23/lpamchcfatttcyclehgsdphdhgehfghkkkdl..."
189
+ // check if it is valid ur string
190
+ if (!UR.validate(lowercase)) {
191
+ throw new Error("Invalid UR string");
192
+ }
193
+ const components = lowercase.slice(3).split("/");
194
+ let isFragment = false;
195
+ let seqNum = 0;
196
+ let seqLength = 0;
197
+ let payload = "";
198
+ // Check for the number of components
199
+ switch (components.length) {
200
+ case 2:
201
+ // single part ur
202
+ isFragment = false;
203
+ break;
204
+ case 3:
205
+ // multi part ur
206
+ isFragment = true;
207
+ break;
208
+ default:
209
+ throw new errors_js_1.InvalidPathLengthError();
210
+ }
211
+ const type = components[0]; //e.g. "bytes"
212
+ if (isFragment) {
213
+ const seq = components[1]; //e.g. "6-23"
214
+ const [num, length] = seq.split("-").map(Number);
215
+ seqNum = num;
216
+ seqLength = length;
217
+ payload = components[2];
218
+ }
219
+ else {
220
+ payload = components[1];
221
+ }
222
+ return {
223
+ type,
224
+ payload,
225
+ seqNum,
226
+ seqLength,
227
+ isFragment,
228
+ };
229
+ }
230
+ }
231
+ exports.UR = UR;
232
+ /// Helper functions
233
+ /**
234
+ * Generates a uri. e.g. 'ur:bytes/6-22/lpamcmcfatrdcyzcpldpgwhdhtiaiaecgyktgsflguhshthfghjtjngrhsfegtiafegaktgugui'
235
+ * @param scheme scheme of the uri. e.g. "ur"
236
+ * @param pathComponents adds additional information to the uri in the form of a path (divided by "/"). e.g. "bytes/6-22/lpamcmcfatrdcyzcpldpgwhdhtiaiaecgyktgsflguhshthfghjtjngrhsfegtiafegaktgugui"
237
+ * @returns the complete uri.
238
+ */
239
+ function joinUri(scheme, pathComponents) {
240
+ const path = pathComponents.join("/");
241
+ return [scheme, path].join(":");
242
+ }
243
+ //# sourceMappingURL=UR.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"UR.js","sourceRoot":"","sources":["../../../src/classes/UR.ts"],"names":[],"mappings":";;;AAAA,uDAAmF;AACnF,0EAAoE;AAEpE,4CAIsB;AACtB,0DAA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G,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;YACjD,MAAM,GAAG,GAAG,CAAC;YACb,SAAS,GAAG,MAAM,CAAC;YACnB,OAAO,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC;QAC1B,CAAC;aAAM,CAAC;YACN,OAAO,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC;QAC1B,CAAC;QAED,OAAO;YACL,IAAI;YACJ,OAAO;YACP,MAAM;YACN,SAAS;YACT,UAAU;SACX,CAAC;IACJ,CAAC;;AAnPH,gBAoPC;AAED,oBAAoB;AAEpB;;;;;GAKG;AACH,SAAS,OAAO,CAAC,MAAc,EAAE,cAAwB;IACvD,MAAM,IAAI,GAAG,cAAc,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACtC,OAAO,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAClC,CAAC"}
@@ -0,0 +1,15 @@
1
+ import { RegistryItem } from "../classes/RegistryItem.js";
2
+ import { FountainDecoder, MultipartPayload } from "./FountainDecoder.js";
3
+ import { UR } from "./UR.js";
4
+ export declare class UrFountainDecoder extends FountainDecoder {
5
+ expectedType: string;
6
+ resultUr: UR;
7
+ decodedData: RegistryItem | any;
8
+ constructor(parts?: UR[] | string[]);
9
+ reset(): void;
10
+ setExpectedValuesUr(part: UR, decodedPart: MultipartPayload): void;
11
+ protected validateUr(part: UR, decodedPart: MultipartPayload): boolean;
12
+ receivePartUr(part: UR | string): boolean;
13
+ finalize(): void;
14
+ getDecodedData(): RegistryItem | any;
15
+ }
@@ -0,0 +1,127 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UrFountainDecoder = void 0;
4
+ const EncodingMethodName_js_1 = require("../enums/EncodingMethodName.js");
5
+ const FountainDecoder_js_1 = require("./FountainDecoder.js");
6
+ const UR_js_1 = require("./UR.js");
7
+ class UrFountainDecoder extends FountainDecoder_js_1.FountainDecoder {
8
+ expectedType;
9
+ resultUr;
10
+ decodedData;
11
+ constructor(parts = []) {
12
+ super();
13
+ parts.forEach((part) => {
14
+ this.receivePartUr(part);
15
+ });
16
+ }
17
+ reset() {
18
+ super.reset();
19
+ this.resultUr = undefined;
20
+ this.expectedType = undefined;
21
+ }
22
+ setExpectedValuesUr(part, decodedPart) {
23
+ this.expectedType = part.type;
24
+ this.expectedPartCount = part.seqLength;
25
+ super.setExpectedValues(decodedPart);
26
+ }
27
+ validateUr(part, decodedPart) {
28
+ // Check if UR is a fragment
29
+ if (!part.isFragment) {
30
+ return false;
31
+ }
32
+ // Check the type of the UR
33
+ if (part.type !== this.expectedType) {
34
+ return false;
35
+ }
36
+ // Expect metadata ur seqNum to be equal to the decoded part seqNum
37
+ if (part.seqNum !== decodedPart.seqNum) {
38
+ return false;
39
+ }
40
+ // Expect metadata ur seqLength to be equal to the decoded part seqLength
41
+ if (part.seqLength !== decodedPart.seqLength) {
42
+ return false;
43
+ }
44
+ // Validate decoded paylad
45
+ return super.validatePart(decodedPart);
46
+ }
47
+ receivePartUr(part) {
48
+ // If we already have a result, we're done
49
+ if (this.done) {
50
+ return false;
51
+ }
52
+ // Convert string into UR
53
+ if (typeof part === "string") {
54
+ part = UR_js_1.UR.fromString(part);
55
+ }
56
+ // If what we received is not a multupart UR, then we're done
57
+ if (!part.isFragment) {
58
+ // If this is not a fragment and we have not received any fragments yet then its the whole UR
59
+ if (!this.started) {
60
+ this.resultUr = part;
61
+ this.started = true;
62
+ this.done = true;
63
+ try {
64
+ this.decodedData = this.resultUr.decode();
65
+ }
66
+ catch (error) {
67
+ this.error = error;
68
+ }
69
+ // Finish here
70
+ return true;
71
+ }
72
+ // If we have received fragments before then this is an error
73
+ else {
74
+ return false;
75
+ }
76
+ }
77
+ let parsed;
78
+ try {
79
+ const decoded = part.decode();
80
+ parsed = (0, FountainDecoder_js_1.validateDecodedMultipart)(decoded);
81
+ }
82
+ catch (e) {
83
+ console.error(e);
84
+ return false;
85
+ }
86
+ // If this is the first part we've seen then set expected values
87
+ if (!this.started)
88
+ this.setExpectedValuesUr(part, parsed);
89
+ // If this is a fragment validate UR
90
+ if (!this.validateUr(part, parsed)) {
91
+ return false;
92
+ }
93
+ return super.receivePart(parsed);
94
+ }
95
+ // Assemble data and generate single result UR
96
+ finalize() {
97
+ super.finalize();
98
+ if (this.error) {
99
+ return;
100
+ }
101
+ if (this.resultRaw !== undefined) {
102
+ // Result data is already in CBOR
103
+ // Just convert it to bytewords instead of encoding it again
104
+ const payload = UR_js_1.UR.pipeline.encode(this.resultRaw, { from: EncodingMethodName_js_1.EncodingMethodName.hex });
105
+ this.resultUr = new UR_js_1.UR({
106
+ type: this.expectedType,
107
+ payload: payload,
108
+ });
109
+ // Now Try to decode the result UR
110
+ try {
111
+ this.decodedData = this.resultUr.decode();
112
+ }
113
+ catch (error) {
114
+ this.error = error;
115
+ }
116
+ }
117
+ }
118
+ getDecodedData() {
119
+ if (!this.isSuccessful()) {
120
+ console.log("Fountain decoding was not successful");
121
+ return undefined;
122
+ }
123
+ return this.decodedData;
124
+ }
125
+ }
126
+ exports.UrFountainDecoder = UrFountainDecoder;
127
+ //# sourceMappingURL=UrFountainDecoder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"UrFountainDecoder.js","sourceRoot":"","sources":["../../../src/classes/UrFountainDecoder.ts"],"names":[],"mappings":";;;AACA,0EAAoE;AACpE,6DAAmG;AACnG,mCAA6B;AAE7B,MAAa,iBAAkB,SAAQ,oCAAe;IAC7C,YAAY,CAAS;IACrB,QAAQ,CAAK;IACb,WAAW,CAAqB;IAEvC,YAAY,QAAyB,EAAE;QACrC,KAAK,EAAE,CAAC;QACR,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;YACrB,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC;QAC3B,CAAC,CAAC,CAAC;IACL,CAAC;IAEM,KAAK;QACV,KAAK,CAAC,KAA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AAS,EAAE,CAAC;YACjC,iCAAiC;YACjC,4DAA4D;YAC5D,MAAM,OAAO,GAAG,UAAE,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,EAAE,EAAE,IAAI,EAAE,0CAAkB,CAAC,GAAG,EAAE,CAAC,CAAC;YACrF,IAAI,CAAC,QAAQ,GAAG,IAAI,UAAE,CAAC;gBACrB,IAAI,EAAE,IAAI,CAAC,YAAY;gBACvB,OAAO,EAAE,OAAO;aACjB,CAAC,CAAC;YAEH,kCAAkC;YAClC,IAAI,CAAC;gBACH,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAC;YAC5C,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;YACrB,CAAC;QACH,CAAC;IACH,CAAC;IAED,cAAc;QACZ,IAAI,CAAC,IAAI,CAAC,YAAY,EAAE,EAAE,CAAC;YACzB,OAAO,CAAC,GAAG,CAAC,sCAAsC,CAAC,CAAC;YACpD,OAAO,SAAS,CAAC;QACnB,CAAC;QACD,OAAO,IAAI,CAAC,WAAW,CAAC;IAC1B,CAAC;CACF;AAxID,8CAwIC"}
@@ -0,0 +1,42 @@
1
+ import { RegistryItem } from "../classes/RegistryItem.js";
2
+ import { UR } from "./UR.js";
3
+ import { FountainEncoder } from "./FountainEncoder.js";
4
+ /**
5
+ * UrFountainEncoder encodes data on the fly using an internal state to keep generating UR fragments of the payload.
6
+ * It extends the FountainEncoder class to provide additional functionality specific to UR encoding.
7
+ */
8
+ export declare class UrFountainEncoder extends FountainEncoder {
9
+ /** The UR type of the input data */
10
+ private _type;
11
+ /** The original input data as a UR object */
12
+ private _inputUr;
13
+ /** Reset counter to 1 after hitting `fragmentCount * ratio`; if undefined or 0, it goes forever */
14
+ repeatAfterRatio: number;
15
+ /**
16
+ * Creates an instance of UrFountainEncoder.
17
+ * @param input - The input data to be encoded, either as a RegistryItem or UR.
18
+ * @param maxFragmentLength - The maximum length of each fragment.
19
+ * @param minFragmentLength - The minimum length of each fragment.
20
+ * @param firstSeqNum - The sequence number to start with.
21
+ * @param repeatAfterRatio - The ratio after which the sequence number resets. Default is 2. If 0 fountain goes forever.
22
+ */
23
+ constructor(input: RegistryItem | UR, maxFragmentLength?: number, minFragmentLength?: number, firstSeqNum?: number, repeatAfterRatio?: number);
24
+ /**
25
+ * Return all the fragments based on the fountain ratio at once as an array of Uint8Arrays.
26
+ * @param fountainRatio - The ratio of the fountain fragments to the pure fragments. Default is 0.
27
+ * @returns An array of UR objects representing the fragments.
28
+ */
29
+ getAllPartsUr(fountainRatio?: number): UR[];
30
+ /**
31
+ * Give the 'next' fragment for the ur for which the fountainEncoder was created.
32
+ * @returns The 'next' fragment, represented as a Ur multipart string.
33
+ */
34
+ nextPartUr(): UR;
35
+ /**
36
+ * Converts a fragment to a UR object.
37
+ * @param seqNum - The sequence number of the fragment.
38
+ * @param fragment - The fragment data as a Uint8Array.
39
+ * @returns A UR object representing the fragment.
40
+ */
41
+ private fragment2Ur;
42
+ }
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UrFountainEncoder = void 0;
4
+ const RegistryItem_js_1 = require("../classes/RegistryItem.js");
5
+ const UR_js_1 = require("./UR.js");
6
+ const FountainEncoder_js_1 = require("./FountainEncoder.js");
7
+ const EncodingMethodName_js_1 = require("../enums/EncodingMethodName.js");
8
+ /**
9
+ * UrFountainEncoder encodes data on the fly using an internal state to keep generating UR fragments of the payload.
10
+ * It extends the FountainEncoder class to provide additional functionality specific to UR encoding.
11
+ */
12
+ class UrFountainEncoder extends FountainEncoder_js_1.FountainEncoder {
13
+ /** The UR type of the input data */
14
+ _type;
15
+ /** The original input data as a UR object */
16
+ _inputUr;
17
+ /** Reset counter to 1 after hitting `fragmentCount * ratio`; if undefined or 0, it goes forever */
18
+ repeatAfterRatio = 2;
19
+ /**
20
+ * Creates an instance of UrFountainEncoder.
21
+ * @param input - The input data to be encoded, either as a RegistryItem or UR.
22
+ * @param maxFragmentLength - The maximum length of each fragment.
23
+ * @param minFragmentLength - The minimum length of each fragment.
24
+ * @param firstSeqNum - The sequence number to start with.
25
+ * @param repeatAfterRatio - The ratio after which the sequence number resets. Default is 2. If 0 fountain goes forever.
26
+ */
27
+ constructor(input, maxFragmentLength = 100, minFragmentLength = 10, firstSeqNum = 0, repeatAfterRatio = 2) {
28
+ let inputUr;
29
+ // If the input is a RegistryItem, convert it to a Ur
30
+ if (input instanceof RegistryItem_js_1.RegistryItemBase || (0, RegistryItem_js_1.isRegistryItem)(input)) {
31
+ inputUr = UR_js_1.UR.fromRegistryItem(input);
32
+ }
33
+ else {
34
+ inputUr = input;
35
+ }
36
+ // Input in CBOR
37
+ let inputCBOR = inputUr.getPayloadCbor();
38
+ super(inputCBOR, maxFragmentLength, minFragmentLength, firstSeqNum);
39
+ this._inputUr = inputUr;
40
+ this._type = inputUr.type;
41
+ this.repeatAfterRatio = repeatAfterRatio;
42
+ }
43
+ /**
44
+ * Return all the fragments based on the fountain ratio at once as an array of Uint8Arrays.
45
+ * @param fountainRatio - The ratio of the fountain fragments to the pure fragments. Default is 0.
46
+ * @returns An array of UR objects representing the fragments.
47
+ */
48
+ getAllPartsUr(fountainRatio = 0) {
49
+ const allParts = super.getAllParts(fountainRatio);
50
+ return allParts.map((part, index) => this.fragment2Ur(index + 1, part));
51
+ }
52
+ /**
53
+ * Give the 'next' fragment for the ur for which the fountainEncoder was created.
54
+ * @returns The 'next' fragment, represented as a Ur multipart string.
55
+ */
56
+ nextPartUr() {
57
+ // Reset the sequence number if we hit the ratio
58
+ if (this.repeatAfterRatio) {
59
+ if (this._seqNum > this._pureFragments.length * this.repeatAfterRatio) {
60
+ this._seqNum = 0;
61
+ }
62
+ }
63
+ const encodedFragment = super.nextPart();
64
+ return this.fragment2Ur(this._seqNum, encodedFragment);
65
+ }
66
+ /**
67
+ * Converts a fragment to a UR object.
68
+ * @param seqNum - The sequence number of the fragment.
69
+ * @param fragment - The fragment data as a Uint8Array.
70
+ * @returns A UR object representing the fragment.
71
+ */
72
+ fragment2Ur(seqNum, fragment) {
73
+ // Fragment is already encoded in CBOR
74
+ // Just convert it to bytewords instead of encoding it again
75
+ const payload = UR_js_1.UR.pipeline.encode(fragment, { from: EncodingMethodName_js_1.EncodingMethodName.hex });
76
+ if (this.isSinglePart()) {
77
+ return new UR_js_1.UR({
78
+ type: this._type,
79
+ payload: payload,
80
+ });
81
+ }
82
+ return new UR_js_1.UR({
83
+ type: this._type,
84
+ payload: payload,
85
+ seqNum: seqNum,
86
+ seqLength: this._pureFragments.length,
87
+ isFragment: true,
88
+ });
89
+ }
90
+ }
91
+ exports.UrFountainEncoder = UrFountainEncoder;
92
+ //# sourceMappingURL=UrFountainEncoder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"UrFountainEncoder.js","sourceRoot":"","sources":["../../../src/classes/UrFountainEncoder.ts"],"names":[],"mappings":";;;AAAA,gEAA4F;AAC5F,mCAA6B;AAC7B,6DAAuD;AACvD,0EAAoE;AAEpE;;;GAGG;AACH,MAAa,iBAAkB,SAAQ,oCAAe;IACpD,oCAAoC;IAC5B,KAAK,CAAS;IACtB,6CAA6C;IACrC,QAAQ,CAAK;IACrB,mGAAmG;IAC5F,gBAAgB,GAAG,CAAC,CAAC;IAE5B;;;;;;;OAOG;IACH,YACE,KAAwB,EACxB,oBAA4B,GAAG,EAC/B,oBAA4B,EAAE,EAC9B,cAAsB,CAAC,EACvB,gBAAgB,GAAG,CAAC;QAGpB,IAAI,OAAO,CAAC;QACZ,qDAAqD;QACrD,IAAI,KAAK,YAAY,kCAAgB,IAAI,IAAA,gCAAc,EAAC,KAAK,CAAC,EAAE,CAAC;YAC/D,OAAO,GAAG,UAAE,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAC;QACvC,CAAC;aACI,CAAC;YACJ,OAAO,GAAG,KAAK,CAAC;QAClB,CAAC;QAED,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,CAAC;YACxB,OAAO,IAAI,UAAE,CAAC;gBACZ,IAAI,EAAE,IAAI,CAAC,KAAK;gBAChB,OAAO,EAAE,OAAO;aACjB,CAAC,CAAC;QACL,CAAC;QACD,OAAO,IAAI,UAAE,CAAC;YACZ,IAAI,EAAE,IAAI,CAAC,KAAK;YAChB,OAAO,EAAE,OAAO;YAChB,MAAM,EAAE,MAAM;YACd,SAAS,EAAE,IAAI,CAAC,cAAc,CAAC,MAAM;YACrC,UAAU,EAAE,IAAI;SACjB,CAAC,CAAA;IACJ,CAAC;CACF;AA7FD,8CA6FC"}
@@ -0,0 +1,27 @@
1
+ /**
2
+ * This file contains helper functions for encoding and decoding keys
3
+ */
4
+ /**
5
+ * Interface for the key to integer mapping
6
+ */
7
+ export type IKeyMap = Record<string, string | number>;
8
+ /**
9
+ * For some CDDL types we use integer as keys to keep the CBOR encoded data small
10
+ * This function converts the data keys to their respective integer values
11
+ *
12
+ * It needs to return MAP because cbor library only then encodes the keys as integers
13
+ *
14
+ * @param data Object that has key value pairs
15
+ * @param keyMap Map of the keys to integers values
16
+ * @returns Map with integer keys
17
+ */
18
+ export declare function encodeKeys(data: object, keyMap: IKeyMap, allowKeysNotInMap: boolean): Map<string | number, any>;
19
+ /**
20
+ * For some CDDL types we use integer as keys to keep the CBOR encoded data small
21
+ * This function converts the data keys back to their respective string values
22
+ *
23
+ * @param _data Object that has encoded key - value pairs
24
+ * @param keyMap Map of the keys to integer values
25
+ * @returns Object with string keys
26
+ */
27
+ export declare function decodeKeys(data: Map<string | number, any> | Record<string | number, any>, keyMap: IKeyMap, allowKeysNotInMap: boolean): object;
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ /**
3
+ * This file contains helper functions for encoding and decoding keys
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.encodeKeys = encodeKeys;
7
+ exports.decodeKeys = decodeKeys;
8
+ /**
9
+ * For some CDDL types we use integer as keys to keep the CBOR encoded data small
10
+ * This function converts the data keys to their respective integer values
11
+ *
12
+ * It needs to return MAP because cbor library only then encodes the keys as integers
13
+ *
14
+ * @param data Object that has key value pairs
15
+ * @param keyMap Map of the keys to integers values
16
+ * @returns Map with integer keys
17
+ */
18
+ function encodeKeys(data, keyMap, allowKeysNotInMap) {
19
+ const map = new Map();
20
+ // If we have a mapping, use it to map the data
21
+ // Check if our data is an object
22
+ if (typeof data !== "object")
23
+ return undefined;
24
+ // Create a set from the keys of the data
25
+ const keys = new Set(Object.keys(data));
26
+ // Add the keys in the correct order to the map
27
+ for (const key in keyMap) {
28
+ // Check if the key exists in the data
29
+ if (data[key] !== undefined)
30
+ map.set(keyMap[key], data[key]);
31
+ keys.delete(key);
32
+ }
33
+ if (allowKeysNotInMap) {
34
+ // Add other keys as string if they are not existent in the map
35
+ keys.forEach((key) => {
36
+ map.set(key, data[key]);
37
+ });
38
+ }
39
+ return map;
40
+ }
41
+ /**
42
+ * For some CDDL types we use integer as keys to keep the CBOR encoded data small
43
+ * This function converts the data keys back to their respective string values
44
+ *
45
+ * @param _data Object that has encoded key - value pairs
46
+ * @param keyMap Map of the keys to integer values
47
+ * @returns Object with string keys
48
+ */
49
+ function decodeKeys(data, keyMap, allowKeysNotInMap) {
50
+ // If we have a mapping, use it to map the data
51
+ const result = {};
52
+ // Convert the data to a map if it is not already
53
+ const _data = data instanceof Map ? data : new Map(Object.entries(data));
54
+ // Get all the keys in the data
55
+ const keys = new Set(_data.keys());
56
+ // Add the keys in the correct order
57
+ for (const key in keyMap) {
58
+ if (_data.has(keyMap[key]))
59
+ result[key] = _data.get(keyMap[key]);
60
+ keys.delete(keyMap[key]);
61
+ }
62
+ if (allowKeysNotInMap) {
63
+ // Add other keys as string if they are not existent in the map
64
+ keys.forEach((key) => {
65
+ result[key] = _data.get(key);
66
+ });
67
+ }
68
+ return result;
69
+ }
70
+ //# sourceMappingURL=key.helper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"key.helper.js","sourceRoot":"","sources":["../../../src/classes/key.helper.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/B,MAAM,IAAI,GAAG,IAAI,GAAG,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC;IAEnC,oCAAoC;IACpC,KAAK,MAAM,GAAG,IAAI,MAAM,EAAE,CAAC;QACzB,IAAI,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;YAAE,MAAM,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;QACjE,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;IAC3B,CAAC;IAED,IAAI,iBAAiB,EAAE,CAAC;QACtB,+DAA+D;QAC/D,IAAI,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;YACnB,MAAM,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAC/B,CAAC,CAAC,CAAC;IACL,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC"}
@@ -0,0 +1,11 @@
1
+ import { STYLES } from "./bytewords.js";
2
+ import { EncodingMethodName } from "../enums/EncodingMethodName.js";
3
+ import { IEncodingMethod } from "../interfaces/IEncodingMethod.js";
4
+ export declare class BytewordEncoding implements IEncodingMethod<string, string> {
5
+ private _name;
6
+ readonly _style: STYLES;
7
+ get name(): EncodingMethodName;
8
+ constructor(style?: STYLES);
9
+ encode(payload: string): string;
10
+ decode(payload: string): string;
11
+ }