Skip to content

Commit

Permalink
Create new isControlledAddress helper
Browse files Browse the repository at this point in the history
  • Loading branch information
jessepinho committed Mar 23, 2024
1 parent f26d1e3 commit 7e91a09
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
24 changes: 23 additions & 1 deletion packages/wasm/src/address.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';
import { generateSpendKey, getAddressByIndex, getFullViewingKey } from './keys';
import { getAddressIndexByAddress } from './address';
import { getAddressIndexByAddress, isControlledAddress } from './address';
import { bech32ToAddress } from '@penumbra-zone/bech32/src/address';
import { Address } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/keys/v1/keys_pb';

Expand Down Expand Up @@ -29,4 +29,26 @@ describe('address', () => {
expect(getAddressIndexByAddress(fullViewingKey, address)).toBeUndefined();
});
});

describe('isControlledAddress()', () => {
it('returns true if the address is controlled', () => {
const spendKey = generateSpendKey(seedPhrase);
const fullViewingKey = getFullViewingKey(spendKey);
const address = getAddressByIndex(fullViewingKey, 1);

expect(isControlledAddress(fullViewingKey, address)).toBe(true);
});

it('returns false if the address is not controlled', () => {
const spendKey = generateSpendKey(seedPhrase);
const fullViewingKey = getFullViewingKey(spendKey);
const address = new Address({
inner: bech32ToAddress(
'penumbra1ftmn2a3hf8pxe0e48es8u9rqhny4xggq9wn2caxcjnfwfhwr5s0t3y6nzs9gx3ty5czd0sd9ssfgjt2pcxrq93yvgk2gu3ynmayuwgddkxthce8l445v8x6v07y2sjd8djcr6v',
),
});

expect(isControlledAddress(fullViewingKey, address)).toBe(false);
});
});
});
3 changes: 3 additions & 0 deletions packages/wasm/src/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ export const getAddressIndexByAddress = (
const res = is_controlled_address(fullViewingKey, bech32Address(address)) as JsonValue;
return res ? AddressIndex.fromJson(res) : undefined;
};

export const isControlledAddress = (fullViewingKey: string, address: Address): boolean =>
Boolean(is_controlled_address(fullViewingKey, bech32Address(address)));

0 comments on commit 7e91a09

Please sign in to comment.