@tronlink/core
Version:
The library serves as a core module within TronLink Extension, which provides low-level wallet functionality for both Tron and Ethereum networks, primary features includes account generation and transaction signing
45 lines • 1.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.splitStringByLength = void 0;
exports.isHex = isHex;
exports.strInsert = strInsert;
exports.msgHexToText = msgHexToText;
const util_1 = require("@ethereumjs/util");
function isHex(hex) {
return /^[A-Fa-f0-9]+$/.test(hex);
}
const splitStringByLength = ({ str, length }) => {
const arr = [];
let index = 0;
if (length < 1) {
length = 1;
}
while (index < str.length) {
arr.push(str.slice(index, (index += length)));
}
return arr;
};
exports.splitStringByLength = splitStringByLength;
function strInsert(str) {
const arr = (0, exports.splitStringByLength)({ str, length: 2 }).map((item) => {
return String.fromCharCode(parseInt(`0x${item}`));
});
return arr.join('');
}
function msgHexToText(hex) {
try {
const stripped = (0, util_1.stripHexPrefix)(hex);
const buff = Buffer.from(stripped, 'hex');
if (hex.indexOf('0x') === 0) {
return buff.toString('utf8');
}
else if (isHex(hex)) {
return strInsert(hex);
}
return hex;
}
catch (e) {
return hex;
}
}
//# sourceMappingURL=util.js.map