From 6dccd4df191dadb0fb5467537962b20e0a6e2516 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Mon, 3 Aug 2026 13:13:42 -0700 Subject: [PATCH] Improve block link validation. --- src/lib/block/receive.ts | 11 ++++++----- src/lib/block/send.ts | 9 +++++---- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/lib/block/receive.ts b/src/lib/block/receive.ts index 89044cd..99ca1ea 100644 --- a/src/lib/block/receive.ts +++ b/src/lib/block/receive.ts @@ -33,12 +33,13 @@ export function _receive (block: Block, sendBlock: unknown, amount: unknown, uni } block.balance += convert(amount, unit, 'raw', 'bigint') - if (typeof sendBlock !== 'string' && !(sendBlock instanceof (block.constructor as typeof Block))) { - throw new TypeError('Invalid send block') + if (sendBlock instanceof (block.constructor as typeof Block)) { + sendBlock = sendBlock.hash } - block.link = (typeof sendBlock === 'string') - ? hex.toBytes(sendBlock) - : hex.toBytes(sendBlock.hash) + if (typeof sendBlock !== 'string' || sendBlock.length !== 64) { + throw new TypeError('Invalid send block hash', { cause: sendBlock }) + } + block.link = hex.toBytes(sendBlock, 32) return block } catch (err) { diff --git a/src/lib/block/send.ts b/src/lib/block/send.ts index 5d42a76..54634de 100644 --- a/src/lib/block/send.ts +++ b/src/lib/block/send.ts @@ -36,12 +36,13 @@ export function _send (block: Block, account: unknown, amount: unknown, unit: un throw new RangeError('Insufficient funds', { cause: block.balance }) } - if (typeof account !== 'string' && !(account instanceof Account)) { + if (typeof account === 'string') { + account = new Account(account) + } + if (!(account instanceof Account)) { throw new TypeError('Invalid account', { cause: account }) } - block.link = (typeof account === 'string') - ? hex.toBytes(new Account(account).publicKey) - : hex.toBytes(account.publicKey) + block.link = hex.toBytes(account.publicKey, 32) return block } catch (err) { -- 2.52.0