@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,168 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FountainEncoder = void 0;
|
|
4
|
+
const fountainUtils_js_1 = require("../helpers/fountainUtils.js");
|
|
5
|
+
const utils_js_1 = require("../helpers/utils.js");
|
|
6
|
+
const CborEncoding_js_1 = require("../encodingMethods/CborEncoding.js");
|
|
7
|
+
const cborEncoder = new CborEncoding_js_1.CborEncoding();
|
|
8
|
+
/**
|
|
9
|
+
* Encode data on the fly. This encoder uses an internal state to keep generating ur fragments of the payload.
|
|
10
|
+
*/
|
|
11
|
+
class FountainEncoder {
|
|
12
|
+
fragmentLenghtFinder = fountainUtils_js_1.findNominalFragmentLength;
|
|
13
|
+
/** Total data size of the input message */
|
|
14
|
+
_messageLength;
|
|
15
|
+
/** Maximum data size in the fragment */
|
|
16
|
+
_maxFragmentLength;
|
|
17
|
+
/** Minumum data size in the fragment */
|
|
18
|
+
_minFragmentLength;
|
|
19
|
+
/** Calculated data size in the fragment */
|
|
20
|
+
_nominalFragmentLength;
|
|
21
|
+
/** Array of pure fragments (without any fountain encoded) */
|
|
22
|
+
_pureFragments;
|
|
23
|
+
/** Current index of the fragment start from 1 */
|
|
24
|
+
_seqNum;
|
|
25
|
+
/** Checksum of the original data */
|
|
26
|
+
_checksum;
|
|
27
|
+
/** Original Input data as UR */
|
|
28
|
+
_input;
|
|
29
|
+
constructor(input, maxFragmentLength = 100, minFragmentLength = 10, firstSeqNum = 0) {
|
|
30
|
+
// Validate the input
|
|
31
|
+
if (!input) {
|
|
32
|
+
throw new Error("input should be defined");
|
|
33
|
+
}
|
|
34
|
+
if (!(input instanceof Uint8Array)) {
|
|
35
|
+
throw new Error("input should be Uint8Array");
|
|
36
|
+
}
|
|
37
|
+
if (typeof maxFragmentLength !== "number") {
|
|
38
|
+
throw new Error("maxFragmentLength should be a number");
|
|
39
|
+
}
|
|
40
|
+
if (typeof minFragmentLength !== "number") {
|
|
41
|
+
throw new Error("minFragmentLength should be a number");
|
|
42
|
+
}
|
|
43
|
+
if (typeof firstSeqNum !== "number") {
|
|
44
|
+
throw new Error("firstSeqNum should be a number");
|
|
45
|
+
}
|
|
46
|
+
if (maxFragmentLength < 1) {
|
|
47
|
+
throw new Error("maxFragmentLength should be > 0");
|
|
48
|
+
}
|
|
49
|
+
if (maxFragmentLength < minFragmentLength) {
|
|
50
|
+
throw new Error("maxFragmentLength should be >= minFragmentLength");
|
|
51
|
+
}
|
|
52
|
+
this._input = input;
|
|
53
|
+
this._maxFragmentLength = maxFragmentLength;
|
|
54
|
+
this._minFragmentLength = minFragmentLength;
|
|
55
|
+
this._seqNum = (0, utils_js_1.toUint32)(firstSeqNum);
|
|
56
|
+
// Get the length of the message
|
|
57
|
+
this._messageLength = input.length;
|
|
58
|
+
// Check if message is less than maxFragmentLength, then return the message as a single fragment
|
|
59
|
+
if (input.length <= maxFragmentLength) {
|
|
60
|
+
this._pureFragments = [input];
|
|
61
|
+
this._nominalFragmentLength = this._messageLength;
|
|
62
|
+
}
|
|
63
|
+
// Otherwise calciulate the nominal fragment length and neseccary fragments
|
|
64
|
+
else {
|
|
65
|
+
// Check for the nominal length of a fragment.
|
|
66
|
+
const fragmentLength = this.fragmentLenghtFinder(input.length, maxFragmentLength, minFragmentLength);
|
|
67
|
+
this._nominalFragmentLength = fragmentLength;
|
|
68
|
+
// Calculate the checksum of the message
|
|
69
|
+
this._checksum = (0, utils_js_1.getCRC)(input);
|
|
70
|
+
// Split up the message buffer in an array of buffers, by the nominal length
|
|
71
|
+
this._pureFragments = (0, fountainUtils_js_1.partitionMessage)(input, fragmentLength);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
setFragmentLengthFinder(fn) {
|
|
75
|
+
this.fragmentLenghtFinder = fn;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Return all the fragments based on the fountain ratio at once as an array of Uint8Arrays.
|
|
79
|
+
* @param fountainRatio The ratio of the fountain fragments to the pure fragments. Default is 0.
|
|
80
|
+
* @returns
|
|
81
|
+
*/
|
|
82
|
+
getAllParts(fountainRatio = 0) {
|
|
83
|
+
// First check if the input is a single fragment
|
|
84
|
+
if (this.isSinglePart()) {
|
|
85
|
+
return [this._input];
|
|
86
|
+
}
|
|
87
|
+
// Ceil to always get an integer
|
|
88
|
+
const numberofParts = Math.ceil(this.getPureFragmentCount() * (1 + fountainRatio));
|
|
89
|
+
// Save state
|
|
90
|
+
const oldSeqNum = this._seqNum;
|
|
91
|
+
// Reset state to start generating fragments from the beginning
|
|
92
|
+
this._seqNum = 0;
|
|
93
|
+
// Generate fragments
|
|
94
|
+
let fragments = [];
|
|
95
|
+
for (let i = 0; i < numberofParts; i++) {
|
|
96
|
+
fragments.push(this.nextPart());
|
|
97
|
+
}
|
|
98
|
+
// Bring state back
|
|
99
|
+
this._seqNum = oldSeqNum;
|
|
100
|
+
return fragments;
|
|
101
|
+
}
|
|
102
|
+
// Encode fragment with correct CBOR structure
|
|
103
|
+
encodeFragment(seqNum, fragment) {
|
|
104
|
+
// Shape for the CBOR payload
|
|
105
|
+
const payload = [seqNum, this._pureFragments.length, this._messageLength, this._checksum, fragment];
|
|
106
|
+
const encodedFragment = cborEncoder.encode(payload);
|
|
107
|
+
return encodedFragment;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Reset the state of the encoder to start generating fragments from the beginning.
|
|
111
|
+
*/
|
|
112
|
+
reset() {
|
|
113
|
+
this._seqNum = 0;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Checks if all the pure fragments (full payload data) for this ur is generated.
|
|
117
|
+
* @returns boolean indicating if generated fragments have included all the data.
|
|
118
|
+
*/
|
|
119
|
+
isComplete() {
|
|
120
|
+
return this._seqNum >= this.getPureFragmentCount();
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Checks if there is only one fragment generated for the ur.
|
|
124
|
+
* @returns boolean if the ur payload is contained in one fragment.
|
|
125
|
+
*/
|
|
126
|
+
isSinglePart() {
|
|
127
|
+
return this.getPureFragmentCount() === 1;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Gets the count of the "pure" fragments. These are fragments where the data is not mixed.
|
|
131
|
+
* @returns The count of the "pure" fragments.
|
|
132
|
+
*/
|
|
133
|
+
getPureFragmentCount() {
|
|
134
|
+
return this._pureFragments.length;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Get the pure fragments of the ur.
|
|
138
|
+
* @returns the pure fragments of the ur as an array of Uint8Arrays.
|
|
139
|
+
*/
|
|
140
|
+
getPureFragments() {
|
|
141
|
+
return this._pureFragments;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Give the 'next' fragment for the ur for which the fountainEncoder was created.
|
|
145
|
+
* @returns the 'next' fragment, represented as a Ur multipart string.
|
|
146
|
+
*/
|
|
147
|
+
nextPart() {
|
|
148
|
+
this._seqNum = (0, utils_js_1.toUint32)(this._seqNum + 1);
|
|
149
|
+
// when the seqnum restarts because of a number bigger than Uint32, we need to make sure to skip 0 to prevent invalid Multipart URs.
|
|
150
|
+
if (this._seqNum === 0) {
|
|
151
|
+
this._seqNum = (0, utils_js_1.toUint32)(this._seqNum + 1);
|
|
152
|
+
}
|
|
153
|
+
// If its single part, return the original input
|
|
154
|
+
if (this.isSinglePart()) {
|
|
155
|
+
return this._input;
|
|
156
|
+
}
|
|
157
|
+
// For the first seqNum of the pure fragments, return the pure fragment
|
|
158
|
+
if (!this.isComplete()) {
|
|
159
|
+
return this.encodeFragment(this._seqNum, this._pureFragments[this._seqNum - 1]);
|
|
160
|
+
}
|
|
161
|
+
// For the fountain fragments, mix the fragments
|
|
162
|
+
const indexes = (0, fountainUtils_js_1.chooseFragments)(this._seqNum, this._pureFragments.length, this._checksum);
|
|
163
|
+
const mixed = (0, fountainUtils_js_1.mixFragments)(indexes, this._pureFragments, this._nominalFragmentLength);
|
|
164
|
+
return this.encodeFragment(this._seqNum, mixed);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
exports.FountainEncoder = FountainEncoder;
|
|
168
|
+
//# sourceMappingURL=FountainEncoder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FountainEncoder.js","sourceRoot":"","sources":["../../../src/classes/FountainEncoder.ts"],"names":[],"mappings":";;;AAAA,kEAAyH;AACzH,kDAAuD;AACvD,wEAAkE;AAElE,MAAM,WAAW,GAAG,IAAI,8BAAY,EAAE,CAAC;AAKvC;;GAEG;AACH,MAAa,eAAe;IACnB,oBAAoB,GAAG,4CAAyB,CAAC;IAExD,2CAA2C;IACpC,cAAc,CAAS;IAC9B,wCAAwC;IAC9B,kBAAkB,CAAS;IACrC,wCAAwC;IAC9B,kBAAkB,CAAS;IACrC,2CAA2C;IACpC,sBAAsB,CAAS;IACtC,6DAA6D;IACtD,cAAc,CAAe;IACpC,iDAAiD;IACvC,OAAO,CAAS;IAC1B,oCAAoC;IAC1B,SAAS,CAAS;IAC5B,gCAAgC;IACtB,MAAM,CAAa;IAE7B,YACE,KAAiB,EACjB,oBAA4B,GAAG,EAC/B,oBAA4B,EAAE,EAC9B,cAAsB,CAAC;QAIvB,qBAAqB;QACrB,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;QAC7C,CAAC;QAED,IAAG,CAAC,CAAC,KAAK,YAAY,UAAU,CAAC,EAAE,CAAC;YAClC,MAAM,IAAI,KAAK,CAAC,4BAA4B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IAAI,CAAC,OAAO,EACZ,IAAI,CAAC,cAAc,CAAC,MAAM,EAC1B,IAAI,CAAC,SAAS,CACf,CAAC;QACF,MAAM,KAAK,GAAG,IAAA,+BAAY,EACxB,OAAO,EACP,IAAI,CAAC,cAAc,EACnB,IAAI,CAAC,sBAAsB,CAC5B,CAAC;QAEF,OAAO,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;IAClD,CAAC;CACF;AAtMD,0CAsMC"}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { UR } from "./UR.js";
|
|
2
|
+
import { encodeKeys, decodeKeys, IKeyMap } from "./key.helper.js";
|
|
3
|
+
export declare const registryItemSymbol: unique symbol;
|
|
4
|
+
/**
|
|
5
|
+
* Static interface that RegistryItem classes should implement
|
|
6
|
+
*/
|
|
7
|
+
export interface IRegistryType {
|
|
8
|
+
/**
|
|
9
|
+
* Cbor Tags
|
|
10
|
+
* Finalized versions defined in: https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml
|
|
11
|
+
*/
|
|
12
|
+
tag: number;
|
|
13
|
+
/**
|
|
14
|
+
* Uniform Resource ID
|
|
15
|
+
*
|
|
16
|
+
* Links:
|
|
17
|
+
* - https://developer.blockchaincommons.com/ur/
|
|
18
|
+
* - https://github.com/BlockchainCommons/Research/blob/master/papers/bcr-2020-006-urtypes.md
|
|
19
|
+
* - https://github.com/ngraveio/Research
|
|
20
|
+
* - https://github.com/KeystoneHQ/Keystone-developer-hub/tree/main/research
|
|
21
|
+
*
|
|
22
|
+
*/
|
|
23
|
+
URType: string;
|
|
24
|
+
/** CDDL definition for CBOR encoding */
|
|
25
|
+
CDDL: string;
|
|
26
|
+
/** Key name to key in integer map for smaller encoded data size */
|
|
27
|
+
keyMap?: IKeyMap;
|
|
28
|
+
/** allow the keys that are not explicitely defined in the keyMap */
|
|
29
|
+
allowKeysNotInMap?: boolean;
|
|
30
|
+
}
|
|
31
|
+
export declare abstract class RegistryItemBase {
|
|
32
|
+
readonly type: IRegistryType;
|
|
33
|
+
/** If CDDL contains keys as numbers, map them to their respective values */
|
|
34
|
+
keyMap: IKeyMap;
|
|
35
|
+
/** Data that our item contains */
|
|
36
|
+
data: any;
|
|
37
|
+
static allowKeysNotInMap: any;
|
|
38
|
+
constructor(registryType: IRegistryType, data?: any, keyMap?: IKeyMap);
|
|
39
|
+
/**
|
|
40
|
+
* Verify the input data
|
|
41
|
+
*
|
|
42
|
+
* @param input
|
|
43
|
+
*/
|
|
44
|
+
verifyInput(input: any): {
|
|
45
|
+
valid: boolean;
|
|
46
|
+
reasons?: Error[];
|
|
47
|
+
};
|
|
48
|
+
toString(): string;
|
|
49
|
+
toJSON(): any;
|
|
50
|
+
/**
|
|
51
|
+
* Preprocess the data before encoding into CBOR Tagged instance
|
|
52
|
+
*
|
|
53
|
+
* @param data, data before keymap conversion, if left empty, it will use the this.data property
|
|
54
|
+
*/
|
|
55
|
+
preCBOR(data?: any): any;
|
|
56
|
+
/**
|
|
57
|
+
* Called by the CBOR encoder for encoding the data
|
|
58
|
+
*
|
|
59
|
+
* [CBOR Docs](https://github.com/hildjj/cbor2?tab=readme-ov-file#tocbor-method)
|
|
60
|
+
*
|
|
61
|
+
* This is the easiest approach, if you can modify the class being encoded.
|
|
62
|
+
* Add a toCBOR() method to your class, which should return a two-element array containing the tag number and data item that encodes your class.
|
|
63
|
+
* If the tag number is NaN, no tag will be written. If you return undefined, nothing will be written.
|
|
64
|
+
* In this case you will likely write custom bytes to the Writer instance that is passed in,
|
|
65
|
+
* perhaps using the encoding options.
|
|
66
|
+
*
|
|
67
|
+
* @param encoder
|
|
68
|
+
* @returns
|
|
69
|
+
*/
|
|
70
|
+
toCBOR(_writer: any, _options: any): any[];
|
|
71
|
+
toUr(): UR;
|
|
72
|
+
toHex(): string;
|
|
73
|
+
toBytes(): Uint8Array<ArrayBufferLike>;
|
|
74
|
+
encodeKeys: typeof encodeKeys;
|
|
75
|
+
decodeKeys: typeof decodeKeys;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Factory function to create a new RegistryItem class
|
|
79
|
+
*
|
|
80
|
+
* It injects static properties to the class and does preprocessing when needed
|
|
81
|
+
*
|
|
82
|
+
* @param input
|
|
83
|
+
* @returns
|
|
84
|
+
*/
|
|
85
|
+
export declare function registryItemFactory<T extends RegistryItemBase>(input: IRegistryType): RegistryItemClass<T>;
|
|
86
|
+
export type RegistryItemClass<T extends RegistryItemBase = RegistryItemBase> = {
|
|
87
|
+
new (...args: any[]): T;
|
|
88
|
+
tag: number;
|
|
89
|
+
URType: string;
|
|
90
|
+
CDDL: string;
|
|
91
|
+
keyMap?: IKeyMap;
|
|
92
|
+
allowKeysNotInMap: boolean;
|
|
93
|
+
postCBOR(val: any, allowKeysNotInMapOverwrite?: boolean): any;
|
|
94
|
+
fromCBORData(val: any, allowKeysNotInMap?: boolean, tagged?: any): T;
|
|
95
|
+
fromUr(ur: UR | string): T;
|
|
96
|
+
fromHex(hex: string): T;
|
|
97
|
+
};
|
|
98
|
+
export type RegistryItem = InstanceType<RegistryItemClass>;
|
|
99
|
+
/**
|
|
100
|
+
* Function to check if an object is an instance of RegistryItemBase or its subclasses
|
|
101
|
+
* @param obj - The object to check
|
|
102
|
+
* @returns true if the object is an instance of RegistryItemBase or its subclasses
|
|
103
|
+
*/
|
|
104
|
+
export declare function isRegistryItem(obj: any): obj is RegistryItem;
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RegistryItemBase = exports.registryItemSymbol = void 0;
|
|
4
|
+
exports.registryItemFactory = registryItemFactory;
|
|
5
|
+
exports.isRegistryItem = isRegistryItem;
|
|
6
|
+
const UR_js_1 = require("./UR.js");
|
|
7
|
+
const key_helper_js_1 = require("./key.helper.js");
|
|
8
|
+
// Define the symbol
|
|
9
|
+
exports.registryItemSymbol = Symbol.for("RegistryItemBase");
|
|
10
|
+
class RegistryItemBase {
|
|
11
|
+
type;
|
|
12
|
+
/** If CDDL contains keys as numbers, map them to their respective values */
|
|
13
|
+
keyMap;
|
|
14
|
+
/** Data that our item contains */
|
|
15
|
+
// TODO: should we force this to be a map? It is much safer that way for injection attacks
|
|
16
|
+
data;
|
|
17
|
+
static allowKeysNotInMap;
|
|
18
|
+
constructor(registryType, data, keyMap) {
|
|
19
|
+
this.type = registryType;
|
|
20
|
+
this.keyMap = keyMap;
|
|
21
|
+
// Verify input
|
|
22
|
+
const { valid, reasons } = this.verifyInput(data);
|
|
23
|
+
if (!valid) {
|
|
24
|
+
throw new Error(`Invalid input: ${reasons?.map((r) => r.message).join(", ")}`);
|
|
25
|
+
}
|
|
26
|
+
this.data = data;
|
|
27
|
+
this[exports.registryItemSymbol] = true;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Verify the input data
|
|
31
|
+
*
|
|
32
|
+
* @param input
|
|
33
|
+
*/
|
|
34
|
+
verifyInput(input) {
|
|
35
|
+
// This should be implemented by the child class
|
|
36
|
+
return {
|
|
37
|
+
valid: true,
|
|
38
|
+
reasons: undefined,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
toString() {
|
|
42
|
+
return `${this.type.URType}[${this.type.tag}](${JSON.stringify(this.data)})`;
|
|
43
|
+
}
|
|
44
|
+
toJSON() {
|
|
45
|
+
// TODO: if there is any registry item in the data (could be nested or in array), we should call toJSON on them as well
|
|
46
|
+
return {
|
|
47
|
+
type: this.type.URType,
|
|
48
|
+
tag: this.type.tag,
|
|
49
|
+
...this.data,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Preprocess the data before encoding into CBOR Tagged instance
|
|
54
|
+
*
|
|
55
|
+
* @param data, data before keymap conversion, if left empty, it will use the this.data property
|
|
56
|
+
*/
|
|
57
|
+
preCBOR(data = this.data) {
|
|
58
|
+
// If key-map exists, convert keys to integers
|
|
59
|
+
if (this.keyMap) {
|
|
60
|
+
const allowKeysNotInMap = this.constructor.allowKeysNotInMap;
|
|
61
|
+
return (0, key_helper_js_1.encodeKeys)(data, this.keyMap, allowKeysNotInMap);
|
|
62
|
+
}
|
|
63
|
+
return data;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Called by the CBOR encoder for encoding the data
|
|
67
|
+
*
|
|
68
|
+
* [CBOR Docs](https://github.com/hildjj/cbor2?tab=readme-ov-file#tocbor-method)
|
|
69
|
+
*
|
|
70
|
+
* This is the easiest approach, if you can modify the class being encoded.
|
|
71
|
+
* Add a toCBOR() method to your class, which should return a two-element array containing the tag number and data item that encodes your class.
|
|
72
|
+
* If the tag number is NaN, no tag will be written. If you return undefined, nothing will be written.
|
|
73
|
+
* In this case you will likely write custom bytes to the Writer instance that is passed in,
|
|
74
|
+
* perhaps using the encoding options.
|
|
75
|
+
*
|
|
76
|
+
* @param encoder
|
|
77
|
+
* @returns
|
|
78
|
+
*/
|
|
79
|
+
toCBOR(_writer, _options) {
|
|
80
|
+
const processed = this.preCBOR(this.data);
|
|
81
|
+
let tag = this.type.tag;
|
|
82
|
+
// TODO: find a better way to ignore top level tag on encoder
|
|
83
|
+
if (_options?.ignoreTopLevelTag) {
|
|
84
|
+
tag = NaN; // Do not tag the top level item
|
|
85
|
+
// Set it back to false for child items
|
|
86
|
+
_options.ignoreTopLevelTag = false;
|
|
87
|
+
}
|
|
88
|
+
return [tag, processed];
|
|
89
|
+
}
|
|
90
|
+
toUr() {
|
|
91
|
+
return new UR_js_1.UR(this);
|
|
92
|
+
}
|
|
93
|
+
toHex() {
|
|
94
|
+
return this.toUr().getPayloadHex();
|
|
95
|
+
}
|
|
96
|
+
toBytes() {
|
|
97
|
+
return this.toUr().getPayloadCbor();
|
|
98
|
+
}
|
|
99
|
+
encodeKeys = key_helper_js_1.encodeKeys;
|
|
100
|
+
decodeKeys = key_helper_js_1.decodeKeys;
|
|
101
|
+
// Custom inspection method for Node.js
|
|
102
|
+
[Symbol.for("nodejs.util.inspect.custom")](_depth, inspectOptions, inspect) {
|
|
103
|
+
return `${this.type.URType}[${this.type.tag}](${inspect(this.data, inspectOptions)})`;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
exports.RegistryItemBase = RegistryItemBase;
|
|
107
|
+
/**
|
|
108
|
+
* Factory function to create a new RegistryItem class
|
|
109
|
+
*
|
|
110
|
+
* It injects static properties to the class and does preprocessing when needed
|
|
111
|
+
*
|
|
112
|
+
* @param input
|
|
113
|
+
* @returns
|
|
114
|
+
*/
|
|
115
|
+
function registryItemFactory(input) {
|
|
116
|
+
const { tag, URType, CDDL, keyMap, allowKeysNotInMap = true } = input;
|
|
117
|
+
const _keyMap = keyMap;
|
|
118
|
+
return class extends RegistryItemBase {
|
|
119
|
+
// Add static properties to the class
|
|
120
|
+
static tag = tag;
|
|
121
|
+
static URType = URType;
|
|
122
|
+
static CDDL = CDDL;
|
|
123
|
+
static keyMap = _keyMap;
|
|
124
|
+
static allowKeysNotInMap = allowKeysNotInMap;
|
|
125
|
+
// Initiate base class with the values
|
|
126
|
+
constructor(data, keyMap = _keyMap) {
|
|
127
|
+
super(input, data, keyMap);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Post process the data after decoding CBOR
|
|
131
|
+
*/
|
|
132
|
+
static postCBOR(val, allowKeysNotInMapOverwrite) {
|
|
133
|
+
// If key-map exists, convert integer keys back to string keys
|
|
134
|
+
if (keyMap) {
|
|
135
|
+
return (0, key_helper_js_1.decodeKeys)(val, keyMap, allowKeysNotInMapOverwrite ?? allowKeysNotInMap);
|
|
136
|
+
}
|
|
137
|
+
return val;
|
|
138
|
+
}
|
|
139
|
+
static fromUr(ur) {
|
|
140
|
+
const urObj = typeof ur === "string" ? UR_js_1.UR.fromString(ur) : ur;
|
|
141
|
+
const decoded = urObj.decode();
|
|
142
|
+
return decoded;
|
|
143
|
+
}
|
|
144
|
+
static fromHex(hex) {
|
|
145
|
+
const ur = UR_js_1.UR.fromHex({ type: URType, payload: hex });
|
|
146
|
+
const decoded = ur.decode();
|
|
147
|
+
return decoded;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Static method to create an instance from CBOR DataItem data.
|
|
151
|
+
* It processes the raw CBOR data if needed and returns a new instance of the class.
|
|
152
|
+
*/
|
|
153
|
+
static fromCBORData(val, allowKeysNotInMap, tagged) {
|
|
154
|
+
// Do some post processing data coming from the cbor decoder
|
|
155
|
+
const data = this.postCBOR(val, allowKeysNotInMap);
|
|
156
|
+
// Return an instance of the generated class
|
|
157
|
+
return new this(data);
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Function to check if an object is an instance of RegistryItemBase or its subclasses
|
|
163
|
+
* @param obj - The object to check
|
|
164
|
+
* @returns true if the object is an instance of RegistryItemBase or its subclasses
|
|
165
|
+
*/
|
|
166
|
+
function isRegistryItem(obj) {
|
|
167
|
+
return (obj instanceof RegistryItemBase ||
|
|
168
|
+
(obj && obj[exports.registryItemSymbol] === true) ||
|
|
169
|
+
//(obj && typeof obj.toCBOR === 'function' && typeof obj.toUr === 'function' && typeof obj.toHex === 'function' && typeof obj.toBytes === 'function' && obj.type !== undefined && obj.data !== undefined)
|
|
170
|
+
(obj && typeof obj.toCBOR === 'function' && typeof obj.toUr === 'function' && typeof obj.toHex === 'function' && obj.type !== undefined && obj.data !== undefined));
|
|
171
|
+
}
|
|
172
|
+
//# sourceMappingURL=RegistryItem.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RegistryItem.js","sourceRoot":"","sources":["../../../src/classes/RegistryItem.ts"],"names":[],"mappings":";;;AA2JA,kDAuDC;AAsBD,wCAOC;AA/OD,mCAA6B;AAC7B,mDAAkE;AAElE,oBAAoB;AACP,QAAA,kBAAkB,GAAG,MAAM,CAAC,GAAG,CAAC,kBAAkB,CAAC,CAAC;AA8BjE,MAAsB,gBAAgB;IAC3B,IAAI,CAAgB;IAC7B,4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,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,EAAE,CAAC,CAAC;YACtD,MAAM,OAAO,GAAG,EAAE,CAAC,MAAM,EAAa,CAAC;YAEvC,OAAO,OAAY,CAAC;QACtB,CAAC;QAED;;;WAGG;QACH,MAAM,CAAC,YAAY,CAAC,GAAQ,EAAE,iBAA2B,EAAE,MAAY;YACrE,4DAA4D;YAC5D,MAAM,IAAI,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,EAAE,iBAAiB,CAAC,CAAC;YAEnD,4CAA4C;YAC5C,OAAO,IAAI,IAAI,CAAC,IAAI,CAAC,CAAC;QACxB,CAAC;KAEsB,CAAC;AAC5B,CAAC;AAiBD;;;;GAIG;AACH,SAAgB,cAAc,CAAC,GAAQ;IACrC,OAAO,CACL,GAAG,YAAY,gBAAgB;QAC/B,CAAC,GAAG,IAAI,GAAG,CAAC,0BAAkB,CAAC,KAAK,IAAI,CAAC;QACzC,yMAAyM;QACzM,CAAC,GAAG,IAAI,OAAO,GAAG,CAAC,MAAM,KAAK,UAAU,IAAI,OAAO,GAAG,CAAC,IAAI,KAAK,UAAU,IAAI,OAAO,GAAG,CAAC,KAAK,KAAK,UAAU,IAAI,GAAG,CAAC,IAAI,KAAK,SAAS,IAAI,GAAG,CAAC,IAAI,KAAK,SAAS,CAAC,CACnK,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { RegistryItem, RegistryItemBase } from "./RegistryItem.js";
|
|
2
|
+
import { EncodingMethodName } from "../enums/EncodingMethodName.js";
|
|
3
|
+
import { EncodingPipeline } from "../encodingMethods/pipeline.js";
|
|
4
|
+
import { ReplaceKeyType } from "../helpers/type.helper.js";
|
|
5
|
+
export interface IUR {
|
|
6
|
+
type: string;
|
|
7
|
+
payload: string;
|
|
8
|
+
seqNum?: number;
|
|
9
|
+
seqLength?: number;
|
|
10
|
+
isFragment?: boolean;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* https://github.com/BlockchainCommons/Research/blob/master/papers/bcr-2020-005-ur.md
|
|
14
|
+
* Class that represents the structure of the data we encode/decode in this package.
|
|
15
|
+
* e.g. 'ur:bytes/lpamcmcfatrdcyzcpldpgwhdhtiaiaecgyktgsflguhshthfghjtjngrhsfegtiafegaktgugui'
|
|
16
|
+
*
|
|
17
|
+
*
|
|
18
|
+
* Single part = ur:<type>/<message(payload)>
|
|
19
|
+
* Multi part = ur:<type>/<seqNum-seqLength>/<fragment(payload)>
|
|
20
|
+
*
|
|
21
|
+
* CBOR encoding should not include tag on top level when UR has a tag
|
|
22
|
+
*
|
|
23
|
+
* I want to be able to create UR from encoding normal js types
|
|
24
|
+
* Or from any type in the pipeline
|
|
25
|
+
*
|
|
26
|
+
* Should i do it on the encoder or ur class itself?
|
|
27
|
+
*/
|
|
28
|
+
export declare class UR {
|
|
29
|
+
type: string;
|
|
30
|
+
payload: string;
|
|
31
|
+
seqNum?: number;
|
|
32
|
+
seqLength?: number;
|
|
33
|
+
isFragment: boolean;
|
|
34
|
+
static pipeline: EncodingPipeline<any, string>;
|
|
35
|
+
constructor(input: IUR | RegistryItem);
|
|
36
|
+
decode(until?: EncodingMethodName): any;
|
|
37
|
+
toString(): string;
|
|
38
|
+
getPayloadBytewords(): string;
|
|
39
|
+
getPayloadHex(): string;
|
|
40
|
+
getPayloadCbor(): Uint8Array<ArrayBufferLike>;
|
|
41
|
+
toRegistryItem(): RegistryItemBase;
|
|
42
|
+
static fromRegistryItem(item: RegistryItem): UR;
|
|
43
|
+
/**
|
|
44
|
+
* Create UR from native javascript types by encoding them to bytewords
|
|
45
|
+
* @param input
|
|
46
|
+
* @returns
|
|
47
|
+
*/
|
|
48
|
+
static fromData(input: ReplaceKeyType<IUR, 'payload', any>): UR;
|
|
49
|
+
static fromCbor(input: ReplaceKeyType<IUR, 'payload', Uint8Array>): UR;
|
|
50
|
+
static fromHex(input: ReplaceKeyType<IUR, 'payload', string>): UR;
|
|
51
|
+
static fromBytewords(input: IUR): UR;
|
|
52
|
+
static from(input: ReplaceKeyType<IUR, 'payload', any>, type: EncodingMethodName): UR;
|
|
53
|
+
static fromString(ur: string): UR;
|
|
54
|
+
static encode: typeof UR.fromRegistryItem;
|
|
55
|
+
static decode(ur: UR): RegistryItem;
|
|
56
|
+
/**
|
|
57
|
+
* Check if the given string is a valid UR
|
|
58
|
+
* For single part ur, it should be in the form of "ur:<type>/<payload>" which is "ur:<lowercase letters, numbers or dashes>/<bytewords>"
|
|
59
|
+
* 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>"
|
|
60
|
+
* ur uses minimal bytewords encoding style which is a-z and has checksome bytes at the end
|
|
61
|
+
*
|
|
62
|
+
* @param input
|
|
63
|
+
* @returns
|
|
64
|
+
*/
|
|
65
|
+
static validate(input: string): boolean;
|
|
66
|
+
/**
|
|
67
|
+
* Generates a UR string from the given type and payload.
|
|
68
|
+
* Single part = ur:<type>/<message(payload)> if seqNum and seqLength are 0
|
|
69
|
+
* Multi part = ur:<type>/<seqNum-seqLength>/<fragment(payload)>
|
|
70
|
+
*/
|
|
71
|
+
static getUrString(type: string, payload: string, seqNum?: number, seqLen?: number): string;
|
|
72
|
+
/**
|
|
73
|
+
* Parses a UR and performs basic validation
|
|
74
|
+
* @param message e.g. "UR:BYTES/6-23/LPAMCHCFATTTCYCLEHGSDPHDHGEHFGHKKKDL..."
|
|
75
|
+
*/
|
|
76
|
+
static parseUr(message: string): IUR;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* [seqNum, fragments.length, totalPayloadLength, checksum, fragment]
|
|
80
|
+
*
|
|
81
|
+
* part = [
|
|
82
|
+
* uint32 seqNum,
|
|
83
|
+
* uint seqLen,
|
|
84
|
+
* uint messageLen,
|
|
85
|
+
* uint32 checksum,
|
|
86
|
+
* bytes data
|
|
87
|
+
* ]
|
|
88
|
+
*/
|
|
89
|
+
export type IMultipartUrPayload = [number, number, number, number, Uint8Array];
|