UNPKG

@dioxide-js/silas

Version:

RPC utility for Silas

46 lines (43 loc) 1.63 kB
import base32Decode from '../_virtual/index.mjs'; import base32Encode from '../_virtual/index2.mjs'; import crc32c from '../_virtual/crc32c.mjs'; import { concat } from './buffer.mjs'; function validCRC(publicKey, alg) { let code = 0; switch (alg === null || alg === void 0 ? void 0 : alg.toLowerCase()) { case 'ed25519': code = 0x3; break; case 'sm2': code = 0x4; break; default: throw 'Valid CRC failed, unsupported alg: ' + alg; } let errorCorrectingCode = crc32c.buf(publicKey, code); errorCorrectingCode = (errorCorrectingCode & 0xfffffff0) | code; errorCorrectingCode = errorCorrectingCode >>> 0; return errorCorrectingCode; } function isValidAddress(addr) { try { const [addressStr, alg] = addr.split(':'); if (!addressStr || !alg) { return false; } const address = new Uint8Array(base32Decode(addressStr, 'Crockford')); const publicKey = address.slice(0, 32); const errorCorrectingCode = validCRC(publicKey, alg); const buffer = new Int32Array([errorCorrectingCode]).buffer; const errorCorrectingCodeBuffer = new Uint8Array(buffer); const mergedBuffer = concat(publicKey, errorCorrectingCodeBuffer); const encodedMergeBuffer = base32Encode(mergedBuffer, 'Crockford'); return encodedMergeBuffer === addressStr.toUpperCase(); } catch (error) { console.error('Exception ' + error); return false; } } export { isValidAddress, validCRC }; //# sourceMappingURL=validator.mjs.map