ethers.js/packages/bytes/lib/pad.js

29 lines
848 B
JavaScript
Raw Permalink Normal View History

2022-04-11 17:14:19 -04:00
import { _noCopyArrayify } from "./array.js";
import { hexlify } from "./hex.js";
import { logger } from "./logger.js";
function zeroPad(data, length, left) {
const bytes = _noCopyArrayify(data);
if (length < bytes.length) {
logger.throwError("padding exceeds data length", "BUFFER_OVERRUN", {
buffer: new Uint8Array(bytes),
length: length,
offset: length + 1
});
}
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-04-17 03:13:04 -04:00
export function zeroPadValue(data, length) {
2022-04-11 17:14:19 -04:00
return zeroPad(data, length, true);
}
2022-04-17 03:13:04 -04:00
export function zeroPadBytes(data, length) {
2022-04-11 17:14:19 -04:00
return zeroPad(data, length, false);
}
//# sourceMappingURL=pad.js.map