]> git.codecow.com Git - libnemo.git/commitdiff
Fix wallet block usage. Add hex format option to wallet signature output.
authorChris Duncan <chris@zoso.dev>
Wed, 6 Aug 2025 19:25:22 +0000 (12:25 -0700)
committerChris Duncan <chris@zoso.dev>
Wed, 6 Aug 2025 19:25:22 +0000 (12:25 -0700)
src/lib/block.ts
src/lib/wallet.ts

index 218c59518a5b9530bae75294bbee235557392940..50f7d155d951c6e38583c8af7a047d542e9ed764 100644 (file)
@@ -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')
                                }
index 54b5ce67e1e5f815e4ea5f6a79c606e389a1ad31..5617a016309704c430906f81ea95ac96d00cac27 100644 (file)
@@ -2,7 +2,7 @@
 //! SPDX-License-Identifier: GPL-3.0-or-later\r
 \r
 import { Account, AccountList } from './account'\r
-import { ChangeBlock, ReceiveBlock, SendBlock } from './block'\r
+import { Block } from './block'\r
 import { Bip39Mnemonic } from './bip39-mnemonic'\r
 import { ADDRESS_GAP } from './constants'\r
 import { bytes, hex, utf8 } from './convert'\r
@@ -361,21 +361,32 @@ export class Wallet {
        * before being returned. The wallet must be unlocked prior to signing.\r
        *\r
        * @param {number} index - Account to use for signing\r
-       * @param {(ChangeBlock|ReceiveBlock|SendBlock)} block - Block data to be hashed and signed\r
+       * @param {(Block)} block - Block data to be hashed and signed\r
        * @returns {Promise<string>} Hexadecimal-formatted 64-byte signature\r
        */\r
-       async sign (index: number, block: ChangeBlock | ReceiveBlock | SendBlock): Promise<string> {\r
+       async sign (index: number, block: Block): Promise<Uint8Array<ArrayBuffer>>\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
+       *\r
+       * @param {number} index - Account to use for signing\r
+       * @param {(Block)} block - Block data to be hashed and signed\r
+       * @returns {Promise<string>} Hexadecimal-formatted 64-byte signature\r
+       */\r
+       async sign (index: number, block: Block, format: 'hex'): Promise<string>\r
+       async sign (index: number, block: Block, format?: 'hex'): Promise<string | Uint8Array<ArrayBuffer>> {\r
                try {\r
                        const { signature } = await this.#safe.request<ArrayBuffer>({\r
                                action: 'sign',\r
                                index,\r
-                               data: hex.toBuffer(block.hash)\r
+                               data: block.hash.buffer\r
                        })\r
-                       const sig = bytes.toHex(new Uint8Array(signature))\r
-                       block.signature = sig\r
+                       const sig = new Uint8Array(signature)\r
+                       block.signature = bytes.toHex(sig)\r
                        clearTimeout(this.#lockTimer)\r
                        this.#lockTimer = setTimeout(() => this.lock(), 300000)\r
-                       return sig\r
+                       return format === 'hex' ? sig : block.signature\r
                } catch (err) {\r
                        throw new Error(`Failed to sign block`, { cause: err })\r
                }\r