@okxweb3/coin-base
Version:
A base package for @ok/coin-*
34 lines • 1.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isHexPrefixed = exports.stripHexPrefix = exports.fromHex = exports.toHex = void 0;
const helper_1 = require("./helper");
function toHex(data, addPrefix = false) {
const buffer = Buffer.from(data);
return addPrefix ? '0x' + buffer.toString('hex') : buffer.toString('hex');
}
exports.toHex = toHex;
function fromHex(data) {
if (data.startsWith('0x') || data.startsWith('0X')) {
data = data.substring(2);
}
if (data.length % 2 != 0) {
data = '0' + data;
}
if (!(0, helper_1.validateHexString)(data)) {
throw new Error('invalid hex string');
}
return Buffer.from(data, 'hex');
}
exports.fromHex = fromHex;
function stripHexPrefix(hex) {
if (hex.startsWith('0x')) {
return hex.substring(2);
}
return hex;
}
exports.stripHexPrefix = stripHexPrefix;
function isHexPrefixed(hex) {
return hex.startsWith('0x');
}
exports.isHexPrefixed = isHexPrefixed;
//# sourceMappingURL=hex.js.map