UNPKG

@okxweb3/coin-ethereum

Version:

An Ethereum SDK for building Web3 wallets and applications.

53 lines 2.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.toType = exports.TypeOutput = exports.bnToRlp = exports.bnToUnpaddedBuffer = exports.bnToHex = void 0; const coin_base_1 = require("@okxweb3/coin-base"); const util_1 = require("./util"); const bytes_1 = require("./bytes"); function bnToHex(value) { return `0x${value.toString(16)}`; } exports.bnToHex = bnToHex; function bnToUnpaddedBuffer(value) { return (0, bytes_1.unpadBuffer)(value.toArrayLike(Buffer)); } exports.bnToUnpaddedBuffer = bnToUnpaddedBuffer; function bnToRlp(value) { return (0, bytes_1.unpadBuffer)(value.toArrayLike(Buffer)); } exports.bnToRlp = bnToRlp; var TypeOutput; (function (TypeOutput) { TypeOutput[TypeOutput["Number"] = 0] = "Number"; TypeOutput[TypeOutput["BN"] = 1] = "BN"; TypeOutput[TypeOutput["Buffer"] = 2] = "Buffer"; TypeOutput[TypeOutput["PrefixedHexString"] = 3] = "PrefixedHexString"; })(TypeOutput = exports.TypeOutput || (exports.TypeOutput = {})); function toType(input, outputType) { if (typeof input === 'string' && !(0, util_1.isHexString)(input)) { throw new Error(`A string must be provided with a 0x-prefix, given: ${input}`); } else if (typeof input === 'number' && !Number.isSafeInteger(input)) { throw new Error('The provided number is greater than MAX_SAFE_INTEGER (please use an alternative input type)'); } input = (0, bytes_1.toBuffer)(input); if (outputType === TypeOutput.Buffer) { return input; } else if (outputType === TypeOutput.BN) { return new coin_base_1.BN(input); } else if (outputType === TypeOutput.Number) { const bn = new coin_base_1.BN(input); const max = new coin_base_1.BN(Number.MAX_SAFE_INTEGER.toString()); if (bn.gt(max)) { throw new Error('The provided number is greater than MAX_SAFE_INTEGER (please use an alternative output type)'); } return bn.toNumber(); } else { return `0x${input.toString('hex')}`; } } exports.toType = toType; //# sourceMappingURL=types.js.map