]> git.codecow.com Git - libnemo.git/commitdiff
Add signing method to Account class and simplify Block sign method accordingly.
authorChris Duncan <chris@zoso.dev>
Wed, 16 Jul 2025 04:06:22 +0000 (21:06 -0700)
committerChris Duncan <chris@zoso.dev>
Wed, 16 Jul 2025 04:06:22 +0000 (21:06 -0700)
src/lib/account.ts
src/lib/block.ts

index 23cf0b7a56bd08bef6773bbfeca1620cfde03b60..f85699dcd710dbf05626db6b3c5e46a39fc9dca9 100644 (file)
@@ -6,6 +6,7 @@ import { ACCOUNT_KEY_BYTE_LENGTH, ACCOUNT_KEY_HEX_LENGTH, ALPHABET, PREFIX, PREF
 import { base32, bytes, hex, utf8 } from './convert'\r
 import { Rpc } from './rpc'\r
 import { NanoNaClWorker, SafeWorker } from '#workers'\r
+import { ChangeBlock, ReceiveBlock, SendBlock } from './block'\r
 \r
 /**\r
 * Represents a single Nano address and the associated public key. To include the\r
@@ -216,6 +217,29 @@ export class Account {
                this.#weight = BigInt(weight)\r
        }\r
 \r
+       /**\r
+       * Signs a block using the private key of the account.\r
+       *\r
+       * @param {(ChangeBlock|ReceiveBlock|SendBlock)} block -\r
+       */\r
+       async sign (block: ChangeBlock | ReceiveBlock | SendBlock): Promise<string> {\r
+               if (this.isLocked || this.#prv.buffer.detached) {\r
+                       throw new Error('Failed to sign block with locked Account')\r
+               }\r
+               try {\r
+                       const headers = {\r
+                               method: 'detached'\r
+                       }\r
+                       const data = {\r
+                               privateKey: new Uint8Array(this.#prv).buffer,\r
+                               msg: hex.toBytes(block.hash).buffer\r
+                       }\r
+                       return await NanoNaClWorker.add(headers, data)\r
+               } catch (err) {\r
+                       throw new Error(`Failed to sign block`, { cause: err })\r
+               }\r
+       }\r
+\r
        /**\r
        * Unlocks the account using the same password as used prior to lock it.\r
        *\r
index 12bc6c8a34f107397f7d28c1671e5a132c98c94f..025eb80b22d358ee631cf47e9116cec92ea09ba1 100644 (file)
@@ -16,7 +16,7 @@ import { NanoNaClWorker } from '#workers'
 */
 abstract class Block {
        account: Account
-       type: string = 'state'
+       type: 'state' = 'state'
        abstract subtype: 'send' | 'receive' | 'change'
        abstract previous: string
        abstract representative: Account
@@ -139,20 +139,11 @@ abstract class Block {
                        }
                        this.signature = result.signature
                } else {
-                       const key = input ?? this.account.privateKey
-                       if (key == null) {
-                               throw new Error('No valid key found to sign block')
-                       }
-                       const account = await Account.fromPrivateKey(key)
                        try {
-                               const headers = {
-                                       method: 'detached'
-                               }
-                               const data = {
-                                       privateKey: hex.toBytes(account.privateKey).buffer,
-                                       msg: hex.toBytes(this.hash).buffer
-                               }
-                               this.signature = await NanoNaClWorker.add(headers, data)
+                               const account = (typeof input === 'string')
+                                       ? await Account.fromPrivateKey(input)
+                                       : this.account
+                               this.signature = await account.sign(this)
                        } catch (err) {
                                throw new Error(`Failed to sign block`, { cause: err })
                        }