UNPKG

minterjs-util

Version:
136 lines (124 loc) 3.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.addressToString = addressToString; exports.checkToString = checkToString; exports.isMinterPrefixed = isMinterPrefixed; exports.isValidAddress = isValidAddress; exports.isValidCheck = isValidCheck; exports.isValidPublicKeyString = isValidPublicKeyString; exports.isValidTransaction = isValidTransaction; exports.mPrefixStrip = mPrefixStrip; exports.mPrefixToHex = mPrefixToHex; exports.mToBuffer = mToBuffer; exports.privateToAddressString = privateToAddressString; exports.toBuffer = toBuffer; var _ethjsUtil = require("ethjs-util"); var _bytes = require("ethereumjs-util/dist/bytes"); var _account = require("ethereumjs-util/dist/account"); /** * Replace Minter prefixes with hex prefix * @param {string} value */ function mPrefixToHex(value) { return value.replace(/^(Mx|Mp|Mt|Mc|Mh)/, '0x'); } /** * Strip Minter prefixes * @param {string} value */ function mPrefixStrip(value) { return value.replace(/^(Mx|Mp|Mt|Mc|Mh)/, ''); } /** * Converts Minter prefixed hex string to Buffer * @param {string} value * @return {Buffer} */ function mToBuffer(value) { if (typeof value !== 'string') { throw new TypeError('Type error: string expected'); } if (!isMinterPrefixed(value)) { throw new Error('Not minter prefixed'); } value = mPrefixStrip(value); return Buffer.from(value, 'hex'); } /** * Attempts to turn a value into a `Buffer`. * Supports Minter prefixed hex strings. * Otherwise use `ethereumjs-util.toBuffer`. As input it supports `Buffer`, `String`, `Number`, null/undefined, `BN` and other objects with a `toArray()` method. * @param {*} value * @return {Buffer} */ function toBuffer(value) { if (typeof value === 'string' && isMinterPrefixed(value)) { return mToBuffer(value); } if (typeof value === 'string' && !(0, _ethjsUtil.isHexString)(value, 0)) { // eslint-disable-next-line unicorn/prefer-type-error throw new Error('Cannot convert string to buffer. toBuffer only supports Minter-prefixed or 0x-prefixed hex strings. You can pass buffer instead of string.'); } return (0, _bytes.toBuffer)(value); } /** * @param {string} address * @return {string} */ function addressToString(address) { return "Mx".concat(toBuffer(address).toString('hex')); } /** * @param {string} check * @return {string} */ function checkToString(check) { return "Mc".concat(toBuffer(check).toString('hex')); } /** * Returns the Minter style address string of a given private key * @param {Buffer} privateKey A private key must be 256 bits wide * @return {string} */ function privateToAddressString(privateKey) { return "Mx".concat((0, _account.privateToAddress)(privateKey).toString('hex')); } /** * @param {string} value * @returns {boolean} */ function isMinterPrefixed(value) { return /^(Mx|Mp|Mt|Mc|Mh)[0-9a-fA-F]*$/.test(value); } /** * Checks only prefix, length and hex body. * Don't check secp256k1 curve. * @param {string} publicKey * @return {boolean} */ function isValidPublicKeyString(publicKey) { return /^Mp[0-9a-fA-F]{64}$/.test(publicKey); } /** * @param {string} address * @return {boolean} */ function isValidAddress(address) { return /^Mx[0-9a-fA-F]{40}$/.test(address); } /** * @param {string} check * @return {boolean} */ function isValidCheck(check) { return /^Mc[0-9a-fA-F]+$/.test(check); } /** * @param {string} tx * @return {boolean} */ function isValidTransaction(tx) { return /^Mt[0-9a-fA-F]{64}$/.test(tx); }