]> git.codecow.com Git - libnemo.git/commitdiff
Alias nano25519 submodules for browser builds to allow Node build tree-shaking. Updat...
authorChris Duncan <chris@zoso.dev>
Tue, 14 Apr 2026 18:51:37 +0000 (11:51 -0700)
committerChris Duncan <chris@zoso.dev>
Tue, 14 Apr 2026 18:51:37 +0000 (11:51 -0700)
esbuild/config.mjs
src/lib/block.ts

index dd5c20c85fe1caefb05348d9493a6c631ad2bfdb..074d8af379db23796644e531141f4b4a8119f37b 100644 (file)
@@ -34,6 +34,10 @@ export const browserOptions = {
        ],
        define: {
                'VAULT_WORKER': browserVaultWorker
+       },
+       alias: {
+               'nano25519/async': 'nano25519',
+               'nano25519/sync': 'nano25519'
        }
 }
 
index bed0a112664453de6eca5b87263111d9fb0e56a6..34964c84b2e80e43e9af4d04534f144e345d4538 100644 (file)
@@ -2,7 +2,7 @@
 //! SPDX-License-Identifier: GPL-3.0-or-later
 
 import { NanoPow } from 'nano-pow'
-import { derive as nano25519_derive, sign as nano25519_sign, verify as nano25519_verify } from 'nano25519'
+import { derive as nano25519_derive, sign as nano25519_sign, verify as nano25519_verify } from 'nano25519/sync'
 import { Account } from './account'
 import { BURN_PUBLIC_KEY, DIFFICULTY_RECEIVE, DIFFICULTY_SEND, PREAMBLE, UNITS } from './constants'
 import { bytes, dec, hex } from './convert'
@@ -12,12 +12,13 @@ import { Tools } from './tools'
 import { Wallet } from './wallet'
 
 /**
-* Represents a block as defined by the Nano cryptocurrency protocol.
-*/
+ * Represents a block as defined by the Nano cryptocurrency protocol.
+ */
 export class Block {
        [key: string]: bigint | string | Account | Function | Uint8Array<ArrayBuffer> | 'send' | 'receive' | 'change' | undefined
+
        /**
-        * Validates block data.
+        * Validates the format of Block properties.
         *
         * @param {Block} block - SendBlock, ReceiveBlock, or ChangeBlock to validate
         */
@@ -78,18 +79,18 @@ export class Block {
        work?: string
 
        /**
-       * Initialize a block with the current state of an account so that it can
-       * subsequently be configured as a change, receive, or send transaction.
-       *
-       * All parameters are eventually required in order to initialize the block, but
-       * but if `account` is an Account class object, its properties can be used for
-       * the other parameters instead of passing them into the constructor.
-       *
-       * @param {(string|Account)} account - Target of the transaction; can include `balance`, `frontier`, `representative`
-       * @param {(bigint|number|string)} [balance] - Current balance of the target account in raw
-       * @param {string} [previous] - Current frontier block hash of the target account
-       * @param {(string|Account)} [representative] - Current representative of the target account
-       */
+        * Initialize a block with the current state of an account so that it can
+        * subsequently be configured as a change, receive, or send transaction.
+        *
+        * All parameters are eventually required in order to initialize the block, but
+        * but if `account` is an Account class object, its properties can be used for
+        * the other parameters instead of passing them into the constructor.
+        *
+        * @param {(string|Account)} account - Target of the transaction; can include `balance`, `frontier`, `representative`
+        * @param {(bigint|number|string)} [balance] - Current balance of the target account in raw
+        * @param {string} [previous] - Current frontier block hash of the target account
+        * @param {(string|Account)} [representative] - Current representative of the target account
+        */
        constructor (account: string | Account, balance?: bigint | number | string, previous?: string, representative?: string | Account)
        constructor (account: unknown, balance: unknown, previous: unknown, representative: unknown) {
                try {
@@ -123,10 +124,10 @@ export class Block {
        }
 
        /**
-       * Calculates the block hash using Blake2b.
-       *
-       * @returns {string} Hexadecimal representation of 32-byte hash of block data
-       */
+        * Uses BLAKE2b to generate a 32-byte hash of the block data.
+        *
+        * @returns {string} Hexadecimal representation of hash
+        */
        get hash (): string {
                try {
                        if (this.link == null) {
@@ -153,10 +154,10 @@ export class Block {
        }
 
        /**
-       * Converts the block to JSON format as expected by the `process` RPC.
-       *
-       * @returns {string} JSON representation of the block
-       */
+        * Converts the block to JSON format as expected by the `process` RPC.
+        *
+        * @returns {string} JSON representation of the block
+        */
        toJSON (): Record<string, string> {
                try {
                        if (this.link == null) {
@@ -182,12 +183,12 @@ export class Block {
        }
 
        /**
-       * Set the subtype, link, and target account to configure this as a change
-       * representative block.
-       *
-       * @param {(string|Account)} account - Account to choose as representative, or its address or public key
-       * @returns {Block} This block with link, representative, and subtype configured
-       */
+        * Set the subtype, link, and target account to configure this as a change
+        * representative block.
+        *
+        * @param {(string|Account)} account - Account to choose as representative, or its address or public key
+        * @returns {Block} This block with link, representative, and subtype configured
+        */
        change (representative: string | Account): Block
        change (representative: unknown): Block {
                const { link, representative: rep, subtype } = this
@@ -216,10 +217,10 @@ export class Block {
        }
 
        /**
-       * Calculates proof-of-work using a pool of Web Workers.
-       *
-       * A successful response sets the `work` property.
-       */
+        * Calculates proof-of-work using NanoPow.
+        *
+        * @returns {Block} This block with `work` set
+        */
        async pow (work?: string): Promise<Block>
        async pow (work: unknown): Promise<Block> {
                const difficulty: bigint = (this.subtype === 'send' || this.subtype === 'change')
@@ -248,15 +249,15 @@ export class Block {
        }
 
        /**
-       * Sends the block to a node for processing on the network.
-       *
-       * The block must already be signed (see `sign()` for more information).
-       *
-       * If the block has no `work` value, `pow()` will be called automatically.
-       *
-       * @param {Rpc} rpc - RPC node information required to call `process`
-       * @returns {Promise<string>} Hash of the processed block
-       */
+        * Sends the block to a node for processing on the network.
+        *
+        * The block must already be signed (see `sign()` for more information).
+        *
+        * If the block has no `work` value, `pow()` will be called automatically.
+        *
+        * @param {Rpc} rpc - RPC node information required to call `process`
+        * @returns {Promise<string>} Hash of the processed block
+        */
        async process (rpc: Rpc): Promise<string> {
                const b: typeof Block = this.constructor as typeof Block
                b.validate(this)
@@ -277,13 +278,13 @@ export class Block {
        }
 
        /**
-       * Set the amount of nano that this block will receive from a corresponding
-       * send block.
-       *
-       * @param {(string|Block)} sendBlock - Corresponding send block or its hash
-       * @param {(bigint|number|string)} amount - Amount to be received from sender
-       * @param {string} [unit] - Unit of measure for amount (e.g. 'NANO' = 10³⁰ RAW). Default: "RAW"
-       * @returns {Block} This block with balance, link, and subtype configured
+        * Set the amount of nano that this block will receive from a corresponding
+        * send block.
+        *
+        * @param {(string|Block)} sendBlock - Corresponding send block or its hash
+        * @param {(bigint|number|string)} amount - Amount to be received from sender
+        * @param {string} [unit] - Unit of measure for amount (e.g. 'NANO' = 10³⁰ RAW). Default: "RAW"
+        * @returns {Block} This block with balance, link, and subtype configured
        */
        receive (sendBlock: string | Block, amount: bigint | number | string, unit?: string): Block
        receive (sendBlock: unknown, amount: unknown, unit: unknown): Block {
@@ -321,13 +322,13 @@ export class Block {
        }
 
        /**
-       * Set the amount of nano that this block will send to a recipient account.
-       *
-       * @param {(string|Account)} account - Account to target or its address or public key
-       * @param {(bigint|number|string)} amount - Amount to send to recipient
-       * @param {string} [unit] - Unit of measure for amount (e.g. 'NANO' = 10³⁰ RAW). Default: "RAW"
-       * @returns {Block} This block with balance, link, and subtype configured
-       */
+        * Set the amount of nano that this block will send to a recipient account.
+        *
+        * @param {(string|Account)} account - Account to target or its address or public key
+        * @param {(bigint|number|string)} amount - Amount to send to recipient
+        * @param {string} [unit] - Unit of measure for amount (e.g. 'NANO' = 10³⁰ RAW). Default: "RAW"
+        * @returns {Block} This block with balance, link, and subtype configured
+        */
        send (account: string | Account, amount: bigint | number | string, unit?: string): Block
        send (account: unknown, amount: unknown, unit: unknown): Block {
                const { balance, link, subtype } = this
@@ -368,40 +369,40 @@ export class Block {
        }
 
        /**
-       * Sets the `signature` property of the block to a precalculated value.
-       *
-       * @param {string} signature - 64-byte hexadecimal signature
-       * @returns Block with `signature` value set
-       */
+        * Sets the `signature` property of the block to a precalculated value.
+        *
+        * @param {string} signature - 64-byte hexadecimal signature
+        * @returns Block with `signature` value set
+        */
        sign (signature: string): Block
        /**
-       * Signs the block using a private key. If successful, the result is stored in
-       * the `signature` property of the block.
-       *
-       * @param {string} [key] - 32-byte hexadecimal private key to use for signing
-       * @returns Block with `signature` value set
-       */
+        * Signs the block using a private key. If successful, the result is stored in
+        * the `signature` property of the block.
+        *
+        * @param {string} [key] - 32-byte hexadecimal private key to use for signing
+        * @returns Block with `signature` value set
+        */
        async sign (key: string): Promise<Block>
        /**
-       * Signs the block using a Wallet. If successful, the result is stored in
-       * the `signature` property of the block. 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
-       * @returns Block with `signature` value set
-       */
+        * Signs the block using a Wallet. If successful, the result is stored in
+        * the `signature` property of the block. 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
+        * @returns Block with `signature` value set
+        */
        async 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. If successful, the result is
-       * stored in the `signature` property of the block. The wallet must be unlocked
-       * prior to signing.
-       *
-       * @param {number} index - Account index between 0x0 and 0x7fffffff
-       * @param {object} [frontier] - JSON of frontier block for offline signing
-       * @returns Block with `signature` value set
-       */
+        * Signs the block using a Ledger hardware wallet. If that fails, an error is
+        * thrown with the status code from the device. If successful, the result is
+        * stored in the `signature` property of the block. The wallet must be unlocked
+        * prior to signing.
+        *
+        * @param {number} index - Account index between 0x0 and 0x7fffffff
+        * @param {object} [frontier] - JSON of frontier block for offline signing
+        * @returns Block with `signature` value set
+        */
        async sign (wallet: Wallet, index: number, frontier?: Block): Promise<Block>
        sign (input: unknown, index?: unknown, frontier?: unknown): Block | Promise<Block> {
                if (typeof input === 'string' && /^[A-F0-9]{128}$/i.test(input)) {
@@ -432,12 +433,12 @@ export class 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.
-       *
-       * @param {string} [key] - Hexadecimal-formatted public key to use for verification
-       * @returns {boolean} True if block was signed by the matching private key
-       */
+        * 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.
+        *
+        * @param {string} [key] - Hexadecimal-formatted public key to use for verification
+        * @returns {boolean} True if block was signed by the matching private key
+        */
        async verify (key?: string): Promise<boolean>
        async verify (key: unknown): Promise<boolean> {
                try {