UNPKG

@swtc/common

Version:
117 lines 3.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.funcGetChain = funcGetChain; exports.funcSeqEqual = funcSeqEqual; exports.funcConcatArgs = funcConcatArgs; exports.funcHexToBytes = funcHexToBytes; exports.funcBytesToHex = funcBytesToHex; exports.funcHexToString = funcHexToString; exports.funcStringToHex = funcStringToHex; exports.funcString2Hex = funcString2Hex; exports.funcNumber2Hex = funcNumber2Hex; exports.funcHex2Number = funcHex2Number; exports.funcIsEmpty = funcIsEmpty; exports.funcAssert = funcAssert; const constants_1 = require("./constants"); function funcGetChain(chain_or_token) { const chains = constants_1.CHAINS.filter(chain => chain.code.toLowerCase() === chain_or_token.toLowerCase() || chain.currency.toUpperCase() === chain_or_token.toUpperCase()); if (chains.length > 1) { console.log(`!!!!!!!!!!more than one chains found, use first!!!!!!!!!!!!!`); } return chains.length > 0 ? chains[0] : undefined; } function funcSeqEqual(arr1, arr2) { if (arr1.length !== arr2.length) { return false; } for (let i = 0; i < arr1.length; i++) { if (arr1[i] !== arr2[i]) { return false; } } return true; } function isSequence(val) { return val.length !== undefined; } function funcConcatArgs(...args) { const ret = []; args.forEach(arg => { if (isSequence(arg)) { for (const e of arg) { ret.push(e); } } else { ret.push(arg); } }); return ret; } function funcHexToBytes(hex) { const bytes = []; for (let c = 0; c < hex.length; c += 2) { bytes.push(parseInt(hex.substr(c, 2), 16)); } return bytes; } function funcBytesToHex(bytes) { const hex = []; for (const byte of bytes) { const current = byte < 0 ? byte + 256 : byte; hex.push((current >>> 4).toString(16)); hex.push((current & 0xf).toString(16)); } return hex.join("").toUpperCase(); } function funcHexToString(h) { const a = []; let i = 0; if (h.length % 2) { a.push(String.fromCharCode(parseInt(h.substring(0, 1), 16))); i = 1; } for (; i < h.length; i += 2) { a.push(String.fromCharCode(parseInt(h.substring(i, i + 2), 16))); } return a.join(""); } function funcStringToHex(s) { let result = ""; for (const e of s) { const b = e.charCodeAt(0); result += b < 16 ? "0" + b.toString(16) : b.toString(16); } return result; } function funcString2Hex(s) { let result = funcStringToHex(s); if (result.length < 64) { result += constants_1.ZERO.substr(result.length); } return result; } function funcNumber2Hex(n) { n = n.toString(16); return constants_1.ZERO.substr(0, 64 - n.length) + n; } function funcHex2Number(h) { return parseInt(h, 16); } function funcIsEmpty(value) { const type = typeof value; if ((value !== null && type === "object") || type === "function") { const properties = Object.keys(value); if (properties.length === 0 || properties.size === 0) { return true; } } return !value; } function funcAssert(condition, msg = "Assertion failed") { if (!condition) { throw new Error(msg); } } //# sourceMappingURL=functions.js.map