Skip to content

Commit

Permalink
Rename helper
Browse files Browse the repository at this point in the history
  • Loading branch information
jessepinho committed Mar 25, 2024
1 parent 9989451 commit f5a720d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
import { assertValidSwaps } from './assert-valid-swaps';
import { assertSwapClaimAddressesBelongToCurrentUser } from './assert-swap-claim-addresses-belong-to-current-user';
import {
ActionPlan,
TransactionPlan,
Expand Down Expand Up @@ -64,10 +64,12 @@ const swapWithUndefinedAddress = new ActionPlan({
const mockIsControlledAddress = (address?: Address) =>
!!address && [currentUserAddress1, currentUserAddress2].includes(address);

describe('assertValidSwaps()', () => {
describe('assertSwapClaimAddressesBelongToCurrentUser()', () => {
describe('when the transaction plan has no swaps', () => {
it('does not throw', () => {
expect(() => assertValidSwaps(new TransactionPlan(), mockIsControlledAddress)).not.toThrow();
expect(() =>
assertSwapClaimAddressesBelongToCurrentUser(new TransactionPlan(), mockIsControlledAddress),
).not.toThrow();
});
});

Expand All @@ -78,7 +80,9 @@ describe('assertValidSwaps()', () => {
actions: [swapWithCurrentUserAddress1, swapWithCurrentUserAddress2],
});

expect(() => assertValidSwaps(plan, mockIsControlledAddress)).not.toThrow();
expect(() =>
assertSwapClaimAddressesBelongToCurrentUser(plan, mockIsControlledAddress),
).not.toThrow();
});
});

Expand All @@ -91,7 +95,7 @@ describe('assertValidSwaps()', () => {
expect.assertions(2);

try {
assertValidSwaps(plan, mockIsControlledAddress);
assertSwapClaimAddressesBelongToCurrentUser(plan, mockIsControlledAddress);
} catch (error) {
expect(error).toBeInstanceOf(ConnectError);
expect((error as ConnectError).code).toBe(Code.PermissionDenied);
Expand All @@ -108,7 +112,7 @@ describe('assertValidSwaps()', () => {
expect.assertions(2);

try {
assertValidSwaps(plan, mockIsControlledAddress);
assertSwapClaimAddressesBelongToCurrentUser(plan, mockIsControlledAddress);
} catch (error) {
expect(error).toBeInstanceOf(ConnectError);
expect((error as ConnectError).code).toBe(Code.PermissionDenied);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Address } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/k
import { TransactionPlan } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/transaction/v1/transaction_pb';
import { Code, ConnectError } from '@connectrpc/connect';

export const assertValidSwaps = (
export const assertSwapClaimAddressesBelongToCurrentUser = (
plan: TransactionPlan,
isControlledAddress: (address?: Address) => boolean,
): void => {
Expand Down
6 changes: 4 additions & 2 deletions packages/router/src/grpc/custody/authorize/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Key } from '@penumbra-zone/crypto-web/src/encryption';
import { Code, ConnectError } from '@connectrpc/connect';
import { Box } from '@penumbra-zone/types/src/box';
import { UserChoice } from '@penumbra-zone/types/src/user-choice';
import { assertValidSwaps } from './assert-valid-swaps';
import { assertSwapClaimAddressesBelongToCurrentUser } from './assert-swap-claim-addresses-belong-to-current-user';
import { isControlledAddress } from '@penumbra-zone/wasm/src/address';

export const authorize: Impl['authorize'] = async (req, ctx) => {
Expand All @@ -19,7 +19,9 @@ export const authorize: Impl['authorize'] = async (req, ctx) => {
const walletServices = await ctx.values.get(servicesCtx).getWalletServices();

const { fullViewingKey } = walletServices.viewServer;
assertValidSwaps(req.plan, address => isControlledAddress(fullViewingKey, address));
assertSwapClaimAddressesBelongToCurrentUser(req.plan, address =>
isControlledAddress(fullViewingKey, address),
);

if (!approveReq) throw new ConnectError('Approver not found', Code.Unavailable);

Expand Down

0 comments on commit f5a720d

Please sign in to comment.