From b61e4efda63775e29c90eaf0014303878758ed51 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Thu, 30 Apr 2026 21:07:28 -0700 Subject: [PATCH] Remove redundant awaits in wallet API. Return signed block when signing from wallet. --- src/lib/wallet/index.ts | 42 +++++++++++++++++++++++------------------ src/lib/wallet/sign.ts | 5 +++-- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/lib/wallet/index.ts b/src/lib/wallet/index.ts index f5f39aa..ebb999e 100644 --- a/src/lib/wallet/index.ts +++ b/src/lib/wallet/index.ts @@ -237,7 +237,7 @@ export class Wallet { * @returns Promise for the Account at the specified index */ async account (index: number = 0): Promise { - return await _accounts(this.type, this.#accounts, this.#vault, index) + return _accounts(this.type, this.#accounts, this.#vault, index) } /** @@ -277,7 +277,7 @@ export class Wallet { * @returns {Promise>} Promise for a list of Accounts at the specified indexes */ async accounts (from: number = 0, to: number = from): Promise> { - return await _accounts(this.type, this.#accounts, this.#vault, from, to) + return _accounts(this.type, this.#accounts, this.#vault, from, to) } /** @@ -291,7 +291,7 @@ export class Wallet { */ async config (settings: { timeout: number }): Promise async config (settings: { connection: 'hid' | 'ble' | 'usb' } | { timeout: number }): Promise { - return await _config(this.type, this.#vault, settings) + return _config(this.type, this.#vault, settings) } /** @@ -299,7 +299,7 @@ export class Wallet { * allow garbage collection, and terminates vault worker. */ async destroy (): Promise { - return await _destroy(this, this.#vault) + return _destroy(this, this.#vault) } /** @@ -326,7 +326,7 @@ export class Wallet { * @returns {Promise>} Promise for accounts with updated balances, frontiers, and representatives */ async refresh (rpc: Rpc | string | URL, from: number = 0, to: number = from): Promise> { - return await _refresh(this, rpc, from, to) + return _refresh(this, rpc, from, to) } /** @@ -349,32 +349,37 @@ export class Wallet { * * @param {number} index - Account to use for signing * @param {(string|string[])} data - Arbitrary data to be signed + * @returns {Promise} 128-character hexadecimal detached signature */ async sign (index: number, data: string | string[]): Promise /** * Signs a block using the private key of the account at the wallet index - * specified. The signature is appended to the signature field of the block - * before being returned. The wallet must be unlocked prior to signing. + * specified. The signature is appended to the signature field of the block, + * and the block is returned to allow chaining with other Block functions. The + * wallet must be unlocked prior to signing. * * @param {number} index - Account to use for signing * @param {Block} block - Block data to be hashed and signed + * @returns {Promise} Block with signature */ - async sign (index: number, block: Block): Promise + async sign (index: number, block: Block): Promise /** * Signs a block using a Ledger hardware wallet at the wallet index specified. - * The signature is appended to the signature field of the block before being - * returned. The wallet must be unlocked prior to signing. + * The signature is appended to the signature field of the block, and the block + * is returned to allow chaining with other Block functions. The wallet must be + * unlocked prior to signing. * * @param {number} index - Account to use for signing * @param {Block} block - Block data to be hashed and signed * @param {Block} [frontier] - Previous block data to be cached by Ledger wallet + * @returns {Promise} Block with signature */ - async sign (index: number, block: Block, frontier?: Block): Promise - async sign (index: number, data: string | string[] | Block, frontier?: Block): Promise { + async sign (index: number, block: Block, frontier?: Block): Promise + async sign (index: number, data: string | string[] | Block, frontier?: Block): Promise { const { address } = await this.account(index) return data instanceof Block - ? await _signBlock(this, this.#vault, index, address, data, frontier) - : await _signData(this, this.#vault, index, address, data) + ? _signBlock(this, this.#vault, index, address, data, frontier) + : _signData(this, this.#vault, index, address, data) } /** @@ -403,7 +408,7 @@ export class Wallet { * @returns {Promise} The lowest-indexed unopened account belonging to the wallet */ async unopened (rpc: Rpc, batchSize: number = ADDRESS_GAP, from: number = 0): Promise { - return await _unopened(this, rpc, batchSize, from) + return _unopened(this, rpc, batchSize, from) } /** @@ -412,9 +417,10 @@ export class Wallet { * * @param {string} password Used to re-encrypt the wallet */ - async update (password: string): Promise { + async update (password: string): Promise + async update (password?: string): Promise { const pending = _update(this, this.#vault, password) - password = undefined! + password = undefined await pending } @@ -436,6 +442,6 @@ export class Wallet { */ async verify (mnemonic: string): Promise async verify (secret: string): Promise { - return await _verify(this.type, this.#vault, secret) + return _verify(this.type, this.#vault, secret) } } diff --git a/src/lib/wallet/sign.ts b/src/lib/wallet/sign.ts index ce9f756..cf5ee23 100644 --- a/src/lib/wallet/sign.ts +++ b/src/lib/wallet/sign.ts @@ -7,8 +7,8 @@ import { Ledger } from '../ledger' import { Vault } from '../vault' import { Wallet } from '../wallet' -export async function _signBlock (wallet: Wallet, vault: Vault, index: number, address: string, block: Block, frontier?: Block): Promise -export async function _signBlock (wallet: Wallet, vault: Vault, index: unknown, address: unknown, block: unknown, frontier?: unknown): Promise { +export async function _signBlock (wallet: Wallet, vault: Vault, index: number, address: string, block: Block, frontier?: Block): Promise +export async function _signBlock (wallet: Wallet, vault: Vault, index: unknown, address: unknown, block: unknown, frontier?: unknown): Promise { try { if (typeof index !== 'number') { throw new TypeError('Index must be a number', { cause: index }) @@ -45,6 +45,7 @@ export async function _signBlock (wallet: Wallet, vault: Vault, index: unknown, }) block.signature = bytes.toHex(new Uint8Array(signature)) } + return block } catch (err) { throw new Error(`Failed to sign block`, { cause: err }) } -- 2.47.3