Skip to content

Commit

Permalink
Simply accept an Address
Browse files Browse the repository at this point in the history
  • Loading branch information
jessepinho committed Mar 23, 2024
1 parent 77a1643 commit f26d1e3
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 15 deletions.
3 changes: 1 addition & 2 deletions packages/perspective/plan/get-address-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import {
AddressView,
} from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/keys/v1/keys_pb';
import { getAddressIndexByAddress } from '@penumbra-zone/wasm/src/address';
import { bech32Address } from '@penumbra-zone/bech32/src/address';

export const getAddressView = (address: Address, fullViewingKey: string): AddressView => {
const index = getAddressIndexByAddress(fullViewingKey, bech32Address(address));
const index = getAddressIndexByAddress(fullViewingKey, address);

Check failure on line 8 in packages/perspective/plan/get-address-view.ts

View workflow job for this annotation

GitHub Actions / test

plan/get-address-view.test.ts > getAddressView() > when the address is controlled by the user represented by the full viewing key > returns a visible `AddressView`

Error: [vitest] No "getAddressIndexByAddress" export is defined on the "@penumbra-zone/wasm/src/address" mock. Did you forget to return it from "vi.mock"? If you need to partially mock a module, you can use "importOriginal" helper inside: vi.mock("@penumbra-zone/wasm/src/address", async (importOriginal) => { const actual = await importOriginal() return { ...actual, // your mocked methods } }) ❯ Module.getAddressView plan/get-address-view.ts:8:17 ❯ plan/get-address-view.test.ts:43:14

Check failure on line 8 in packages/perspective/plan/get-address-view.ts

View workflow job for this annotation

GitHub Actions / test

plan/get-address-view.test.ts > getAddressView() > when the address is not controlled by the user represented by the full viewing key > returns an opaque `AddressView`

Error: [vitest] No "getAddressIndexByAddress" export is defined on the "@penumbra-zone/wasm/src/address" mock. Did you forget to return it from "vi.mock"? If you need to partially mock a module, you can use "importOriginal" helper inside: vi.mock("@penumbra-zone/wasm/src/address", async (importOriginal) => { const actual = await importOriginal() return { ...actual, // your mocked methods } }) ❯ Module.getAddressView plan/get-address-view.ts:8:17 ❯ plan/get-address-view.test.ts:62:14

if (index) {
return new AddressView({
Expand Down
3 changes: 1 addition & 2 deletions packages/router/src/grpc/custody/authorize/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { Box } from '@penumbra-zone/types/src/box';
import { UserChoice } from '@penumbra-zone/types/src/user-choice';
import { assertValidSwaps } from './assert-valid-swaps';
import { getAddressIndexByAddress } from '@penumbra-zone/wasm/src/address';
import { bech32Address } from '@penumbra-zone/bech32/src/address';

export const authorize: Impl['authorize'] = async (req, ctx) => {
if (!req.plan) throw new ConnectError('No plan included in request', Code.InvalidArgument);
Expand All @@ -21,7 +20,7 @@ export const authorize: Impl['authorize'] = async (req, ctx) => {

const { fullViewingKey } = (await services.getWalletServices()).viewServer;
assertValidSwaps(req.plan, address =>
address ? Boolean(getAddressIndexByAddress(fullViewingKey, bech32Address(address))) : false,
address ? Boolean(getAddressIndexByAddress(fullViewingKey, address)) : false,
);

if (!approveReq) throw new ConnectError('Approver not found', Code.Unavailable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { servicesCtx } from '../../ctx/prax';
import { getAddressIndexByAddress } from '@penumbra-zone/wasm/src/address';

import { Code, ConnectError } from '@connectrpc/connect';
import { bech32Address } from '@penumbra-zone/bech32/src/address';

export const indexByAddress: Impl['indexByAddress'] = async (req, ctx) => {
if (!req.address) throw new ConnectError('no address given in request', Code.InvalidArgument);
Expand All @@ -13,9 +12,7 @@ export const indexByAddress: Impl['indexByAddress'] = async (req, ctx) => {
viewServer: { fullViewingKey },
} = await services.getWalletServices();

const address = bech32Address(req.address);

const addressIndex = getAddressIndexByAddress(fullViewingKey, address);
const addressIndex = getAddressIndexByAddress(fullViewingKey, req.address);

if (!addressIndex) return {};

Expand Down
12 changes: 8 additions & 4 deletions packages/wasm/src/address.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { describe, expect, it } from 'vitest';
import { generateSpendKey, getAddressByIndex, getFullViewingKey } from './keys';
import { getAddressIndexByAddress } from './address';
import { bech32Address } from '@penumbra-zone/bech32/src/address';
import { bech32ToAddress } from '@penumbra-zone/bech32/src/address';
import { Address } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/keys/v1/keys_pb';

describe('address', () => {
const seedPhrase =
Expand All @@ -13,14 +14,17 @@ describe('address', () => {
const fullViewingKey = getFullViewingKey(spendKey);
const address = getAddressByIndex(fullViewingKey, 1);

expect(getAddressIndexByAddress(fullViewingKey, bech32Address(address))!.account).toBe(1);
expect(getAddressIndexByAddress(fullViewingKey, address)!.account).toBe(1);
});

it('returns undefined with uncontrolled addr', () => {
const spendKey = generateSpendKey(seedPhrase);
const fullViewingKey = getFullViewingKey(spendKey);
const address =
'penumbra1ftmn2a3hf8pxe0e48es8u9rqhny4xggq9wn2caxcjnfwfhwr5s0t3y6nzs9gx3ty5czd0sd9ssfgjt2pcxrq93yvgk2gu3ynmayuwgddkxthce8l445v8x6v07y2sjd8djcr6v';
const address = new Address({
inner: bech32ToAddress(
'penumbra1ftmn2a3hf8pxe0e48es8u9rqhny4xggq9wn2caxcjnfwfhwr5s0t3y6nzs9gx3ty5czd0sd9ssfgjt2pcxrq93yvgk2gu3ynmayuwgddkxthce8l445v8x6v07y2sjd8djcr6v',
),
});

expect(getAddressIndexByAddress(fullViewingKey, address)).toBeUndefined();
});
Expand Down
10 changes: 7 additions & 3 deletions packages/wasm/src/address.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { get_short_address_by_index, is_controlled_address } from '../wasm';
import { AddressIndex } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/keys/v1/keys_pb';
import {
Address,
AddressIndex,
} from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/keys/v1/keys_pb';
import { JsonValue } from '@bufbuild/protobuf';
import { bech32Address } from '@penumbra-zone/bech32/src/address';

export const getShortAddressByIndex = (fullViewingKey: string, index: number) =>
get_short_address_by_index(fullViewingKey, index) as string;

export const getAddressIndexByAddress = (
fullViewingKey: string,
bech32Address: string,
address: Address,
): AddressIndex | undefined => {
const res = is_controlled_address(fullViewingKey, bech32Address) as JsonValue;
const res = is_controlled_address(fullViewingKey, bech32Address(address)) as JsonValue;
return res ? AddressIndex.fromJson(res) : undefined;
};

0 comments on commit f26d1e3

Please sign in to comment.