From 766395b9ff9b02edd4d6708bb68a8ba61382a590 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Wed, 6 Aug 2025 14:46:12 -0700 Subject: [PATCH] Accept work as pow parameter. Fix Ledger verification. Fix sweep tool. Update type definitions. --- src/lib/block.ts | 28 ++++++++++++++++------------ src/lib/ledger.ts | 17 +++++++++-------- src/lib/tools.ts | 19 +++++++------------ src/types.d.ts | 6 +++--- 4 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/lib/block.ts b/src/lib/block.ts index db27d45..b2b6741 100644 --- a/src/lib/block.ts +++ b/src/lib/block.ts @@ -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 { + async pow (work?: string): Promise + async pow (work: unknown): Promise { 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 } diff --git a/src/lib/ledger.ts b/src/lib/ledger.ts index f232a08..79da5bb 100644 --- a/src/lib/ledger.ts +++ b/src/lib/ledger.ts @@ -355,14 +355,9 @@ export class Ledger extends Wallet { const testWallet = await Wallet.import('BIP-44', '', secret) await testWallet.unlock('') const testAccount = await testWallet.account(0) - const testOpenBlock = new Block( - testAccount.address, - '0', - testAccount.address, - '0', - testAccount.address, - '0' - ) + const testOpenBlock = new Block(testAccount.address, '0', testAccount.publicKey, testAccount.address,) + .receive('0') + .from(testAccount.publicKey) await testWallet.sign(0, testOpenBlock) const testSendBlock = new Block(testAccount.address, '0', bytes.toHex(testOpenBlock.hash), testAccount.address) .send(0) @@ -485,6 +480,9 @@ export class Ledger extends Wallet { if (!(block.link instanceof Uint8Array)) { throw new TypeError('Invalid block link') } + if (!(block.representative instanceof Account)) { + throw new TypeError('Invalid block link') + } if (!block.signature) { throw new ReferenceError('Cannot cache unsigned block') } @@ -523,6 +521,9 @@ export class Ledger extends Wallet { if (!(block.link instanceof Uint8Array)) { throw new TypeError('Invalid block link') } + if (!(block.representative instanceof Account)) { + throw new TypeError('Invalid block representative') + } const account = dec.toBytes(index + HARDENED_OFFSET, 4) const previous = hex.toBytes(block.previous, 32) diff --git a/src/lib/tools.ts b/src/lib/tools.ts index 192aaa5..727c508 100644 --- a/src/lib/tools.ts +++ b/src/lib/tools.ts @@ -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 = 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) { diff --git a/src/types.d.ts b/src/types.d.ts index 4041d41..571de1f 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -266,11 +266,11 @@ export declare class Block { account: Account balance: bigint previous: string - representative: Account + representative?: Account link?: Uint8Array 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 + pow (work?: string): Promise /** * Sends the block to a node for processing on the network. * -- 2.47.3