@xlink-network/xlink-sdk
Version:
92 lines (89 loc) • 2.84 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/utils/hexHelpers.ts
var hexHelpers_exports = {};
__export(hexHelpers_exports, {
decodeHex: () => decodeHex,
encodeHex: () => encodeHex,
encodeZeroPrefixedHex: () => encodeZeroPrefixedHex
});
module.exports = __toCommonJS(hexHelpers_exports);
// src/utils/errors.ts
var XLinkSDKErrorBase = class extends Error {
constructor(...args) {
super(...args);
this.name = "XLinkSDKErrorBase";
}
};
// src/utils/hexHelpers.ts
function decodeHex(hex) {
let hexString = hex.startsWith("0x") ? hex.slice(2) : hex;
if (hexString.length % 2) hexString = `0${hexString}`;
const length = hexString.length / 2;
const bytes = new Uint8Array(length);
for (let index = 0, j = 0; index < length; index++) {
const nibbleLeft = charCodeToBase16(hexString.charCodeAt(j++));
const nibbleRight = charCodeToBase16(hexString.charCodeAt(j++));
if (nibbleLeft === void 0 || nibbleRight === void 0) {
throw new XLinkSDKErrorBase(
`Invalid byte sequence ("${hexString[j - 2]}${hexString[j - 1]}" in "${hexString}").`
);
}
bytes[index] = nibbleLeft * 16 + nibbleRight;
}
return bytes;
}
var charCodeMap = {
zero: 48,
nine: 57,
A: 65,
F: 70,
a: 97,
f: 102
};
function charCodeToBase16(char) {
if (char >= charCodeMap.zero && char <= charCodeMap.nine)
return char - charCodeMap.zero;
if (char >= charCodeMap.A && char <= charCodeMap.F)
return char - (charCodeMap.A - 10);
if (char >= charCodeMap.a && char <= charCodeMap.f)
return char - (charCodeMap.a - 10);
return void 0;
}
function encodeHex(value) {
let string = "";
for (let i = 0; i < value.length; i++) {
string += hexes[value[i]];
}
return string;
}
var hexes = Array.from(
{ length: 256 },
(_v, i) => i.toString(16).padStart(2, "0")
);
function encodeZeroPrefixedHex(value) {
return `0x${encodeHex(value)}`;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
decodeHex,
encodeHex,
encodeZeroPrefixedHex
});
//# sourceMappingURL=hexHelpers.js.map