From 6b20e6cf1d2c5b83ff14943dd745c61250fd39f3 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Wed, 6 Aug 2025 12:25:22 -0700 Subject: [PATCH] Fix wallet block usage. Add hex format option to wallet signature output. --- src/lib/block.ts | 2 +- src/lib/wallet.ts | 25 ++++++++++++++++++------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/lib/block.ts b/src/lib/block.ts index 218c595..50f7d15 100644 --- a/src/lib/block.ts +++ b/src/lib/block.ts @@ -414,7 +414,7 @@ export class Block { } else if (input instanceof Wallet && typeof param === 'number') { const wallet = input - const sig = await wallet.sign(param, this) + const sig = await wallet.sign(param, this, 'hex') if (this.signature !== sig) { throw new Error('Wallet signature does not match block signature') } diff --git a/src/lib/wallet.ts b/src/lib/wallet.ts index 54b5ce6..5617a01 100644 --- a/src/lib/wallet.ts +++ b/src/lib/wallet.ts @@ -2,7 +2,7 @@ //! SPDX-License-Identifier: GPL-3.0-or-later import { Account, AccountList } from './account' -import { ChangeBlock, ReceiveBlock, SendBlock } from './block' +import { Block } from './block' import { Bip39Mnemonic } from './bip39-mnemonic' import { ADDRESS_GAP } from './constants' import { bytes, hex, utf8 } from './convert' @@ -361,21 +361,32 @@ export class Wallet { * before being returned. The wallet must be unlocked prior to signing. * * @param {number} index - Account to use for signing - * @param {(ChangeBlock|ReceiveBlock|SendBlock)} block - Block data to be hashed and signed + * @param {(Block)} block - Block data to be hashed and signed * @returns {Promise} Hexadecimal-formatted 64-byte signature */ - async sign (index: number, block: ChangeBlock | ReceiveBlock | SendBlock): Promise { + async sign (index: number, block: Block): 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. + * + * @param {number} index - Account to use for signing + * @param {(Block)} block - Block data to be hashed and signed + * @returns {Promise} Hexadecimal-formatted 64-byte signature + */ + async sign (index: number, block: Block, format: 'hex'): Promise + async sign (index: number, block: Block, format?: 'hex'): Promise> { try { const { signature } = await this.#safe.request({ action: 'sign', index, - data: hex.toBuffer(block.hash) + data: block.hash.buffer }) - const sig = bytes.toHex(new Uint8Array(signature)) - block.signature = sig + const sig = new Uint8Array(signature) + block.signature = bytes.toHex(sig) clearTimeout(this.#lockTimer) this.#lockTimer = setTimeout(() => this.lock(), 300000) - return sig + return format === 'hex' ? sig : block.signature } catch (err) { throw new Error(`Failed to sign block`, { cause: err }) } -- 2.47.3