Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify wasm func naming #833

Merged
merged 2 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions packages/wasm/crate/src/keys.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use std::str::FromStr;

use anyhow;
use rand_core::OsRng;
use wasm_bindgen::prelude::*;

use penumbra_keys::keys::{Bip44Path, SeedPhrase, SpendKey};
use penumbra_keys::{Address, FullViewingKey};
use penumbra_proof_params::{
CONVERT_PROOF_PROVING_KEY, DELEGATOR_VOTE_PROOF_PROVING_KEY, OUTPUT_PROOF_PROVING_KEY,
SPEND_PROOF_PROVING_KEY, SWAPCLAIM_PROOF_PROVING_KEY, SWAP_PROOF_PROVING_KEY,
};
use penumbra_proto::{core::keys::v1 as pb, serializers::bech32str, DomainType};
use rand_core::OsRng;
use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::js_sys::Uint8Array;

use crate::error::WasmResult;
Expand Down Expand Up @@ -132,19 +131,19 @@ pub fn get_ephemeral_address(full_viewing_key: &str, index: u32) -> WasmResult<J
Ok(result)
}

/// Check if the address is FVK controlled
/// Returns the AddressIndex of an address.
/// If it is not controlled by the FVK, it returns a `None`
/// Arguments:
/// full_viewing_key: `bech32 String`
/// address: `bech32 String`
/// address: `Address`
/// Returns: `Option<pb::AddressIndex>`
#[wasm_bindgen]
pub fn is_controlled_address(full_viewing_key: &str, address: &str) -> WasmResult<JsValue> {
pub fn get_index_by_address(full_viewing_key: &str, address: JsValue) -> WasmResult<JsValue> {
utils::set_panic_hook();

let address: Address = serde_wasm_bindgen::from_value(address)?;
let fvk = FullViewingKey::from_str(full_viewing_key)?;
let index: Option<pb::AddressIndex> = fvk
.address_index(&Address::from_str(address)?)
.map(Into::into);
let index: Option<pb::AddressIndex> = fvk.address_index(&address).map(Into::into);
let result = serde_wasm_bindgen::to_value(&index)?;
Ok(result)
}
Expand Down
14 changes: 9 additions & 5 deletions packages/wasm/src/address.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { get_short_address_by_index, is_controlled_address } from '../wasm';
import { get_index_by_address, get_short_address_by_index } from '../wasm';
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;
Expand All @@ -13,9 +12,14 @@ export const getAddressIndexByAddress = (
fullViewingKey: string,
address: Address,
): AddressIndex | undefined => {
const res = is_controlled_address(fullViewingKey, bech32Address(address)) as JsonValue;
const res = get_index_by_address(fullViewingKey, address.toJson()) as JsonValue;
return res ? AddressIndex.fromJson(res) : undefined;
};

export const isControlledAddress = (fullViewingKey: string, address?: Address): boolean =>
address ? Boolean(is_controlled_address(fullViewingKey, bech32Address(address))) : false;
// Only an address controlled by the FVK can view its index
export const isControlledAddress = (fullViewingKey: string, address?: Address): boolean => {
if (!address) return false;

const viewableIndex = get_index_by_address(fullViewingKey, address.toJson()) as JsonValue;
return Boolean(viewableIndex);
};
Loading