From 50e0f4e36b4a9b13cc3ef0aba93653c77789d1b1 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Fri, 31 Jul 2026 11:54:40 -0700 Subject: [PATCH] Fix type definitions since options should be optional. --- src/index.ts | 10 +++++----- src/lib/config/index.ts | 23 ++++++++++++----------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/index.ts b/src/index.ts index 7df4686..b2faeb9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -57,14 +57,14 @@ export type WorkValidateResponse = { /** * Finds a nonce that satisfies the Nano proof-of-work requirements. * - * @param {bigint | string} hash - Hexadecimal hash of previous block, or public key for new accounts - * @param {object} [options] - Used to configure execution + * @param {(bigint|string)} hash - Hexadecimal hash of previous block, or public key for new accounts + * @param {NanoPowOptions} [options] - Used to configure execution * @param {string} [options.api] - Specifies how work is generated. Default: best available * @param {boolean} [options.debug=false] - Enables additional debug logging to the console. Default: false * @param {number} [options.effort=0x4] - GPU load when generating work. Larger values are not necessarily better since they can quickly overwhelm the GPU. Default: 0x4 * @param {bigint} [options.difficulty=0xfffffff800000000] - Minimum value result of `BLAKE2b(nonce||blockhash)`. Default: 0xFFFFFFF800000000 */ -export async function work_generate (hash: bigint | string, options: NanoPowOptions): Promise { +export async function work_generate (hash: bigint | string, options?: NanoPowOptions): Promise { return generate(hash, options) } @@ -73,11 +73,11 @@ export async function work_generate (hash: bigint | string, options: NanoPowOpti * * @param {(bigint|string)} work - Value to validate against hash and difficulty * @param {(bigint|string)} hash - Hash of previous block, or public key for new accounts - * @param {object} [options] - Used to configure execution + * @param {NanoPowOptions} [options] - Used to configure execution * @param {boolean} [options.debug=false] - Enables additional debug logging to the console. Default: false * @param {bigint} [options.difficulty=0xfffffff800000000] - Minimum value result of `BLAKE2b(nonce||blockhash)`. Default: 0xFFFFFFF800000000 */ -export async function work_validate (work: bigint | string, hash: bigint | string, options: NanoPowOptions): Promise { +export async function work_validate (work: bigint | string, hash: bigint | string, options?: NanoPowOptions): Promise { return validate(work, hash, options) } diff --git a/src/lib/config/index.ts b/src/lib/config/index.ts index 3ffd796..66e8bbc 100644 --- a/src/lib/config/index.ts +++ b/src/lib/config/index.ts @@ -48,8 +48,8 @@ class NanoPowConfigConstructor implements NanoPowOptions { this.effort = effort } - static async create (options: { api: unknown, debug: unknown, difficulty: unknown, effort: unknown } | unknown): Promise { - const input = options as Record + static async create (options: NanoPowOptions | unknown): Promise { + const input = (options ?? {}) as Record const api = await this.#getValidApi(input) const debug = this.#getValidDebug(input) @@ -72,7 +72,7 @@ class NanoPowConfigConstructor implements NanoPowOptions { // Assign API if valid value passed static async #getValidApi (input: Record): Promise { - if (input != null && input.api != null) { + if (input.api != null) { if (typeof input.api === 'string') { try { input.api = input.api.toLowerCase() @@ -97,7 +97,7 @@ class NanoPowConfigConstructor implements NanoPowOptions { // Assign debug if valid value passed static #getValidDebug (input: Record): boolean { - if (input != null && input.debug != null) { + if (input.debug != null) { if (typeof input.debug === 'bigint' || typeof input.debug === 'number') { input.debug = input.debug.toString() } @@ -114,7 +114,7 @@ class NanoPowConfigConstructor implements NanoPowOptions { // Assign difficulty if valid value passed static #getValidDifficulty (input: Record): bigint { - if (input != null && input.difficulty != null) { + if (input.difficulty != null) { if (typeof input.difficulty === 'string') { try { input.difficulty = big(input.difficulty) @@ -133,7 +133,7 @@ class NanoPowConfigConstructor implements NanoPowOptions { // Assign effort if valid value passed static #getValidEffort (input: Record): number { - if (input != null && input.effort != null) { + if (input.effort != null) { if (typeof input.effort !== 'number') { throw new Error(`Invalid effort (${typeof input.effort})${input.effort}`) } @@ -150,12 +150,13 @@ class NanoPowConfigConstructor implements NanoPowOptions { * Validated NanoPowOptions object used to guarantee values and types. * Attempting to call using `new` with throw a TypeError. * - * @param {string} api - Specifies how work is generated. Default: best available - * @param {boolean} debug - Enables additional debug logging to the console. Default: false - * @param {bigint} difficulty - Minimum value result of `BLAKE2b(nonce||blockhash)`. Default: 0xFFFFFFF800000000 - * @param {number} effort - GPU load when generating work. Larger values are not necessarily better since they can quickly overwhelm the GPU. Default: 0x4 + * @param {unknown} [options] - Object with zero or more properties for configuring NanoPow. + * @param {string} [api] - Specifies how work is generated. Default: best available + * @param {boolean} [debug] - Enables additional debug logging to the console. Default: false + * @param {bigint} [difficulty] - Minimum value result of `BLAKE2b(nonce||blockhash)`. Default: 0xFFFFFFF800000000 + * @param {number} [effort] - GPU load when generating work. Larger values are not necessarily better since they can quickly overwhelm the GPU. Default: 0x4 */ -export const NanoPowConfig = (options: any): Promise => { +export const NanoPowConfig = (options: unknown): Promise => { try { return NanoPowConfigConstructor.create(options) } catch (err) { -- 2.52.0