From c205a1ba24ac49fee3b173c745a3ea3d74ebea4c Mon Sep 17 00:00:00 2001 From: Hugh Cunningham Date: Fri, 28 Jun 2024 10:34:30 -0700 Subject: [PATCH] ensures that postTransaction uses a spending account replaces 'getAccountByPublicAddress' with a more generic 'findAccount' that takes a predicate uses findAccount in postTransaction to find the account that has the same public address and is a spending account fixes issue where wallet has a spending account and a view only account with the same public address and cannot post a transaction --- ironfish/src/rpc/routes/wallet/postTransaction.ts | 5 ++++- ironfish/src/wallet/wallet.ts | 14 +++++--------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/ironfish/src/rpc/routes/wallet/postTransaction.ts b/ironfish/src/rpc/routes/wallet/postTransaction.ts index f763a86c28..326b41792b 100644 --- a/ironfish/src/rpc/routes/wallet/postTransaction.ts +++ b/ironfish/src/rpc/routes/wallet/postTransaction.ts @@ -3,6 +3,7 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ import * as yup from 'yup' import { RawTransactionSerde } from '../../../primitives/rawTransaction' +import { Account } from '../../../wallet' import { RpcValidationError } from '../../adapters' import { ApiNamespace } from '../namespaces' import { routes } from '../router' @@ -55,7 +56,9 @@ routes.register( throw new RpcValidationError('Unable to determine sender account for raw transaction') } - const account = context.wallet.getAccountByPublicAddress(sender) + const account = context.wallet.findAccount( + (account: Account) => account.publicAddress === sender && account.isSpendingAccount(), + ) if (account === null) { throw new RpcValidationError( diff --git a/ironfish/src/wallet/wallet.ts b/ironfish/src/wallet/wallet.ts index 8fce590faf..8e6f92d2a1 100644 --- a/ironfish/src/wallet/wallet.ts +++ b/ironfish/src/wallet/wallet.ts @@ -1537,22 +1537,18 @@ export class Wallet { this.defaultAccount = nextId } - getAccountByName(name: string): Account | null { + findAccount(predicate: (account: Account) => boolean): Account | null { for (const account of this.accounts.values()) { - if (name === account.name) { + if (predicate(account)) { return account } } + return null } - getAccountByPublicAddress(publicAddress: string): Account | null { - for (const account of this.accounts.values()) { - if (publicAddress === account.publicAddress) { - return account - } - } - return null + getAccountByName(name: string): Account | null { + return this.findAccount((account) => account.name === name) } getAccount(id: string): Account | null {