@kstasi/jest-tolk
Version:
<p align="center"> <picture> <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/tonkite/tonkite/main/assets/logo-dark.svg"> <img alt="tonkite logo" src="https://raw.githubusercontent.com/tonkite/tonkite/main/a
39 lines (38 loc) • 1.46 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateAddress = generateAddress;
const core_1 = require("@ton/core");
const node_crypto_1 = require("node:crypto");
function generateAddress(fixtures) {
if (fixtures && fixtures.length > 0) {
fixtures.forEach((item) => {
if (item.type !== 'slice') {
throw new Error(`All fixtures must be of type slice, but got ${item.type}`);
}
});
return selectAddress(fixtures);
}
const EDGE_WEIGHT = 5;
const RANDOM_WEIGHT = 95;
const totalWeight = EDGE_WEIGHT + RANDOM_WEIGHT;
const bias = Math.floor(Math.random() * totalWeight);
if (bias < EDGE_WEIGHT)
return generateEdgeAddress();
return generateRandomAddress();
}
function generateRandomAddress() {
const workchain = Math.random() < 0.5 ? 0 : -1;
return new core_1.Address(workchain, (0, node_crypto_1.randomBytes)(32));
}
function selectAddress(fixtures) {
return fixtures[Math.floor(Math.random() * fixtures.length)].cell
.beginParse()
.loadAddress();
}
function generateEdgeAddress() {
const EDGE_CASES = [
core_1.Address.parseRaw('0:0000000000000000000000000000000000000000000000000000000000000000'),
core_1.Address.parseRaw('-1:0000000000000000000000000000000000000000000000000000000000000000'),
];
return EDGE_CASES[Math.floor(Math.random() * EDGE_CASES.length)];
}