@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,335 @@
|
|
|
1
|
+
BigInt("0xffffffffffffffff");
|
|
2
|
+
|
|
3
|
+
const CRC32_TABLE = new Uint32Array(256);
|
|
4
|
+
for (let i = 0; i < 256; i++) {
|
|
5
|
+
let c = i;
|
|
6
|
+
for (let j = 0; j < 8; j++) {
|
|
7
|
+
c = (c & 1) !== 0 ? (0xedb88320 ^ (c >>> 1)) : (c >>> 1);
|
|
8
|
+
}
|
|
9
|
+
CRC32_TABLE[i] = c >>> 0;
|
|
10
|
+
}
|
|
11
|
+
function crc32(message) {
|
|
12
|
+
let crc = 0xffffffff;
|
|
13
|
+
for (const byte of message) {
|
|
14
|
+
crc = CRC32_TABLE[(crc ^ byte) & 0xff] ^ (crc >>> 8);
|
|
15
|
+
}
|
|
16
|
+
return (crc ^ 0xffffffff) >>> 0;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Taken from uint8array-extras "^1.4.0" package
|
|
20
|
+
// https://github.com/sindresorhus/uint8array-extras
|
|
21
|
+
const objectToString = Object.prototype.toString;
|
|
22
|
+
const uint8ArrayStringified = '[object Uint8Array]';
|
|
23
|
+
function isType(value, typeConstructor, typeStringified) {
|
|
24
|
+
if (!value) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
if (value.constructor === typeConstructor) {
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
return objectToString.call(value) === typeStringified;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
Check if the given value is an instance of `Uint8Array`.
|
|
34
|
+
|
|
35
|
+
Replacement for [`Buffer.isBuffer()`](https://nodejs.org/api/buffer.html#static-method-bufferisbufferobj).
|
|
36
|
+
|
|
37
|
+
@example
|
|
38
|
+
```
|
|
39
|
+
import {isUint8Array} from 'uint8array-extras';
|
|
40
|
+
|
|
41
|
+
console.log(isUint8Array(new Uint8Array()));
|
|
42
|
+
//=> true
|
|
43
|
+
|
|
44
|
+
console.log(isUint8Array(Buffer.from('x')));
|
|
45
|
+
//=> true
|
|
46
|
+
|
|
47
|
+
console.log(isUint8Array(new ArrayBuffer(10)));
|
|
48
|
+
//=> false
|
|
49
|
+
```
|
|
50
|
+
*/
|
|
51
|
+
function isUint8Array(value) {
|
|
52
|
+
return isType(value, Uint8Array, uint8ArrayStringified);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
Throw a `TypeError` if the given value is not an instance of `Uint8Array`.
|
|
56
|
+
|
|
57
|
+
@example
|
|
58
|
+
```
|
|
59
|
+
import {assertUint8Array} from 'uint8array-extras';
|
|
60
|
+
|
|
61
|
+
try {
|
|
62
|
+
assertUint8Array(new ArrayBuffer(10)); // Throws a TypeError
|
|
63
|
+
} catch (error) {
|
|
64
|
+
console.error(error.message);
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
*/
|
|
68
|
+
function assertUint8Array(value) {
|
|
69
|
+
if (!isUint8Array(value)) {
|
|
70
|
+
throw new TypeError(`Expected \`Uint8Array\`, got \`${typeof value}\``);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
function miniTextDecoder() {
|
|
74
|
+
return {
|
|
75
|
+
decode(input) {
|
|
76
|
+
const uint8Array = input instanceof Uint8Array ? input : new Uint8Array(input);
|
|
77
|
+
let result = '';
|
|
78
|
+
for (let i = 0; i < uint8Array.length; i++) {
|
|
79
|
+
result += String.fromCharCode(uint8Array[i]);
|
|
80
|
+
}
|
|
81
|
+
return result;
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
({
|
|
86
|
+
// Use TextDecoder if available (web, Node.js), otherwise use miniTextDecoder for React Native
|
|
87
|
+
utf8: typeof globalThis.TextDecoder !== 'undefined' ? new globalThis.TextDecoder('utf8') : miniTextDecoder(),
|
|
88
|
+
});
|
|
89
|
+
function assertString(value) {
|
|
90
|
+
if (typeof value !== 'string') {
|
|
91
|
+
throw new TypeError(`Expected \`string\`, got \`${typeof value}\``);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
new globalThis.TextEncoder();
|
|
95
|
+
const byteToHexLookupTable = Array.from({ length: 256 }, (_, index) => index.toString(16).padStart(2, '0'));
|
|
96
|
+
/**
|
|
97
|
+
Convert a `Uint8Array` to a Hex string.
|
|
98
|
+
|
|
99
|
+
Replacement for [`Buffer#toString('hex')`](https://nodejs.org/api/buffer.html#buftostringencoding-start-end).
|
|
100
|
+
|
|
101
|
+
@example
|
|
102
|
+
```
|
|
103
|
+
import {uint8ArrayToHex} from 'uint8array-extras';
|
|
104
|
+
|
|
105
|
+
const byteArray = new Uint8Array([72, 101, 108, 108, 111]);
|
|
106
|
+
|
|
107
|
+
console.log(uint8ArrayToHex(byteArray));
|
|
108
|
+
//=> '48656c6c6f'
|
|
109
|
+
```
|
|
110
|
+
*/
|
|
111
|
+
function uint8ArrayToHex(array) {
|
|
112
|
+
assertUint8Array(array);
|
|
113
|
+
// Concatenating a string is faster than using an array.
|
|
114
|
+
let hexString = '';
|
|
115
|
+
// eslint-disable-next-line unicorn/no-for-loop -- Max performance is critical.
|
|
116
|
+
for (let index = 0; index < array.length; index++) {
|
|
117
|
+
hexString += byteToHexLookupTable[array[index]];
|
|
118
|
+
}
|
|
119
|
+
return hexString;
|
|
120
|
+
}
|
|
121
|
+
const hexToDecimalLookupTable = {
|
|
122
|
+
0: 0,
|
|
123
|
+
1: 1,
|
|
124
|
+
2: 2,
|
|
125
|
+
3: 3,
|
|
126
|
+
4: 4,
|
|
127
|
+
5: 5,
|
|
128
|
+
6: 6,
|
|
129
|
+
7: 7,
|
|
130
|
+
8: 8,
|
|
131
|
+
9: 9,
|
|
132
|
+
a: 10,
|
|
133
|
+
b: 11,
|
|
134
|
+
c: 12,
|
|
135
|
+
d: 13,
|
|
136
|
+
e: 14,
|
|
137
|
+
f: 15,
|
|
138
|
+
A: 10,
|
|
139
|
+
B: 11,
|
|
140
|
+
C: 12,
|
|
141
|
+
D: 13,
|
|
142
|
+
E: 14,
|
|
143
|
+
F: 15,
|
|
144
|
+
};
|
|
145
|
+
/**
|
|
146
|
+
Convert a Hex string to a `Uint8Array`.
|
|
147
|
+
|
|
148
|
+
Replacement for [`Buffer.from('48656c6c6f', 'hex')`](https://nodejs.org/api/buffer.html#static-method-bufferfromstring-encoding).
|
|
149
|
+
|
|
150
|
+
@example
|
|
151
|
+
```
|
|
152
|
+
import {hexToUint8Array} from 'uint8array-extras';
|
|
153
|
+
|
|
154
|
+
console.log(hexToUint8Array('48656c6c6f'));
|
|
155
|
+
//=> Uint8Array [72, 101, 108, 108, 111]
|
|
156
|
+
```
|
|
157
|
+
*/
|
|
158
|
+
function hexToUint8Array(hexString) {
|
|
159
|
+
assertString(hexString);
|
|
160
|
+
if (hexString.length % 2 !== 0) {
|
|
161
|
+
throw new Error('Invalid Hex string length.');
|
|
162
|
+
}
|
|
163
|
+
const resultLength = hexString.length / 2;
|
|
164
|
+
const bytes = new Uint8Array(resultLength);
|
|
165
|
+
for (let index = 0; index < resultLength; index++) {
|
|
166
|
+
const highNibble = hexToDecimalLookupTable[hexString[index * 2]];
|
|
167
|
+
const lowNibble = hexToDecimalLookupTable[hexString[(index * 2) + 1]];
|
|
168
|
+
if (highNibble === undefined || lowNibble === undefined) {
|
|
169
|
+
throw new Error(`Invalid Hex character encountered at position ${index * 2}`);
|
|
170
|
+
}
|
|
171
|
+
bytes[index] = (highNibble << 4) | lowNibble; // eslint-disable-line no-bitwise
|
|
172
|
+
}
|
|
173
|
+
return bytes;
|
|
174
|
+
}
|
|
175
|
+
// concatUint8Arrays
|
|
176
|
+
// stringToUint8Array
|
|
177
|
+
// toUint8Array
|
|
178
|
+
// hexToUint8Array
|
|
179
|
+
// uint8ArrayToHex
|
|
180
|
+
|
|
181
|
+
const partition = (s, n) => s.match(new RegExp(".{1," + n + "}", "g")) || [s];
|
|
182
|
+
const split = (s, length) => [s.slice(0, -4), s.slice(-4)];
|
|
183
|
+
const getCRCHex = (message) => crc32(message).toString(16).padStart(8, "0");
|
|
184
|
+
function assert(condition, message) {
|
|
185
|
+
if (!condition) {
|
|
186
|
+
throw new Error(message || "Assertion failed");
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const bytewords = "ableacidalsoapexaquaarchatomauntawayaxisbackbaldbarnbeltbetabiasbluebodybragbrewbulbbuzzcalmcashcatschefcityclawcodecolacookcostcruxcurlcuspcyandarkdatadaysdelidicedietdoordowndrawdropdrumdulldutyeacheasyechoedgeepicevenexamexiteyesfactfairfernfigsfilmfishfizzflapflewfluxfoxyfreefrogfuelfundgalagamegeargemsgiftgirlglowgoodgraygrimgurugushgyrohalfhanghardhawkheathelphighhillholyhopehornhutsicedideaidleinchinkyintoirisironitemjadejazzjoinjoltjowljudojugsjumpjunkjurykeepkenokeptkeyskickkilnkingkitekiwiknoblamblavalazyleaflegsliarlimplionlistlogoloudloveluaulucklungmainmanymathmazememomenumeowmildmintmissmonknailnavyneednewsnextnoonnotenumbobeyoboeomitonyxopenovalowlspaidpartpeckplaypluspoempoolposepuffpumapurrquadquizraceramprealredorichroadrockroofrubyruinrunsrustsafesagascarsetssilkskewslotsoapsolosongstubsurfswantacotasktaxitenttiedtimetinytoiltombtoystriptunatwinuglyundouniturgeuservastveryvetovialvibeviewvisavoidvowswallwandwarmwaspwavewaxywebswhatwhenwhizwolfworkyankyawnyellyogayurtzapszerozestzinczonezoom";
|
|
191
|
+
let bytewordsLookUpTable = [];
|
|
192
|
+
const BYTEWORDS_NUM = 256;
|
|
193
|
+
const BYTEWORD_LENGTH = 4;
|
|
194
|
+
const MINIMAL_BYTEWORD_LENGTH = 2;
|
|
195
|
+
var STYLES;
|
|
196
|
+
(function (STYLES) {
|
|
197
|
+
STYLES["STANDARD"] = "standard";
|
|
198
|
+
STYLES["URI"] = "uri";
|
|
199
|
+
STYLES["MINIMAL"] = "minimal";
|
|
200
|
+
})(STYLES || (STYLES = {}));
|
|
201
|
+
const getWord = (index) => {
|
|
202
|
+
return bytewords.slice(index * BYTEWORD_LENGTH, index * BYTEWORD_LENGTH + BYTEWORD_LENGTH);
|
|
203
|
+
};
|
|
204
|
+
const getMinimalWord = (index) => {
|
|
205
|
+
const byteword = getWord(index);
|
|
206
|
+
return `${byteword[0]}${byteword[BYTEWORD_LENGTH - 1]}`;
|
|
207
|
+
};
|
|
208
|
+
const addCRC = (string) => {
|
|
209
|
+
const crc = getCRCHex(hexToUint8Array(string));
|
|
210
|
+
return `${string}${crc}`;
|
|
211
|
+
};
|
|
212
|
+
const encodeWithSeparator = (word, separator, includeChecksum) => {
|
|
213
|
+
const crcAppendedWord = includeChecksum ? addCRC(word) : word;
|
|
214
|
+
const crcWordArray = hexToUint8Array(crcAppendedWord);
|
|
215
|
+
const result = crcWordArray.reduce((result, w) => [...result, getWord(w)], []);
|
|
216
|
+
return result.join(separator);
|
|
217
|
+
};
|
|
218
|
+
const encodeMinimal = (word, includeChecksum) => {
|
|
219
|
+
const crcAppendedWord = includeChecksum ? addCRC(word) : word;
|
|
220
|
+
const crcWordArray = hexToUint8Array(crcAppendedWord);
|
|
221
|
+
const result = crcWordArray.reduce((result, w) => result + getMinimalWord(w), "");
|
|
222
|
+
return result;
|
|
223
|
+
};
|
|
224
|
+
const decodeWord = (word, wordLength) => {
|
|
225
|
+
assert(word.length === wordLength, "Invalid Bytewords: word.length does not match wordLength provided");
|
|
226
|
+
const dim = 26;
|
|
227
|
+
// Since the first and last letters of each Byteword are unique,
|
|
228
|
+
// we can use them as indexes into a two-dimensional lookup table.
|
|
229
|
+
// This table is generated lazily.
|
|
230
|
+
if (bytewordsLookUpTable.length === 0) {
|
|
231
|
+
const array_len = dim * dim;
|
|
232
|
+
bytewordsLookUpTable = [...new Array(array_len)].map(() => -1);
|
|
233
|
+
for (let i = 0; i < BYTEWORDS_NUM; i++) {
|
|
234
|
+
const byteword = getWord(i);
|
|
235
|
+
let x = byteword[0].charCodeAt(0) - "a".charCodeAt(0);
|
|
236
|
+
let y = byteword[3].charCodeAt(0) - "a".charCodeAt(0);
|
|
237
|
+
let offset = y * dim + x;
|
|
238
|
+
bytewordsLookUpTable[offset] = i;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
// If the coordinates generated by the first and last letters are out of bounds,
|
|
242
|
+
// or the lookup table contains -1 at the coordinates, then the word is not valid.
|
|
243
|
+
let x = word[0].toLowerCase().charCodeAt(0) - "a".charCodeAt(0);
|
|
244
|
+
let y = word[wordLength == 4 ? 3 : 1].toLowerCase().charCodeAt(0) -
|
|
245
|
+
"a".charCodeAt(0);
|
|
246
|
+
assert(0 <= x && x < dim && 0 <= y && y < dim, "Invalid Bytewords: invalid word");
|
|
247
|
+
let offset = y * dim + x;
|
|
248
|
+
let value = bytewordsLookUpTable[offset];
|
|
249
|
+
assert(value !== -1, "Invalid Bytewords: value not in lookup table");
|
|
250
|
+
// If we're decoding a full four-letter word, verify that the two middle letters are correct.
|
|
251
|
+
if (wordLength == BYTEWORD_LENGTH) {
|
|
252
|
+
const byteword = getWord(value);
|
|
253
|
+
let c1 = word[1].toLowerCase();
|
|
254
|
+
let c2 = word[2].toLowerCase();
|
|
255
|
+
assert(c1 === byteword[1] && c2 === byteword[2], "Invalid Bytewords: invalid middle letters of word");
|
|
256
|
+
}
|
|
257
|
+
// Successful decode.
|
|
258
|
+
return value.toString(16).padStart(2, "0");
|
|
259
|
+
};
|
|
260
|
+
/**
|
|
261
|
+
* Decode a string of Bytewords into a hex string.
|
|
262
|
+
* @param string string of Bytewords. e.g. "lpamchcfatttcyclehgsdphdhgehfghkkkdl..."
|
|
263
|
+
* @param separator e.g. " " or "-" or "" or any other separator
|
|
264
|
+
* @param wordLength e.g. 4 or 2 or any other length
|
|
265
|
+
* @param includeChecksum whether to include checksum in the decoding.
|
|
266
|
+
* @returns hex string
|
|
267
|
+
*/
|
|
268
|
+
const _decode = (string, separator, wordLength, includeChecksum) => {
|
|
269
|
+
// 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", ...]
|
|
270
|
+
const words = wordLength == BYTEWORD_LENGTH
|
|
271
|
+
? string.split(separator)
|
|
272
|
+
: partition(string, 2);
|
|
273
|
+
// Decode each word. e.g. ["85", "06", "17", "19", "07", "d1", "1a", "21", "31", "4c", "2d", "58", "57", "31", "46", "59", "79", "2f", ...]
|
|
274
|
+
const decodedWords = words.map((word) => decodeWord(word, wordLength));
|
|
275
|
+
// e.g. "8506171907d11a21314c2d5857314659792f..."
|
|
276
|
+
const decodedString = decodedWords.join("");
|
|
277
|
+
if (includeChecksum) {
|
|
278
|
+
// 4 bytes for checksum, at least 1 byte for body
|
|
279
|
+
assert(decodedString.length >= 5, "Invalid Bytewords: invalid decoded string length");
|
|
280
|
+
// decoded string consists of `body` and `checksum`
|
|
281
|
+
const decodedArray = hexToUint8Array(decodedString);
|
|
282
|
+
const [body, bodyChecksum] = split(decodedArray);
|
|
283
|
+
const checksum = getCRCHex(body); // convert to hex
|
|
284
|
+
assert(checksum === uint8ArrayToHex(bodyChecksum), "Invalid Checksum");
|
|
285
|
+
return uint8ArrayToHex(body);
|
|
286
|
+
}
|
|
287
|
+
else {
|
|
288
|
+
return decodedString;
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
/**
|
|
292
|
+
* Decode a string of bytewords into a hex string.
|
|
293
|
+
* @param string string of Bytewords
|
|
294
|
+
* @param style style of Bytewords
|
|
295
|
+
* @param includeChecksum whether to include checksum in the decoding.
|
|
296
|
+
* @returns hex string
|
|
297
|
+
*/
|
|
298
|
+
const decode = (string, style = STYLES.MINIMAL, includeChecksum = true) => {
|
|
299
|
+
switch (style) {
|
|
300
|
+
case STYLES.STANDARD:
|
|
301
|
+
return _decode(string, " ", BYTEWORD_LENGTH, includeChecksum);
|
|
302
|
+
case STYLES.URI:
|
|
303
|
+
return _decode(string, "-", BYTEWORD_LENGTH, includeChecksum);
|
|
304
|
+
case STYLES.MINIMAL:
|
|
305
|
+
return _decode(string, "", MINIMAL_BYTEWORD_LENGTH, includeChecksum);
|
|
306
|
+
default:
|
|
307
|
+
throw new Error(`Invalid style ${style}`);
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
/**
|
|
311
|
+
* Encodes a string (hex representation of a buffer) into bytewords.
|
|
312
|
+
* @param string string to encode.
|
|
313
|
+
* @param style style to use for the encoding.
|
|
314
|
+
* @param includeChecksum whether to include checksum in the encoding.
|
|
315
|
+
* @returns the byteword encoded string
|
|
316
|
+
*/
|
|
317
|
+
const encode = (string, style = STYLES.MINIMAL, includeChecksum = true) => {
|
|
318
|
+
switch (style) {
|
|
319
|
+
case STYLES.STANDARD:
|
|
320
|
+
return encodeWithSeparator(string, " ", includeChecksum);
|
|
321
|
+
case STYLES.URI:
|
|
322
|
+
return encodeWithSeparator(string, "-", includeChecksum);
|
|
323
|
+
case STYLES.MINIMAL:
|
|
324
|
+
return encodeMinimal(string, includeChecksum);
|
|
325
|
+
default:
|
|
326
|
+
throw new Error(`Invalid style ${style}`);
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
var bytewords$1 = {
|
|
330
|
+
decode,
|
|
331
|
+
encode,
|
|
332
|
+
STYLES,
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
export { STYLES, decode, bytewords$1 as default, encode };
|
package/index.html
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
+
<title>Bytewords Interface</title>
|
|
7
|
+
<style>
|
|
8
|
+
body {
|
|
9
|
+
font-family: Arial, sans-serif;
|
|
10
|
+
margin: 20px;
|
|
11
|
+
}
|
|
12
|
+
.container {
|
|
13
|
+
max-width: 600px;
|
|
14
|
+
margin: auto;
|
|
15
|
+
}
|
|
16
|
+
textarea {
|
|
17
|
+
width: 100%;
|
|
18
|
+
height: 100px;
|
|
19
|
+
margin-bottom: 10px;
|
|
20
|
+
}
|
|
21
|
+
.button-container {
|
|
22
|
+
display: flex;
|
|
23
|
+
justify-content: space-between;
|
|
24
|
+
}
|
|
25
|
+
button {
|
|
26
|
+
width: 48%;
|
|
27
|
+
padding: 10px;
|
|
28
|
+
margin-bottom: 10px;
|
|
29
|
+
}
|
|
30
|
+
select {
|
|
31
|
+
width: 100%;
|
|
32
|
+
padding: 10px;
|
|
33
|
+
margin-bottom: 10px;
|
|
34
|
+
}
|
|
35
|
+
.error {
|
|
36
|
+
color: red;
|
|
37
|
+
margin-top: 10px;
|
|
38
|
+
}
|
|
39
|
+
</style>
|
|
40
|
+
</head>
|
|
41
|
+
<body>
|
|
42
|
+
<div class="container">
|
|
43
|
+
<h1>Bytewords Interface</h1>
|
|
44
|
+
<label for="hexInput">Hex Input:</label>
|
|
45
|
+
<textarea id="hexInput"></textarea>
|
|
46
|
+
<label for="encodeStyle">Encode Style:</label>
|
|
47
|
+
<select id="encodeStyle">
|
|
48
|
+
<option value="minimal">Minimal</option>
|
|
49
|
+
<option value="standard">Standard</option>
|
|
50
|
+
<option value="uri">URI</option>
|
|
51
|
+
</select>
|
|
52
|
+
<label for="includeChecksum">
|
|
53
|
+
<input type="checkbox" id="includeChecksum" checked> Include Checksum
|
|
54
|
+
</label>
|
|
55
|
+
<div class="button-container">
|
|
56
|
+
<button id="encodeButton">Encode to Bytewords</button>
|
|
57
|
+
<button id="decodeButton">Decode to Hex</button>
|
|
58
|
+
</div>
|
|
59
|
+
<label for="bytewordsOutput">Bytewords Output:</label>
|
|
60
|
+
<textarea id="bytewordsOutput"></textarea>
|
|
61
|
+
<div id="error" class="error"></div>
|
|
62
|
+
</div>
|
|
63
|
+
<script type="module">
|
|
64
|
+
import { encode, decode, STYLES } from './dist/web/bytewords.js';
|
|
65
|
+
|
|
66
|
+
const showError = (message) => {
|
|
67
|
+
const errorDiv = document.getElementById('error');
|
|
68
|
+
errorDiv.textContent = message;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
document.getElementById('encodeButton').addEventListener('click', () => {
|
|
72
|
+
try {
|
|
73
|
+
const hexInput = document.getElementById('hexInput').value.trim();
|
|
74
|
+
const encodeStyle = document.getElementById('encodeStyle').value;
|
|
75
|
+
const includeChecksum = document.getElementById('includeChecksum').checked;
|
|
76
|
+
const bytewordsOutput = encode(hexInput, STYLES[encodeStyle.toUpperCase()], includeChecksum);
|
|
77
|
+
document.getElementById('bytewordsOutput').value = bytewordsOutput;
|
|
78
|
+
showError(''); // Clear any previous error
|
|
79
|
+
} catch (error) {
|
|
80
|
+
showError(`Encoding Error: ${error.message}`);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
document.getElementById('decodeButton').addEventListener('click', () => {
|
|
85
|
+
try {
|
|
86
|
+
const bytewordsInput = document.getElementById('bytewordsOutput').value;
|
|
87
|
+
const decodeStyle = document.getElementById('encodeStyle').value;
|
|
88
|
+
const includeChecksum = document.getElementById('includeChecksum').checked;
|
|
89
|
+
const hexOutput = decode(bytewordsInput, STYLES[decodeStyle.toUpperCase()], includeChecksum);
|
|
90
|
+
document.getElementById('hexInput').value = hexOutput;
|
|
91
|
+
showError(''); // Clear any previous error
|
|
92
|
+
} catch (error) {
|
|
93
|
+
showError(`Decoding Error: ${error.message}`);
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
</script>
|
|
97
|
+
</body>
|
|
98
|
+
</html>
|
package/package.json
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@qrkit/bc-ur",
|
|
3
|
+
"version": "2.0.0-beta.9-qrkit.1",
|
|
4
|
+
"author": "Irfan Bilaloğlu <irfanbilaloglu@gmail.com>, Pieter Uyttersprot <pieter.uyttersprot@ngrave.io>, Antonis Poulakis <antwnic4@gmail.com>",
|
|
5
|
+
"contributors": [
|
|
6
|
+
"Mladen Milankovic <developer@mmlado.com>"
|
|
7
|
+
],
|
|
8
|
+
"description": "Browser-native JavaScript implementation of Blockchain Commons UR for Node.js, browsers, and React Native",
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"directories": {
|
|
11
|
+
"lib": "src",
|
|
12
|
+
"test": "tests"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"src",
|
|
16
|
+
"dist",
|
|
17
|
+
"index.html"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"clean": "rimraf dist && rimraf tsconfig.tsbuildinfo",
|
|
21
|
+
"build": "yarn clean && yarn compile && node ./scripts/post.build.js",
|
|
22
|
+
"compile": "tshy && rollup -c && \\cp -f ./src/wrappers/cbor2Wrapper.ts ./dist/commonjs/wrappers/cbor2Wrapper.d.ts",
|
|
23
|
+
"test": "node --experimental-vm-modules node_modules/.bin/jest",
|
|
24
|
+
"prepublishOnly": "yarn build",
|
|
25
|
+
"lint": "eslint \"./src/**/*.{ts,tsx}\" --max-warnings=0",
|
|
26
|
+
"start": "live-server --watch=src,dist --port=8080"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@bacons/text-decoder": "0.0.0",
|
|
30
|
+
"cbor2": "^1.8.0"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@amanda-mitchell/semantic-release-npm-multiple": "^3.17.0",
|
|
34
|
+
"@rollup/plugin-commonjs": "^28.0.2",
|
|
35
|
+
"@rollup/plugin-node-resolve": "^16.0.0",
|
|
36
|
+
"@types/jest": "^29.5.13",
|
|
37
|
+
"@types/node": "^22.7.4",
|
|
38
|
+
"jest": "^29.7.0",
|
|
39
|
+
"live-server": "^1.2.1",
|
|
40
|
+
"rimraf": "^6.0.1",
|
|
41
|
+
"rollup": "^4.28.1",
|
|
42
|
+
"rollup-plugin-node-polyfills": "^0.2.1",
|
|
43
|
+
"ts-jest": "^29.2.5",
|
|
44
|
+
"tshy": "^3.0.2",
|
|
45
|
+
"tslib": "^2.8.1",
|
|
46
|
+
"typescript": "5.6.2"
|
|
47
|
+
},
|
|
48
|
+
"repository": {
|
|
49
|
+
"type": "git",
|
|
50
|
+
"url": "git+https://github.com/ngraveio/bc-ur.git"
|
|
51
|
+
},
|
|
52
|
+
"keywords": [
|
|
53
|
+
"bc-ur"
|
|
54
|
+
],
|
|
55
|
+
"bugs": {
|
|
56
|
+
"url": "https://github.com/ngraveio/bc-ur/issues"
|
|
57
|
+
},
|
|
58
|
+
"homepage": "https://github.com/ngraveio/bc-ur#readme",
|
|
59
|
+
"tshy": {
|
|
60
|
+
"exports": {
|
|
61
|
+
"./package.json": "./package.json",
|
|
62
|
+
".": "./src/index.ts"
|
|
63
|
+
},
|
|
64
|
+
"include": [
|
|
65
|
+
"./src/**/*.ts",
|
|
66
|
+
"./src/**/*.cts",
|
|
67
|
+
"./src/**/*.mts",
|
|
68
|
+
"./src/**/*.tsx",
|
|
69
|
+
"./src/**/*.json"
|
|
70
|
+
]
|
|
71
|
+
},
|
|
72
|
+
"type": "module",
|
|
73
|
+
"types": "./dist/commonjs/index.d.ts",
|
|
74
|
+
"exports": {
|
|
75
|
+
"./package.json": "./package.json",
|
|
76
|
+
".": {
|
|
77
|
+
"react-native": {
|
|
78
|
+
"types": "./dist/commonjs/index-react-native.d.ts",
|
|
79
|
+
"default": "./dist/commonjs/index-react-native.js"
|
|
80
|
+
},
|
|
81
|
+
"import": {
|
|
82
|
+
"types": "./dist/esm/index.d.ts",
|
|
83
|
+
"default": "./dist/esm/index.js"
|
|
84
|
+
},
|
|
85
|
+
"require": {
|
|
86
|
+
"types": "./dist/commonjs/index.d.ts",
|
|
87
|
+
"default": "./dist/commonjs/index.js"
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
"main": "./dist/commonjs/index.js",
|
|
92
|
+
"react-native": "./dist/commonjs/index-react-native.js",
|
|
93
|
+
"module": "./dist/esm/index.js"
|
|
94
|
+
}
|