]> git.codecow.com Git - libnemo.git/commitdiff
Include link as account in Block subtype calls.
authorChris Duncan <chris@codecow.com>
Fri, 7 Aug 2026 18:04:55 +0000 (11:04 -0700)
committerChris Duncan <chris@codecow.com>
Fri, 7 Aug 2026 18:04:55 +0000 (11:04 -0700)
src/lib/block/change.ts
src/lib/block/index.ts
src/lib/block/receive.ts
src/lib/block/send.ts
src/lib/rpc/process.ts

index a8d7748532f5a92ab97f3ad1818a5a411aab979b..c5cb0633dfc17c98787d59ba75db4bca53c8c599 100644 (file)
@@ -3,11 +3,11 @@
 
 import type { Block } from '.'
 import { Account } from '../account'
-import { BURN_PUBLIC_KEY } from '../constants'
+import { BURN_ADDRESS, BURN_PUBLIC_KEY } from '../constants'
 import { hex } from '../convert'
 
 export function _change (block: Block, representative: unknown): Block {
-       const { link, representative: rep, subtype } = block
+       const { link, link_as_account, representative: rep, subtype } = block
        try {
                if (block.subtype != null) {
                        throw new Error(`Block already configured as ${block.subtype}`)
@@ -22,10 +22,12 @@ 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 85deeddf40211a87405404ebf357eb9026beace3..8f2b2c771b3faec75cd90e1b97cca70717fbd001 100644 (file)
@@ -121,7 +121,7 @@ export class Block {
         *
         * @returns {string} JSON representation of the block
         */
-       toJSON (): Record<string, string> {
+       toJSON () {
                try {
                        if (this.link == null) {
                                throw new Error('Link is required')
@@ -130,12 +130,13 @@ export class Block {
                                throw new Error('Representative is required')
                        }
                        return {
-                               "type": "state",
+                               "type": "state" as const,
                                "account": this.account.address,
                                "previous": bytes.toHex(this.previous),
                                "representative": this.representative.address ?? '',
                                "balance": dec.toString(this.balance),
                                "link": bytes.toHex(this.link),
+                               "link_as_account": bytes.toHex(this.link),
                                "signature": this.signature ?? '',
                                "work": this.work ?? ''
                        }
@@ -204,11 +205,16 @@ export class Block {
                if (this.signature == null) {
                        throw new Error('Block is missing signature. Use sign() and try again.')
                }
+               if (this.subtype == null) {
+                       throw new Error('Block is missing subtype. Call respective method and try again.')
+               }
                if (this.work == null) await this.pow()
                const data: ProcessRequest = {
-                       subtype: this.subtype,
+                       async: false,
+                       block: this.toJSON(),
+                       force: false,
                        json_block: true,
-                       block: this.toJSON()
+                       subtype: this.subtype,
                }
                try {
                        const res = await rpc.post('process', data)
index 99ca1ea08164a33fea9363b225c12b4c016ca776..fca80d5d09cb063cc88704ad126480a6213352f7 100644 (file)
@@ -2,6 +2,7 @@
 //! 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'
@@ -16,7 +17,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, subtype } = block
+       const { balance, link, link_as_account, subtype } = block
        try {
                if (block.subtype != null) {
                        throw new Error(`Block already configured as ${block.subtype}`)
@@ -40,11 +41,13 @@ 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 54634def674d7131d297bf8431bffcb4bcebddd4..4621bde26a839e9a567240a470be436125546a2d 100644 (file)
@@ -15,7 +15,7 @@ import { convert } from '../tools'
  * @returns {Block} This block with balance, link, and subtype configured
  */
 export function _send (block: Block, account: unknown, amount: unknown, unit: unknown): Block {
-       const { balance, link, subtype } = block
+       const { balance, link, link_as_account, subtype } = block
        try {
                if (block.subtype != null) {
                        throw new Error(`Block already configured as ${block.subtype}`)
@@ -43,11 +43,13 @@ 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 })
        }
index 450399a4fd26a44134201a51fd4899778e930a85..8c69b30fec8da3a4fbb2a4aeb8a539a81d6f81b3 100644 (file)
@@ -6,10 +6,10 @@ import { RpcError } from '../errors'
 import { BlockInfoContents } from './block_info'
 
 export type ProcessRequest = {
+       async: false
        block: BlockInfoContents
-       async: boolean
-       force: boolean
-       json_block: boolean
+       force: false
+       json_block: true
        subtype: 'change' | 'epoch' | 'open' | 'receive' | 'send'
 }