mirror of
https://github.com/waku-org/js-waku.git
synced 2025-01-27 12:45:21 +00:00
Replace custom hex implementations with uint8arrays
This commit is contained in:
parent
a915acfcda
commit
33e6c3fe39
@ -1,16 +1,16 @@
|
|||||||
import { keccak256, Message } from "js-sha3";
|
import { keccak256, Message } from "js-sha3";
|
||||||
|
import { fromString } from "uint8arrays/from-string";
|
||||||
|
import { toString } from "uint8arrays/to-string";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert input to a byte array.
|
* Convert input to a byte array.
|
||||||
|
*
|
||||||
|
* Handles both `0x` prefixed and non-prefixed strings.
|
||||||
*/
|
*/
|
||||||
export function hexToBytes(hex: string | Uint8Array): Uint8Array {
|
export function hexToBytes(hex: string | Uint8Array): Uint8Array {
|
||||||
if (typeof hex === "string") {
|
if (typeof hex === "string") {
|
||||||
const _hex = hex.replace(/^0x/i, "");
|
const _hex = hex.replace(/^0x/i, "");
|
||||||
const bytes = [];
|
return fromString(_hex, "base16");
|
||||||
for (let c = 0; c < _hex.length; c += 2)
|
|
||||||
bytes.push(parseInt(_hex.substring(c, c + 2), 16));
|
|
||||||
|
|
||||||
return new Uint8Array(bytes);
|
|
||||||
}
|
}
|
||||||
return hex;
|
return hex;
|
||||||
}
|
}
|
||||||
@ -18,15 +18,8 @@ export function hexToBytes(hex: string | Uint8Array): Uint8Array {
|
|||||||
/**
|
/**
|
||||||
* Convert byte array to hex string (no `0x` prefix).
|
* Convert byte array to hex string (no `0x` prefix).
|
||||||
*/
|
*/
|
||||||
export function bytesToHex(bytes: Uint8Array): string {
|
export const bytesToHex = (bytes: Uint8Array): string =>
|
||||||
const hex = [];
|
toString(bytes, "base16");
|
||||||
for (let i = 0; i < bytes.length; i++) {
|
|
||||||
const current = bytes[i] < 0 ? bytes[i] + 256 : bytes[i];
|
|
||||||
hex.push((current >>> 4).toString(16));
|
|
||||||
hex.push((current & 0xf).toString(16));
|
|
||||||
}
|
|
||||||
return hex.join("");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return Keccak-256 of the input.
|
* Return Keccak-256 of the input.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user