UNPKG

@fruitsjs/crypto

Version:

Cryptographic functions for building Fruits Eco-Blockchain apps.

37 lines 1.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getAccountIdFromPublicKey = void 0; const converter_1 = require("./converter"); const CryptoJS = require("crypto-js"); const getAccountIdFromPublicKey = (publicKey) => { const hash = CryptoJS.SHA256(CryptoJS.enc.Hex.parse(publicKey)); const bytes = converter_1.Converter.convertWordArrayToByteArray(hash); let result = ''; for (let i = 0; i < 8; i++) { let byte = bytes[i].toString(16); if (byte.length < 2) { byte = '0' + byte; } result = byte + result; } return hexToDec(result); }; exports.getAccountIdFromPublicKey = getAccountIdFromPublicKey; function hexToDec(s) { const digits = [0]; let i, j, carry; for (i = 0; i < s.length; i += 1) { carry = parseInt(s.charAt(i), 16); for (j = 0; j < digits.length; j += 1) { digits[j] = digits[j] * 16 + carry; carry = digits[j] / 10 | 0; digits[j] %= 10; } while (carry > 0) { digits.push(carry % 10); carry = carry / 10 | 0; } } return digits.reverse().join(''); } //# sourceMappingURL=getAccountIdFromPublicKey.js.map