]> git.codecow.com Git - libnemo.git/commitdiff
Derive link_as_account from link in Blocks.
authorChris Duncan <chris@codecow.com>
Fri, 7 Aug 2026 21:43:28 +0000 (14:43 -0700)
committerChris Duncan <chris@codecow.com>
Fri, 7 Aug 2026 21:43:28 +0000 (14:43 -0700)
src/lib/block/change.ts
src/lib/block/index.ts
src/lib/block/receive.ts
src/lib/block/send.ts

index c5cb0633dfc17c98787d59ba75db4bca53c8c599..a8d7748532f5a92ab97f3ad1818a5a411aab979b 100644 (file)
@@ -3,11 +3,11 @@
 
 import type { Block } from '.'
 import { Account } from '../account'
-import { BURN_ADDRESS, BURN_PUBLIC_KEY } from '../constants'
+import { BURN_PUBLIC_KEY } from '../constants'
 import { hex } from '../convert'
 
 export function _change (block: Block, representative: unknown): Block {
-       const { link, link_as_account, representative: rep, subtype } = block
+       const { link, representative: rep, subtype } = block
        try {
                if (block.subtype != null) {
                        throw new Error(`Block already configured as ${block.subtype}`)
@@ -22,12 +22,10 @@ export function _change (block: Block, representative: unknown): Block {
                        : representative
 
                block.link = hex.toBytes(BURN_PUBLIC_KEY)
-               block.link_as_account = BURN_ADDRESS
 
                return block
        } catch (err) {
                block.link = link
-               block.link_as_account = link_as_account
                block.representative = rep
                block.subtype = subtype
                throw new TypeError('Failed to configure change block', { cause: err })
index de8984b1424675efffbd7f6c7af11b1eb2c61e62..9fa41148e631f17d659d90710ef904c129a120ae 100644 (file)
@@ -38,6 +38,11 @@ export class Block {
        previous: Bytes
        representative?: Account
        link?: Bytes
+       get link_as_account (): string {
+               return typeof this.link === 'undefined'
+                       ? ''
+                       : new Account(bytes.toHex(this.link)).address
+       }
        signature?: string
        work?: string
 
@@ -136,7 +141,7 @@ export class Block {
                                "representative": this.representative.address ?? '',
                                "balance": dec.toString(this.balance),
                                "link": bytes.toHex(this.link),
-                               "link_as_account": bytes.toHex(this.link),
+                               "link_as_account": this.link_as_account,
                                "signature": this.signature ?? '',
                                "work": this.work ?? ''
                        }
index fca80d5d09cb063cc88704ad126480a6213352f7..99ca1ea08164a33fea9363b225c12b4c016ca776 100644 (file)
@@ -2,7 +2,6 @@
 //! SPDX-License-Identifier: GPL-3.0-or-later
 
 import type { Block } from '.'
-import { Account } from '../account'
 import { UNITS } from '../constants'
 import { hex } from '../convert'
 import { convert } from '../tools'
@@ -17,7 +16,7 @@ import { convert } from '../tools'
  * @returns {Block} This block with balance, link, and subtype configured
 */
 export function _receive (block: Block, sendBlock: unknown, amount: unknown, unit: unknown): Block {
-       const { balance, link, link_as_account, subtype } = block
+       const { balance, link, subtype } = block
        try {
                if (block.subtype != null) {
                        throw new Error(`Block already configured as ${block.subtype}`)
@@ -41,13 +40,11 @@ export function _receive (block: Block, sendBlock: unknown, amount: unknown, uni
                        throw new TypeError('Invalid send block hash', { cause: sendBlock })
                }
                block.link = hex.toBytes(sendBlock, 32)
-               block.link_as_account = new Account(sendBlock).address
 
                return block
        } catch (err) {
                block.balance = balance
                block.link = link
-               block.link_as_account = link_as_account
                block.subtype = subtype
                throw new TypeError('Failed to configure receive block', { cause: err })
        }
index 4621bde26a839e9a567240a470be436125546a2d..d812f431bbad0ccc5fdb627bc8b6b07bfc15da9b 100644 (file)
@@ -43,13 +43,11 @@ export function _send (block: Block, account: unknown, amount: unknown, unit: un
                        throw new TypeError('Invalid account', { cause: account })
                }
                block.link = hex.toBytes(account.publicKey, 32)
-               block.link_to_account = account.address
 
                return block
        } catch (err) {
                block.balance = balance
                block.link = link
-               block.link_as_account = link_as_account
                block.subtype = subtype
                throw new TypeError('Failed to configure send block', { cause: err })
        }