]> git.codecow.com Git - libnemo.git/commitdiff
Remove redundant awaits in wallet API. Return signed block when signing from wallet.
authorChris Duncan <chris@zoso.dev>
Fri, 1 May 2026 04:07:28 +0000 (21:07 -0700)
committerChris Duncan <chris@zoso.dev>
Fri, 1 May 2026 04:07:28 +0000 (21:07 -0700)
src/lib/wallet/index.ts
src/lib/wallet/sign.ts

index f5f39aa3dd7e2fb0e8887d1a0e606197ed63d975..ebb999e2a33eb4f00d35690273a449f57b6eef8c 100644 (file)
@@ -237,7 +237,7 @@ export class Wallet {
        * @returns Promise for the Account at the specified index\r
        */\r
        async account (index: number = 0): Promise<Account> {\r
-               return await _accounts(this.type, this.#accounts, this.#vault, index)\r
+               return _accounts(this.type, this.#accounts, this.#vault, index)\r
        }\r
 \r
        /**\r
@@ -277,7 +277,7 @@ export class Wallet {
        * @returns {Promise<Map<number, Account>>} Promise for a list of Accounts at the specified indexes\r
        */\r
        async accounts (from: number = 0, to: number = from): Promise<Map<number, Account>> {\r
-               return await _accounts(this.type, this.#accounts, this.#vault, from, to)\r
+               return _accounts(this.type, this.#accounts, this.#vault, from, to)\r
        }\r
 \r
        /**\r
@@ -291,7 +291,7 @@ export class Wallet {
        */\r
        async config (settings: { timeout: number }): Promise<void>\r
        async config (settings: { connection: 'hid' | 'ble' | 'usb' } | { timeout: number }): Promise<void> {\r
-               return await _config(this.type, this.#vault, settings)\r
+               return _config(this.type, this.#vault, settings)\r
        }\r
 \r
        /**\r
@@ -299,7 +299,7 @@ export class Wallet {
        * allow garbage collection, and terminates vault worker.\r
        */\r
        async destroy (): Promise<void> {\r
-               return await _destroy(this, this.#vault)\r
+               return _destroy(this, this.#vault)\r
        }\r
 \r
        /**\r
@@ -326,7 +326,7 @@ export class Wallet {
        * @returns {Promise<Map<number, Account>>} Promise for accounts with updated balances, frontiers, and representatives\r
        */\r
        async refresh (rpc: Rpc | string | URL, from: number = 0, to: number = from): Promise<Map<number, Account>> {\r
-               return await _refresh(this, rpc, from, to)\r
+               return _refresh(this, rpc, from, to)\r
        }\r
 \r
        /**\r
@@ -349,32 +349,37 @@ export class Wallet {
        *\r
        * @param {number} index - Account to use for signing\r
        * @param {(string|string[])} data - Arbitrary data to be signed\r
+       * @returns {Promise<string>} 128-character hexadecimal detached signature\r
        */\r
        async sign (index: number, data: string | string[]): Promise<string>\r
        /**\r
        * Signs a block using the private key of the account at the wallet index\r
-       * specified. The signature is appended to the signature field of the block\r
-       * before being returned. The wallet must be unlocked prior to signing.\r
+       * specified. The signature is appended to the signature field of the block,\r
+       * and the block is returned to allow chaining with other Block functions. The\r
+       * wallet must be unlocked prior to signing.\r
        *\r
        * @param {number} index - Account to use for signing\r
        * @param {Block} block - Block data to be hashed and signed\r
+       * @returns {Promise<Block>} Block with signature\r
        */\r
-       async sign (index: number, block: Block): Promise<void>\r
+       async sign (index: number, block: Block): Promise<Block>\r
        /**\r
        * Signs a block using a Ledger hardware wallet at the wallet index specified.\r
-       * The signature is appended to the signature field of the block before being\r
-       * returned. The wallet must be unlocked prior to signing.\r
+       * The signature is appended to the signature field of the block, and the block\r
+       * is returned to allow chaining with other Block functions. The wallet must be\r
+       * unlocked prior to signing.\r
        *\r
        * @param {number} index - Account to use for signing\r
        * @param {Block} block - Block data to be hashed and signed\r
        * @param {Block} [frontier] - Previous block data to be cached by Ledger wallet\r
+       * @returns {Promise<Block>} Block with signature\r
        */\r
-       async sign (index: number, block: Block, frontier?: Block): Promise<void>\r
-       async sign (index: number, data: string | string[] | Block, frontier?: Block): Promise<void | string> {\r
+       async sign (index: number, block: Block, frontier?: Block): Promise<Block>\r
+       async sign (index: number, data: string | string[] | Block, frontier?: Block): Promise<Block | string> {\r
                const { address } = await this.account(index)\r
                return data instanceof Block\r
-                       ? await _signBlock(this, this.#vault, index, address, data, frontier)\r
-                       : await _signData(this, this.#vault, index, address, data)\r
+                       ? _signBlock(this, this.#vault, index, address, data, frontier)\r
+                       : _signData(this, this.#vault, index, address, data)\r
        }\r
 \r
        /**\r
@@ -403,7 +408,7 @@ export class Wallet {
        * @returns {Promise<Account>} The lowest-indexed unopened account belonging to the wallet\r
        */\r
        async unopened (rpc: Rpc, batchSize: number = ADDRESS_GAP, from: number = 0): Promise<Account> {\r
-               return await _unopened(this, rpc, batchSize, from)\r
+               return _unopened(this, rpc, batchSize, from)\r
        }\r
 \r
        /**\r
@@ -412,9 +417,10 @@ export class Wallet {
        *\r
        * @param {string} password Used to re-encrypt the wallet\r
        */\r
-       async update (password: string): Promise<void> {\r
+       async update (password: string): Promise<void>\r
+       async update (password?: string): Promise<void> {\r
                const pending = _update(this, this.#vault, password)\r
-               password = undefined!\r
+               password = undefined\r
                await pending\r
        }\r
 \r
@@ -436,6 +442,6 @@ export class Wallet {
        */\r
        async verify (mnemonic: string): Promise<boolean>\r
        async verify (secret: string): Promise<boolean> {\r
-               return await _verify(this.type, this.#vault, secret)\r
+               return _verify(this.type, this.#vault, secret)\r
        }\r
 }\r
index ce9f75647ec6dadce13f0cadd1b62b745bd50e0d..cf5ee23b2f2f83664cea6338b04d4c94f9772d95 100644 (file)
@@ -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<void>
-export async function _signBlock (wallet: Wallet, vault: Vault, index: unknown, address: unknown, block: unknown, frontier?: unknown): Promise<void> {
+export async function _signBlock (wallet: Wallet, vault: Vault, index: number, address: string, block: Block, frontier?: Block): Promise<Block>
+export async function _signBlock (wallet: Wallet, vault: Vault, index: unknown, address: unknown, block: unknown, frontier?: unknown): Promise<Block | string> {
        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 })
        }