UNPKG

multichain-address-validator

Version:

Multichain address validator for Bitcoin and other blockchains.

43 lines (42 loc) 1.51 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const base58_js_1 = __importDefault(require("../crypto/base58.js")); const utils_js_1 = __importDefault(require("../crypto/utils.js")); const helpers_js_1 = require("../helpers.js"); const VALID_PREFIXES = [ [6, 161, 159], // tz1 (ed25519) [6, 161, 161], // tz2 (secp256k1) [6, 161, 164], // tz3 (p256) [2, 90, 121], // KT1 (originated) ]; function decodeRaw(buffer) { const payload = buffer.slice(0, -4); const checksum = buffer.slice(-4); const newChecksum = utils_js_1.default.hexStr2byteArray(utils_js_1.default.sha256x2(utils_js_1.default.byteArray2hexStr(payload))); if (checksum[0] ^ newChecksum[0] | checksum[1] ^ newChecksum[1] | checksum[2] ^ newChecksum[2] | checksum[3] ^ newChecksum[3]) return; return payload; } function hasValidPrefix(payload) { return VALID_PREFIXES.some(prefix => prefix.every((byte, i) => payload[i] === byte)); } exports.default = { isValidAddress(address) { try { const buffer = base58_js_1.default.decode((0, helpers_js_1.getAddress)(address)); const payload = decodeRaw(buffer); if (!payload) return false; return hasValidPrefix(payload); } catch { return false; } } };