@okxweb3/coin-base
Version:
A base package for @ok/coin-*
103 lines • 3.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isHexString = exports.getKeys = exports.fromAscii = exports.fromUtf8 = exports.toAscii = exports.arrayContainsArray = exports.getBinarySize = exports.padToEven = exports.stripHexPrefix = exports.isHexPrefixed = void 0;
function isHexPrefixed(str) {
if (typeof str !== 'string') {
throw new Error(`[isHexPrefixed] input must be type 'string', received type ${typeof str}`);
}
return str[0] === '0' && str[1] === 'x';
}
exports.isHexPrefixed = isHexPrefixed;
const stripHexPrefix = (str) => {
if (typeof str !== 'string')
throw new Error(`[stripHexPrefix] input must be type 'string', received ${typeof str}`);
return isHexPrefixed(str) ? str.slice(2) : str;
};
exports.stripHexPrefix = stripHexPrefix;
function padToEven(value) {
let a = value;
if (typeof a !== 'string') {
throw new Error(`[padToEven] value must be type 'string', received ${typeof a}`);
}
if (a.length % 2)
a = `0${a}`;
return a;
}
exports.padToEven = padToEven;
function getBinarySize(str) {
if (typeof str !== 'string') {
throw new Error(`[getBinarySize] method requires input type 'string', received ${typeof str}`);
}
return Buffer.byteLength(str, 'utf8');
}
exports.getBinarySize = getBinarySize;
function arrayContainsArray(superset, subset, some) {
if (Array.isArray(superset) !== true) {
throw new Error(`[arrayContainsArray] method requires input 'superset' to be an array, got type '${typeof superset}'`);
}
if (Array.isArray(subset) !== true) {
throw new Error(`[arrayContainsArray] method requires input 'subset' to be an array, got type '${typeof subset}'`);
}
return subset[some === true ? 'some' : 'every']((value) => superset.indexOf(value) >= 0);
}
exports.arrayContainsArray = arrayContainsArray;
function toAscii(hex) {
let str = '';
let i = 0;
const l = hex.length;
if (hex.substring(0, 2) === '0x')
i = 2;
for (; i < l; i += 2) {
const code = parseInt(hex.substr(i, 2), 16);
str += String.fromCharCode(code);
}
return str;
}
exports.toAscii = toAscii;
function fromUtf8(stringValue) {
const str = Buffer.from(stringValue, 'utf8');
return `0x${padToEven(str.toString('hex')).replace(/(^0+)|(0+$)/g, '')}`;
}
exports.fromUtf8 = fromUtf8;
function fromAscii(stringValue) {
let hex = '';
for (let i = 0; i < stringValue.length; i++) {
const code = stringValue.charCodeAt(i);
const n = code.toString(16);
hex += n.length < 2 ? `0${n}` : n;
}
return `0x${hex}`;
}
exports.fromAscii = fromAscii;
function getKeys(params, key, allowEmpty) {
if (!Array.isArray(params)) {
throw new Error(`[getKeys] method expects input 'params' to be an array, got ${typeof params}`);
}
if (typeof key !== 'string') {
throw new Error(`[getKeys] method expects input 'key' to be type 'string', got ${typeof params}`);
}
const result = [];
for (let i = 0; i < params.length; i++) {
let value = params[i][key];
if (allowEmpty === true && !value) {
value = '';
}
else if (typeof value !== 'string') {
throw new Error(`invalid abi - expected type 'string', received ${typeof value}`);
}
result.push(value);
}
return result;
}
exports.getKeys = getKeys;
function isHexString(value, length) {
if (typeof value !== 'string' || !value.match(/^0x[0-9A-Fa-f]*$/))
return false;
if (typeof length !== 'undefined' &&
length > 0 &&
value.length !== 2 + 2 * length)
return false;
return true;
}
exports.isHexString = isHexString;
//# sourceMappingURL=internal.js.map