@blake.regalia/belt
Advanced tools
Comparing version 0.38.3 to 0.39.0
@@ -34,2 +34,8 @@ import type { NaiveBase58, NaiveBase64, NaiveBase93, NaiveHexLower } from './strings'; | ||
/** | ||
* Creates a new function that wraps the given function in a `try_sync` and returns the result without throwing | ||
* @param f_attempt - the function to attempt | ||
* @returns | ||
*/ | ||
export declare const safely_sync: <a_args extends unknown[], w_return>(f_attempt: (...a_args: a_args) => w_return) => (...a_args: a_args) => w_return | undefined; | ||
/** | ||
* Typed alias to `JSON.stringify` | ||
@@ -271,4 +277,13 @@ */ | ||
/** | ||
* Cryptographically strong random bytes | ||
* @param nb_len - number of bytes to fill | ||
*/ | ||
export declare const crypto_random_bytes: (nb_len?: number) => Uint8Array; | ||
/** | ||
* Cryptographically strong random number in the range [0, 1) | ||
*/ | ||
export declare const crypto_random_unit_double: () => number; | ||
/** | ||
* @deprecated Replace with {@link crypto_random_unit_double} | ||
*/ | ||
export declare const crypto_random: () => number; | ||
@@ -275,0 +290,0 @@ /** |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.base93_to_bytes = exports.bytes_to_base93 = exports.bytes_to_string8 = exports.string8_to_bytes = exports.base64_to_bytes = exports.bytes_to_base64 = exports.base64_to_bytes_slim = exports.bytes_to_base64_slim = exports.hex_to_bytes = exports.bytes_to_hex = exports.concat2 = exports.concat = exports.bytes_to_bigint_be = exports.bytes_to_biguint_be = exports.bigint_to_bytes_be = exports.biguint_to_bytes_be = exports.bytes_to_uint32_be = exports.uint32_to_bytes_be = exports.bytes_to_json = exports.json_to_bytes = exports.text_to_base64 = exports.base64_to_text = exports.bytes_to_text = exports.text_to_bytes = exports.decode_length_prefix_u16 = exports.encode_length_prefix_u16 = exports.zero_out = exports.hkdf = exports.hmac = exports.import_key = exports.sha512 = exports.sha384 = exports.sha256d = exports.sha256 = exports.dataview = exports.bytes = exports.canonicalize_json = exports.safe_json = exports.parse_json_safe = exports.parse_json = exports.stringify_json = exports.uuid_v4 = exports.bigint_min = exports.bigint_max = exports.bigint_abs = exports.bigint_greater = exports.bigint_lesser = exports.SI_HASH_ALGORITHM_SHA512 = exports.SI_HASH_ALGORITHM_SHA384 = exports.SI_HASH_ALGORITHM_SHA256 = void 0; | ||
exports.crypto_random_int = exports.crypto_random = exports.base58_to_bytes = exports.bytes_to_base58 = void 0; | ||
exports.bytes_to_base93 = exports.bytes_to_string8 = exports.string8_to_bytes = exports.base64_to_bytes = exports.bytes_to_base64 = exports.base64_to_bytes_slim = exports.bytes_to_base64_slim = exports.hex_to_bytes = exports.bytes_to_hex = exports.concat2 = exports.concat = exports.bytes_to_bigint_be = exports.bytes_to_biguint_be = exports.bigint_to_bytes_be = exports.biguint_to_bytes_be = exports.bytes_to_uint32_be = exports.uint32_to_bytes_be = exports.bytes_to_json = exports.json_to_bytes = exports.text_to_base64 = exports.base64_to_text = exports.bytes_to_text = exports.text_to_bytes = exports.decode_length_prefix_u16 = exports.encode_length_prefix_u16 = exports.zero_out = exports.hkdf = exports.hmac = exports.import_key = exports.sha512 = exports.sha384 = exports.sha256d = exports.sha256 = exports.dataview = exports.bytes = exports.canonicalize_json = exports.safe_json = exports.parse_json_safe = exports.parse_json = exports.stringify_json = exports.safely_sync = exports.uuid_v4 = exports.bigint_min = exports.bigint_max = exports.bigint_abs = exports.bigint_greater = exports.bigint_lesser = exports.SI_HASH_ALGORITHM_SHA512 = exports.SI_HASH_ALGORITHM_SHA384 = exports.SI_HASH_ALGORITHM_SHA256 = void 0; | ||
exports.crypto_random_int = exports.crypto_random = exports.crypto_random_unit_double = exports.crypto_random_bytes = exports.base58_to_bytes = exports.bytes_to_base58 = exports.base93_to_bytes = void 0; | ||
const belt_js_1 = require("./belt.js"); | ||
@@ -67,2 +67,9 @@ exports.SI_HASH_ALGORITHM_SHA256 = 'SHA-256'; | ||
/** | ||
* Creates a new function that wraps the given function in a `try_sync` and returns the result without throwing | ||
* @param f_attempt - the function to attempt | ||
* @returns | ||
*/ | ||
const safely_sync = (f_attempt) => (...a_args) => (0, belt_js_1.try_sync)(_ => f_attempt(...a_args))[0]; | ||
exports.safely_sync = safely_sync; | ||
/** | ||
* Typed alias to `JSON.stringify` | ||
@@ -640,7 +647,17 @@ */ | ||
/** | ||
* Cryptographically strong random bytes | ||
* @param nb_len - number of bytes to fill | ||
*/ | ||
const crypto_random_bytes = (nb_len = 32) => crypto.getRandomValues((0, exports.bytes)(nb_len)); | ||
exports.crypto_random_bytes = crypto_random_bytes; | ||
/** | ||
* Cryptographically strong random number in the range [0, 1) | ||
*/ | ||
const crypto_random = () => crypto.getRandomValues((0, exports.bytes)(1))[0] / (2 ** 32); | ||
exports.crypto_random = crypto_random; | ||
const crypto_random_unit_double = () => (0, exports.crypto_random_bytes)(1)[0] / (2 ** 32); | ||
exports.crypto_random_unit_double = crypto_random_unit_double; | ||
/** | ||
* @deprecated Replace with {@link crypto_random_unit_double} | ||
*/ | ||
exports.crypto_random = exports.crypto_random_unit_double; | ||
/** | ||
* Generate a cryptographically strong random int within a given range | ||
@@ -652,5 +669,5 @@ */ | ||
// confine to range | ||
return Math.floor((0, exports.crypto_random)() * (x_max - x_min)) + x_min; | ||
return Math.floor((0, exports.crypto_random_unit_double)() * (x_max - x_min)) + x_min; | ||
}; | ||
exports.crypto_random_int = crypto_random_int; | ||
//# sourceMappingURL=data.js.map |
@@ -34,2 +34,8 @@ import type { NaiveBase58, NaiveBase64, NaiveBase93, NaiveHexLower } from './strings'; | ||
/** | ||
* Creates a new function that wraps the given function in a `try_sync` and returns the result without throwing | ||
* @param f_attempt - the function to attempt | ||
* @returns | ||
*/ | ||
export declare const safely_sync: <a_args extends unknown[], w_return>(f_attempt: (...a_args: a_args) => w_return) => (...a_args: a_args) => w_return | undefined; | ||
/** | ||
* Typed alias to `JSON.stringify` | ||
@@ -271,4 +277,13 @@ */ | ||
/** | ||
* Cryptographically strong random bytes | ||
* @param nb_len - number of bytes to fill | ||
*/ | ||
export declare const crypto_random_bytes: (nb_len?: number) => Uint8Array; | ||
/** | ||
* Cryptographically strong random number in the range [0, 1) | ||
*/ | ||
export declare const crypto_random_unit_double: () => number; | ||
/** | ||
* @deprecated Replace with {@link crypto_random_unit_double} | ||
*/ | ||
export declare const crypto_random: () => number; | ||
@@ -275,0 +290,0 @@ /** |
@@ -58,2 +58,8 @@ import { XG_8, is_array, is_dict_es, is_string, entries, from_entries, die, try_sync } from './belt.js'; | ||
/** | ||
* Creates a new function that wraps the given function in a `try_sync` and returns the result without throwing | ||
* @param f_attempt - the function to attempt | ||
* @returns | ||
*/ | ||
export const safely_sync = (f_attempt) => (...a_args) => try_sync(_ => f_attempt(...a_args))[0]; | ||
/** | ||
* Typed alias to `JSON.stringify` | ||
@@ -593,6 +599,15 @@ */ | ||
/** | ||
* Cryptographically strong random bytes | ||
* @param nb_len - number of bytes to fill | ||
*/ | ||
export const crypto_random_bytes = (nb_len = 32) => crypto.getRandomValues(bytes(nb_len)); | ||
/** | ||
* Cryptographically strong random number in the range [0, 1) | ||
*/ | ||
export const crypto_random = () => crypto.getRandomValues(bytes(1))[0] / (2 ** 32); | ||
export const crypto_random_unit_double = () => crypto_random_bytes(1)[0] / (2 ** 32); | ||
/** | ||
* @deprecated Replace with {@link crypto_random_unit_double} | ||
*/ | ||
export const crypto_random = crypto_random_unit_double; | ||
/** | ||
* Generate a cryptographically strong random int within a given range | ||
@@ -604,4 +619,4 @@ */ | ||
// confine to range | ||
return Math.floor(crypto_random() * (x_max - x_min)) + x_min; | ||
return Math.floor(crypto_random_unit_double() * (x_max - x_min)) + x_min; | ||
}; | ||
//# sourceMappingURL=data.js.map |
{ | ||
"name": "@blake.regalia/belt", | ||
"version": "0.38.3", | ||
"version": "0.39.0", | ||
"repository": "github:blake-regalia/belt", | ||
@@ -5,0 +5,0 @@ "license": "ISC", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
327834
6248