UNPKG

airgap-coin-lib

Version:

The airgap-coin-lib is a protocol agnostic library to prepare, sign and broadcast cryptocurrency transactions.

132 lines 5.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var bignumber_1 = require("../../../dependencies/src/bignumber.js-9.0.0/bignumber"); // var _ = require('underscore') // var BN = require('../../../dependencies/src/bn.js-4.11.8/bn') // var numberToBN = require('number-to-bn') var utf8 = require('../../../dependencies/src/utf8-3.0.0/utf8'); var createKeccakHash = require('../../../dependencies/src/keccak-1.0.2/js'); // this code was adapted from web3.js (https://github.com/ethereum/web3.js/blob/2.x/packages/web3-utils/src/Utils.js) var EthereumUtils = /** @class */ (function () { function EthereumUtils() { } EthereumUtils.toHex = function (value) { if (EthereumUtils.isAddress(value)) { return "0x" + value.toLowerCase().replace(/^0x/i, ''); } if (typeof value === 'boolean') { return value ? '0x01' : '0x00'; } if (EthereumUtils.isObject(value) && !EthereumUtils.isBigNumber(value) /* && !EthereumUtils.isBN(value)*/) { return EthereumUtils.utf8ToHex(JSON.stringify(value)); } // if its a negative number, pass it through numberToHex if (typeof value === 'string') { if (value.indexOf('-0x') === 0 || value.indexOf('-0X') === 0) { return EthereumUtils.numberToHex(value); } else if (value.indexOf('0x') === 0 || value.indexOf('0X') === 0) { return value; } else if (!isFinite(Number(value))) { return EthereumUtils.utf8ToHex(value); } } return EthereumUtils.numberToHex(value); }; EthereumUtils.sha3 = function (value) { var valueInBytes = value; if (EthereumUtils.isHexStrict(value) && /^0x/i.test(value.toString())) { valueInBytes = EthereumUtils.hexToBytes(value); } var hash = createKeccakHash('keccak256').update(valueInBytes).digest('hex'); var returnValue = "0x" + hash; if (returnValue === EthereumUtils.SHA3_NULL_S) { return null; } else { return returnValue; } }; EthereumUtils.numberToHex = function (value) { if (value === null || value === undefined) { return value; } if (!isFinite(Number(value)) && !EthereumUtils.isHexStrict(value)) { throw new Error("Given input \"" + value + "\" is not a number."); } // var number = EthereumUtils.toBN(value) var myNumber = new bignumber_1.BigNumber(value); var result = myNumber.toString(16); return myNumber.lt(new bignumber_1.BigNumber(0)) ? "-0x" + result.substr(1) : "0x" + result; }; EthereumUtils.hexToBytes = function (value) { var hex = typeof value === 'number' ? value.toString(16) : value; if (!EthereumUtils.isHexStrict(hex)) { throw new Error("Given value \"" + hex + "\" is not a valid hex string."); } hex = hex.replace(/^0x/i, ''); var bytes = []; for (var c = 0; c < hex.length; c += 2) { bytes.push(parseInt(hex.substr(c, 2), 16)); } return bytes; }; EthereumUtils.isHexStrict = function (hex) { return (typeof hex === 'string' || typeof hex === 'number') && /^(-)?0x[0-9a-f]*$/i.test(hex.toString()); }; EthereumUtils.checkAddressChecksum = function (value) { var address = value.replace(/^0x/i, ''); var addressHash = EthereumUtils.sha3(address.toLowerCase()); if (addressHash === null) { return false; } addressHash = addressHash.replace(/^0x/i, ''); for (var i = 0; i < 40; i++) { if ((parseInt(addressHash[i], 16) > 7 && address[i].toUpperCase() !== address[i]) || (parseInt(addressHash[i], 16) <= 7 && address[i].toLowerCase() !== address[i])) { return false; } } return true; }; EthereumUtils.isAddress = function (value) { if (!/^(0x)?[0-9a-f]{40}$/i.test(value)) { return false; // If it's ALL lowercase or ALL upppercase } else if (/^(0x|0X)?[0-9a-f]{40}$/.test(value) || /^(0x|0X)?[0-9A-F]{40}$/.test(value)) { return true; // Otherwise check each case } else { return EthereumUtils.checkAddressChecksum(value); } }; EthereumUtils.isBigNumber = function (value) { return value && bignumber_1.BigNumber.isBigNumber(value); }; EthereumUtils.utf8ToHex = function (value) { var str = utf8.encode(value); var hex = ''; // remove \u0000 padding from either side str = str.replace(/^(?:\u0000)*/, ''); str = str.split('').reverse().join(''); str = str.replace(/^(?:\u0000)*/, ''); str = str.split('').reverse().join(''); for (var i = 0; i < str.length; i++) { var code = str.charCodeAt(i); var n = code.toString(16); hex += n.length < 2 ? "0" + n : n; } return "0x" + hex; }; EthereumUtils.isObject = function (value) { var type = typeof value; return type === 'function' || (type === 'object' && !!value); }; EthereumUtils.SHA3_NULL_S = '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'; return EthereumUtils; }()); exports.EthereumUtils = EthereumUtils; //# sourceMappingURL=utils.js.map