ethers.js/lib.esm/utils/data.js

170 lines
4.9 KiB
JavaScript
Raw Permalink Normal View History

2022-11-30 15:44:23 -05:00
/**
* Some data helpers.
*
*
2022-12-02 21:27:06 -05:00
* @_subsection api/utils:Data Helpers [about-data]
2022-11-30 15:44:23 -05:00
*/
2022-11-09 02:57:02 -05:00
import { assert, assertArgument } from "./errors.js";
2022-09-15 22:58:45 -04:00
function _getBytes(value, name, copy) {
if (value instanceof Uint8Array) {
if (copy) {
return new Uint8Array(value);
}
return value;
}
if (typeof (value) === "string" && value.match(/^0x([0-9a-f][0-9a-f])*$/i)) {
const result = new Uint8Array((value.length - 2) / 2);
let offset = 2;
for (let i = 0; i < result.length; i++) {
result[i] = parseInt(value.substring(offset, offset + 2), 16);
offset += 2;
}
return result;
}
2022-11-09 02:57:02 -05:00
assertArgument(false, "invalid BytesLike value", name || "value", value);
2022-09-15 22:58:45 -04:00
}
/**
* Get a typed Uint8Array for %%value%%. If already a Uint8Array
* the original %%value%% is returned; if a copy is required use
* [[getBytesCopy]].
*
* @see: getBytesCopy
*/
export function getBytes(value, name) {
return _getBytes(value, name, false);
}
/**
* Get a typed Uint8Array for %%value%%, creating a copy if necessary
* to prevent any modifications of the returned value from being
* reflected elsewhere.
*
* @see: getBytes
*/
export function getBytesCopy(value, name) {
return _getBytes(value, name, true);
}
/**
2022-11-30 15:44:23 -05:00
* Returns true if %%value%% is a valid [[HexString]].
2022-09-15 22:58:45 -04:00
*
2022-11-30 15:44:23 -05:00
* If %%length%% is ``true`` or a //number//, it also checks that
* %%value%% is a valid [[DataHexString]] of %%length%% (if a //number//)
2022-09-15 22:58:45 -04:00
* bytes of data (e.g. ``0x1234`` is 2 bytes).
*/
2022-09-05 16:57:11 -04:00
export function isHexString(value, length) {
if (typeof (value) !== "string" || !value.match(/^0x[0-9A-Fa-f]*$/)) {
return false;
}
if (typeof (length) === "number" && value.length !== 2 + 2 * length) {
return false;
}
if (length === true && (value.length % 2) !== 0) {
return false;
}
return true;
}
2022-09-15 22:58:45 -04:00
/**
* Returns true if %%value%% is a valid representation of arbitrary
2022-11-30 15:44:23 -05:00
* data (i.e. a valid [[DataHexString]] or a Uint8Array).
2022-09-15 22:58:45 -04:00
*/
2022-09-05 16:57:11 -04:00
export function isBytesLike(value) {
return (isHexString(value, true) || (value instanceof Uint8Array));
}
const HexCharacters = "0123456789abcdef";
2022-09-15 22:58:45 -04:00
/**
2022-11-30 15:44:23 -05:00
* Returns a [[DataHexString]] representation of %%data%%.
2022-09-15 22:58:45 -04:00
*/
2022-09-05 16:57:11 -04:00
export function hexlify(data) {
2022-09-15 22:58:45 -04:00
const bytes = getBytes(data);
2022-09-05 16:57:11 -04:00
let result = "0x";
for (let i = 0; i < bytes.length; i++) {
const v = bytes[i];
result += HexCharacters[(v & 0xf0) >> 4] + HexCharacters[v & 0x0f];
}
return result;
}
2022-09-15 22:58:45 -04:00
/**
2022-11-30 15:44:23 -05:00
* Returns a [[DataHexString]] by concatenating all values
2022-09-15 22:58:45 -04:00
* within %%data%%.
*/
2022-09-05 16:57:11 -04:00
export function concat(datas) {
return "0x" + datas.map((d) => hexlify(d).substring(2)).join("");
}
2022-09-15 22:58:45 -04:00
/**
* Returns the length of %%data%%, in bytes.
*/
2022-09-05 16:57:11 -04:00
export function dataLength(data) {
if (isHexString(data, true)) {
return (data.length - 2) / 2;
}
2022-09-15 22:58:45 -04:00
return getBytes(data).length;
2022-09-05 16:57:11 -04:00
}
2022-09-15 22:58:45 -04:00
/**
2022-11-30 15:44:23 -05:00
* Returns a [[DataHexString]] by slicing %%data%% from the %%start%%
2022-09-15 22:58:45 -04:00
* offset to the %%end%% offset.
*
* By default %%start%% is 0 and %%end%% is the length of %%data%%.
*/
2022-09-05 16:57:11 -04:00
export function dataSlice(data, start, end) {
2022-09-15 22:58:45 -04:00
const bytes = getBytes(data);
2022-09-05 16:57:11 -04:00
if (end != null && end > bytes.length) {
2022-11-09 02:57:02 -05:00
assert(false, "cannot slice beyond data bounds", "BUFFER_OVERRUN", {
2022-09-05 16:57:11 -04:00
buffer: bytes, length: bytes.length, offset: end
});
}
return hexlify(bytes.slice((start == null) ? 0 : start, (end == null) ? bytes.length : end));
}
2022-09-15 22:58:45 -04:00
/**
2022-11-30 15:44:23 -05:00
* Return the [[DataHexString]] result by stripping all **leading**
2022-09-15 22:58:45 -04:00
** zero bytes from %%data%%.
*/
2022-09-05 16:57:11 -04:00
export function stripZerosLeft(data) {
let bytes = hexlify(data).substring(2);
2023-02-18 22:18:42 -05:00
while (bytes.startsWith("00")) {
2022-09-05 16:57:11 -04:00
bytes = bytes.substring(2);
}
return "0x" + bytes;
}
function zeroPad(data, length, left) {
2022-09-15 22:58:45 -04:00
const bytes = getBytes(data);
2022-11-09 02:57:02 -05:00
assert(length >= bytes.length, "padding exceeds data length", "BUFFER_OVERRUN", {
buffer: new Uint8Array(bytes),
length: length,
offset: length + 1
});
2022-09-05 16:57:11 -04:00
const result = new Uint8Array(length);
result.fill(0);
if (left) {
result.set(bytes, length - bytes.length);
}
else {
result.set(bytes, 0);
}
return hexlify(result);
}
2022-09-15 22:58:45 -04:00
/**
2022-11-30 15:44:23 -05:00
* Return the [[DataHexString]] of %%data%% padded on the **left**
2022-09-15 22:58:45 -04:00
* to %%length%% bytes.
2022-12-30 11:30:03 -05:00
*
2023-02-12 22:14:26 -05:00
* If %%data%% already exceeds %%length%%, a [[BufferOverrunError]] is
2022-12-30 11:30:03 -05:00
* thrown.
*
* This pads data the same as **values** are in Solidity
* (e.g. ``uint128``).
2022-09-15 22:58:45 -04:00
*/
2022-09-05 16:57:11 -04:00
export function zeroPadValue(data, length) {
return zeroPad(data, length, true);
}
2022-09-15 22:58:45 -04:00
/**
2022-11-30 15:44:23 -05:00
* Return the [[DataHexString]] of %%data%% padded on the **right**
2022-09-15 22:58:45 -04:00
* to %%length%% bytes.
2022-12-30 11:30:03 -05:00
*
2023-02-12 22:14:26 -05:00
* If %%data%% already exceeds %%length%%, a [[BufferOverrunError]] is
2022-12-30 11:30:03 -05:00
* thrown.
*
* This pads data the same as **bytes** are in Solidity
* (e.g. ``bytes16``).
2022-09-15 22:58:45 -04:00
*/
2022-09-05 16:57:11 -04:00
export function zeroPadBytes(data, length) {
return zeroPad(data, length, false);
}
//# sourceMappingURL=data.js.map