From 35ed14caad4c13767d1238efbb3b324a3df061a3 Mon Sep 17 00:00:00 2001 From: Tal Derei <70081547+TalDerei@users.noreply.github.com> Date: Wed, 17 Jul 2024 14:04:08 -0700 Subject: [PATCH] check for source in transaction planner request (#1499) * assert validity of source in transaction request * pass tests --- .../transaction-planner/assert-transaction-source.ts | 12 ++++++++++++ .../view-service/transaction-planner/index.test.ts | 5 ++++- .../src/view-service/transaction-planner/index.ts | 2 ++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 packages/services/src/view-service/transaction-planner/assert-transaction-source.ts diff --git a/packages/services/src/view-service/transaction-planner/assert-transaction-source.ts b/packages/services/src/view-service/transaction-planner/assert-transaction-source.ts new file mode 100644 index 0000000000..b4e28a1f98 --- /dev/null +++ b/packages/services/src/view-service/transaction-planner/assert-transaction-source.ts @@ -0,0 +1,12 @@ +import { TransactionPlannerRequest } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/view/v1/view_pb.js'; +import { Code, ConnectError } from '@connectrpc/connect'; + +export const assertTransactionSource = (transactionPlannerRequest: TransactionPlannerRequest) => { + // Ensure that a source is provided in the transaction request. + if (!transactionPlannerRequest.source) { + throw new ConnectError( + 'Source is required in the TransactionPlannerRequest', + Code.InvalidArgument, + ); + } +}; diff --git a/packages/services/src/view-service/transaction-planner/index.test.ts b/packages/services/src/view-service/transaction-planner/index.test.ts index a6e8a8130c..5dbad0f01f 100644 --- a/packages/services/src/view-service/transaction-planner/index.test.ts +++ b/packages/services/src/view-service/transaction-planner/index.test.ts @@ -18,6 +18,7 @@ import { AssetId, Value, } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/asset/v1/asset_pb.js'; +import { AddressIndex } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/keys/v1/keys_pb.js'; const mockPlanTransaction = vi.hoisted(() => vi.fn()); vi.mock('@penumbra-zone/wasm/planner', () => ({ @@ -60,7 +61,9 @@ describe('TransactionPlanner request handler', () => { .set(fvkCtx, () => Promise.resolve(testFullViewingKey)), }); - req = new TransactionPlannerRequest({}); + req = new TransactionPlannerRequest({ + source: new AddressIndex({ account: 0 }), + }); }); test('should throw if request is not valid', async () => { diff --git a/packages/services/src/view-service/transaction-planner/index.ts b/packages/services/src/view-service/transaction-planner/index.ts index 9c06b451e1..a575fa6763 100644 --- a/packages/services/src/view-service/transaction-planner/index.ts +++ b/packages/services/src/view-service/transaction-planner/index.ts @@ -6,6 +6,7 @@ import { assertSwapAssetsAreNotTheSame } from './assert-swap-assets-are-not-the- import { TransactionPlannerRequest } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/view/v1/view_pb.js'; import { fvkCtx } from '../../ctx/full-viewing-key.js'; import { extractAltFee } from '../fees.js'; +import { assertTransactionSource } from './assert-transaction-source.js'; export const transactionPlanner: Impl['transactionPlanner'] = async (req, ctx) => { assertValidRequest(req); @@ -66,4 +67,5 @@ export const transactionPlanner: Impl['transactionPlanner'] = async (req, ctx) = */ const assertValidRequest = (req: TransactionPlannerRequest): void => { assertSwapAssetsAreNotTheSame(req); + assertTransactionSource(req); };