@twin.org/core 0.0.1-next.8 → 0.0.1-next.9

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.
@@ -900,6 +900,127 @@ class Base32 {
900
900
  }
901
901
  }
902
902
 
903
+ /**
904
+ * Class to help with base58 Encoding/Decoding.
905
+ */
906
+ class Base58 {
907
+ /**
908
+ * Runtime name for the class.
909
+ * @internal
910
+ */
911
+ static _CLASS_NAME = "Base58";
912
+ /**
913
+ * Alphabet table for encoding.
914
+ * @internal
915
+ */
916
+ static _ALPHABET =
917
+ // cspell:disable-next-line
918
+ "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
919
+ /**
920
+ * Reverse map for decoding.
921
+ * @internal
922
+ */
923
+ static _ALPHABET_REVERSE = [
924
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
925
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
926
+ -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, -1, -1, -1, -1, -1, -1, -1, 9, 10, 11, 12, 13, 14, 15, 16, -1,
927
+ 17, 18, 19, 20, 21, -1, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, -1, -1, -1, -1, -1, -1, 33,
928
+ 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, -1, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
929
+ 57, -1, -1, -1, -1, -1
930
+ ];
931
+ /**
932
+ * Convert the base 58 string to a byte array.
933
+ * @param base58 The base58 string to convert.
934
+ * @returns The byte array.
935
+ * @throws If the input string contains a character not in the Base58 alphabet.
936
+ */
937
+ static decode(base58) {
938
+ let zeroes = 0;
939
+ for (let i = 0; i < base58.length; i++) {
940
+ if (base58[i] !== "1") {
941
+ break;
942
+ }
943
+ zeroes += 1;
944
+ }
945
+ const size = Math.trunc((base58.length * 733) / 1000) + 1;
946
+ const b256 = new Uint8Array(size).fill(0);
947
+ let length = 0;
948
+ for (let i = zeroes; i < base58.length; i++) {
949
+ const ch = base58.charCodeAt(i);
950
+ if (ch & 0xff80) {
951
+ throw new GeneralError(Base58._CLASS_NAME, "invalidCharacter", { invalidCharacter: ch });
952
+ }
953
+ const val = Base58._ALPHABET_REVERSE[ch];
954
+ if (val === -1) {
955
+ throw new GeneralError(Base58._CLASS_NAME, "invalidCharacter", { invalidCharacter: ch });
956
+ }
957
+ let carry = val;
958
+ let j = 0;
959
+ for (let k = size - 1; k >= 0; k--, j++) {
960
+ if (carry === 0 && j >= length) {
961
+ break;
962
+ }
963
+ carry += b256[k] * 58;
964
+ b256[k] = carry;
965
+ carry >>>= 8;
966
+ }
967
+ length = j;
968
+ }
969
+ const out = new Uint8Array(zeroes + length);
970
+ let j;
971
+ for (j = 0; j < zeroes; j++) {
972
+ out[j] = 0;
973
+ }
974
+ let i = size - length;
975
+ while (i < size) {
976
+ out[j++] = b256[i++];
977
+ }
978
+ return out;
979
+ }
980
+ /**
981
+ * Convert a byte array to base 58.
982
+ * @param bytes The byte array to encode.
983
+ * @returns The data as base58 string.
984
+ */
985
+ static encode(bytes) {
986
+ let zeroes = 0;
987
+ for (let i = 0; i < bytes.length; i++) {
988
+ if (bytes[i] !== 0) {
989
+ break;
990
+ }
991
+ zeroes += 1;
992
+ }
993
+ const size = Math.trunc(((bytes.length - zeroes) * 138) / 100) + 1;
994
+ const b58 = new Uint8Array(size).fill(0);
995
+ let length = 0;
996
+ for (let i = zeroes; i < bytes.length; i++) {
997
+ let carry = bytes[i];
998
+ let j = 0;
999
+ for (let k = size - 1; k >= 0; k--, j++) {
1000
+ if (carry === 0 && j >= length) {
1001
+ break;
1002
+ }
1003
+ carry += b58[k] * 256;
1004
+ b58[k] = carry % 58;
1005
+ carry = Math.trunc(carry / 58);
1006
+ }
1007
+ length = j;
1008
+ }
1009
+ let i = size - length;
1010
+ while (i < size && b58[i] === 0) {
1011
+ i += 1;
1012
+ }
1013
+ let str = "";
1014
+ for (let j = 0; j < zeroes; j++) {
1015
+ str += "1";
1016
+ }
1017
+ while (i < size) {
1018
+ str += Base58._ALPHABET[b58[i++]];
1019
+ }
1020
+ return str;
1021
+ }
1022
+ }
1023
+
903
1024
  // Copyright 2024 IOTA Stiftung.
904
1025
  // SPDX-License-Identifier: Apache-2.0.
905
1026
  /* eslint-disable no-bitwise */
@@ -2661,6 +2782,22 @@ class Converter {
2661
2782
  static base64UrlToBytes(base64Url) {
2662
2783
  return Base64Url.decode(base64Url);
2663
2784
  }
2785
+ /**
2786
+ * Convert bytes to base58 string.
2787
+ * @param bytes The bytes to convert.
2788
+ * @returns A base58 string of the bytes.
2789
+ */
2790
+ static bytesToBase58(bytes) {
2791
+ return Base58.encode(bytes);
2792
+ }
2793
+ /**
2794
+ * Convert a base58 string to bytes.
2795
+ * @param base58 The base58 string.
2796
+ * @returns The bytes.
2797
+ */
2798
+ static base58ToBytes(base58) {
2799
+ return Base58.decode(base58);
2800
+ }
2664
2801
  /**
2665
2802
  * Build the static lookup tables.
2666
2803
  * @internal
@@ -4207,6 +4344,7 @@ exports.AlreadyExistsError = AlreadyExistsError;
4207
4344
  exports.ArrayHelper = ArrayHelper;
4208
4345
  exports.AsyncCache = AsyncCache;
4209
4346
  exports.Base32 = Base32;
4347
+ exports.Base58 = Base58;
4210
4348
  exports.Base64 = Base64;
4211
4349
  exports.Base64Url = Base64Url;
4212
4350
  exports.BaseError = BaseError;
@@ -898,6 +898,127 @@ class Base32 {
898
898
  }
899
899
  }
900
900
 
901
+ /**
902
+ * Class to help with base58 Encoding/Decoding.
903
+ */
904
+ class Base58 {
905
+ /**
906
+ * Runtime name for the class.
907
+ * @internal
908
+ */
909
+ static _CLASS_NAME = "Base58";
910
+ /**
911
+ * Alphabet table for encoding.
912
+ * @internal
913
+ */
914
+ static _ALPHABET =
915
+ // cspell:disable-next-line
916
+ "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
917
+ /**
918
+ * Reverse map for decoding.
919
+ * @internal
920
+ */
921
+ static _ALPHABET_REVERSE = [
922
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
923
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
924
+ -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, -1, -1, -1, -1, -1, -1, -1, 9, 10, 11, 12, 13, 14, 15, 16, -1,
925
+ 17, 18, 19, 20, 21, -1, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, -1, -1, -1, -1, -1, -1, 33,
926
+ 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, -1, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
927
+ 57, -1, -1, -1, -1, -1
928
+ ];
929
+ /**
930
+ * Convert the base 58 string to a byte array.
931
+ * @param base58 The base58 string to convert.
932
+ * @returns The byte array.
933
+ * @throws If the input string contains a character not in the Base58 alphabet.
934
+ */
935
+ static decode(base58) {
936
+ let zeroes = 0;
937
+ for (let i = 0; i < base58.length; i++) {
938
+ if (base58[i] !== "1") {
939
+ break;
940
+ }
941
+ zeroes += 1;
942
+ }
943
+ const size = Math.trunc((base58.length * 733) / 1000) + 1;
944
+ const b256 = new Uint8Array(size).fill(0);
945
+ let length = 0;
946
+ for (let i = zeroes; i < base58.length; i++) {
947
+ const ch = base58.charCodeAt(i);
948
+ if (ch & 0xff80) {
949
+ throw new GeneralError(Base58._CLASS_NAME, "invalidCharacter", { invalidCharacter: ch });
950
+ }
951
+ const val = Base58._ALPHABET_REVERSE[ch];
952
+ if (val === -1) {
953
+ throw new GeneralError(Base58._CLASS_NAME, "invalidCharacter", { invalidCharacter: ch });
954
+ }
955
+ let carry = val;
956
+ let j = 0;
957
+ for (let k = size - 1; k >= 0; k--, j++) {
958
+ if (carry === 0 && j >= length) {
959
+ break;
960
+ }
961
+ carry += b256[k] * 58;
962
+ b256[k] = carry;
963
+ carry >>>= 8;
964
+ }
965
+ length = j;
966
+ }
967
+ const out = new Uint8Array(zeroes + length);
968
+ let j;
969
+ for (j = 0; j < zeroes; j++) {
970
+ out[j] = 0;
971
+ }
972
+ let i = size - length;
973
+ while (i < size) {
974
+ out[j++] = b256[i++];
975
+ }
976
+ return out;
977
+ }
978
+ /**
979
+ * Convert a byte array to base 58.
980
+ * @param bytes The byte array to encode.
981
+ * @returns The data as base58 string.
982
+ */
983
+ static encode(bytes) {
984
+ let zeroes = 0;
985
+ for (let i = 0; i < bytes.length; i++) {
986
+ if (bytes[i] !== 0) {
987
+ break;
988
+ }
989
+ zeroes += 1;
990
+ }
991
+ const size = Math.trunc(((bytes.length - zeroes) * 138) / 100) + 1;
992
+ const b58 = new Uint8Array(size).fill(0);
993
+ let length = 0;
994
+ for (let i = zeroes; i < bytes.length; i++) {
995
+ let carry = bytes[i];
996
+ let j = 0;
997
+ for (let k = size - 1; k >= 0; k--, j++) {
998
+ if (carry === 0 && j >= length) {
999
+ break;
1000
+ }
1001
+ carry += b58[k] * 256;
1002
+ b58[k] = carry % 58;
1003
+ carry = Math.trunc(carry / 58);
1004
+ }
1005
+ length = j;
1006
+ }
1007
+ let i = size - length;
1008
+ while (i < size && b58[i] === 0) {
1009
+ i += 1;
1010
+ }
1011
+ let str = "";
1012
+ for (let j = 0; j < zeroes; j++) {
1013
+ str += "1";
1014
+ }
1015
+ while (i < size) {
1016
+ str += Base58._ALPHABET[b58[i++]];
1017
+ }
1018
+ return str;
1019
+ }
1020
+ }
1021
+
901
1022
  // Copyright 2024 IOTA Stiftung.
902
1023
  // SPDX-License-Identifier: Apache-2.0.
903
1024
  /* eslint-disable no-bitwise */
@@ -2659,6 +2780,22 @@ class Converter {
2659
2780
  static base64UrlToBytes(base64Url) {
2660
2781
  return Base64Url.decode(base64Url);
2661
2782
  }
2783
+ /**
2784
+ * Convert bytes to base58 string.
2785
+ * @param bytes The bytes to convert.
2786
+ * @returns A base58 string of the bytes.
2787
+ */
2788
+ static bytesToBase58(bytes) {
2789
+ return Base58.encode(bytes);
2790
+ }
2791
+ /**
2792
+ * Convert a base58 string to bytes.
2793
+ * @param base58 The base58 string.
2794
+ * @returns The bytes.
2795
+ */
2796
+ static base58ToBytes(base58) {
2797
+ return Base58.decode(base58);
2798
+ }
2662
2799
  /**
2663
2800
  * Build the static lookup tables.
2664
2801
  * @internal
@@ -4201,4 +4338,4 @@ class Validation {
4201
4338
  }
4202
4339
  }
4203
4340
 
4204
- export { AlreadyExistsError, ArrayHelper, AsyncCache, Base32, Base64, Base64Url, BaseError, BitString, Coerce, ComponentFactory, Compression, CompressionType, ConflictError, Converter, ErrorHelper, Factory, FilenameHelper, GeneralError, GuardError, Guards, HexHelper, I18n, Is, JsonHelper, NotFoundError, NotImplementedError, NotSupportedError, ObjectHelper, RandomHelper, StringHelper, UnauthorizedError, UnprocessableError, Url, Urn, Validation, ValidationError };
4341
+ export { AlreadyExistsError, ArrayHelper, AsyncCache, Base32, Base58, Base64, Base64Url, BaseError, BitString, Coerce, ComponentFactory, Compression, CompressionType, ConflictError, Converter, ErrorHelper, Factory, FilenameHelper, GeneralError, GuardError, Guards, HexHelper, I18n, Is, JsonHelper, NotFoundError, NotImplementedError, NotSupportedError, ObjectHelper, RandomHelper, StringHelper, UnauthorizedError, UnprocessableError, Url, Urn, Validation, ValidationError };
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Class to help with base58 Encoding/Decoding.
3
+ */
4
+ export declare class Base58 {
5
+ /**
6
+ * Convert the base 58 string to a byte array.
7
+ * @param base58 The base58 string to convert.
8
+ * @returns The byte array.
9
+ * @throws If the input string contains a character not in the Base58 alphabet.
10
+ */
11
+ static decode(base58: string): Uint8Array;
12
+ /**
13
+ * Convert a byte array to base 58.
14
+ * @param bytes The byte array to encode.
15
+ * @returns The data as base58 string.
16
+ */
17
+ static encode(bytes: Uint8Array): string;
18
+ }
@@ -1,4 +1,5 @@
1
1
  export * from "./encoding/base32";
2
+ export * from "./encoding/base58";
2
3
  export * from "./encoding/base64";
3
4
  export * from "./encoding/base64Url";
4
5
  export * from "./errors/alreadyExistsError";
@@ -82,4 +82,16 @@ export declare class Converter {
82
82
  * @returns The bytes.
83
83
  */
84
84
  static base64UrlToBytes(base64Url: string): Uint8Array;
85
+ /**
86
+ * Convert bytes to base58 string.
87
+ * @param bytes The bytes to convert.
88
+ * @returns A base58 string of the bytes.
89
+ */
90
+ static bytesToBase58(bytes: Uint8Array): string;
91
+ /**
92
+ * Convert a base58 string to bytes.
93
+ * @param base58 The base58 string.
94
+ * @returns The bytes.
95
+ */
96
+ static base58ToBytes(base58: string): Uint8Array;
85
97
  }
package/docs/changelog.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # @twin.org/core - Changelog
2
2
 
3
- ## 0.0.1-next.8
3
+ ## 0.0.1-next.9
4
4
 
5
5
  - Initial Release
@@ -0,0 +1,57 @@
1
+ # Class: Base58
2
+
3
+ Class to help with base58 Encoding/Decoding.
4
+
5
+ ## Constructors
6
+
7
+ ### new Base58()
8
+
9
+ > **new Base58**(): [`Base58`](Base58.md)
10
+
11
+ #### Returns
12
+
13
+ [`Base58`](Base58.md)
14
+
15
+ ## Methods
16
+
17
+ ### decode()
18
+
19
+ > `static` **decode**(`base58`): `Uint8Array`
20
+
21
+ Convert the base 58 string to a byte array.
22
+
23
+ #### Parameters
24
+
25
+ • **base58**: `string`
26
+
27
+ The base58 string to convert.
28
+
29
+ #### Returns
30
+
31
+ `Uint8Array`
32
+
33
+ The byte array.
34
+
35
+ #### Throws
36
+
37
+ If the input string contains a character not in the Base58 alphabet.
38
+
39
+ ***
40
+
41
+ ### encode()
42
+
43
+ > `static` **encode**(`bytes`): `string`
44
+
45
+ Convert a byte array to base 58.
46
+
47
+ #### Parameters
48
+
49
+ • **bytes**: `Uint8Array`
50
+
51
+ The byte array to encode.
52
+
53
+ #### Returns
54
+
55
+ `string`
56
+
57
+ The data as base58 string.
@@ -283,3 +283,43 @@ The base64 url string.
283
283
  `Uint8Array`
284
284
 
285
285
  The bytes.
286
+
287
+ ***
288
+
289
+ ### bytesToBase58()
290
+
291
+ > `static` **bytesToBase58**(`bytes`): `string`
292
+
293
+ Convert bytes to base58 string.
294
+
295
+ #### Parameters
296
+
297
+ • **bytes**: `Uint8Array`
298
+
299
+ The bytes to convert.
300
+
301
+ #### Returns
302
+
303
+ `string`
304
+
305
+ A base58 string of the bytes.
306
+
307
+ ***
308
+
309
+ ### base58ToBytes()
310
+
311
+ > `static` **base58ToBytes**(`base58`): `Uint8Array`
312
+
313
+ Convert a base58 string to bytes.
314
+
315
+ #### Parameters
316
+
317
+ • **base58**: `string`
318
+
319
+ The base58 string.
320
+
321
+ #### Returns
322
+
323
+ `Uint8Array`
324
+
325
+ The bytes.
@@ -3,6 +3,7 @@
3
3
  ## Classes
4
4
 
5
5
  - [Base32](classes/Base32.md)
6
+ - [Base58](classes/Base58.md)
6
7
  - [Base64](classes/Base64.md)
7
8
  - [Base64Url](classes/Base64Url.md)
8
9
  - [AlreadyExistsError](classes/AlreadyExistsError.md)
package/locales/en.json CHANGED
@@ -78,6 +78,15 @@
78
78
  },
79
79
  "bitString": {
80
80
  "outOfRange": "The index should be >= 0 and less than the length of the bit string"
81
+ },
82
+ "base32": {
83
+ "invalidCharacter": "Data contains a character \"{invalidCharacter}\" which is not in the charset"
84
+ },
85
+ "base64": {
86
+ "length4Multiple": "Invalid length should be a multiple of 4, it is \"{value}\""
87
+ },
88
+ "base58": {
89
+ "invalidCharacter": "Data contains a character \"{invalidCharacter}\" which is not in the charset"
81
90
  }
82
91
  },
83
92
  "errorNames": {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/core",
3
- "version": "0.0.1-next.8",
3
+ "version": "0.0.1-next.9",
4
4
  "description": "Helper methods/classes for data type checking/validation/guarding/error handling",
5
5
  "repository": {
6
6
  "type": "git",