]> git.codecow.com Git - libnemo.git/commitdiff
Accept work as pow parameter. Fix Ledger verification. Fix sweep tool. Update type...
authorChris Duncan <chris@zoso.dev>
Wed, 6 Aug 2025 21:46:12 +0000 (14:46 -0700)
committerChris Duncan <chris@zoso.dev>
Wed, 6 Aug 2025 21:46:12 +0000 (14:46 -0700)
src/lib/block.ts
src/lib/ledger.ts
src/lib/tools.ts
src/types.d.ts

index db27d454cc6d5fbd12fb2dd8dd5b5848648a92e9..b2b67413b0c63b5286ac5d55a693479d4cab74e9 100644 (file)
@@ -71,8 +71,8 @@ export class Block {
        signature?: string
        work?: string
 
-       constructor (account: string | Account, balance?: bigint | string, previous?: string, representative?: string | Account, signature?: string, work?: string)
-       constructor (account: unknown, balance: unknown, previous: unknown, representative: unknown, signature: unknown, work: unknown) {
+       constructor (account: string | Account, balance?: bigint | string, previous?: string, representative?: string | Account, signature?: string)
+       constructor (account: unknown, balance: unknown, previous: unknown, representative: unknown, signature: unknown) {
                try {
                        if (typeof account === 'string') {
                                account = Account.import(account)
@@ -101,9 +101,6 @@ export class Block {
                        if (typeof signature === 'string') {
                                this.signature = signature
                        }
-                       if (typeof work === 'string') {
-                               this.work = work
-                       }
                } catch (err) {
                        throw new Error('Failed to initialize Block', { cause: err })
                }
@@ -224,22 +221,29 @@ export class Block {
        *
        * A successful response sets the `work` property.
        */
-       async pow (): Promise<Block> {
+       async pow (work?: string): Promise<Block>
+       async pow (work: unknown): Promise<Block> {
                const difficulty: bigint = (this.subtype === 'send' || this.subtype === 'change')
                        ? DIFFICULTY_SEND
                        : DIFFICULTY_RECEIVE
-               const result = await NanoPow.work_generate(this.previous, { difficulty })
-               if ('error' in result) {
-                       throw new Error('Failed to generate work', { cause: result.error })
+               if (work == null) {
+                       const result = await NanoPow.work_generate(this.previous, { difficulty })
+                       if ('error' in result) {
+                               throw new Error('Failed to generate work', { cause: result.error })
+                       }
+                       work = result.work
+               }
+               if (typeof work !== 'string') {
+                       throw new TypeError('Invalid work')
                }
-               const check = await NanoPow.work_validate(result.work, this.previous, { difficulty })
+               const check = await NanoPow.work_validate(work, this.previous, { difficulty })
                if ('error' in check) {
                        throw new Error('Failed to validate work generated', { cause: check.error })
                }
                if (check.valid === '0') {
-                       throw new Error('Wwork generated is invalid', { cause: `${check.difficulty} < ${difficulty}` })
+                       throw new Error('Invalid work generated', { cause: `${check.difficulty} < ${difficulty}` })
                }
-               this.work = result.work
+               this.work = work
                return this
        }
 
index f232a0811294f68e2e89d1a121dabc3fef44d135..79da5bbbf0c4b36d79d60f5f6242f34fdd7b0c8d 100644 (file)
@@ -355,14 +355,9 @@ export class Ledger extends Wallet {
                const testWallet = await Wallet.import('BIP-44', '', secret)\r
                await testWallet.unlock('')\r
                const testAccount = await testWallet.account(0)\r
-               const testOpenBlock = new Block(\r
-                       testAccount.address,\r
-                       '0',\r
-                       testAccount.address,\r
-                       '0',\r
-                       testAccount.address,\r
-                       '0'\r
-               )\r
+               const testOpenBlock = new Block(testAccount.address, '0', testAccount.publicKey, testAccount.address,)\r
+                       .receive('0')\r
+                       .from(testAccount.publicKey)\r
                await testWallet.sign(0, testOpenBlock)\r
                const testSendBlock = new Block(testAccount.address, '0', bytes.toHex(testOpenBlock.hash), testAccount.address)\r
                        .send(0)\r
@@ -485,6 +480,9 @@ export class Ledger extends Wallet {
                if (!(block.link instanceof Uint8Array)) {\r
                        throw new TypeError('Invalid block link')\r
                }\r
+               if (!(block.representative instanceof Account)) {\r
+                       throw new TypeError('Invalid block link')\r
+               }\r
                if (!block.signature) {\r
                        throw new ReferenceError('Cannot cache unsigned block')\r
                }\r
@@ -523,6 +521,9 @@ export class Ledger extends Wallet {
                if (!(block.link instanceof Uint8Array)) {\r
                        throw new TypeError('Invalid block link')\r
                }\r
+               if (!(block.representative instanceof Account)) {\r
+                       throw new TypeError('Invalid block representative')\r
+               }\r
 \r
                const account = dec.toBytes(index + HARDENED_OFFSET, 4)\r
                const previous = hex.toBytes(block.previous, 32)\r
index 192aaa55494bf8e12e7cd90bfe8441e5278c8503..727c508677a97cc18bb4d0ed0e1d8aec182d71fe 100644 (file)
@@ -3,7 +3,7 @@
 
 import { Account } from './account'
 import { Blake2b } from './blake2b'
-import { SendBlock } from './block'
+import { Block } from './block'
 import { UNITS } from './constants'
 import { bytes, hex } from './convert'
 import { Ledger } from './ledger'
@@ -135,19 +135,14 @@ export async function sweep (
        const recipientAccount = Account.import(recipient)
        const accounts = await wallet.refresh(rpc, from, to)
        for (const account of accounts) {
-               if (account.representative?.address && account.frontier) {
-                       const block = new SendBlock(
-                               account,
-                               account.balance?.toString() ?? '0',
-                               recipientAccount.address,
-                               account.balance?.toString() ?? '0',
-                               account.representative.address,
-                               account.frontier
-                       )
+               if (account.representative?.address && account.frontier && account.index) {
+                       const block = await (await new Block(account)
+                               .send(account.balance ?? 0n)
+                               .to(recipientAccount)
+                               .pow())
+                               .sign(wallet, account.index)
                        const blockRequest: Promise<void> = new Promise(async (resolve) => {
                                try {
-                                       await block.pow()
-                                       await block.sign()
                                        const hash = await block.process(rpc)
                                        results.push({ status: 'success', address: block.account.address, message: hash })
                                } catch (err: any) {
index 4041d41f39d8c6ec8c7e87c8905fe0ae58259512..571de1f50bbf7e38bc793bd1e75a94868e0ea3d1 100644 (file)
@@ -266,11 +266,11 @@ export declare class Block {
        account: Account
        balance: bigint
        previous: string
-       representative: Account
+       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)
+       constructor (account: string | Account, balance?: bigint | string, previous?: string, representative?: string | Account, signature?: string)
        /**
        * Calculates the block hash using Blake2b.
        *
@@ -310,7 +310,7 @@ export declare class Block {
        *
        * A successful response sets the `work` property.
        */
-       pow (): Promise<Block>
+       pow (work?: string): Promise<Block>
        /**
        * Sends the block to a node for processing on the network.
        *