]> git.codecow.com Git - libnemo.git/commitdiff
Update block type definition.
authorChris Duncan <chris@zoso.dev>
Wed, 6 Aug 2025 19:36:02 +0000 (12:36 -0700)
committerChris Duncan <chris@zoso.dev>
Wed, 6 Aug 2025 19:36:02 +0000 (12:36 -0700)
src/lib/block.ts
src/types.d.ts

index 50f7d155d951c6e38583c8af7a047d542e9ed764..6aca69f44acd4765a028f40ce3adb693b4a6c788 100644 (file)
@@ -11,9 +11,7 @@ import { Rpc } from './rpc'
 import { Wallet } from './wallet'
 
 /**
-* Represents a block as defined by the Nano cryptocurrency protocol. The Block
-* class is abstract and cannot be directly instantiated. Every block must be one
-* of three derived classes: SendBlock, ReceiveBlock, ChangeBlock.
+* Represents a block as defined by the Nano cryptocurrency protocol.
 */
 export class Block {
        /**
index 3c16b10a2ef0b837b5381817275d92069b6a5827..6d166281b59ca8fcba6cdbad624eacadd2c51bac 100644 (file)
@@ -135,11 +135,11 @@ export declare class Account {
        * Signs a block using the private key of the account. The signature is
        * appended to the signature field of the block before being returned.
        *
-       * @param {(ChangeBlock|ReceiveBlock|SendBlock)} block - The block data to be hashed and signed
+       * @param {(Block)} block - The block data to be hashed and signed
        * @param {Key} password - Required to decrypt the private key for signing
        * @returns {Promise<string>} Hexadecimal-formatted 64-byte signature
        */
-       sign (block: ChangeBlock | ReceiveBlock | SendBlock, password: Key): Promise<string>
+       sign (block: Block, password: Key): Promise<string>
        /**
        * Validates a Nano address with 'nano' and 'xrb' prefixes
        * Derived from https://github.com/alecrios/nano-address-validator
@@ -258,27 +258,25 @@ export declare class Blake2b {
 }
 
 /**
-* Represents a block as defined by the Nano cryptocurrency protocol. The Block
-* class is abstract and cannot be directly instantiated. Every block must be one
-* of three derived classes: SendBlock, ReceiveBlock, ChangeBlock.
+* Represents a block as defined by the Nano cryptocurrency protocol.
 */
-declare abstract class Block {
+export declare class Block {
+       #private
+       subtype?: 'send' | 'receive' | 'change'
        account: Account
-       type: 'state'
-       abstract subtype: 'send' | 'receive' | 'change'
-       abstract previous: string
-       abstract representative: Account
-       abstract balance: bigint
-       abstract link: string
-       abstract signature?: string
-       abstract work?: string
-       constructor (account: Account | string)
+       balance: bigint
+       previous: string
+       representative: Account
+       link?: Uint8Array<ArrayBuffer>
+       signature?: string
+       work?: string
+       constructor (account: string | Account, balance?: bigint | string, previous?: string, representative?: string | Account, signature?: string, work?: string)
        /**
        * Calculates the block hash using Blake2b.
        *
-       * @returns {Promise<string>} Block data hashed to a byte array
+       * @returns {Uint8Array<ArrayBuffer>} Block data hashed to a byte array
        */
-       get hash (): string
+       get hash (): Uint8Array<ArrayBuffer>
        /**
        * Converts the block to JSON format as expected by the `process` RPC.
        *
@@ -288,11 +286,31 @@ declare abstract class Block {
                [key: string]: string
        }
        /**
+       * Set the subtype and link to configure this as a change representative block.
+       *
+       * @returns {Block} This block so that additional calls can be chained
+       */
+       change (): Block
+       /**
+       * Set a block hash as the source send of a receive block.
+       *
+       * @param {string} hash - Hash of send block to be received
+       * @returns {Block} This block so that additional calls can be chained
+       */
+       from (hash: string): Block
+       /**
+       * Set a send block as the source of a receive block.
+       *
+       * @param {Block} block - Send block to be received
+       * @returns {Block} This block so that additional calls can be chained
+       */
+       from (block: Block): Block
+       /**
        * Calculates proof-of-work using a pool of Web Workers.
        *
        * A successful response sets the `work` property.
        */
-       pow (): Promise<void>
+       pow (): Promise<Block>
        /**
        * Sends the block to a node for processing on the network.
        *
@@ -304,12 +322,63 @@ declare abstract class Block {
        */
        process (rpc: Rpc): Promise<string>
        /**
+       * Set the amount of nano that this block will receive from a paired send.
+       *
+       * @param {bigint} amount - Amount that was sent from sender in raw
+       * @returns {Block} This block so that additional calls can be chained
+       */
+       receive (amount: bigint): Block
+       /**
+       * Set the amount of nano that this block will receive from a paired send.
+       *
+       * @param {number} amount - Amount that was sent from sender in nano (10³⁰ raw)
+       * @returns {Block} This block so that additional calls can be chained
+       */
+       receive (amount: number): Block
+       /**
+       * Set the amount of nano that this block will receive from a paired send.
+       *
+       * @param {string} amount - Amount that was sent from sender in raw
+       * @returns {Block} This block so that additional calls can be chained
+       */
+       receive (amount: string): Block
+       /**
+       * Set the amount of nano that this block will send to a recipient account.
+       *
+       * @param {bigint} amount - Amount to send to recipient in raw
+       * @returns {Block} This block so that additional calls can be chained
+       */
+       send (amount: bigint): Block
+       /**
+       * Set the amount of nano that this block will send to a recipient account.
+       *
+       * @param {number} amount - Amount to send to recipient in nano (10³⁰ raw)
+       * @returns {Block} This block so that additional calls can be chained
+       */
+       send (amount: number): Block
+       /**
+       * Set the amount of nano that this block will send to a recipient account.
+       *
+       * @param {string} amount - Amount to send to recipient in raw
+       * @returns {Block} This block so that additional calls can be chained
+       */
+       send (amount: string): Block
+       /**
        * Signs the block using a private key. If successful, the result is stored in
        * the object's `signature` property.
        *
        * @param {string} [key] - Hexadecimal-formatted private key to use for signing
        */
-       sign (key?: string): Promise<void>
+       sign (key: string): Promise<Block>
+       /**
+       * Signs the block using a Wallet. If successful, the result is stored in
+       * the object's `signature` property. The wallet must be unlocked prior to
+       * signing.
+       *
+       * @param {Wallet} wallet - Wallet to use for signing
+       * @param {number} index - Account in wallet to use for signing
+       */
+       sign (wallet: Wallet, index: number): Promise<Block>
        /**
        * Signs the block using a Ledger hardware wallet. If that fails, an error is
        * thrown with the status code from the device.
@@ -320,7 +389,23 @@ declare abstract class Block {
        * @param {number} index - Account index between 0x0 and 0x7fffffff
        * @param {object} [frontier] - JSON of frontier block for offline signing
        */
-       sign (index?: number, frontier?: ChangeBlock | ReceiveBlock | SendBlock): Promise<void>
+       sign (index: number, frontier?: Block): Promise<Block>
+       /**
+       * Set the recipient of a send block or the target representative of a change
+       * block.
+       *
+       * @param {string} account - Address or public key of Account to target
+       * @returns {Block} This block so that additional calls can be chained
+       */
+       to (account: string): Block
+       /**
+       * Set the recipient of a send block or the target representative of a change
+       * block.
+       *
+       * @param {Account} account - Account to target
+       * @returns {Block} This block so that additional calls can be chained
+       */
+       to (account: Account): Block
        /**
        * Verifies the signature of the block. If a key is not provided, the public
        * key of the block's account will be used if it exists.
@@ -330,52 +415,6 @@ declare abstract class Block {
        */
        verify (key?: string): Promise<boolean>
 }
-/**
-* Represents a block that sends funds from one address to another as defined by
-* the Nano cryptocurrency protocol.
-*/
-export declare class SendBlock extends Block {
-       type: 'state'
-       subtype: 'send'
-       previous: string
-       representative: Account
-       balance: bigint
-       link: string
-       signature?: string
-       work?: string
-       constructor (sender: Account | string, balance: string, recipient: string, amount: string, representative: Account | string, frontier: string, work?: string)
-}
-/**
-* Represents a block that receives funds sent to one address from another as
-* defined by the Nano cryptocurrency protocol.
-*/
-export declare class ReceiveBlock extends Block {
-       type: 'state'
-       subtype: 'receive'
-       previous: string
-       representative: Account
-       balance: bigint
-       link: string
-       signature?: string
-       work?: string
-       constructor (recipient: Account | string, balance: string, origin: string, amount: string, representative: Account | string, frontier?: string, work?: string)
-}
-/**
-* Represents a block that changes the representative account to which the user
-* account delegates their vote weight using the the Open Representative Voting
-* specification as defined by the Nano cryptocurrency protocol.
-*/
-export declare class ChangeBlock extends Block {
-       type: 'state'
-       subtype: 'change'
-       previous: string
-       representative: Account
-       balance: bigint
-       link: string
-       signature?: string
-       work?: string
-       constructor (account: Account | string, balance: string, representative: Account | string, frontier: string, work?: string)
-}
 
 export type Data = boolean | number | number[] | string | string[] | ArrayBuffer | CryptoKey | { [key: string]: Data }
 
@@ -709,10 +748,10 @@ export declare 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<string>} Hexadecimal-formatted 64-byte signature
        */
-       sign (index: number, block: ChangeBlock | ReceiveBlock | SendBlock): Promise<string>
+       sign (index: number, block: Block): Promise<string>
        /**
        * Unlocks the wallet using the same password as used prior to lock it.
        *
@@ -767,7 +806,7 @@ interface LedgerAccountResponse extends LedgerResponse {
 
 interface LedgerSignResponse extends LedgerResponse {
        signature: string | null,
-       hash?: string
+       hash?: Uint8Array<ArrayBuffer>
 }
 
 /**
@@ -834,10 +873,10 @@ export declare class Ledger extends Wallet {
        * Sign a block with the Ledger device.
        *
        * @param {number} index - Account number
-       * @param {object} block - Block data to sign
+       * @param {Block} block - Block data to sign
        * @returns {Promise<string>} Signature
        */
-       sign (index: number, block: SendBlock | ReceiveBlock | ChangeBlock, frontier?: SendBlock | ReceiveBlock | ChangeBlock): Promise<string>
+       sign (index: number, block: Block, frontier?: Block): Promise<string>
        /**
        * Attempts to connect to the Ledger device.
        *
@@ -851,9 +890,9 @@ export declare class Ledger extends Wallet {
        * Update cache from raw block data. Suitable for offline use.
        *
        * @param {number} index - Account number
-       * @param {object} block - JSON-formatted block data
+       * @param {Block} block - JSON-formatted block data
        */
-       updateCache (index: number, block: ChangeBlock | ReceiveBlock | SendBlock): Promise<LedgerResponse>
+       updateCache (index: number, block: Block): Promise<LedgerResponse>
        /**
        * Update cache from a block hash by calling out to a node. Suitable for online
        * use only.