@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 @@
|
|
|
1
|
+
{"version":3,"file":"BytewordEncoding.js","sourceRoot":"","sources":["../../../src/encodingMethods/BytewordEncoding.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AACxD,OAAO,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAC;AAGpE,MAAM,OAAO,gBAAgB;IACnB,KAAK,GAAuB,kBAAkB,CAAC,SAAS,CAAC;IAExD,MAAM,CAAS;IAExB,IAAI,IAAI;QACJ,OAAO,IAAI,CAAC,KAAK,CAAC;IACtB,CAAC;IAED,YAAY,QAAgB,MAAM,CAAC,OAAO;QACxC,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;IACtB,CAAC;IACD,MAAM,CAAC,OAAe;QACpB,OAAO,MAAM,CAAC,OAAO,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;IACtC,CAAC;IACD,MAAM,CAAC,OAAe;QACpB,OAAO,MAAM,CAAC,OAAO,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;IACtC,CAAC;CACF"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { URRegistryClass } from "../registry.js";
|
|
2
|
+
import { RegistryItem, RegistryItemClass } from "../classes/RegistryItem.js";
|
|
3
|
+
import { EncodingMethodName } from "../enums/EncodingMethodName.js";
|
|
4
|
+
import { IEncodingMethod } from "../interfaces/IEncodingMethod.js";
|
|
5
|
+
import { DecodeOptions, EncodeOptions } from "../wrappers/cbor2.js";
|
|
6
|
+
interface inputOptions {
|
|
7
|
+
registry?: URRegistryClass;
|
|
8
|
+
cborLibEncoderOptions?: EncodeOptions & {
|
|
9
|
+
ignoreTopLevelTag?: boolean;
|
|
10
|
+
};
|
|
11
|
+
cborLibDecoderOptions?: DecodeOptions;
|
|
12
|
+
}
|
|
13
|
+
interface decodeOptions {
|
|
14
|
+
enforceType?: RegistryItemClass | string;
|
|
15
|
+
cborLibOptions?: DecodeOptions;
|
|
16
|
+
}
|
|
17
|
+
export declare class CborEncoding<T extends RegistryItem> implements IEncodingMethod<T, Uint8Array> {
|
|
18
|
+
private _name;
|
|
19
|
+
registry: URRegistryClass;
|
|
20
|
+
/** Decoding options for CBOR2 library */
|
|
21
|
+
cborLibEncoderOptions: EncodeOptions;
|
|
22
|
+
cborLibDecoderOptions: DecodeOptions;
|
|
23
|
+
constructor(options?: inputOptions);
|
|
24
|
+
get name(): EncodingMethodName;
|
|
25
|
+
/**
|
|
26
|
+
* Encode the given payload to CBOR
|
|
27
|
+
*
|
|
28
|
+
* @param payload @type RegistryItem
|
|
29
|
+
* @param cborLibOptions @type EncodeOptions
|
|
30
|
+
* @returns @type Uint8Array
|
|
31
|
+
*/
|
|
32
|
+
encode(payload: any, cborLibOptions?: EncodeOptions & {
|
|
33
|
+
ignoreTopLevelTag?: boolean;
|
|
34
|
+
}): Uint8Array;
|
|
35
|
+
/**
|
|
36
|
+
* Decode the CBOR encoded payload to the given type
|
|
37
|
+
* @param payload @type Uint8Array
|
|
38
|
+
* @param enforceType Forces decoding into given type or throws error if it cannot be decoded @type RegistryItemClass
|
|
39
|
+
* @param cborLibOptions @type DecodeOptions
|
|
40
|
+
* @returns @type T
|
|
41
|
+
*/
|
|
42
|
+
decode(payload: Uint8Array, options?: decodeOptions): T;
|
|
43
|
+
}
|
|
44
|
+
export {};
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { UrRegistry } from "../registry.js";
|
|
2
|
+
import { registryItemFactory } from "../classes/RegistryItem.js";
|
|
3
|
+
import { EncodingMethodName } from "../enums/EncodingMethodName.js";
|
|
4
|
+
import { decode, encode, Tag } from "../wrappers/cbor2.js";
|
|
5
|
+
export class CborEncoding {
|
|
6
|
+
_name = EncodingMethodName.cbor;
|
|
7
|
+
registry = UrRegistry;
|
|
8
|
+
/** Decoding options for CBOR2 library */
|
|
9
|
+
cborLibEncoderOptions;
|
|
10
|
+
cborLibDecoderOptions;
|
|
11
|
+
constructor(options) {
|
|
12
|
+
this.cborLibEncoderOptions = options?.cborLibEncoderOptions;
|
|
13
|
+
this.cborLibDecoderOptions = options?.cborLibDecoderOptions;
|
|
14
|
+
}
|
|
15
|
+
get name() {
|
|
16
|
+
return this._name;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Encode the given payload to CBOR
|
|
20
|
+
*
|
|
21
|
+
* @param payload @type RegistryItem
|
|
22
|
+
* @param cborLibOptions @type EncodeOptions
|
|
23
|
+
* @returns @type Uint8Array
|
|
24
|
+
*/
|
|
25
|
+
encode(payload, cborLibOptions) {
|
|
26
|
+
// Combine instance cborLibOptions with the given cborLibOptions
|
|
27
|
+
const combinedOptions = {
|
|
28
|
+
...this.cborLibEncoderOptions,
|
|
29
|
+
...cborLibOptions,
|
|
30
|
+
};
|
|
31
|
+
// By default encode return Uint8Array
|
|
32
|
+
const encoded = encode(payload, combinedOptions);
|
|
33
|
+
return encoded;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Decode the CBOR encoded payload to the given type
|
|
37
|
+
* @param payload @type Uint8Array
|
|
38
|
+
* @param enforceType Forces decoding into given type or throws error if it cannot be decoded @type RegistryItemClass
|
|
39
|
+
* @param cborLibOptions @type DecodeOptions
|
|
40
|
+
* @returns @type T
|
|
41
|
+
*/
|
|
42
|
+
decode(payload, options) {
|
|
43
|
+
// Combine instance cborLibOptions with the given cborLibOptions
|
|
44
|
+
const { cborLibOptions, enforceType } = options || {};
|
|
45
|
+
const combinedOptions = {
|
|
46
|
+
...this.cborLibDecoderOptions,
|
|
47
|
+
...cborLibOptions,
|
|
48
|
+
};
|
|
49
|
+
const decoded = decode(payload, combinedOptions);
|
|
50
|
+
// Check if enforce type is given, if so then give the value to the enforced type
|
|
51
|
+
if (enforceType) {
|
|
52
|
+
let foundType;
|
|
53
|
+
// If enforceType is string then find the type from the registry
|
|
54
|
+
if (typeof enforceType === "string") {
|
|
55
|
+
foundType = this.registry.queryByURType(enforceType);
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
foundType = enforceType;
|
|
59
|
+
}
|
|
60
|
+
// If we have a registry item then force the type
|
|
61
|
+
if (foundType) {
|
|
62
|
+
return forceType(decoded, foundType);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
// If we still have a cbor tag but not registry item then return try to convert it to registry item
|
|
66
|
+
if (decoded instanceof Tag) {
|
|
67
|
+
return tag2registryItem(decoded);
|
|
68
|
+
}
|
|
69
|
+
// Otherwise just return the decoded value
|
|
70
|
+
// TODO: fix as unknown as T;
|
|
71
|
+
return decoded;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Force the data to be of the enforced type registry item
|
|
76
|
+
* @param data
|
|
77
|
+
* @param enforceType
|
|
78
|
+
* @returns
|
|
79
|
+
*/
|
|
80
|
+
function forceType(data, enforceType) {
|
|
81
|
+
// If its already the enforced type then return
|
|
82
|
+
if (data instanceof enforceType) {
|
|
83
|
+
return data;
|
|
84
|
+
}
|
|
85
|
+
// If data is CBOR tag item, try to convert it to enforced type
|
|
86
|
+
if (data instanceof Tag) {
|
|
87
|
+
if (data.tag !== enforceType.tag) {
|
|
88
|
+
throw new Error(`Enforced type does not match the tag of ${enforceType.URType}:${enforceType.tag} !== ${data.tag}`);
|
|
89
|
+
}
|
|
90
|
+
return enforceType.fromCBORData(data.contents, enforceType.allowKeysNotInMap);
|
|
91
|
+
}
|
|
92
|
+
// If data is raw then try to convert it to enforced type
|
|
93
|
+
return enforceType.fromCBORData(data, enforceType.allowKeysNotInMap);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Converts a CBOR tag to a registry item
|
|
97
|
+
* @param unkownTag
|
|
98
|
+
* @returns
|
|
99
|
+
*/
|
|
100
|
+
function tag2registryItem(unkownTag) {
|
|
101
|
+
const tag = toRegistryTagNumber(unkownTag.tag);
|
|
102
|
+
const data = unkownTag.contents;
|
|
103
|
+
const registryItem = UrRegistry.queryByTag(tag);
|
|
104
|
+
if (registryItem) {
|
|
105
|
+
return registryItem.fromCBORData(data, registryItem.allowKeysNotInMap);
|
|
106
|
+
}
|
|
107
|
+
// If we dont have the tag in the registry return unknown item
|
|
108
|
+
const UnknownTag = class extends registryItemFactory({
|
|
109
|
+
tag: tag,
|
|
110
|
+
URType: "unknown-tag",
|
|
111
|
+
CDDL: ``,
|
|
112
|
+
}) {
|
|
113
|
+
};
|
|
114
|
+
const unknownItem = new UnknownTag(data);
|
|
115
|
+
// Search through all the children in unknownItem.data if any one of them are tags
|
|
116
|
+
deepSearchObject(unknownItem.data);
|
|
117
|
+
return unknownItem;
|
|
118
|
+
}
|
|
119
|
+
function toRegistryTagNumber(tag) {
|
|
120
|
+
if (typeof tag === "bigint") {
|
|
121
|
+
const converted = Number(tag);
|
|
122
|
+
if (!Number.isSafeInteger(converted)) {
|
|
123
|
+
throw new Error(`CBOR tag ${tag.toString()} exceeds JavaScript safe integer range`);
|
|
124
|
+
}
|
|
125
|
+
return converted;
|
|
126
|
+
}
|
|
127
|
+
return Number(tag);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Search through the object and convert tags to registry items
|
|
131
|
+
* @param obj
|
|
132
|
+
* @returns
|
|
133
|
+
*/
|
|
134
|
+
function deepSearchObject(obj) {
|
|
135
|
+
if (typeof obj !== "object") {
|
|
136
|
+
return obj;
|
|
137
|
+
}
|
|
138
|
+
for (const key in obj) {
|
|
139
|
+
if (obj[key] instanceof Tag) {
|
|
140
|
+
obj[key] = tag2registryItem(obj[key]);
|
|
141
|
+
}
|
|
142
|
+
else if (typeof obj[key] === "object") {
|
|
143
|
+
deepSearchObject(obj[key]);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
//# sourceMappingURL=CborEncoding.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CborEncoding.js","sourceRoot":"","sources":["../../../src/encodingMethods/CborEncoding.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAC7D,OAAO,EAAmC,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AAClG,OAAO,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAC;AAGpE,OAAO,EAGL,MAAM,EAAE,MAAM,EAAE,GAAG,EACpB,MAAM,sBAAsB,CAAC;AAa9B,MAAM,OAAO,YAAY;IAGf,KAAK,GAAuB,kBAAkB,CAAC,IAAI,CAAC;IACrD,QAAQ,GAAoB,UAAU,CAAC;IAE9C,yCAAyC;IACzC,qBAAqB,CAAgB;IACrC,qBAAqB,CAAgB;IAErC,YAAY,OAAsB;QAChC,IAAI,CAAC,qBAAqB,GAAG,OAAO,EAAE,qBAAqB,CAAC;QAC5D,IAAI,CAAC,qBAAqB,GAAG,OAAO,EAAE,qBAAqB,CAAC;IAC9D,CAAC;IAED,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED;;;;;;OAMG;IACH,MAAM,CAAC,OAAY,EAAE,cAA8D;QACjF,gEAAgE;QAChE,MAAM,eAAe,GAAG;YACtB,GAAG,IAAI,CAAC,qBAAqB;YAC7B,GAAG,cAAc;SAClB,CAAC;QAEF,sCAAsC;QACtC,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,EAAE,eAAe,CAAC,CAAC;QACjD,OAAO,OAAO,CAAC;IACjB,CAAC;IAED;;;;;;OAMG;IACH,MAAM,CACJ,OAAmB,EACnB,OAAuB;Q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,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC;AACrB,CAAC;AAED;;;;GAIG;AACH,SAAS,gBAAgB,CAAC,GAAQ;IAChC,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE,CAAC;QAC5B,OAAO,GAAG,CAAC;IACb,CAAC;IAED,KAAK,MAAM,GAAG,IAAI,GAAG,EAAE,CAAC;QACtB,IAAI,GAAG,CAAC,GAAG,CAAC,YAAY,GAAG,EAAE,CAAC;YAC5B,GAAG,CAAC,GAAG,CAAC,GAAG,gBAAgB,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;QACxC,CAAC;aAAM,IAAI,OAAO,GAAG,CAAC,GAAG,CAAC,KAAK,QAAQ,EAAE,CAAC;YACxC,gBAAgB,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;QAC7B,CAAC;IACH,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { EncodingMethodName } from "../enums/EncodingMethodName.js";
|
|
2
|
+
import { IEncodingMethod } from "../interfaces/IEncodingMethod.js";
|
|
3
|
+
export declare class HexEncoding implements IEncodingMethod<Uint8Array, string> {
|
|
4
|
+
private _name;
|
|
5
|
+
get name(): EncodingMethodName;
|
|
6
|
+
encode(payload: Uint8Array): string;
|
|
7
|
+
decode(payload: string): Uint8Array;
|
|
8
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { hexToUint8Array, uint8ArrayToHex } from "../helpers/uintArrayHelper.js";
|
|
2
|
+
import { EncodingMethodName } from "../enums/EncodingMethodName.js";
|
|
3
|
+
export class HexEncoding {
|
|
4
|
+
_name = EncodingMethodName.hex;
|
|
5
|
+
get name() {
|
|
6
|
+
return this._name;
|
|
7
|
+
}
|
|
8
|
+
encode(payload) {
|
|
9
|
+
// return payload.toString("hex");
|
|
10
|
+
return uint8ArrayToHex(payload);
|
|
11
|
+
}
|
|
12
|
+
decode(payload) {
|
|
13
|
+
const hexRegex = /^[0-9a-fA-F]+$/;
|
|
14
|
+
if (!hexRegex.test(payload)) {
|
|
15
|
+
throw new Error("Invalid hex string");
|
|
16
|
+
}
|
|
17
|
+
return hexToUint8Array(payload);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=HexEncoding.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"HexEncoding.js","sourceRoot":"","sources":["../../../src/encodingMethods/HexEncoding.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAC;AACjF,OAAO,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAC;AAGpE,MAAM,OAAO,WAAW;IACd,KAAK,GAAuB,kBAAkB,CAAC,GAAG,CAAC;IAE3D,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED,MAAM,CAAC,OAAmB;QACxB,kCAAkC;QAClC,OAAO,eAAe,CAAC,OAAO,CAAC,CAAC;IAClC,CAAC;IACD,MAAM,CAAC,OAAe;QACpB,MAAM,QAAQ,GAAG,gBAAgB,CAAC;QAClC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC;YAC5B,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,CAAC;QACxC,CAAC;QACD,OAAO,eAAe,CAAC,OAAO,CAAC,CAAC;IAClC,CAAC;CACF"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { EncodingMethodName } from "../enums/EncodingMethodName.js";
|
|
2
|
+
import { IEncodingMethod } from "../interfaces/IEncodingMethod.js";
|
|
3
|
+
import { UR } from "../classes/UR.js";
|
|
4
|
+
import { RegistryItem } from "../classes/RegistryItem.js";
|
|
5
|
+
export declare class UrEncoding implements IEncodingMethod<RegistryItem, UR> {
|
|
6
|
+
private _name;
|
|
7
|
+
get name(): EncodingMethodName;
|
|
8
|
+
encode(payload: RegistryItem): UR;
|
|
9
|
+
decode(payload: UR): RegistryItem;
|
|
10
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { EncodingMethodName } from "../enums/EncodingMethodName.js";
|
|
2
|
+
import { UR } from "../classes/UR.js";
|
|
3
|
+
export class UrEncoding {
|
|
4
|
+
_name = EncodingMethodName.ur;
|
|
5
|
+
get name() {
|
|
6
|
+
return this._name;
|
|
7
|
+
}
|
|
8
|
+
encode(payload) {
|
|
9
|
+
return UR.fromRegistryItem(payload);
|
|
10
|
+
}
|
|
11
|
+
decode(payload) {
|
|
12
|
+
return payload.toRegistryItem();
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=UrEncoding.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"UrEncoding.js","sourceRoot":"","sources":["../../../src/encodingMethods/UrEncoding.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAC;AAEpE,OAAO,EAAE,EAAE,EAAE,MAAM,kBAAkB,CAAC;AAGtC,MAAM,OAAO,UAAU;IACb,KAAK,GAAuB,kBAAkB,CAAC,EAAE,CAAC;IAE1D,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED,MAAM,CAAC,OAAqB;QAC1B,OAAO,EAAE,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC;IACtC,CAAC;IACD,MAAM,CAAC,OAAW;QAChB,OAAO,OAAO,CAAC,cAAc,EAAE,CAAC;IAClC,CAAC;CACF"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export declare enum STYLES {
|
|
2
|
+
STANDARD = "standard",
|
|
3
|
+
URI = "uri",
|
|
4
|
+
MINIMAL = "minimal"
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Decode a string of bytewords into a hex string.
|
|
8
|
+
* @param string string of Bytewords
|
|
9
|
+
* @param style style of Bytewords
|
|
10
|
+
* @param includeChecksum whether to include checksum in the decoding.
|
|
11
|
+
* @returns hex string
|
|
12
|
+
*/
|
|
13
|
+
export declare const decode: (string: string, style?: STYLES, includeChecksum?: boolean) => string;
|
|
14
|
+
/**
|
|
15
|
+
* Encodes a string (hex representation of a buffer) into bytewords.
|
|
16
|
+
* @param string string to encode.
|
|
17
|
+
* @param style style to use for the encoding.
|
|
18
|
+
* @param includeChecksum whether to include checksum in the encoding.
|
|
19
|
+
* @returns the byteword encoded string
|
|
20
|
+
*/
|
|
21
|
+
export declare const encode: (string: string, style?: STYLES, includeChecksum?: boolean) => string;
|
|
22
|
+
declare const _default: {
|
|
23
|
+
decode: (string: string, style?: STYLES, includeChecksum?: boolean) => string;
|
|
24
|
+
encode: (string: string, style?: STYLES, includeChecksum?: boolean) => string;
|
|
25
|
+
STYLES: typeof STYLES;
|
|
26
|
+
};
|
|
27
|
+
export default _default;
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { assert, getCRCHex, partition, split } from "../helpers/utils.js";
|
|
2
|
+
import { uint8ArrayToHex, hexToUint8Array } from "../helpers/uintArrayHelper.js";
|
|
3
|
+
const bytewords = "ableacidalsoapexaquaarchatomauntawayaxisbackbaldbarnbeltbetabiasbluebodybragbrewbulbbuzzcalmcashcatschefcityclawcodecolacookcostcruxcurlcuspcyandarkdatadaysdelidicedietdoordowndrawdropdrumdulldutyeacheasyechoedgeepicevenexamexiteyesfactfairfernfigsfilmfishfizzflapflewfluxfoxyfreefrogfuelfundgalagamegeargemsgiftgirlglowgoodgraygrimgurugushgyrohalfhanghardhawkheathelphighhillholyhopehornhutsicedideaidleinchinkyintoirisironitemjadejazzjoinjoltjowljudojugsjumpjunkjurykeepkenokeptkeyskickkilnkingkitekiwiknoblamblavalazyleaflegsliarlimplionlistlogoloudloveluaulucklungmainmanymathmazememomenumeowmildmintmissmonknailnavyneednewsnextnoonnotenumbobeyoboeomitonyxopenovalowlspaidpartpeckplaypluspoempoolposepuffpumapurrquadquizraceramprealredorichroadrockroofrubyruinrunsrustsafesagascarsetssilkskewslotsoapsolosongstubsurfswantacotasktaxitenttiedtimetinytoiltombtoystriptunatwinuglyundouniturgeuservastveryvetovialvibeviewvisavoidvowswallwandwarmwaspwavewaxywebswhatwhenwhizwolfworkyankyawnyellyogayurtzapszerozestzinczonezoom";
|
|
4
|
+
let bytewordsLookUpTable = [];
|
|
5
|
+
const BYTEWORDS_NUM = 256;
|
|
6
|
+
const BYTEWORD_LENGTH = 4;
|
|
7
|
+
const MINIMAL_BYTEWORD_LENGTH = 2;
|
|
8
|
+
export var STYLES;
|
|
9
|
+
(function (STYLES) {
|
|
10
|
+
STYLES["STANDARD"] = "standard";
|
|
11
|
+
STYLES["URI"] = "uri";
|
|
12
|
+
STYLES["MINIMAL"] = "minimal";
|
|
13
|
+
})(STYLES || (STYLES = {}));
|
|
14
|
+
const getWord = (index) => {
|
|
15
|
+
return bytewords.slice(index * BYTEWORD_LENGTH, index * BYTEWORD_LENGTH + BYTEWORD_LENGTH);
|
|
16
|
+
};
|
|
17
|
+
const getMinimalWord = (index) => {
|
|
18
|
+
const byteword = getWord(index);
|
|
19
|
+
return `${byteword[0]}${byteword[BYTEWORD_LENGTH - 1]}`;
|
|
20
|
+
};
|
|
21
|
+
const addCRC = (string) => {
|
|
22
|
+
const crc = getCRCHex(hexToUint8Array(string));
|
|
23
|
+
return `${string}${crc}`;
|
|
24
|
+
};
|
|
25
|
+
const encodeWithSeparator = (word, separator, includeChecksum) => {
|
|
26
|
+
const crcAppendedWord = includeChecksum ? addCRC(word) : word;
|
|
27
|
+
const crcWordArray = hexToUint8Array(crcAppendedWord);
|
|
28
|
+
const result = crcWordArray.reduce((result, w) => [...result, getWord(w)], []);
|
|
29
|
+
return result.join(separator);
|
|
30
|
+
};
|
|
31
|
+
const encodeMinimal = (word, includeChecksum) => {
|
|
32
|
+
const crcAppendedWord = includeChecksum ? addCRC(word) : word;
|
|
33
|
+
const crcWordArray = hexToUint8Array(crcAppendedWord);
|
|
34
|
+
const result = crcWordArray.reduce((result, w) => result + getMinimalWord(w), "");
|
|
35
|
+
return result;
|
|
36
|
+
};
|
|
37
|
+
const decodeWord = (word, wordLength) => {
|
|
38
|
+
assert(word.length === wordLength, "Invalid Bytewords: word.length does not match wordLength provided");
|
|
39
|
+
const dim = 26;
|
|
40
|
+
// Since the first and last letters of each Byteword are unique,
|
|
41
|
+
// we can use them as indexes into a two-dimensional lookup table.
|
|
42
|
+
// This table is generated lazily.
|
|
43
|
+
if (bytewordsLookUpTable.length === 0) {
|
|
44
|
+
const array_len = dim * dim;
|
|
45
|
+
bytewordsLookUpTable = [...new Array(array_len)].map(() => -1);
|
|
46
|
+
for (let i = 0; i < BYTEWORDS_NUM; i++) {
|
|
47
|
+
const byteword = getWord(i);
|
|
48
|
+
let x = byteword[0].charCodeAt(0) - "a".charCodeAt(0);
|
|
49
|
+
let y = byteword[3].charCodeAt(0) - "a".charCodeAt(0);
|
|
50
|
+
let offset = y * dim + x;
|
|
51
|
+
bytewordsLookUpTable[offset] = i;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
// If the coordinates generated by the first and last letters are out of bounds,
|
|
55
|
+
// or the lookup table contains -1 at the coordinates, then the word is not valid.
|
|
56
|
+
let x = word[0].toLowerCase().charCodeAt(0) - "a".charCodeAt(0);
|
|
57
|
+
let y = word[wordLength == 4 ? 3 : 1].toLowerCase().charCodeAt(0) -
|
|
58
|
+
"a".charCodeAt(0);
|
|
59
|
+
assert(0 <= x && x < dim && 0 <= y && y < dim, "Invalid Bytewords: invalid word");
|
|
60
|
+
let offset = y * dim + x;
|
|
61
|
+
let value = bytewordsLookUpTable[offset];
|
|
62
|
+
assert(value !== -1, "Invalid Bytewords: value not in lookup table");
|
|
63
|
+
// If we're decoding a full four-letter word, verify that the two middle letters are correct.
|
|
64
|
+
if (wordLength == BYTEWORD_LENGTH) {
|
|
65
|
+
const byteword = getWord(value);
|
|
66
|
+
let c1 = word[1].toLowerCase();
|
|
67
|
+
let c2 = word[2].toLowerCase();
|
|
68
|
+
assert(c1 === byteword[1] && c2 === byteword[2], "Invalid Bytewords: invalid middle letters of word");
|
|
69
|
+
}
|
|
70
|
+
// Successful decode.
|
|
71
|
+
return value.toString(16).padStart(2, "0");
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* Decode a string of Bytewords into a hex string.
|
|
75
|
+
* @param string string of Bytewords. e.g. "lpamchcfatttcyclehgsdphdhgehfghkkkdl..."
|
|
76
|
+
* @param separator e.g. " " or "-" or "" or any other separator
|
|
77
|
+
* @param wordLength e.g. 4 or 2 or any other length
|
|
78
|
+
* @param includeChecksum whether to include checksum in the decoding.
|
|
79
|
+
* @returns hex string
|
|
80
|
+
*/
|
|
81
|
+
const _decode = (string, separator, wordLength, includeChecksum) => {
|
|
82
|
+
// Split the string into words. e.g. ["lp", "am", "ch", "cf", "at", "tt", "cy", "cl", "eh", "gs", "dp", "hd", "hg", "eh", "fg", "hk", "kk", "dl", ...]
|
|
83
|
+
const words = wordLength == BYTEWORD_LENGTH
|
|
84
|
+
? string.split(separator)
|
|
85
|
+
: partition(string, 2);
|
|
86
|
+
// Decode each word. e.g. ["85", "06", "17", "19", "07", "d1", "1a", "21", "31", "4c", "2d", "58", "57", "31", "46", "59", "79", "2f", ...]
|
|
87
|
+
const decodedWords = words.map((word) => decodeWord(word, wordLength));
|
|
88
|
+
// e.g. "8506171907d11a21314c2d5857314659792f..."
|
|
89
|
+
const decodedString = decodedWords.join("");
|
|
90
|
+
if (includeChecksum) {
|
|
91
|
+
// 4 bytes for checksum, at least 1 byte for body
|
|
92
|
+
assert(decodedString.length >= 5, "Invalid Bytewords: invalid decoded string length");
|
|
93
|
+
// decoded string consists of `body` and `checksum`
|
|
94
|
+
const decodedArray = hexToUint8Array(decodedString);
|
|
95
|
+
const [body, bodyChecksum] = split(decodedArray, 4);
|
|
96
|
+
const checksum = getCRCHex(body); // convert to hex
|
|
97
|
+
assert(checksum === uint8ArrayToHex(bodyChecksum), "Invalid Checksum");
|
|
98
|
+
return uint8ArrayToHex(body);
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
return decodedString;
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
/**
|
|
105
|
+
* Decode a string of bytewords into a hex string.
|
|
106
|
+
* @param string string of Bytewords
|
|
107
|
+
* @param style style of Bytewords
|
|
108
|
+
* @param includeChecksum whether to include checksum in the decoding.
|
|
109
|
+
* @returns hex string
|
|
110
|
+
*/
|
|
111
|
+
export const decode = (string, style = STYLES.MINIMAL, includeChecksum = true) => {
|
|
112
|
+
switch (style) {
|
|
113
|
+
case STYLES.STANDARD:
|
|
114
|
+
return _decode(string, " ", BYTEWORD_LENGTH, includeChecksum);
|
|
115
|
+
case STYLES.URI:
|
|
116
|
+
return _decode(string, "-", BYTEWORD_LENGTH, includeChecksum);
|
|
117
|
+
case STYLES.MINIMAL:
|
|
118
|
+
return _decode(string, "", MINIMAL_BYTEWORD_LENGTH, includeChecksum);
|
|
119
|
+
default:
|
|
120
|
+
throw new Error(`Invalid style ${style}`);
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
/**
|
|
124
|
+
* Encodes a string (hex representation of a buffer) into bytewords.
|
|
125
|
+
* @param string string to encode.
|
|
126
|
+
* @param style style to use for the encoding.
|
|
127
|
+
* @param includeChecksum whether to include checksum in the encoding.
|
|
128
|
+
* @returns the byteword encoded string
|
|
129
|
+
*/
|
|
130
|
+
export const encode = (string, style = STYLES.MINIMAL, includeChecksum = true) => {
|
|
131
|
+
switch (style) {
|
|
132
|
+
case STYLES.STANDARD:
|
|
133
|
+
return encodeWithSeparator(string, " ", includeChecksum);
|
|
134
|
+
case STYLES.URI:
|
|
135
|
+
return encodeWithSeparator(string, "-", includeChecksum);
|
|
136
|
+
case STYLES.MINIMAL:
|
|
137
|
+
return encodeMinimal(string, includeChecksum);
|
|
138
|
+
default:
|
|
139
|
+
throw new Error(`Invalid style ${style}`);
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
export default {
|
|
143
|
+
decode,
|
|
144
|
+
encode,
|
|
145
|
+
STYLES,
|
|
146
|
+
};
|
|
147
|
+
//# sourceMappingURL=bytewords.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bytewords.js","sourceRoot":"","sources":["../../../src/encodingMethods/bytewords.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAC1E,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAC;AAEjF,MAAM,SAAS,GACb,kgCAAkgC,CAAC;AACrgC,IAAI,oBAAoB,GAAa,EAAE,CAAC;AACxC,MAAM,aAAa,GAAG,GAAG,CAAC;AAC1B,MAAM,eAAe,GAAG,CAAC,CAAC;AAC1B,MAAM,uBAAuB,GAAG,CAAC,CAAC;AAElC,MAAM,CAAN,IAAY,MAIX;AAJD,WAAY,MAAM;IAChB,+BAAqB,CAAA;IACrB,qBAAW,CAAA;IACX,6BAAmB,CAAA;AACrB,CAAC,EAJW,MAAM,KAAN,MAAM,QAIjB;AAED,MAAM,OAAO,GAAG,CAAC,KAAa,EAAU,EAAE;IACxC,OAAO,SAAS,CAAC,KAAK,CACpB,KAAK,GAAG,eAAe,EACvB,KAAK,GAAG,eAAe,GAAG,eAAe,CAC1C,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,cAAc,GAAG,CAAC,KAAa,EAAU,EAAE;IAC/C,MAAM,QAAQ,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC;IAEhC,OAAO,GAAG,QAAQ,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,eAAe,GAAG,CAAC,CAAC,EAAE,CAAC;AAC1D,CAAC,CAAC;AAEF,MAAM,MAAM,GAAG,CAAC,MAAc,EAAU,EAAE;IACxC,MAAM,GAAG,GAAG,SAAS,CAAC,eAAe,CAAC,MAAM,CAAC,CAAC,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EACvB,EAAE;IACV,QAAQ,KAAK,EAAE,CAAC;QACd,KAAK,MAAM,CAAC,QAAQ;YAClB,OAAO,mBAAmB,CAAC,MAAM,EAAE,GAAG,EAAE,eAAe,CAAC,CAAC;QAC3D,KAAK,MAAM,CAAC,GAAG;YACb,OAAO,mBAAmB,CAAC,MAAM,EAAE,GAAG,EAAE,eAAe,CAAC,CAAC;QAC3D,KAAK,MAAM,CAAC,OAAO;YACjB,OAAO,aAAa,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC;QAChD;YACE,MAAM,IAAI,KAAK,CAAC,iBAAiB,KAAK,EAAE,CAAC,CAAC;IAC9C,CAAC;AACH,CAAC,CAAC;AAEF,eAAe;IACb,MAAM;IACN,MAAM;IACN,MAAM;CACP,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { BytewordEncoding } from "./BytewordEncoding.js";
|
|
2
|
+
import { CborEncoding } from "./CborEncoding.js";
|
|
3
|
+
import { HexEncoding } from "./HexEncoding.js";
|
|
4
|
+
import { EncodingPipeline } from "./pipeline.js";
|
|
5
|
+
/**
|
|
6
|
+
* Pipeline that encodes registry registryItem | any -> cbor -> hex -> bytewords
|
|
7
|
+
*
|
|
8
|
+
* Uses default encoding options for each encoding method
|
|
9
|
+
* 1. Cbor: CBOR2 library
|
|
10
|
+
* 2. Hex: Uint8Array -> hex string
|
|
11
|
+
* 3. Bytewords: Uint8Array -> bytewords string with checksum and minimal style
|
|
12
|
+
*
|
|
13
|
+
*/
|
|
14
|
+
export declare const defaultEncoders: {
|
|
15
|
+
cbor: CborEncoding<import("../index.js").RegistryItemBase>;
|
|
16
|
+
hex: HexEncoding;
|
|
17
|
+
bytewords: BytewordEncoding;
|
|
18
|
+
};
|
|
19
|
+
export declare const dataPipeline: EncodingPipeline<any, string>;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { BytewordEncoding } from "./BytewordEncoding.js";
|
|
2
|
+
import { CborEncoding } from "./CborEncoding.js";
|
|
3
|
+
import { HexEncoding } from "./HexEncoding.js";
|
|
4
|
+
import { EncodingPipeline } from "./pipeline.js";
|
|
5
|
+
/**
|
|
6
|
+
* Pipeline that encodes registry registryItem | any -> cbor -> hex -> bytewords
|
|
7
|
+
*
|
|
8
|
+
* Uses default encoding options for each encoding method
|
|
9
|
+
* 1. Cbor: CBOR2 library
|
|
10
|
+
* 2. Hex: Uint8Array -> hex string
|
|
11
|
+
* 3. Bytewords: Uint8Array -> bytewords string with checksum and minimal style
|
|
12
|
+
*
|
|
13
|
+
*/
|
|
14
|
+
export const defaultEncoders = {
|
|
15
|
+
cbor: new CborEncoding(),
|
|
16
|
+
hex: new HexEncoding(),
|
|
17
|
+
bytewords: new BytewordEncoding(),
|
|
18
|
+
};
|
|
19
|
+
export const dataPipeline = new EncodingPipeline([
|
|
20
|
+
defaultEncoders.cbor,
|
|
21
|
+
defaultEncoders.hex,
|
|
22
|
+
defaultEncoders.bytewords,
|
|
23
|
+
]);
|
|
24
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/encodingMethods/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAEjD;;;;;;;;GAQG;AAEH,MAAM,CAAC,MAAM,eAAe,GAAG;IAC7B,IAAI,EAAE,IAAI,YAAY,EAAE;IACxB,GAAG,EAAE,IAAI,WAAW,EAAE;IACtB,SAAS,EAAE,IAAI,gBAAgB,EAAE;CAClC,CAAA;AAED,MAAM,CAAC,MAAM,YAAY,GAAG,IAAI,gBAAgB,CAAc;IAC5D,eAAe,CAAC,IAAI;IACpB,eAAe,CAAC,GAAG;IACnB,eAAe,CAAC,SAAS;CAC1B,CAAC,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { EncodingMethodName } from "../enums/EncodingMethodName.js";
|
|
2
|
+
import { IEncodingMethod } from "../interfaces/IEncodingMethod.js";
|
|
3
|
+
interface pipelineConfig {
|
|
4
|
+
from?: EncodingMethodName;
|
|
5
|
+
until?: EncodingMethodName;
|
|
6
|
+
[key: string]: any;
|
|
7
|
+
}
|
|
8
|
+
export declare class EncodingPipeline<T, U> implements IEncodingMethod<T, U> {
|
|
9
|
+
private _name;
|
|
10
|
+
private _encodingMethods;
|
|
11
|
+
constructor(encodingMethods: IEncodingMethod<any, any>[]);
|
|
12
|
+
get name(): EncodingMethodName;
|
|
13
|
+
get encodingMethods(): IEncodingMethod<any, any>[];
|
|
14
|
+
get decodingMethods(): IEncodingMethod<any, any>[];
|
|
15
|
+
encode<O = U>(payload: T, config?: pipelineConfig): O;
|
|
16
|
+
decode<O = T>(payload: U, config?: pipelineConfig): O;
|
|
17
|
+
getMethod(methodName: EncodingMethodName): IEncodingMethod<any, any>;
|
|
18
|
+
}
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { EncodingMethodName } from "../enums/EncodingMethodName.js";
|
|
2
|
+
export class EncodingPipeline {
|
|
3
|
+
_name = EncodingMethodName.pipe;
|
|
4
|
+
_encodingMethods;
|
|
5
|
+
constructor(encodingMethods) {
|
|
6
|
+
this._encodingMethods = encodingMethods;
|
|
7
|
+
}
|
|
8
|
+
get name() {
|
|
9
|
+
return this._name;
|
|
10
|
+
}
|
|
11
|
+
get encodingMethods() {
|
|
12
|
+
return this._encodingMethods;
|
|
13
|
+
}
|
|
14
|
+
get decodingMethods() {
|
|
15
|
+
return this._encodingMethods.slice().reverse();
|
|
16
|
+
}
|
|
17
|
+
// TODO: change until to "to"
|
|
18
|
+
encode(payload, config = { from: undefined, until: undefined }) {
|
|
19
|
+
let encodedValue = payload;
|
|
20
|
+
let fromIndex = 0;
|
|
21
|
+
let untilIndex = this.encodingMethods.length;
|
|
22
|
+
const { from, until, ...rest } = config;
|
|
23
|
+
// Find the index of the specified start and until encoding methods
|
|
24
|
+
if (from) {
|
|
25
|
+
fromIndex = this.encodingMethods.findIndex((method) => method.name === from);
|
|
26
|
+
if (fromIndex === -1) {
|
|
27
|
+
throw new Error(`Encoding method ${from} not found`);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (until) {
|
|
31
|
+
untilIndex = this.encodingMethods.findIndex((method) => method.name === until);
|
|
32
|
+
if (untilIndex === -1) {
|
|
33
|
+
throw new Error(`Encoding method ${until} not found`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (fromIndex > untilIndex) {
|
|
37
|
+
throw new Error("Invalid encoding method range");
|
|
38
|
+
}
|
|
39
|
+
// Apply each encoding method in sequence until the specified encoding method or the end
|
|
40
|
+
for (let i = fromIndex; i < untilIndex; i++) {
|
|
41
|
+
encodedValue = this.encodingMethods[i].encode(encodedValue, rest);
|
|
42
|
+
}
|
|
43
|
+
return encodedValue;
|
|
44
|
+
}
|
|
45
|
+
decode(payload, config = { from: undefined, until: undefined }) {
|
|
46
|
+
let decodedValue = payload;
|
|
47
|
+
let fromIndex = 0;
|
|
48
|
+
let untilIndex = this.decodingMethods.length;
|
|
49
|
+
const { from, until, ...rest } = config;
|
|
50
|
+
// Find the index of the specified start and until encoding methods
|
|
51
|
+
if (from) {
|
|
52
|
+
fromIndex = this.decodingMethods.findIndex((method) => method.name === from);
|
|
53
|
+
if (fromIndex === -1) {
|
|
54
|
+
throw new Error(`Encoding method ${from} not found`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (until) {
|
|
58
|
+
untilIndex = this.decodingMethods.findIndex((method) => method.name === until);
|
|
59
|
+
if (untilIndex === -1) {
|
|
60
|
+
throw new Error(`Encoding method ${until} not found`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (fromIndex > untilIndex) {
|
|
64
|
+
throw new Error("Invalid encoding method range");
|
|
65
|
+
}
|
|
66
|
+
// Apply each encoding method in sequence until the specified encoding method or the end
|
|
67
|
+
for (let i = fromIndex; i < untilIndex; i++) {
|
|
68
|
+
decodedValue = this.decodingMethods[i].decode(decodedValue, rest);
|
|
69
|
+
}
|
|
70
|
+
return decodedValue;
|
|
71
|
+
}
|
|
72
|
+
getMethod(methodName) {
|
|
73
|
+
return this.encodingMethods.find((method) => method.name === methodName);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=pipeline.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pipeline.js","sourceRoot":"","sources":["../../../src/encodingMethods/pipeline.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAC;AASpE,MAAM,OAAO,gBAAgB;IACnB,KAAK,GAAuB,kBAAkB,CAAC,IAAI,CAAC;IACpD,gBAAgB,CAA8B;IAEtD,YAAY,eAA4C;QACtD,IAAI,CAAC,gBAAgB,GAAG,eAAe,CAAC;IAC1C,CAAC;IAED,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED,IAAI,eAAe;QACjB,OAAO,IAAI,CAAC,gBAAgB,CAAC;IAC/B,CAAC;IAED,IAAI,eAAe;QACjB,OAAO,IAAI,CAAC,gBAAgB,CAAC,KAAK,EAAE,CAAC,OAAO,EAAE,CAAC;IACjD,CAAC;IAGD,6BAA6B;IAC7B,MAAM,CAAQ,OAAU,EAAE,SAAyB,EAAC,IAAI,EAAC,SAAS,EAAE,KAAK,EAAC,SAAS,EAAC;QAClF,IAAI,YAAY,GAAQ,OAAO,CAAC;QAChC,IAAI,SAAS,GAAG,CAAC,CAAC;QAClB,IAAI,UAAU,GAAG,IAAI,CAAC,eAAe,CAAC,MAAM,CAAC;QAE7C,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,IAA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ultly used encoding methods.
|
|
3
|
+
*/
|
|
4
|
+
export var EncodingMethodName;
|
|
5
|
+
(function (EncodingMethodName) {
|
|
6
|
+
EncodingMethodName["bytewords"] = "bytewords";
|
|
7
|
+
EncodingMethodName["cbor"] = "cbor";
|
|
8
|
+
EncodingMethodName["hex"] = "hex";
|
|
9
|
+
EncodingMethodName["pipe"] = "pipe";
|
|
10
|
+
EncodingMethodName["ur"] = "ur";
|
|
11
|
+
})(EncodingMethodName || (EncodingMethodName = {}));
|
|
12
|
+
//# sourceMappingURL=EncodingMethodName.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EncodingMethodName.js","sourceRoot":"","sources":["../../../src/enums/EncodingMethodName.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,CAAN,IAAY,kBAMX;AAND,WAAY,kBAAkB;IAC5B,6CAAuB,CAAA;IACvB,mCAAa,CAAA;IACb,iCAAW,CAAA;IACX,mCAAa,CAAA;IACb,+BAAS,CAAA;AACX,CAAC,EANW,kBAAkB,KAAlB,kBAAkB,QAM7B"}
|