UNPKG

jcc-ethereum-utils

Version:

Toolkit of crossing chain from Ethereum to SWTC chain

96 lines 4.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.validate = exports.isValidAmount = exports.isValidHash = exports.isValidJingtumAddress = exports.isValidEthereumSecret = exports.isValidEthereumAddress = void 0; const bignumber_js_1 = require("bignumber.js"); const jcc_wallet_1 = require("jcc_wallet"); const jcc_wallet_2 = require("jcc_wallet"); const router = Symbol(); const checkEthereumAddressKey = Symbol(); const checkJingtumAddressKey = Symbol(); const checkEthereumSecretKey = Symbol(); const checkHashKey = Symbol(); const checkAmountKey = Symbol(); const setTarget = (target, name, index, key) => { target[router] = target[router] || {}; target[router][name] = target[router][name] || {}; target[router][name].params = target[router][name].params || []; target[router][name].params[index] = key; }; const isValidEthereumAddress = (target, name, index) => { setTarget(target, name, index, checkEthereumAddressKey); }; exports.isValidEthereumAddress = isValidEthereumAddress; const isValidEthereumSecret = (target, name, index) => { setTarget(target, name, index, checkEthereumSecretKey); }; exports.isValidEthereumSecret = isValidEthereumSecret; const isValidJingtumAddress = (target, name, index) => { setTarget(target, name, index, checkJingtumAddressKey); }; exports.isValidJingtumAddress = isValidJingtumAddress; const isValidHash = (target, name, index) => { setTarget(target, name, index, checkHashKey); }; exports.isValidHash = isValidHash; const isValidAmount = (target, name, index) => { setTarget(target, name, index, checkAmountKey); }; exports.isValidAmount = isValidAmount; // Sanitize untrusted values before embedding in error messages to prevent // log injection (OWASP A03). Truncate to 100 chars and strip control chars. const sanitize = (value) => { return String(value) .replace(/[\r\n\t]/g, " ") .substring(0, 100); }; const validate = (target, name, descriptor) => { const method = descriptor.value; descriptor.value = function () { const params = target[router][name].params; /* istanbul ignore else */ if (Array.isArray(params)) { const length = params.length; for (let index = 0; index < length; index++) { const element = params[index]; let value = arguments[index]; switch (element) { case checkEthereumAddressKey: if (name === "depositState" && index === 1 && value === undefined) { value = "0x0000000000000000000000000000000000000000"; } if (!jcc_wallet_1.ethWallet.isValidAddress(value)) { throw new Error(`${sanitize(value)} is invalid ethereum address.`); } break; case checkEthereumSecretKey: if (!jcc_wallet_1.ethWallet.isValidSecret(value)) { throw new Error(`${sanitize(value)} is invalid ethereum secret.`); } break; case checkAmountKey: const bn = new bignumber_js_1.default(value); if (!bignumber_js_1.default.isBigNumber(bn) || !bn.isGreaterThan(0)) { throw new Error(`${sanitize(value)} is invalid amount.`); } break; case checkHashKey: if (!/^0x([A-Fa-f0-9]{64})$/.test(value)) { throw new Error(`${sanitize(value)} is invalid hash.`); } break; case checkJingtumAddressKey: if (!jcc_wallet_2.jtWallet.isValidAddress(value)) { throw new Error(`${sanitize(value)} is invalid jingtum address.`); } break; /* istanbul ignore next */ default: break; } } } return method.apply(this, arguments); }; }; exports.validate = validate; //# sourceMappingURL=index.js.map