@hive-p2p/server 1.0.78 → 1.0.80

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hive-p2p/server",
3
- "version": "1.0.78",
3
+ "version": "1.0.80",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -8,16 +8,32 @@ export class Converter {
8
8
  textDecoder = new TextDecoder();
9
9
  buffer2 = new ArrayBuffer(2); view2 = new DataView(this.buffer2);
10
10
  buffer4 = new ArrayBuffer(4); view4 = new DataView(this.buffer4);
11
+ buffer6 = new ArrayBuffer(6); view6 = new DataView(this.buffer6);
11
12
  buffer8 = new ArrayBuffer(8); view8 = new DataView(this.buffer8);
12
13
  hexMap = { '0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15, 'a': 10, 'b': 11, 'c': 12, 'd': 13, 'e': 14, 'f': 15 };
13
14
 
14
15
  // ... TO BYTES
15
16
  /** Number should be between 0 and 65535 @param {number} num - Integer to convert to 2 bytes Uint8Array */
16
- numberTo2Bytes(num) { this.view2.setUint16(0, num, true); return new Uint8Array(this.buffer2); }
17
+ numberTo2Bytes(num) {
18
+ this.view2.setUint16(0, num, true);
19
+ return new Uint8Array(this.buffer2);
20
+ }
17
21
  /** Number should be between 0 and 4294967295 @param {number} num - Integer to convert to 4 bytes Uint8Array */
18
- numberTo4Bytes(num) { this.view4.setUint32(0, num, true); return new Uint8Array(this.buffer4); }
22
+ numberTo4Bytes(num) {
23
+ this.view4.setUint32(0, num, true);
24
+ return new Uint8Array(this.buffer4);
25
+ }
26
+ /** Number should be between 0 and 281474976710655 (2^48-1) @param {number} num - Integer to convert to 6 bytes Uint8Array */
27
+ numberTo6Bytes(num) {
28
+ this.view6.setUint32(0, num % 0x100000000, true); // lower 4 bytes
29
+ this.view6.setUint16(4, Math.floor(num / 0x100000000), true); // upper 2 bytes
30
+ return new Uint8Array(this.buffer6);
31
+ }
19
32
  /** Number should be between 0 and 18446744073709551615 @param {number} num - Integer to convert to 8 bytes Uint8Array */
20
- numberTo8Bytes(num) { this.view8.setBigUint64(0, BigInt(num), true); return new Uint8Array(this.buffer8); }
33
+ numberTo8Bytes(num) {
34
+ this.view8.setBigUint64(0, BigInt(num), true);
35
+ return new Uint8Array(this.buffer8);
36
+ }
21
37
  stringToBytes(str = 'toto') { return this.textEncoder.encode(str); }
22
38
  /** @param {string} hex - Hex string to convert to Uint8Array */
23
39
  hexToBytes(hex) {
@@ -38,11 +54,25 @@ export class Converter {
38
54
 
39
55
  // BYTES TO ...
40
56
  /** @param {Uint8Array} uint8Array - Uint8Array to convert to number */
41
- bytes2ToNumber(uint8Array) { for (let i = 0; i < 2; i++) this.view2.setUint8(i, uint8Array[i]); return this.view2.getUint16(0, true); }
57
+ bytes2ToNumber(uint8Array) {
58
+ for (let i = 0; i < 2; i++) this.view2.setUint8(i, uint8Array[i]);
59
+ return this.view2.getUint16(0, true);
60
+ }
61
+ /** @param {Uint8Array} uint8Array - Uint8Array to convert to number */
62
+ bytes4ToNumber(uint8Array) {
63
+ for (let i = 0; i < 4; i++) this.view4.setUint8(i, uint8Array[i]);
64
+ return this.view4.getUint32(0, true);
65
+ }
42
66
  /** @param {Uint8Array} uint8Array - Uint8Array to convert to number */
43
- bytes4ToNumber(uint8Array) { for (let i = 0; i < 4; i++) this.view4.setUint8(i, uint8Array[i]); return this.view4.getUint32(0, true); }
67
+ bytes6ToNumber(uint8Array) {
68
+ for (let i = 0; i < 6; i++) this.view6.setUint8(i, uint8Array[i]);
69
+ return this.view6.getUint32(0, true) + this.view6.getUint16(4, true) * 0x100000000;
70
+ }
44
71
  /** @param {Uint8Array} uint8Array - Uint8Array to convert to number */
45
- bytes8ToNumber(uint8Array) { for (let i = 0; i < 8; i++) this.view8.setUint8(i, uint8Array[i]); return Number(this.view8.getBigUint64(0, true)); }
72
+ bytes8ToNumber(uint8Array) {
73
+ for (let i = 0; i < 8; i++) this.view8.setUint8(i, uint8Array[i]);
74
+ return Number(this.view8.getBigUint64(0, true));
75
+ }
46
76
  /** @param {Uint8Array} uint8Array - Uint8Array to convert to string */
47
77
  bytesToString(uint8Array) { return this.textDecoder.decode(uint8Array); }
48
78
  /** @param {Uint8Array} uint8Array - Uint8Array to convert to string */
@@ -54,46 +84,25 @@ export class Converter {
54
84
  }
55
85
  /** @param {Uint8Array} uint8Array - Uint8Array to convert to string */
56
86
  bytesToHex(uint8Array, minLength = 0) { return Converter.bytesToHex(uint8Array, minLength); }
57
-
58
- // BASE58
59
- /** @param {string} hex - Hex string to convert to base58 */
60
- hexToBase58(hex) {
61
- const num = Converter.hexToBigInt(hex);
62
- let base58 = '';
63
- let n = num;
64
- while (n > 0) {
65
- const remainder = n % BigInt(58);
66
- base58 = base58AlphabetArray[Number(remainder)] + base58;
67
- n = n / BigInt(58);
68
- }
69
87
 
70
- //const bytes = isNode ? Buffer.from(base58) : new TextEncoder().encode(base58);
71
- const bytes = this.textEncoder.encode(base58);
72
- for (let i = 0; i < bytes.length && bytes[i] === 0; i++) base58 = '1' + base58;
73
-
74
- return base58;
75
- }
76
- /** @param {string} base58 - Base58 string to convert to Hex */
77
- base58ToHex(base58, minLength = 0) {
78
- let num = BigInt(0);
79
- const base = BigInt(58);
80
- for (const char of base58) num = num * base + BigInt(base58Alphabet[char]);
81
-
82
- let hex = num.toString(16);
83
- if (hex.length % 2 !== 0) hex = '0' + hex;
84
- if (minLength > 0) hex = hex.padStart(minLength, '0');
85
- return hex;
86
- }
87
- /** @param {string} addressBase58 - Base58 address to convert to Uint8Array(16 bytes) */
88
- addressBase58ToBytes(addressBase58) {
89
- const hex = this.base58ToHex(addressBase58, 32);
90
- return this.hexToBytes(hex);
91
- }
92
- /** @param {Uint8Array} addressUint8Array - Uint8Array(16 bytes) to convert to base58 */
93
- addressBytesToBase58(addressUint8Array) {
94
- const hex = this.bytesToHex(addressUint8Array, 32);
95
- return this.hexToBase58(hex);
96
- }
88
+ // BASE58
89
+ /** @param {number} num - Unsigned integer 4bytes to convert to Base58 string @param {number} [minLength] - Minimum length of the output string, padded with '1', default: 6 */
90
+ static uint32ToB58(num, minLength = 6) {
91
+ if (num === 0) return '1';
92
+
93
+ let result = '';
94
+ while (num > 0) {
95
+ result = base58AlphabetArray[num % 58] + result;
96
+ num = Math.floor(num / 58);
97
+ }
98
+ return result.padStart(minLength, '1');
99
+ }
100
+ /** @param {string} b58 - Base58 string to convert to uint32 */
101
+ static b58ToUint32(b58) {
102
+ let result = 0;
103
+ for (const char of b58) result = result * 58 + base58Alphabet[char];
104
+ return result;
105
+ }
97
106
 
98
107
  // OTHERS
99
108
  /** @param {string} hex - Hex string to convert to BigInt */