@fruitsjs/core
Version:
Principal package with functions and models for building Fruits Eco-Blockchain applications.
56 lines • 2.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertReedSolomonAddressToNumericId = void 0;
const internal_1 = require("./internal");
const ensureReedSolomonAddress_1 = require("./ensureReedSolomonAddress");
const tokenizeReedSolomonAddress_1 = require("./tokenizeReedSolomonAddress");
const convertNumericIdToReedSolomonAddress_1 = require("./convertNumericIdToReedSolomonAddress");
const convertReedSolomonAddressToNumericId = (address) => {
(0, ensureReedSolomonAddress_1.ensureReedSolomonAddress)(address);
const { rs } = (0, tokenizeReedSolomonAddress_1.tokenizeReedSolomonAddress)(address);
const codeword = internal_1.initialCodeword.slice();
let codewordLength = 0;
for (let i = 0; i < rs.length; i++) {
const pos = internal_1.alphabet.indexOf(rs.charAt(i));
if (pos <= -1 || pos > internal_1.alphabet.length) {
continue;
}
if (codewordLength > 23) {
throw new Error('Invalid codeword length');
}
const codeworkIndex = internal_1.cwmap[codewordLength];
codeword[codeworkIndex] = pos;
codewordLength++;
}
let length = internal_1.base32Length;
const cypherString32 = [];
for (let i = 0; i < length; i++) {
cypherString32[i] = codeword[length - i - 1];
}
let out = '', newLength, digit10 = 0;
do {
newLength = 0;
digit10 = 0;
for (let i = 0; i < length; i++) {
digit10 = digit10 * 32 + cypherString32[i];
if (digit10 >= 10) {
cypherString32[newLength] = Math.floor(digit10 / 10);
digit10 %= 10;
newLength += 1;
}
else if (newLength > 0) {
cypherString32[newLength] = 0;
newLength += 1;
}
}
length = newLength;
out += digit10;
} while (length > 0);
let accountId = out.split('').reverse().join('');
if ((0, convertNumericIdToReedSolomonAddress_1.convertNumericIdToReedSolomonAddress)(accountId, 'FRUITS') !== address) {
throw new Error('Invalid codeword');
}
return accountId;
};
exports.convertReedSolomonAddressToNumericId = convertReedSolomonAddressToNumericId;
//# sourceMappingURL=convertReedSolomonAddressToNumericId.js.map