From: Chris Duncan Date: Wed, 6 Aug 2025 19:36:02 +0000 (-0700) Subject: Update block type definition. X-Git-Tag: v0.10.5~43^2~64 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=a19644e2dfda338969a59723bd7fec4256262010;p=libnemo.git Update block type definition. --- diff --git a/src/lib/block.ts b/src/lib/block.ts index 50f7d15..6aca69f 100644 --- a/src/lib/block.ts +++ b/src/lib/block.ts @@ -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 { /** diff --git a/src/types.d.ts b/src/types.d.ts index 3c16b10..6d16628 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -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} Hexadecimal-formatted 64-byte signature */ - sign (block: ChangeBlock | ReceiveBlock | SendBlock, password: Key): Promise + sign (block: Block, password: Key): Promise /** * 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 + 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} Block data hashed to a byte array + * @returns {Uint8Array} Block data hashed to a byte array */ - get hash (): string + get hash (): Uint8Array /** * 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 + pow (): Promise /** * Sends the block to a node for processing on the network. * @@ -304,12 +322,63 @@ declare abstract class Block { */ process (rpc: Rpc): Promise /** + * 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 + sign (key: string): Promise + /** + * 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 /** * 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 + sign (index: number, frontier?: Block): Promise + /** + * 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 } -/** -* 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} Hexadecimal-formatted 64-byte signature */ - sign (index: number, block: ChangeBlock | ReceiveBlock | SendBlock): Promise + sign (index: number, block: Block): Promise /** * 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 } /** @@ -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} Signature */ - sign (index: number, block: SendBlock | ReceiveBlock | ChangeBlock, frontier?: SendBlock | ReceiveBlock | ChangeBlock): Promise + sign (index: number, block: Block, frontier?: Block): Promise /** * 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 + updateCache (index: number, block: Block): Promise /** * Update cache from a block hash by calling out to a node. Suitable for online * use only.