@hive-p2p/server 1.0.72 → 1.0.74
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 +1 -1
- package/services/converter.mjs +46 -0
- package/services/cryptos.mjs +0 -1
package/package.json
CHANGED
package/services/converter.mjs
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
const base58Alphabet = { '1': 0, '2': 1, '3': 2, '4': 3, '5': 4, '6': 5, '7': 6, '8': 7, '9': 8, 'A': 9, 'B': 10, 'C': 11, 'D': 12, 'E': 13, 'F': 14, 'G': 15, 'H': 16, 'J': 17, 'K': 18, 'L': 19, 'M': 20, 'N': 21, 'P': 22, 'Q': 23, 'R': 24, 'S': 25, 'T': 26, 'U': 27, 'V': 28, 'W': 29, 'X': 30, 'Y': 31, 'Z': 32, 'a': 33, 'b': 34, 'c': 35, 'd': 36, 'e': 37, 'f': 38, 'g': 39, 'h': 40, 'i': 41, 'j': 42, 'k': 43, 'm': 44, 'n': 45, 'o': 46, 'p': 47, 'q': 48, 'r': 49, 's': 50, 't': 51, 'u': 52, 'v': 53, 'w': 54, 'x': 55, 'y': 56, 'z': 57 };
|
|
2
|
+
const base58AlphabetArray = [ '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' ];
|
|
3
|
+
|
|
1
4
|
export class Converter {
|
|
2
5
|
IS_BROWSER = typeof window !== 'undefined';
|
|
3
6
|
FROMBASE64_AVAILABLE = typeof Uint8Array.fromBase64 === 'function';
|
|
@@ -32,6 +35,7 @@ export class Converter {
|
|
|
32
35
|
for (let i = 0; i < binaryString.length; i++) bytes[i] = binaryString.charCodeAt(i);
|
|
33
36
|
return bytes;
|
|
34
37
|
}
|
|
38
|
+
|
|
35
39
|
// BYTES TO ...
|
|
36
40
|
/** @param {Uint8Array} uint8Array - Uint8Array to convert to number */
|
|
37
41
|
bytes2ToNumber(uint8Array) { for (let i = 0; i < 2; i++) this.view2.setUint8(i, uint8Array[i]); return this.view2.getUint16(0, true); }
|
|
@@ -50,7 +54,49 @@ export class Converter {
|
|
|
50
54
|
}
|
|
51
55
|
/** @param {Uint8Array} uint8Array - Uint8Array to convert to string */
|
|
52
56
|
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
|
+
|
|
70
|
+
const bytes = isNode ? Buffer.from(base58) : new TextEncoder().encode(base58);
|
|
71
|
+
for (let i = 0; i < bytes.length && bytes[i] === 0; i++) base58 = '1' + base58;
|
|
72
|
+
|
|
73
|
+
return base58;
|
|
74
|
+
}
|
|
75
|
+
/** @param {string} base58 - Base58 string to convert to Hex */
|
|
76
|
+
base58ToHex(base58, minLength = 0) {
|
|
77
|
+
let num = BigInt(0);
|
|
78
|
+
const base = BigInt(58);
|
|
79
|
+
for (const char of base58) num = num * base + BigInt(base58Alphabet[char]);
|
|
80
|
+
|
|
81
|
+
let hex = num.toString(16);
|
|
82
|
+
if (hex.length % 2 !== 0) hex = '0' + hex;
|
|
83
|
+
if (minLength > 0) hex = hex.padStart(minLength, '0');
|
|
84
|
+
return hex;
|
|
85
|
+
}
|
|
86
|
+
/** @param {string} addressBase58 - Base58 address to convert to Uint8Array(16 bytes) */
|
|
87
|
+
addressBase58ToBytes(addressBase58) {
|
|
88
|
+
const hex = this.base58ToHex(addressBase58, 32);
|
|
89
|
+
return this.hexToBytes(hex);
|
|
90
|
+
}
|
|
91
|
+
/** @param {Uint8Array} addressUint8Array - Uint8Array(16 bytes) to convert to base58 */
|
|
92
|
+
addressBytesToBase58(addressUint8Array) {
|
|
93
|
+
const hex = this.bytesToHex(addressUint8Array, 32);
|
|
94
|
+
return this.hexToBase58(hex);
|
|
95
|
+
}
|
|
96
|
+
|
|
53
97
|
// OTHERS
|
|
98
|
+
/** @param {string} hex - Hex string to convert to BigInt */
|
|
99
|
+
static hexToBigInt(hex) { return BigInt('0x' + hex); }
|
|
54
100
|
/** @param {string} hex - Hex string to convert to bits @param {'string' | 'arrayOfString' | 'arrayOfNumbers'} format - Output format, default: string */
|
|
55
101
|
static hexToBits(hex = 'ffffff', format = 'string') {
|
|
56
102
|
let bitsString = []; // WE USE STRING METHODS FOR PERFORMANCE (number '0' = 8 Bytes | string '0' = 1 Byte)
|
package/services/cryptos.mjs
CHANGED